From 2c3239e2383cd8da0451e79de407f7c48193d360 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 18 Aug 2022 11:15:17 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=20migrations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0022_auto_20220816_1015.py | 24 --- ...817_1346.py => 0022_auto_20220817_1346.py} | 18 +-- .../migrations/0023_auto_20220816_1021.py | 21 --- .../migrations/0023_auto_20220817_1716.py | 26 ++++ .../migrations/0024_auto_20220818_1057.py | 23 +++ apps/applications/models.py | 28 ++++ .../migrations/0101_auto_20220711_1409.py | 8 +- .../migrations/0102_auto_20220711_1413.py | 4 +- .../migrations/0104_auto_20220803_1859.py | 5 +- .../migrations/0105_auto_20220810_1449.py | 5 + .../migrations/0106_auto_20220811_1358.py | 22 --- ...811_1449.py => 0106_auto_20220811_1449.py} | 2 +- ...811_1511.py => 0107_auto_20220811_1511.py} | 6 +- ...816_1022.py => 0108_auto_20220816_1022.py} | 9 +- .../migrations/0109_auto_20220815_1811.py | 36 ----- ...817_1544.py => 0109_auto_20220817_1544.py} | 19 +-- .../migrations/0110_auto_20220815_1831.py | 24 --- .../migrations/0110_auto_20220817_1716.py | 32 ++++ apps/assets/models/__init__.py | 2 +- apps/assets/models/_user.py | 137 ------------------ .../migrations/0020_auto_20220817_1346.py | 4 +- 21 files changed, 133 insertions(+), 322 deletions(-) delete mode 100644 apps/applications/migrations/0022_auto_20220816_1015.py rename apps/applications/migrations/{0024_auto_20220817_1346.py => 0022_auto_20220817_1346.py} (66%) delete mode 100644 apps/applications/migrations/0023_auto_20220816_1021.py create mode 100644 apps/applications/migrations/0023_auto_20220817_1716.py create mode 100644 apps/applications/migrations/0024_auto_20220818_1057.py create mode 100644 apps/applications/models.py delete mode 100644 apps/assets/migrations/0106_auto_20220811_1358.py rename apps/assets/migrations/{0107_auto_20220811_1449.py => 0106_auto_20220811_1449.py} (94%) rename apps/assets/migrations/{0108_auto_20220811_1511.py => 0107_auto_20220811_1511.py} (88%) rename apps/assets/migrations/{0111_auto_20220816_1022.py => 0108_auto_20220816_1022.py} (91%) delete mode 100644 apps/assets/migrations/0109_auto_20220815_1811.py rename apps/assets/migrations/{0112_auto_20220817_1544.py => 0109_auto_20220817_1544.py} (67%) delete mode 100644 apps/assets/migrations/0110_auto_20220815_1831.py create mode 100644 apps/assets/migrations/0110_auto_20220817_1716.py diff --git a/apps/applications/migrations/0022_auto_20220816_1015.py b/apps/applications/migrations/0022_auto_20220816_1015.py deleted file mode 100644 index 58e3291c9..000000000 --- a/apps/applications/migrations/0022_auto_20220816_1015.py +++ /dev/null @@ -1,24 +0,0 @@ -# Generated by Django 3.2.14 on 2022-08-16 02:15 - -import common.db.fields -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('applications', '0021_auto_20220629_1826'), - ] - - operations = [ - migrations.AddField( - model_name='account', - name='token', - field=common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token'), - ), - migrations.AddField( - model_name='historicalaccount', - name='token', - field=common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token'), - ), - ] diff --git a/apps/applications/migrations/0024_auto_20220817_1346.py b/apps/applications/migrations/0022_auto_20220817_1346.py similarity index 66% rename from apps/applications/migrations/0024_auto_20220817_1346.py rename to apps/applications/migrations/0022_auto_20220817_1346.py index a6d03a5ca..eae021831 100644 --- a/apps/applications/migrations/0024_auto_20220817_1346.py +++ b/apps/applications/migrations/0022_auto_20220817_1346.py @@ -6,7 +6,7 @@ from django.db import migrations class Migration(migrations.Migration): dependencies = [ - ('applications', '0023_auto_20220816_1021'), + ('applications', '0021_auto_20220629_1826'), ] operations = [ @@ -22,10 +22,6 @@ class Migration(migrations.Migration): model_name='account', name='systemuser', ), - migrations.AlterUniqueTogether( - name='application', - unique_together=None, - ), migrations.RemoveField( model_name='application', name='domain', @@ -42,16 +38,4 @@ class Migration(migrations.Migration): model_name='historicalaccount', name='systemuser', ), - migrations.DeleteModel( - name='ApplicationUser', - ), - migrations.DeleteModel( - name='Account', - ), - migrations.DeleteModel( - name='Application', - ), - migrations.DeleteModel( - name='HistoricalAccount', - ), ] diff --git a/apps/applications/migrations/0023_auto_20220816_1021.py b/apps/applications/migrations/0023_auto_20220816_1021.py deleted file mode 100644 index 34d95cbe6..000000000 --- a/apps/applications/migrations/0023_auto_20220816_1021.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 3.2.14 on 2022-08-16 02:21 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('applications', '0022_auto_20220816_1015'), - ] - - operations = [ - migrations.RemoveField( - model_name='account', - name='token', - ), - migrations.RemoveField( - model_name='historicalaccount', - name='token', - ), - ] diff --git a/apps/applications/migrations/0023_auto_20220817_1716.py b/apps/applications/migrations/0023_auto_20220817_1716.py new file mode 100644 index 000000000..b8b235fe5 --- /dev/null +++ b/apps/applications/migrations/0023_auto_20220817_1716.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.14 on 2022-08-17 09:16 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('applications', '0022_auto_20220817_1346'), + ('perms', '0031_auto_20220816_1600'), + ('ops', '0022_auto_20220817_1346'), + ('assets', '0109_auto_20220817_1544'), + ('tickets', '0020_auto_20220817_1346'), + ] + + operations = [ + migrations.DeleteModel( + name='Account', + ), + migrations.DeleteModel( + name='HistoricalAccount', + ), + migrations.DeleteModel( + name='ApplicationUser', + ), + ] diff --git a/apps/applications/migrations/0024_auto_20220818_1057.py b/apps/applications/migrations/0024_auto_20220818_1057.py new file mode 100644 index 000000000..7a0b8919a --- /dev/null +++ b/apps/applications/migrations/0024_auto_20220818_1057.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.14 on 2022-08-18 02:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('applications', '0023_auto_20220817_1716'), + ] + + operations = [ + migrations.AlterField( + model_name='application', + name='category', + field=models.CharField(max_length=16, verbose_name='Category'), + ), + migrations.AlterField( + model_name='application', + name='type', + field=models.CharField(max_length=16, verbose_name='Type'), + ), + ] diff --git a/apps/applications/models.py b/apps/applications/models.py new file mode 100644 index 000000000..f1b1bbc8a --- /dev/null +++ b/apps/applications/models.py @@ -0,0 +1,28 @@ + +from django.db import models +from django.utils.translation import ugettext_lazy as _ + +from orgs.mixins.models import OrgModelMixin +from common.mixins import CommonModelMixin + + +class Application(CommonModelMixin, OrgModelMixin): + name = models.CharField(max_length=128, verbose_name=_('Name')) + category = models.CharField( + max_length=16, verbose_name=_('Category') + ) + type = models.CharField( + max_length=16, verbose_name=_('Type') + ) + attrs = models.JSONField(default=dict, verbose_name=_('Attrs')) + comment = models.TextField( + max_length=128, default='', blank=True, verbose_name=_('Comment') + ) + + class Meta: + verbose_name = _('Application') + unique_together = [('org_id', 'name')] + ordering = ('name',) + permissions = [ + ('match_application', _('Can match application')), + ] diff --git a/apps/assets/migrations/0101_auto_20220711_1409.py b/apps/assets/migrations/0101_auto_20220711_1409.py index ff3b1a1c6..30dd8c51f 100644 --- a/apps/assets/migrations/0101_auto_20220711_1409.py +++ b/apps/assets/migrations/0101_auto_20220711_1409.py @@ -29,12 +29,12 @@ class Migration(migrations.Migration): ('password', common.db.fields.EncryptCharField(blank=True, max_length=256, null=True, verbose_name='Password')), ('private_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='SSH private key')), ('public_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='SSH public key')), + ('token', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token')), ('comment', models.TextField(blank=True, verbose_name='Comment')), ('date_created', models.DateTimeField(blank=True, editable=False, verbose_name='Date created')), ('date_updated', models.DateTimeField(blank=True, editable=False, verbose_name='Date updated')), ('created_by', models.CharField(max_length=128, null=True, verbose_name='Created by')), - ('protocol', models.CharField(choices=[('ssh', 'SSH'), ('rdp', 'RDP'), ('telnet', 'Telnet'), ('vnc', 'VNC'), ('mysql', 'MySQL'), ('oracle', 'Oracle'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('sqlserver', 'SQLServer'), ('redis', 'Redis'), ('mongodb', 'MongoDB'), ('k8s', 'K8S')], default='ssh', max_length=16, verbose_name='Protocol')), - ('type', models.CharField(choices=[('common', 'Common user'), ('admin', 'Admin user')], default='common', max_length=16, verbose_name='Type')), + ('privileged', models.BooleanField(default=False, verbose_name='Privileged account')), ('version', models.IntegerField(default=0, verbose_name='Version')), ('history_id', models.AutoField(primary_key=True, serialize=False)), ('history_date', models.DateTimeField(db_index=True)), @@ -63,12 +63,12 @@ class Migration(migrations.Migration): ('password', common.db.fields.EncryptCharField(blank=True, max_length=256, null=True, verbose_name='Password')), ('private_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='SSH private key')), ('public_key', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='SSH public key')), + ('token', common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token')), ('comment', models.TextField(blank=True, verbose_name='Comment')), ('date_created', models.DateTimeField(auto_now_add=True, verbose_name='Date created')), ('date_updated', models.DateTimeField(auto_now=True, verbose_name='Date updated')), ('created_by', models.CharField(max_length=128, null=True, verbose_name='Created by')), - ('protocol', models.CharField(choices=[('ssh', 'SSH'), ('rdp', 'RDP'), ('telnet', 'Telnet'), ('vnc', 'VNC'), ('mysql', 'MySQL'), ('oracle', 'Oracle'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('sqlserver', 'SQLServer'), ('redis', 'Redis'), ('mongodb', 'MongoDB'), ('k8s', 'K8S')], default='ssh', max_length=16, verbose_name='Protocol')), - ('type', models.CharField(choices=[('common', 'Common user'), ('admin', 'Admin user')], default='common', max_length=16, verbose_name='Type')), + ('privileged', models.BooleanField(default=False, verbose_name='Privileged account')), ('version', models.IntegerField(default=0, verbose_name='Version')), ('asset', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='assets.asset', verbose_name='Asset')), ], diff --git a/apps/assets/migrations/0102_auto_20220711_1413.py b/apps/assets/migrations/0102_auto_20220711_1413.py index bc74f336c..e1e87d2f2 100644 --- a/apps/assets/migrations/0102_auto_20220711_1413.py +++ b/apps/assets/migrations/0102_auto_20220711_1413.py @@ -16,10 +16,10 @@ def migrate_accounts(apps, schema_editor): auth_books = auth_book_model.objects \ .prefetch_related('systemuser') \ .all()[count:count+bulk_size] - count += len(auth_books) if not auth_books: break + count += len(auth_books) accounts = [] # auth book 和 account 相同的属性 same_attrs = [ @@ -50,7 +50,7 @@ def migrate_accounts(apps, schema_editor): account_model.objects.bulk_create(accounts, ignore_conflicts=True) print("Create accounts: {}-{} using: {:.2f}s".format( - count - bulk_size, count, time.time()-start + count - len(auth_books), count, time.time()-start )) diff --git a/apps/assets/migrations/0104_auto_20220803_1859.py b/apps/assets/migrations/0104_auto_20220803_1859.py index f1836f713..ad5d99ef5 100644 --- a/apps/assets/migrations/0104_auto_20220803_1859.py +++ b/apps/assets/migrations/0104_auto_20220803_1859.py @@ -15,10 +15,9 @@ def migrate_asset_protocols(apps, schema_editor): while True: start = time.time() assets = asset_model.objects.all()[count:count+bulk_size] - count += len(assets) if not assets: break - + count += len(assets) assets_protocols = [] for asset in assets: old_protocols = asset._protocols @@ -38,7 +37,7 @@ def migrate_asset_protocols(apps, schema_editor): assets_protocols.append(asset_protocol_through(asset_id=asset.id, protocol_id=protocol.id)) asset_model.protocols.through.objects.bulk_create(assets_protocols, ignore_conflicts=True) print("Create asset protocols: {}-{} using: {:.2f}s".format( - count - bulk_size, count, time.time()-start + count - len(assets), count, time.time()-start )) diff --git a/apps/assets/migrations/0105_auto_20220810_1449.py b/apps/assets/migrations/0105_auto_20220810_1449.py index 18b933338..a305c8156 100644 --- a/apps/assets/migrations/0105_auto_20220810_1449.py +++ b/apps/assets/migrations/0105_auto_20220810_1449.py @@ -38,4 +38,9 @@ class Migration(migrations.Migration): 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'), ('general', 'General'), ('k8s', 'Kubernetes')], default='Linux', max_length=32, verbose_name='Type'), ), + migrations.AlterField( + model_name='platform', + name='su_enabled', + field=models.BooleanField(default=False, verbose_name='Su enabled'), + ), ] diff --git a/apps/assets/migrations/0106_auto_20220811_1358.py b/apps/assets/migrations/0106_auto_20220811_1358.py deleted file mode 100644 index 63b7509dd..000000000 --- a/apps/assets/migrations/0106_auto_20220811_1358.py +++ /dev/null @@ -1,22 +0,0 @@ -# Generated by Django 3.2.14 on 2022-08-11 05:58 - -from django.db import migrations, models -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - ('assets', '0105_auto_20220810_1449'), - ] - - operations = [ - migrations.RemoveField( - model_name='account', - name='protocol', - ), - migrations.RemoveField( - model_name='historicalaccount', - name='protocol', - ), - ] diff --git a/apps/assets/migrations/0107_auto_20220811_1449.py b/apps/assets/migrations/0106_auto_20220811_1449.py similarity index 94% rename from apps/assets/migrations/0107_auto_20220811_1449.py rename to apps/assets/migrations/0106_auto_20220811_1449.py index f5aacbecf..decab283b 100644 --- a/apps/assets/migrations/0107_auto_20220811_1449.py +++ b/apps/assets/migrations/0106_auto_20220811_1449.py @@ -6,7 +6,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('assets', '0106_auto_20220811_1358'), + ('assets', '0105_auto_20220810_1449'), ] operations = [ diff --git a/apps/assets/migrations/0108_auto_20220811_1511.py b/apps/assets/migrations/0107_auto_20220811_1511.py similarity index 88% rename from apps/assets/migrations/0108_auto_20220811_1511.py rename to apps/assets/migrations/0107_auto_20220811_1511.py index b4bca4599..c95e336e3 100644 --- a/apps/assets/migrations/0108_auto_20220811_1511.py +++ b/apps/assets/migrations/0107_auto_20220811_1511.py @@ -6,7 +6,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('assets', '0107_auto_20220811_1449'), + ('assets', '0106_auto_20220811_1449'), ] operations = [ @@ -34,8 +34,4 @@ class Migration(migrations.Migration): name='created_by', field=models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by'), ), - migrations.AlterUniqueTogether( - name='asset', - unique_together={('org_id', 'name')}, - ), ] diff --git a/apps/assets/migrations/0111_auto_20220816_1022.py b/apps/assets/migrations/0108_auto_20220816_1022.py similarity index 91% rename from apps/assets/migrations/0111_auto_20220816_1022.py rename to apps/assets/migrations/0108_auto_20220816_1022.py index 55c286ce5..90606c2bf 100644 --- a/apps/assets/migrations/0111_auto_20220816_1022.py +++ b/apps/assets/migrations/0108_auto_20220816_1022.py @@ -13,11 +13,10 @@ def migrate_command_filter_to_assets(apps, schema_editor): while True: start = time.time() command_filters = command_filter_model.objects.all() \ - .prefetch_related('system_users')[count:count + bulk_size] - count += len(command_filters) + .prefetch_related('system_users')[count:count + bulk_size] if not command_filters: break - + count += len(command_filters) updated = [] for command_filter in command_filters: command_filter.accounts = [s.username for s in command_filter.system_users.all()] @@ -25,7 +24,7 @@ def migrate_command_filter_to_assets(apps, schema_editor): command_filter_model.objects.bulk_update(updated, ['accounts']) print("Create assets: {}-{} using: {:.2f}s".format( - count - bulk_size, count, time.time() - start + count - len(command_filters), count, time.time() - start )) @@ -47,7 +46,7 @@ def migrate_command_filter_apps(apps, schema_editor): class Migration(migrations.Migration): dependencies = [ - ('assets', '0110_auto_20220815_1831'), + ('assets', '0107_auto_20220811_1511'), ] operations = [ diff --git a/apps/assets/migrations/0109_auto_20220815_1811.py b/apps/assets/migrations/0109_auto_20220815_1811.py deleted file mode 100644 index c79715533..000000000 --- a/apps/assets/migrations/0109_auto_20220815_1811.py +++ /dev/null @@ -1,36 +0,0 @@ -# Generated by Django 3.2.14 on 2022-08-15 10:11 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('assets', '0108_auto_20220811_1511'), - ] - - operations = [ - migrations.RemoveField( - model_name='account', - name='type', - ), - migrations.RemoveField( - model_name='historicalaccount', - name='type', - ), - migrations.AddField( - model_name='account', - name='privileged', - field=models.BooleanField(default=False, verbose_name='Privileged account'), - ), - migrations.AddField( - model_name='historicalaccount', - name='privileged', - field=models.BooleanField(default=False, verbose_name='Privileged account'), - ), - migrations.AlterField( - model_name='platform', - name='su_enabled', - field=models.BooleanField(default=False, verbose_name='Su enabled'), - ), - ] diff --git a/apps/assets/migrations/0112_auto_20220817_1544.py b/apps/assets/migrations/0109_auto_20220817_1544.py similarity index 67% rename from apps/assets/migrations/0112_auto_20220817_1544.py rename to apps/assets/migrations/0109_auto_20220817_1544.py index d1ba8875b..d9ca483fa 100644 --- a/apps/assets/migrations/0112_auto_20220817_1544.py +++ b/apps/assets/migrations/0109_auto_20220817_1544.py @@ -6,7 +6,7 @@ from django.db import migrations class Migration(migrations.Migration): dependencies = [ - ('assets', '0111_auto_20220816_1022'), + ('assets', '0108_auto_20220816_1022'), ] operations = [ @@ -25,10 +25,6 @@ class Migration(migrations.Migration): model_name='historicalauthbook', name='systemuser', ), - migrations.AlterUniqueTogether( - name='systemuser', - unique_together=None, - ), migrations.RemoveField( model_name='systemuser', name='assets', @@ -41,21 +37,8 @@ class Migration(migrations.Migration): model_name='systemuser', name='nodes', ), - migrations.RemoveField( - model_name='systemuser', - name='su_from', - ), migrations.RemoveField( model_name='systemuser', name='users', ), - migrations.DeleteModel( - name='AuthBook', - ), - migrations.DeleteModel( - name='HistoricalAuthBook', - ), - migrations.DeleteModel( - name='SystemUser', - ), ] diff --git a/apps/assets/migrations/0110_auto_20220815_1831.py b/apps/assets/migrations/0110_auto_20220815_1831.py deleted file mode 100644 index e3d55ced6..000000000 --- a/apps/assets/migrations/0110_auto_20220815_1831.py +++ /dev/null @@ -1,24 +0,0 @@ -# Generated by Django 3.2.14 on 2022-08-15 10:31 - -import common.db.fields -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('assets', '0109_auto_20220815_1811'), - ] - - operations = [ - migrations.AddField( - model_name='account', - name='token', - field=common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token'), - ), - migrations.AddField( - model_name='historicalaccount', - name='token', - field=common.db.fields.EncryptTextField(blank=True, null=True, verbose_name='Token'), - ), - ] diff --git a/apps/assets/migrations/0110_auto_20220817_1716.py b/apps/assets/migrations/0110_auto_20220817_1716.py new file mode 100644 index 000000000..6fdaadd52 --- /dev/null +++ b/apps/assets/migrations/0110_auto_20220817_1716.py @@ -0,0 +1,32 @@ +# Generated by Django 3.2.14 on 2022-08-17 09:16 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0109_auto_20220817_1544'), + ('applications', '0022_auto_20220817_1346'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='authbook', + unique_together=None, + ), + migrations.RemoveField( + model_name='authbook', + name='asset', + ), + migrations.RemoveField( + model_name='authbook', + name='systemuser', + ), + migrations.DeleteModel( + name='HistoricalAuthBook', + ), + migrations.DeleteModel( + name='AuthBook', + ), + ] diff --git a/apps/assets/models/__init__.py b/apps/assets/models/__init__.py index d81856923..6b4bd31a1 100644 --- a/apps/assets/models/__init__.py +++ b/apps/assets/models/__init__.py @@ -1,6 +1,5 @@ from .base import * from .platform import * -# from ._user import * from .asset import * from .label import Label from .group import * @@ -11,6 +10,7 @@ from .gathered_user import * from .favorite_asset import * from .account import * from .backup import * +from ._user import * # 废弃以下 # from ._authbook import * from .protocol import * diff --git a/apps/assets/models/_user.py b/apps/assets/models/_user.py index 8f5c9c9d1..83aaec2c0 100644 --- a/apps/assets/models/_user.py +++ b/apps/assets/models/_user.py @@ -8,8 +8,6 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from django.core.validators import MinValueValidator, MaxValueValidator -from assets.const import Protocol -from common.utils import signer from .base import BaseAccount from .protocol import ProtocolMixin @@ -31,14 +29,6 @@ class SystemUser(ProtocolMixin, BaseAccount): admin = 'admin', _('Admin user') username_same_with_user = models.BooleanField(default=False, verbose_name=_("Username same with user")) - nodes = models.ManyToManyField('assets.Node', blank=True, verbose_name=_("Nodes")) - assets = models.ManyToManyField( - 'assets.Asset', blank=True, verbose_name=_("Assets"), - through='assets.AuthBook', through_fields=['systemuser', 'asset'], - related_name='system_users' - ) - users = models.ManyToManyField('users.User', blank=True, verbose_name=_("Users")) - groups = models.ManyToManyField('users.UserGroup', blank=True, verbose_name=_("User groups")) type = models.CharField(max_length=16, choices=Type.choices, default=Type.common, verbose_name=_('Type')) priority = models.IntegerField(default=81, verbose_name=_("Priority"), help_text=_("1-100, the lower the value will be match first"), validators=[MinValueValidator(1), MaxValueValidator(100)]) protocol = models.CharField(max_length=16, default='ssh', verbose_name=_('Protocol')) @@ -55,84 +45,6 @@ class SystemUser(ProtocolMixin, BaseAccount): su_enabled = models.BooleanField(default=False, verbose_name=_('User switch')) su_from = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='su_to', null=True, verbose_name=_("Switch from")) - def __str__(self): - username = self.username - if self.username_same_with_user: - username = '*' - return '{0.name}({1})'.format(self, username) - - @property - def nodes_amount(self): - return self.nodes.all().count() - - @property - def login_mode_display(self): - return self.get_login_mode_display() - - def is_need_push(self): - if self.auto_push and self.is_protocol_support_push: - return True - else: - return False - - @property - def is_admin_user(self): - return self.type == self.Type.admin - - @property - def is_need_cmd_filter(self): - return self.protocol not in [self.Protocol.rdp, self.Protocol.vnc] - - @property - def is_need_test_asset_connective(self): - return self.protocol in self.ASSET_CATEGORY_PROTOCOLS - - @property - def cmd_filter_rules(self): - from .cmd_filter import CommandFilterRule - rules = CommandFilterRule.objects.filter( - filter__in=self.cmd_filters.all() - ).distinct() - return rules - - def is_command_can_run(self, command): - for rule in self.cmd_filter_rules: - action, matched_cmd = rule.match(command) - if action == rule.ActionChoices.allow: - return True, None - elif action == rule.ActionChoices.deny: - return False, matched_cmd - return True, None - - def get_all_assets(self): - from assets.models import Node, Asset - nodes_keys = self.nodes.all().values_list('key', flat=True) - asset_ids = set(self.assets.all().values_list('id', flat=True)) - nodes_asset_ids = Node.get_nodes_all_asset_ids_by_keys(nodes_keys) - asset_ids.update(nodes_asset_ids) - assets = Asset.objects.filter(id__in=asset_ids) - return assets - - def add_related_assets(self, assets_or_ids): - self.assets.add(*tuple(assets_or_ids)) - self.add_related_assets_to_su_from_if_need(assets_or_ids) - - def add_related_assets_to_su_from_if_need(self, assets_or_ids): - if self.protocol not in [self.Protocol.ssh.value]: - return - if not self.su_enabled: - return - if not self.su_from: - return - if self.su_from.protocol != self.protocol: - return - self.su_from.assets.add(*tuple(assets_or_ids)) - - # TODO 暂时为了接口文档添加 - @property - def auto_push_account(self): - return - class Meta: ordering = ['name'] unique_together = [('name', 'org_id')] @@ -140,52 +52,3 @@ class SystemUser(ProtocolMixin, BaseAccount): permissions = [ ('match_systemuser', _('Can match system user')), ] - - -# Deprecated: 准备废弃 -class AdminUser(BaseAccount): - """ - A privileged user that ansible can use it to push system user and so on - """ - BECOME_METHOD_CHOICES = ( - ('sudo', 'sudo'), - ('su', 'su'), - ) - become = models.BooleanField(default=True) - become_method = models.CharField(choices=BECOME_METHOD_CHOICES, default='sudo', max_length=4) - become_user = models.CharField(default='root', max_length=64) - _become_pass = models.CharField(default='', blank=True, max_length=128) - CONNECTIVITY_CACHE_KEY = '_ADMIN_USER_CONNECTIVE_{}' - _prefer = "admin_user" - - def __str__(self): - return self.name - - @property - def become_pass(self): - password = signer.unsign(self._become_pass) - if password: - return password - else: - return "" - - @become_pass.setter - def become_pass(self, password): - self._become_pass = signer.sign(password) - - @property - def become_info(self): - if self.become: - info = { - "method": self.become_method, - "user": self.become_user, - "pass": self.become_pass, - } - else: - info = None - return info - - class Meta: - ordering = ['name'] - unique_together = [('name', 'org_id')] - verbose_name = _("Admin user") diff --git a/apps/tickets/migrations/0020_auto_20220817_1346.py b/apps/tickets/migrations/0020_auto_20220817_1346.py index 76437f936..b657e7fd3 100644 --- a/apps/tickets/migrations/0020_auto_20220817_1346.py +++ b/apps/tickets/migrations/0020_auto_20220817_1346.py @@ -34,12 +34,12 @@ def migrate_system_to_account(apps, schema_editor): new_value = [s.username for s in old_value] else: old_value = getattr(obj, old_field) - new_value = old_value.username + new_value = old_value.username if old_value else '' setattr(obj, new_field, new_value) updated.append(obj) model.objects.bulk_update(updated, [new_field]) print("Migrate account: {}-{} using: {:.2f}s".format( - count - bulk_size, count, time.time()-start + count - len(objects), count, time.time()-start ))