From 56abf0da23644329d3abe0b46154d739191e87f7 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 7 Sep 2022 17:12:53 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E4=BF=AE=E6=94=B9=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/const.py | 33 ++++++++++++++++++++++--- apps/assets/models/asset/common.py | 2 +- apps/assets/models/protocol.py | 4 +++ apps/assets/serializers/asset/common.py | 23 ++++++----------- apps/assets/serializers/platform.py | 7 +++++- 5 files changed, 48 insertions(+), 21 deletions(-) diff --git a/apps/assets/const.py b/apps/assets/const.py index 6a4fb8cf4..cbb3abd89 100644 --- a/apps/assets/const.py +++ b/apps/assets/const.py @@ -1,5 +1,6 @@ from django.db import models from django.utils.translation import gettext_lazy as _ + from common.db.models import IncludesTextChoicesMeta, ChoicesMixin from common.tree import TreeNode @@ -44,21 +45,35 @@ class Category(PlatformMixin, ChoicesMixin, models.TextChoices): 'change_password_enabled': True, 'create_account_enabled': True, 'gather_accounts_enabled': True, - '_protocols': ['ssh', 'telnet'] + '_protocols': ['ssh', 'sftp'] }, cls.NETWORKING: { 'domain_enabled': True, + 'su_enabled': False, + 'gather_facts_enabled': False, + 'verify_account_enabled': False, + 'change_password_enabled': False, + 'create_account_enabled': False, + 'gather_accounts_enabled': False, '_protocols': ['ssh', 'telnet'] }, cls.DATABASE: { 'domain_enabled': True, + 'su_enabled': False, + 'gather_facts_enabled': True, + 'verify_account_enabled': True, + 'change_password_enabled': True, + 'create_account_enabled': True, + 'gather_accounts_enabled': True, }, cls.WEB: { 'domain_enabled': False, - '_protocols': [] + 'su_enabled': False, + '_protocols': ['http', 'https'] }, cls.CLOUD: { 'domain_enabled': False, + 'su_enabled': False, '_protocols': [] } } @@ -77,7 +92,7 @@ class HostTypes(PlatformMixin, ChoicesMixin, models.TextChoices): def platform_constraints(cls): return { cls.LINUX: { - '_protocols': ['ssh', 'rdp', 'vnc', 'telnet'] + '_protocols': ['ssh', 'sftp', 'rdp', 'vnc', 'telnet'] }, cls.WINDOWS: { '_protocols': ['ssh', 'rdp', 'vnc'], @@ -110,7 +125,7 @@ class DatabaseTypes(PlatformMixin, ChoicesMixin, models.TextChoices): meta = {} for name, label in cls.choices: meta[name] = { - 'protocols': [name] + '_protocols': [name] } return meta @@ -122,6 +137,14 @@ class WebTypes(PlatformMixin, ChoicesMixin, models.TextChoices): class CloudTypes(PlatformMixin, ChoicesMixin, models.TextChoices): K8S = 'k8s', 'Kubernetes' + @classmethod + def platform_constraints(cls): + return { + cls.K8S: { + '_protocols': ['k8s'] + } + } + class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta): choices: list @@ -209,6 +232,7 @@ class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta): class Protocol(ChoicesMixin, models.TextChoices): ssh = 'ssh', 'SSH' + sftp = 'sftp', 'SFTP' rdp = 'rdp', 'RDP' telnet = 'telnet', 'Telnet' vnc = 'vnc', 'VNC' @@ -240,6 +264,7 @@ class Protocol(ChoicesMixin, models.TextChoices): def default_ports(cls): return { cls.ssh: 22, + cls.sftp: 22, cls.rdp: 3389, cls.vnc: 5900, cls.telnet: 21, diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index fad119a03..a9396e17c 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -68,7 +68,7 @@ class NodesRelationMixin: class Asset(AbsConnectivity, NodesRelationMixin, JMSOrgBaseModel): id = models.UUIDField(default=uuid.uuid4, primary_key=True) - name = models.CharField(max_length=128, verbose_name=_('Hostname')) + name = models.CharField(max_length=128, verbose_name=_('Name')) ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True) protocols = models.ManyToManyField('Protocol', verbose_name=_("Protocols"), blank=True) platform = models.ForeignKey(Platform, default=Platform.default, on_delete=models.PROTECT, diff --git a/apps/assets/models/protocol.py b/apps/assets/models/protocol.py index d42ee2754..a2aa2283f 100644 --- a/apps/assets/models/protocol.py +++ b/apps/assets/models/protocol.py @@ -5,3 +5,7 @@ from django.utils.translation import gettext_lazy as _ class Protocol(models.Model): name = models.CharField(max_length=32, verbose_name=_("Name")) port = models.IntegerField(verbose_name=_("Port")) + + def save(self, force_insert=False, force_update=False, using=None, + update_fields=None): + pass diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index 8d41eee84..61e32a7a8 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -22,11 +22,18 @@ class AssetProtocolsSerializer(serializers.ModelSerializer): model = Protocol fields = ['id', 'name', 'port'] + def create(self, validated_data): + instance = Protocol.objects.filter(**validated_data).first() + if instance: + return instance + instance = Protocol.objects.create(**validated_data) + return instance + class AssetLabelSerializer(serializers.ModelSerializer): class Meta: model = Label - fields = ['id', 'name', 'value'] + fields = ['name', 'value'] extra_kwargs = { 'name': {'required': False}, 'value': {'required': False} @@ -51,7 +58,6 @@ class AssetSerializer(JMSWritableNestedModelSerializer): labels = AssetLabelSerializer(many=True, required=False, label=_('Labels')) accounts = AccountSerializer(many=True, required=False, label=_('Accounts')) protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols')) - """ 资产的数据结构 """ @@ -109,23 +115,10 @@ class AssetSerializer(JMSWritableNestedModelSerializer): nodes_to_set.append(node) instance.nodes.set(nodes_to_set) - @staticmethod - def add_accounts(instance, accounts_data): - for data in accounts_data: - data['asset'] = instance.id - serializer = AccountSerializer(data=accounts_data, many=True) - try: - serializer.is_valid(raise_exception=True) - except Exception as e: - raise serializers.ValidationError({'accounts': e}) - serializer.save() - @atomic def create(self, validated_data): nodes_display = validated_data.pop('nodes_display', '') instance = super().create(validated_data) - if self.accounts_data: - self.add_accounts(instance, self.accounts_data) self.perform_nodes_display_create(instance, nodes_display) return instance diff --git a/apps/assets/serializers/platform.py b/apps/assets/serializers/platform.py index cf888ba1a..01f2e8956 100644 --- a/apps/assets/serializers/platform.py +++ b/apps/assets/serializers/platform.py @@ -17,12 +17,17 @@ class ProtocolSettingSerializer(serializers.Serializer): ('tls', 'TLS'), ('nla', 'NLA'), ] + # Common + required = serializers.BooleanField(required=True, initial=False, label=_("Required")) + # RDP console = serializers.BooleanField(required=False) security = serializers.ChoiceField(choices=SECURITY_CHOICES, default='any', required=False) + # SFTP + sftp_home = serializers.CharField(default='/tmp', required=False) class PlatformProtocolsSerializer(serializers.ModelSerializer): - setting = ProtocolSettingSerializer(required=False) + setting = ProtocolSettingSerializer(required=False, allow_null=True) class Meta: model = PlatformProtocol