From 372196ca37d179ff451d03c68fc0932732948c93 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 29 Oct 2024 19:24:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20discover=20account?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/accounts/api/automations/gather_accounts.py | 13 ++++++++----- .../accounts/automations/gather_accounts/manager.py | 11 ++++++++++- apps/accounts/filters.py | 4 ++-- apps/accounts/models/automations/gather_account.py | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/apps/accounts/api/automations/gather_accounts.py b/apps/accounts/api/automations/gather_accounts.py index 29d3d6c00..877571565 100644 --- a/apps/accounts/api/automations/gather_accounts.py +++ b/apps/accounts/api/automations/gather_accounts.py @@ -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) diff --git a/apps/accounts/automations/gather_accounts/manager.py b/apps/accounts/automations/gather_accounts/manager.py index c072fa697..65e6f26fa 100644 --- a/apps/accounts/automations/gather_accounts/manager.py +++ b/apps/accounts/automations/gather_accounts/manager.py @@ -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) \ diff --git a/apps/accounts/filters.py b/apps/accounts/filters.py index 50593dcb7..75fe529cc 100644 --- a/apps/accounts/filters.py +++ b/apps/accounts/filters.py @@ -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 diff --git a/apps/accounts/models/automations/gather_account.py b/apps/accounts/models/automations/gather_account.py index 19266432c..270c9f23a 100644 --- a/apps/accounts/models/automations/gather_account.py +++ b/apps/accounts/models/automations/gather_account.py @@ -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)