mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.3 KiB
60 lines
2.3 KiB
2 years ago
|
from django.utils.translation import ugettext_lazy as _
|
||
|
|
||
2 years ago
|
from common.utils import get_logger
|
||
2 years ago
|
from accounts.const import AutomationTypes, Source
|
||
2 years ago
|
from orgs.utils import tmp_to_org
|
||
|
from .filter import GatherAccountsFilter
|
||
2 years ago
|
from ..base.manager import AccountBasePlaybookManager
|
||
2 years ago
|
|
||
|
logger = get_logger(__name__)
|
||
|
|
||
|
|
||
2 years ago
|
class GatherAccountsManager(AccountBasePlaybookManager):
|
||
2 years ago
|
def __init__(self, *args, **kwargs):
|
||
|
super().__init__(*args, **kwargs)
|
||
|
self.host_asset_mapper = {}
|
||
|
|
||
|
@classmethod
|
||
|
def method_type(cls):
|
||
|
return AutomationTypes.gather_accounts
|
||
|
|
||
|
def host_callback(self, host, asset=None, **kwargs):
|
||
|
super().host_callback(host, asset=asset, **kwargs)
|
||
|
self.host_asset_mapper[host['name']] = asset
|
||
|
return host
|
||
|
|
||
|
def filter_success_result(self, host, result):
|
||
|
result = GatherAccountsFilter(host).run(self.method_id_meta_mapper, result)
|
||
|
return result
|
||
|
|
||
2 years ago
|
@staticmethod
|
||
|
def bulk_create_accounts(asset, result):
|
||
|
account_objs = []
|
||
|
account_model = asset.accounts.model
|
||
|
account_usernames = set(asset.accounts.values_list('username', flat=True))
|
||
|
with tmp_to_org(asset.org_id):
|
||
|
accounts_dict = {}
|
||
|
for username, data in result.items():
|
||
|
comment = ''
|
||
|
d = {'asset': asset, 'username': username, 'name': username, 'source': Source.COLLECTED}
|
||
|
if data.get('date'):
|
||
|
comment += f"{_('Date last login')}: {data['date']}\n "
|
||
|
if data.get('address'):
|
||
|
comment += f"{_('IP last login')}: {data['address'][:32]}"
|
||
|
d['comment'] = comment
|
||
|
accounts_dict[username] = d
|
||
|
for username, data in accounts_dict.items():
|
||
|
if username in account_usernames:
|
||
|
continue
|
||
|
account_objs.append(account_model(**data))
|
||
|
account_model.objects.bulk_create(account_objs)
|
||
|
|
||
2 years ago
|
def on_host_success(self, host, result):
|
||
|
info = result.get('debug', {}).get('res', {}).get('info', {})
|
||
|
asset = self.host_asset_mapper.get(host)
|
||
|
if asset and info:
|
||
|
result = self.filter_success_result(host, info)
|
||
2 years ago
|
self.bulk_create_accounts(asset, result)
|
||
2 years ago
|
else:
|
||
2 years ago
|
logger.error("Not found info".format(host))
|