diff --git a/apps/assets/api/mixin.py b/apps/assets/api/mixin.py index 36460a5b1..3a8ccf396 100644 --- a/apps/assets/api/mixin.py +++ b/apps/assets/api/mixin.py @@ -41,7 +41,7 @@ class SerializeToTreeNodeMixin: def get_platform(self, asset: Asset): default = 'file' icon = {'windows', 'linux'} - platform = asset.platform_base.lower() + platform = asset.platform.type.lower() if platform in icon: return platform return default @@ -70,7 +70,7 @@ class SerializeToTreeNodeMixin: 'name': asset.name, 'ip': asset.ip, 'protocols': asset.protocols_as_list, - 'platform': asset.platform_base, + 'platform': asset.platform.id, 'org_name': asset.org_name }, } diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 922fd8766..24b5bf04e 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -192,7 +192,7 @@ class NodeChildrenAsTreeApi(SerializeToTreeNodeMixin, NodeChildrenApi): if not self.instance or not include_assets: return [] assets = self.instance.get_assets().only( - "id", "name", "ip", "os", "platform_id", + "id", "name", "ip", "platform_id", "org_id", "protocols", "is_active", ).prefetch_related('platform') return self.serialize_assets(assets, self.instance.key) diff --git a/apps/assets/migrations/0096_auto_20220406_1546.py b/apps/assets/migrations/0096_auto_20220406_1546.py index e2c9400e4..bfd7bc277 100644 --- a/apps/assets/migrations/0096_auto_20220406_1546.py +++ b/apps/assets/migrations/0096_auto_20220406_1546.py @@ -4,35 +4,6 @@ from itertools import groupby from django.db import migrations from django.db.models import F -category_mapper = { - 'Linux': ('host', 'linux'), - 'Unix': ('host', 'unix'), - 'MacOS': ('host', 'macos'), - 'BSD': ('host', 'unix'), - 'Windows': ('host', 'windows'), - 'Other': ('host', 'other_host'), -} - - -def migrate_category_and_type(apps, *args): - asset_model = apps.get_model('assets', 'Asset') - assets_bases = asset_model.objects.all()\ - .annotate(base=F("platform__base"))\ - .values_list('id', 'base')\ - .order_by('base') - base_assets = groupby(assets_bases, lambda a: a[1]) - - print("") - for base, grouper in base_assets: - asset_ids = [g[0] for g in grouper] - category_type = category_mapper.get(base) - if not category_type: - continue - print("Migrate {} to => {}".format(base, category_type)) - category, tp = category_type - assets = asset_model.objects.filter(id__in=asset_ids) - assets.update(category=category, type=tp) - class Migration(migrations.Migration): @@ -41,5 +12,4 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(migrate_category_and_type) ] diff --git a/apps/assets/migrations/0097_auto_20220407_1726.py b/apps/assets/migrations/0097_auto_20220407_1726.py index bd08c3ad3..a3d0dc053 100644 --- a/apps/assets/migrations/0097_auto_20220407_1726.py +++ b/apps/assets/migrations/0097_auto_20220407_1726.py @@ -26,18 +26,13 @@ class Migration(migrations.Migration): migrations.AddField( model_name='platform', name='category', - field=models.CharField(choices=[('host', 'Host'), ('network', 'Networking'), ('database', 'Database'), ('remote_app', 'Remote app'), ('cloud', 'Clouding')], default='host', max_length=16, verbose_name='Category'), + field=models.CharField(default='host', max_length=16, verbose_name='Category'), preserve_default=False, ), migrations.AlterField( model_name='platform', name='type', - field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vmware_client', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('general_remote_app', 'Custom'), ('k8s', 'Kubernetes')], default='Linux', max_length=32, verbose_name='Type'), - ), - migrations.AlterField( - model_name='systemuser', - name='protocol', - field=models.CharField(choices=[('ssh', 'SSH'), ('rdp', 'RDP'), ('telnet', 'Telnet'), ('vnc', 'VNC'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('oracle', 'Oracle'), ('postgresql', 'PostgreSQL'), ('sqlserver', 'SQLServer'), ('redis', 'Redis'), ('mongodb', 'MongoDB'), ('k8s', 'K8S')], default='ssh', max_length=16, verbose_name='Protocol'), + field=models.CharField(default='linux', max_length=32, verbose_name='Type'), ), migrations.RunPython(migrate_platform_type_to_lower) ] diff --git a/apps/assets/migrations/0099_auto_20220426_1558.py b/apps/assets/migrations/0099_auto_20220426_1558.py index 6d3bb23a4..0a6d03e89 100644 --- a/apps/assets/migrations/0099_auto_20220426_1558.py +++ b/apps/assets/migrations/0099_auto_20220426_1558.py @@ -17,11 +17,8 @@ def create_app_platform(apps, *args): {'name': 'SQLServer', 'category': 'database', 'type': 'sqlserver'}, {'name': 'MongoDB', 'category': 'database', 'type': 'mongodb'}, {'name': 'Redis', 'category': 'database', 'type': 'redis'}, - {'name': 'Chrome', 'category': 'remote_app', 'type': 'chrome'}, - {'name': 'MysqlWorkbench', 'category': 'remote_app', 'type': 'mysql_workbench'}, - {'name': 'VmwareClient', 'category': 'remote_app', 'type': 'vmware_client'}, - {'name': 'General', 'category': 'remote_app', 'type': 'general_remote_app'}, {'name': 'Kubernetes', 'category': 'cloud', 'type': 'k8s'}, + {'name': 'Web', 'category': 'web', 'type': 'general'}, ] for platform in platforms: @@ -34,7 +31,7 @@ def get_prop_name_id(apps, app, category): asset_model = apps.get_model('assets', 'Asset') _id = app.id id_exists = asset_model.objects.filter(id=_id).exists() - if (id_exists): + if id_exists: _id = uuid.uuid4() name = app.name name_exists = asset_model.objects.filter(hostname=name).exists() diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index 17ef90752..1842f5f1b 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -112,6 +112,10 @@ class Asset(AbsConnectivity, NodesRelationMixin, JMSOrgBaseModel): names.append(n.name + ':' + n.value) return names + @property + def protocols_as_list(self): + return [{'name': p.name, 'port': p.port} for p in self.protocols.all()] + @lazyproperty def type(self): return self.platform.type diff --git a/apps/perms/filters.py b/apps/perms/filters.py index ac23b691c..c890dbcba 100644 --- a/apps/perms/filters.py +++ b/apps/perms/filters.py @@ -94,13 +94,13 @@ class AssetPermissionFilter(PermissionBaseFilter): node_id = filters.UUIDFilter(method='do_nothing') node = filters.CharFilter(method='do_nothing') asset_id = filters.UUIDFilter(method='do_nothing') - hostname = filters.CharFilter(method='do_nothing') + asset_name = filters.CharFilter(method='do_nothing') ip = filters.CharFilter(method='do_nothing') class Meta: model = AssetPermission fields = ( - 'user_id', 'username', 'system_user_id', 'system_user', 'user_group_id', + 'user_id', 'username', 'user_group_id', 'user_group', 'node_id', 'node', 'asset_id', 'name', 'ip', 'name', 'all', 'asset_id', 'is_valid', 'is_effective', 'from_ticket' ) @@ -141,36 +141,36 @@ class AssetPermissionFilter(PermissionBaseFilter): def filter_asset(self, queryset): is_query_all = self.get_query_param('all', True) asset_id = self.get_query_param('asset_id') - hostname = self.get_query_param('name') + asset_name = self.get_query_param('asset_name') ip = self.get_query_param('ip') if asset_id: assets = Asset.objects.filter(pk=asset_id) - elif hostname: - assets = Asset.objects.filter(hostname=hostname) + elif asset_name: + assets = Asset.objects.filter(name=asset_name) elif ip: assets = Asset.objects.filter(ip=ip) else: return queryset if not assets: return queryset.none() - assetids = list(assets.values_list('id', flat=True)) + asset_ids = list(assets.values_list('id', flat=True)) if not is_query_all: - queryset = queryset.filter(assets__in=assetids) + queryset = queryset.filter(assets__in=asset_ids) return queryset - inherit_all_nodekeys = set() - inherit_nodekeys = set(assets.values_list('nodes__key', flat=True)) + inherit_all_node_keys = set() + inherit_node_keys = set(assets.values_list('nodes__key', flat=True)) - for key in inherit_nodekeys: + for key in inherit_node_keys: ancestor_keys = Node.get_node_ancestor_keys(key, with_self=True) - inherit_all_nodekeys.update(ancestor_keys) + inherit_all_node_keys.update(ancestor_keys) - inherit_all_nodeids = Node.objects.filter(key__in=inherit_all_nodekeys).values_list('id', flat=True) - inherit_all_nodeids = list(inherit_all_nodeids) + inherit_all_node_ids = Node.objects.filter(key__in=inherit_all_node_keys).values_list('id', flat=True) + inherit_all_node_ids = list(inherit_all_node_ids) - qs1 = queryset.filter(assets__in=assetids).distinct() - qs2 = queryset.filter(nodes__in=inherit_all_nodeids).distinct() + qs1 = queryset.filter(assets__in=asset_ids).distinct() + qs2 = queryset.filter(nodes__in=inherit_all_node_ids).distinct() qs = UnionQuerySet(qs1, qs2) return qs diff --git a/requirements/requirements.txt b/requirements/requirements.txt index fcb42d5c5..ba1491849 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -137,3 +137,5 @@ ipython==8.4.0 ForgeryPy3==0.3.1 django-debug-toolbar==3.5 Pympler==1.0.1 +IPy==1.1 +