From 8fa15b337848a6af9816aed3a82cf0731fb35ead Mon Sep 17 00:00:00 2001 From: Bai Date: Tue, 27 Oct 2020 19:06:54 +0800 Subject: [PATCH] =?UTF-8?q?perf(assets/terminal):=20=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E5=92=8CSession=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E6=B7=BB=E5=8A=A0=E5=8D=8F=E8=AE=AE=E9=80=89=E9=A1=B9?= =?UTF-8?q?:=20mysql/oracle/postgresql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0059_auto_20201027_1905.py | 18 ++++++++++++ apps/assets/models/user.py | 28 +++++++++++++++++-- apps/perms/forms/asset_permission.py | 2 +- apps/perms/forms/database_app_permission.py | 2 +- .../migrations/0026_auto_20201027_1905.py | 18 ++++++++++++ apps/terminal/models.py | 5 +++- 6 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 apps/assets/migrations/0059_auto_20201027_1905.py create mode 100644 apps/terminal/migrations/0026_auto_20201027_1905.py diff --git a/apps/assets/migrations/0059_auto_20201027_1905.py b/apps/assets/migrations/0059_auto_20201027_1905.py new file mode 100644 index 000000000..643acd356 --- /dev/null +++ b/apps/assets/migrations/0059_auto_20201027_1905.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.13 on 2020-10-27 11:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0058_auto_20201023_1115'), + ] + + operations = [ + migrations.AlterField( + model_name='systemuser', + name='protocol', + field=models.CharField(choices=[('ssh', 'ssh'), ('rdp', 'rdp'), ('telnet', 'telnet'), ('vnc', 'vnc'), ('mysql', 'mysql'), ('oracle', 'oracle'), ('mariadb', 'mariadb'), ('postgresql', 'postgresql'), ('k8s', 'k8s')], default='ssh', max_length=16, verbose_name='Protocol'), + ), + ] diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index 8b878ecde..add83e2ad 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -72,6 +72,9 @@ class SystemUser(BaseUser): PROTOCOL_TELNET = 'telnet' PROTOCOL_VNC = 'vnc' PROTOCOL_MYSQL = 'mysql' + PROTOCOL_ORACLE = 'oracle' + PROTOCOL_MARIADB = 'mariadb' + PROTOCOL_POSTGRESQL = 'postgresql' PROTOCOL_K8S = 'k8s' PROTOCOL_CHOICES = ( (PROTOCOL_SSH, 'ssh'), @@ -79,6 +82,9 @@ class SystemUser(BaseUser): (PROTOCOL_TELNET, 'telnet'), (PROTOCOL_VNC, 'vnc'), (PROTOCOL_MYSQL, 'mysql'), + (PROTOCOL_ORACLE, 'oracle'), + (PROTOCOL_MARIADB, 'mariadb'), + (PROTOCOL_POSTGRESQL, 'postgresql'), (PROTOCOL_K8S, 'k8s'), ) @@ -127,6 +133,24 @@ class SystemUser(BaseUser): def login_mode_display(self): return self.get_login_mode_display() + @property + def db_application_protocols(self): + return [ + self.PROTOCOL_MYSQL, self.PROTOCOL_ORACLE, self.PROTOCOL_MARIADB, + self.PROTOCOL_POSTGRESQL + ] + + @property + def k8s_application_protocols(self): + return [self.PROTOCOL_K8S] + + @property + def application_category_protocols(self): + protocols = [] + protocols.extend(self.db_application_protocols) + protocols.extend(self.k8s_application_protocols) + return protocols + def is_need_push(self): if self.auto_push and self.protocol in [self.PROTOCOL_SSH, self.PROTOCOL_RDP]: return True @@ -139,11 +163,11 @@ class SystemUser(BaseUser): @property def is_need_test_asset_connective(self): - return self.protocol not in [self.PROTOCOL_MYSQL] + return self.protocol not in self.application_category_protocols @property def can_perm_to_asset(self): - return self.protocol not in [self.PROTOCOL_MYSQL] + return self.protocol not in self.application_category_protocols def _merge_auth(self, other): super()._merge_auth(other) diff --git a/apps/perms/forms/asset_permission.py b/apps/perms/forms/asset_permission.py index 92ca8030f..aa75b8be5 100644 --- a/apps/perms/forms/asset_permission.py +++ b/apps/perms/forms/asset_permission.py @@ -60,7 +60,7 @@ class AssetPermissionForm(OrgModelForm): # 过滤系统用户 system_users_field = self.fields.get('system_users') system_users_field.queryset = SystemUser.objects.exclude( - protocol=SystemUser.PROTOCOL_MYSQL + protocol__in=SystemUser.application_category_protocols ) def set_nodes_initial(self, nodes): diff --git a/apps/perms/forms/database_app_permission.py b/apps/perms/forms/database_app_permission.py index 57ce4d0e3..9074491c9 100644 --- a/apps/perms/forms/database_app_permission.py +++ b/apps/perms/forms/database_app_permission.py @@ -25,7 +25,7 @@ class DatabaseAppPermissionCreateUpdateForm(OrgModelForm): # 过滤系统用户 system_users_field = self.fields.get('system_users') system_users_field.queryset = SystemUser.objects.filter( - protocol=SystemUser.PROTOCOL_MYSQL + protocol__in=SystemUser.application_category_protocols ) class Meta: diff --git a/apps/terminal/migrations/0026_auto_20201027_1905.py b/apps/terminal/migrations/0026_auto_20201027_1905.py new file mode 100644 index 000000000..d820166d6 --- /dev/null +++ b/apps/terminal/migrations/0026_auto_20201027_1905.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.13 on 2020-10-27 11:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('terminal', '0025_auto_20200810_1735'), + ] + + operations = [ + migrations.AlterField( + model_name='session', + name='protocol', + field=models.CharField(choices=[('ssh', 'ssh'), ('rdp', 'rdp'), ('vnc', 'vnc'), ('telnet', 'telnet'), ('mysql', 'mysql'), ('oracle', 'oracle'), ('mariadb', 'mariadb'), ('postgresql', 'postgresql'), ('k8s', 'kubernetes')], db_index=True, default='ssh', max_length=16), + ), + ] diff --git a/apps/terminal/models.py b/apps/terminal/models.py index dc747c370..a0f074b48 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -180,6 +180,9 @@ class Session(OrgModelMixin): VNC = 'vnc', 'vnc' TELNET = 'telnet', 'telnet' MYSQL = 'mysql', 'mysql' + ORACLE = 'oracle', 'oracle' + MARIADB = 'mariadb', 'mariadb' + POSTGRESQL = 'postgresql', 'postgresql' K8S = 'k8s', 'kubernetes' id = models.UUIDField(default=uuid.uuid4, primary_key=True) @@ -196,7 +199,7 @@ class Session(OrgModelMixin): has_replay = models.BooleanField(default=False, verbose_name=_("Replay")) has_command = models.BooleanField(default=False, verbose_name=_("Command")) terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL) - protocol = models.CharField(choices=PROTOCOL.choices, default='ssh', max_length=8, db_index=True) + protocol = models.CharField(choices=PROTOCOL.choices, default='ssh', max_length=16, db_index=True) date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True, default=timezone.now) date_end = models.DateTimeField(verbose_name=_("Date end"), null=True)