mirror of https://github.com/jumpserver/jumpserver
perf: 修改 discover account
parent
114a6bf87c
commit
372196ca37
|
@ -60,7 +60,11 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
|
||||||
def status(self, request, *args, **kwargs):
|
def status(self, request, *args, **kwargs):
|
||||||
instance = self.get_object()
|
instance = self.get_object()
|
||||||
instance.status = request.data.get('status')
|
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)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(methods=['get'], detail=False, url_path='discover')
|
@action(methods=['get'], detail=False, url_path='discover')
|
||||||
|
@ -74,18 +78,17 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
|
||||||
'assets': [asset_id],
|
'assets': [asset_id],
|
||||||
'nodes': [],
|
'nodes': [],
|
||||||
'type': 'gather_accounts',
|
'type': 'gather_accounts',
|
||||||
'is_sync_account': True,
|
'is_sync_account': False,
|
||||||
'name': 'Adhoc gather accounts: {}'.format(asset_id),
|
'name': 'Adhoc gather accounts: {}'.format(asset_id),
|
||||||
}
|
}
|
||||||
execution.save()
|
execution.save()
|
||||||
execution.start()
|
execution.start()
|
||||||
accounts = self.model.objects.filter(asset=asset)
|
accounts = self.model.objects.filter(asset=asset)
|
||||||
serializer = self.get_serializer(accounts, many=True)
|
return self.get_paginated_response_from_queryset(accounts)
|
||||||
return Response(status=status.HTTP_200_OK, data=serializer.data)
|
|
||||||
|
|
||||||
@action(methods=['post'], detail=False, url_path='sync-accounts')
|
@action(methods=['post'], detail=False, url_path='sync-accounts')
|
||||||
def sync_accounts(self, request, *args, **kwargs):
|
def sync_accounts(self, request, *args, **kwargs):
|
||||||
gathered_account_ids = request.data.get('gathered_account_ids')
|
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)
|
self.model.sync_accounts(gathered_accounts)
|
||||||
return Response(status=status.HTTP_201_CREATED)
|
return Response(status=status.HTTP_201_CREATED)
|
||||||
|
|
|
@ -71,6 +71,7 @@ class GatherAccountsManager(AccountBasePlaybookManager):
|
||||||
|
|
||||||
def update_or_create_accounts(self):
|
def update_or_create_accounts(self):
|
||||||
for asset, data in self.asset_account_info.items():
|
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)):
|
with (tmp_to_org(asset.org_id)):
|
||||||
gathered_accounts = []
|
gathered_accounts = []
|
||||||
# 把所有的设置为 present = False, 创建的时候如果有就会更新
|
# 把所有的设置为 present = False, 创建的时候如果有就会更新
|
||||||
|
@ -81,7 +82,15 @@ class GatherAccountsManager(AccountBasePlaybookManager):
|
||||||
defaults=d, asset=asset, username=username,
|
defaults=d, asset=asset, username=username,
|
||||||
)
|
)
|
||||||
gathered_accounts.append(gathered_account)
|
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 \
|
GatheredAccount.objects \
|
||||||
.filter(asset=asset, present=False) \
|
.filter(asset=asset, present=False) \
|
||||||
.exclude(status=ConfirmOrIgnore.ignored) \
|
.exclude(status=ConfirmOrIgnore.ignored) \
|
||||||
|
|
|
@ -77,7 +77,7 @@ class AccountFilterSet(BaseFilterSet):
|
||||||
if name == 'latest_discovery':
|
if name == 'latest_discovery':
|
||||||
kwargs.update({'date_created__gte': date, 'source': 'collected'})
|
kwargs.update({'date_created__gte': date, 'source': 'collected'})
|
||||||
elif name == 'latest_accessed':
|
elif name == 'latest_accessed':
|
||||||
kwargs.update({'date_last_access__gte': date})
|
kwargs.update({'date_last_login__gte': date})
|
||||||
elif name == 'latest_updated':
|
elif name == 'latest_updated':
|
||||||
kwargs.update({'date_updated__gte': date})
|
kwargs.update({'date_updated__gte': date})
|
||||||
elif name == 'latest_secret_changed':
|
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')
|
queryset = queryset.filter(date_change_secret__gt=date).exclude(change_secret_status='ok')
|
||||||
|
|
||||||
if kwargs:
|
if kwargs:
|
||||||
queryset = queryset.filter(date_last_access__gte=date)
|
queryset = queryset.filter(date_last_login__gte=date)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -45,7 +45,7 @@ class GatheredAccount(JMSOrgBaseModel):
|
||||||
account = Account(
|
account = Account(
|
||||||
asset_id=asset_id, username=username,
|
asset_id=asset_id, username=username,
|
||||||
name=username, source=Source.COLLECTED,
|
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
|
access_by=access_by
|
||||||
)
|
)
|
||||||
account_objs.append(account)
|
account_objs.append(account)
|
||||||
|
|
Loading…
Reference in New Issue