perf: 修改 discover account

pull/14387/head
ibuler 4 weeks ago
parent 114a6bf87c
commit 372196ca37

@ -60,7 +60,11 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
def status(self, request, *args, **kwargs):
instance = self.get_object()
instance.status = request.data.get('status')
instance.save()
instance.save(update_fields=['status'])
if instance.status == 'confirmed':
GatheredAccount.sync_accounts([instance])
return Response(status=status.HTTP_200_OK)
@action(methods=['get'], detail=False, url_path='discover')
@ -74,18 +78,17 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
'assets': [asset_id],
'nodes': [],
'type': 'gather_accounts',
'is_sync_account': True,
'is_sync_account': False,
'name': 'Adhoc gather accounts: {}'.format(asset_id),
}
execution.save()
execution.start()
accounts = self.model.objects.filter(asset=asset)
serializer = self.get_serializer(accounts, many=True)
return Response(status=status.HTTP_200_OK, data=serializer.data)
return self.get_paginated_response_from_queryset(accounts)
@action(methods=['post'], detail=False, url_path='sync-accounts')
def sync_accounts(self, request, *args, **kwargs):
gathered_account_ids = request.data.get('gathered_account_ids')
gathered_accounts = self.model.objects.filter(id__in=gathered_account_ids)
gathered_accounts = self.model.objects.filter(id__in=gathered_account_ids).filter(status='')
self.model.sync_accounts(gathered_accounts)
return Response(status=status.HTTP_201_CREATED)

@ -71,6 +71,7 @@ class GatherAccountsManager(AccountBasePlaybookManager):
def update_or_create_accounts(self):
for asset, data in self.asset_account_info.items():
asset_accounts_usernames = set(asset.accounts.values_list('username', flat=True))
with (tmp_to_org(asset.org_id)):
gathered_accounts = []
# 把所有的设置为 present = False, 创建的时候如果有就会更新
@ -81,7 +82,15 @@ class GatherAccountsManager(AccountBasePlaybookManager):
defaults=d, asset=asset, username=username,
)
gathered_accounts.append(gathered_account)
# 不存在的标识为待处理
# 账号中不存在的标识为待处理的, 有可能是账号那边删除了
GatheredAccount.objects \
.filter(asset=asset, present=True) \
.exclude(username__in=asset_accounts_usernames) \
.exclude(status=ConfirmOrIgnore.ignored) \
.update(status='')
# 远端资产上不存在的,标识为待处理,需要管理员介入
GatheredAccount.objects \
.filter(asset=asset, present=False) \
.exclude(status=ConfirmOrIgnore.ignored) \

@ -77,7 +77,7 @@ class AccountFilterSet(BaseFilterSet):
if name == 'latest_discovery':
kwargs.update({'date_created__gte': date, 'source': 'collected'})
elif name == 'latest_accessed':
kwargs.update({'date_last_access__gte': date})
kwargs.update({'date_last_login__gte': date})
elif name == 'latest_updated':
kwargs.update({'date_updated__gte': date})
elif name == 'latest_secret_changed':
@ -87,7 +87,7 @@ class AccountFilterSet(BaseFilterSet):
queryset = queryset.filter(date_change_secret__gt=date).exclude(change_secret_status='ok')
if kwargs:
queryset = queryset.filter(date_last_access__gte=date)
queryset = queryset.filter(date_last_login__gte=date)
return queryset
@staticmethod

@ -45,7 +45,7 @@ class GatheredAccount(JMSOrgBaseModel):
account = Account(
asset_id=asset_id, username=username,
name=username, source=Source.COLLECTED,
date_last_access=gathered_account.date_last_login,
date_last_login=gathered_account.date_last_login,
access_by=access_by
)
account_objs.append(account)

Loading…
Cancel
Save