2022-07-12 02:54:23 +00:00
|
|
|
# Generated by Django 3.2.12 on 2022-07-11 06:13
|
|
|
|
|
2022-07-13 08:36:49 +00:00
|
|
|
import time
|
2023-02-24 10:06:11 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from itertools import groupby
|
2023-02-02 08:16:38 +00:00
|
|
|
from django.db import migrations
|
2022-08-31 02:06:16 +00:00
|
|
|
|
|
|
|
|
2023-01-31 10:07:55 +00:00
|
|
|
def migrate_asset_accounts(apps, schema_editor):
|
2022-07-12 02:54:23 +00:00
|
|
|
auth_book_model = apps.get_model('assets', 'AuthBook')
|
2023-01-16 11:02:09 +00:00
|
|
|
account_model = apps.get_model('accounts', 'Account')
|
2023-02-24 10:06:11 +00:00
|
|
|
account_history_model = apps.get_model('accounts', 'HistoricalAccount')
|
2022-07-12 02:54:23 +00:00
|
|
|
|
2022-07-13 08:36:49 +00:00
|
|
|
count = 0
|
|
|
|
bulk_size = 1000
|
2023-01-31 10:07:55 +00:00
|
|
|
print("\n\tStart migrate asset accounts")
|
2022-07-13 08:36:49 +00:00
|
|
|
while True:
|
2022-07-14 02:56:09 +00:00
|
|
|
start = time.time()
|
2022-07-13 08:36:49 +00:00
|
|
|
auth_books = auth_book_model.objects \
|
2023-01-16 11:02:09 +00:00
|
|
|
.prefetch_related('systemuser') \
|
|
|
|
.all()[count:count + bulk_size]
|
2022-07-13 08:36:49 +00:00
|
|
|
if not auth_books:
|
|
|
|
break
|
|
|
|
|
2022-08-18 03:15:17 +00:00
|
|
|
count += len(auth_books)
|
2022-07-14 02:56:09 +00:00
|
|
|
# auth book 和 account 相同的属性
|
2022-07-13 08:36:49 +00:00
|
|
|
same_attrs = [
|
2022-09-20 05:54:25 +00:00
|
|
|
'id', 'username', 'comment', 'date_created', 'date_updated',
|
2022-07-13 08:36:49 +00:00
|
|
|
'created_by', 'asset_id', 'org_id',
|
|
|
|
]
|
2023-02-24 10:06:11 +00:00
|
|
|
# 认证的属性,可能是 auth_book 的,可能是 system_user 的
|
2022-09-20 05:54:25 +00:00
|
|
|
auth_attrs = ['password', 'private_key', 'token']
|
|
|
|
all_attrs = same_attrs + auth_attrs
|
2022-07-13 08:36:49 +00:00
|
|
|
|
2023-02-24 10:06:11 +00:00
|
|
|
accounts = []
|
2022-07-13 08:36:49 +00:00
|
|
|
for auth_book in auth_books:
|
2023-02-24 10:06:11 +00:00
|
|
|
account_values = {'version': 1}
|
2022-07-13 08:36:49 +00:00
|
|
|
|
|
|
|
system_user = auth_book.systemuser
|
2022-09-13 13:07:20 +00:00
|
|
|
if system_user:
|
2022-09-20 05:54:25 +00:00
|
|
|
# 更新一次系统用户的认证属性
|
2023-02-24 10:06:11 +00:00
|
|
|
account_values.update({attr: getattr(system_user, attr, '') for attr in all_attrs})
|
|
|
|
account_values['created_by'] = str(system_user.id)
|
|
|
|
account_values['privileged'] = system_user.type == 'admin' \
|
|
|
|
or system_user.username in ['root', 'Administrator']
|
2022-07-13 08:36:49 +00:00
|
|
|
|
2022-09-20 05:54:25 +00:00
|
|
|
auth_book_auth = {attr: getattr(auth_book, attr, '') for attr in all_attrs if getattr(auth_book, attr, '')}
|
2023-02-24 10:06:11 +00:00
|
|
|
# 最终优先使用 auth_book 的认证属性
|
|
|
|
account_values.update(auth_book_auth)
|
2022-07-13 08:36:49 +00:00
|
|
|
|
2022-09-20 05:54:25 +00:00
|
|
|
auth_infos = []
|
2023-02-24 10:06:11 +00:00
|
|
|
username = account_values['username']
|
2022-09-20 05:54:25 +00:00
|
|
|
for attr in auth_attrs:
|
2023-02-24 10:06:11 +00:00
|
|
|
secret = account_values.pop(attr, None)
|
2022-09-20 05:54:25 +00:00
|
|
|
if not secret:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if attr == 'private_key':
|
|
|
|
secret_type = 'ssh_key'
|
|
|
|
name = f'{username}(ssh key)'
|
|
|
|
elif attr == 'token':
|
|
|
|
secret_type = 'token'
|
|
|
|
name = f'{username}(token)'
|
|
|
|
else:
|
|
|
|
secret_type = attr
|
|
|
|
name = username
|
|
|
|
auth_infos.append((name, secret_type, secret))
|
|
|
|
|
|
|
|
if not auth_infos:
|
|
|
|
auth_infos.append((username, 'password', ''))
|
|
|
|
|
|
|
|
for name, secret_type, secret in auth_infos:
|
2023-02-27 06:00:17 +00:00
|
|
|
if not name:
|
|
|
|
continue
|
2023-02-24 10:06:11 +00:00
|
|
|
account = account_model(**account_values, name=name, secret=secret, secret_type=secret_type)
|
2022-09-20 05:54:25 +00:00
|
|
|
accounts.append(account)
|
2022-07-13 08:36:49 +00:00
|
|
|
|
2023-02-24 10:06:11 +00:00
|
|
|
accounts.sort(key=lambda x: (x.name, x.asset_id, x.date_updated))
|
|
|
|
grouped_accounts = groupby(accounts, lambda x: (x.name, x.asset_id))
|
|
|
|
|
|
|
|
accounts_to_add = []
|
|
|
|
accounts_to_history = []
|
|
|
|
for key, _accounts in grouped_accounts:
|
|
|
|
_accounts = list(_accounts)
|
|
|
|
if not _accounts:
|
|
|
|
continue
|
|
|
|
_account = _accounts[-1]
|
|
|
|
accounts_to_add.append(_account)
|
|
|
|
_account_history = []
|
|
|
|
|
|
|
|
for ac in _accounts:
|
|
|
|
if not ac.secret:
|
|
|
|
continue
|
|
|
|
if ac.id != _account.id and ac.secret == _account.secret:
|
|
|
|
continue
|
|
|
|
history_data = {
|
|
|
|
'id': _account.id,
|
|
|
|
'secret': ac.secret,
|
|
|
|
'secret_type': ac.secret_type,
|
|
|
|
'history_date': ac.date_updated,
|
|
|
|
'history_type': '~',
|
|
|
|
'history_change_reason': 'from account {}'.format(_account.name),
|
|
|
|
}
|
|
|
|
_account_history.append(account_history_model(**history_data))
|
|
|
|
_account.version = len(_account_history)
|
|
|
|
accounts_to_history.extend(_account_history)
|
|
|
|
|
|
|
|
account_model.objects.bulk_create(accounts_to_add, ignore_conflicts=True)
|
|
|
|
account_history_model.objects.bulk_create(accounts_to_history, ignore_conflicts=True)
|
2023-01-31 10:07:55 +00:00
|
|
|
print("\t - Create asset accounts: {}-{} using: {:.2f}s".format(
|
2023-01-16 11:02:09 +00:00
|
|
|
count - len(auth_books), count, time.time() - start
|
2022-07-13 08:36:49 +00:00
|
|
|
))
|
2023-02-24 10:06:11 +00:00
|
|
|
print("\t - accounts: {}".format(len(accounts_to_add)))
|
|
|
|
print("\t - histories: {}".format(len(accounts_to_history)))
|
2022-07-13 08:36:49 +00:00
|
|
|
|
2022-07-12 02:54:23 +00:00
|
|
|
|
2023-01-31 10:07:55 +00:00
|
|
|
def migrate_db_accounts(apps, schema_editor):
|
|
|
|
app_perm_model = apps.get_model('perms', 'ApplicationPermission')
|
|
|
|
account_model = apps.get_model('accounts', 'Account')
|
|
|
|
perms = app_perm_model.objects.filter(category__in=['db', 'cloud'])
|
|
|
|
|
|
|
|
same_attrs = [
|
2023-02-15 12:08:02 +00:00
|
|
|
'username', 'comment', 'date_created', 'date_updated',
|
2023-01-31 10:07:55 +00:00
|
|
|
'created_by', 'org_id',
|
|
|
|
]
|
|
|
|
auth_attrs = ['password', 'private_key', 'token']
|
|
|
|
all_attrs = same_attrs + auth_attrs
|
|
|
|
|
|
|
|
print("\n\tStart migrate app accounts")
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
total = perms.count()
|
|
|
|
|
|
|
|
for perm in perms:
|
|
|
|
index += 1
|
|
|
|
start = time.time()
|
|
|
|
|
2023-02-15 12:08:02 +00:00
|
|
|
apps = perm.applications.all()
|
2023-01-31 10:07:55 +00:00
|
|
|
system_users = perm.system_users.all()
|
|
|
|
accounts = []
|
|
|
|
for s in system_users:
|
|
|
|
values = {'version': 1}
|
|
|
|
values.update({attr: getattr(s, attr, '') for attr in all_attrs})
|
|
|
|
values['created_by'] = str(s.id)
|
|
|
|
|
|
|
|
auth_infos = []
|
|
|
|
username = values['username']
|
|
|
|
for attr in auth_attrs:
|
|
|
|
secret = values.pop(attr, None)
|
|
|
|
if not secret:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if attr == 'private_key':
|
|
|
|
secret_type = 'ssh_key'
|
|
|
|
name = f'{username}(ssh key)'
|
|
|
|
elif attr == 'token':
|
|
|
|
secret_type = 'token'
|
|
|
|
name = f'{username}(token)'
|
|
|
|
else:
|
|
|
|
secret_type = attr
|
|
|
|
name = username
|
|
|
|
auth_infos.append((name, secret_type, secret))
|
|
|
|
|
|
|
|
if not auth_infos:
|
|
|
|
auth_infos.append((username, 'password', ''))
|
|
|
|
|
|
|
|
for name, secret_type, secret in auth_infos:
|
2023-02-15 12:08:02 +00:00
|
|
|
values['name'] = name
|
|
|
|
values['secret_type'] = secret_type
|
|
|
|
values['secret'] = secret
|
|
|
|
|
2023-02-27 06:00:17 +00:00
|
|
|
if not name:
|
|
|
|
continue
|
|
|
|
|
2023-02-15 12:08:02 +00:00
|
|
|
for app in apps:
|
|
|
|
values['asset_id'] = str(app.id)
|
|
|
|
account = account_model(**values)
|
|
|
|
accounts.append(account)
|
2023-01-31 10:07:55 +00:00
|
|
|
|
|
|
|
account_model.objects.bulk_create(accounts, ignore_conflicts=True)
|
|
|
|
|
|
|
|
print("\t - Progress ({}/{}), Create app accounts: {} using: {:.2f}s".format(
|
|
|
|
index, total, len(accounts), time.time() - start
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2022-07-12 02:54:23 +00:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2023-01-16 11:02:09 +00:00
|
|
|
('accounts', '0001_initial'),
|
2022-08-24 08:14:32 +00:00
|
|
|
('assets', '0099_auto_20220711_1409'),
|
2022-07-12 02:54:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2023-01-31 10:07:55 +00:00
|
|
|
migrations.RunPython(migrate_asset_accounts),
|
|
|
|
migrations.RunPython(migrate_db_accounts),
|
2022-07-12 02:54:23 +00:00
|
|
|
]
|