2023-02-21 12:06:45 +00:00
|
|
|
from accounts.const import AutomationTypes
|
2023-02-07 05:27:44 +00:00
|
|
|
from accounts.models import GatheredAccount
|
2023-02-21 12:06:45 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from orgs.utils import tmp_to_org
|
2022-10-27 10:53:10 +00:00
|
|
|
from .filter import GatherAccountsFilter
|
2023-01-16 11:02:09 +00:00
|
|
|
from ..base.manager import AccountBasePlaybookManager
|
2022-10-27 10:53:10 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
class GatherAccountsManager(AccountBasePlaybookManager):
|
2022-10-27 10:53:10 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.host_asset_mapper = {}
|
2023-03-23 10:57:22 +00:00
|
|
|
self.is_sync_account = self.execution.snapshot.get('is_sync_account')
|
2022-10-27 10:53:10 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2023-02-28 10:45:38 +00:00
|
|
|
def filter_success_result(self, tp, result):
|
|
|
|
result = GatherAccountsFilter(tp).run(self.method_id_meta_mapper, result)
|
2022-10-27 10:53:10 +00:00
|
|
|
return result
|
2022-12-27 09:45:41 +00:00
|
|
|
@staticmethod
|
2023-03-23 10:57:22 +00:00
|
|
|
def generate_data(asset, result):
|
|
|
|
data = []
|
|
|
|
for username, info in result.items():
|
|
|
|
d = {'asset': asset, 'username': username, 'present': True}
|
|
|
|
if info.get('date'):
|
|
|
|
d['date_last_login'] = info['date']
|
|
|
|
if info.get('address'):
|
|
|
|
d['address_last_login'] = info['address'][:32]
|
|
|
|
data.append(d)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def update_or_create_accounts(self, asset, result):
|
|
|
|
data = self.generate_data(asset, result)
|
2022-12-27 09:45:41 +00:00
|
|
|
with tmp_to_org(asset.org_id):
|
2023-03-23 10:57:22 +00:00
|
|
|
gathered_accounts = []
|
2023-02-07 05:27:44 +00:00
|
|
|
GatheredAccount.objects.filter(asset=asset, present=True).update(present=False)
|
2023-03-23 10:57:22 +00:00
|
|
|
for d in data:
|
|
|
|
username = d['username']
|
|
|
|
gathered_account, __ = GatheredAccount.objects.update_or_create(
|
2023-02-07 05:27:44 +00:00
|
|
|
defaults=d, asset=asset, username=username,
|
|
|
|
)
|
2023-03-23 10:57:22 +00:00
|
|
|
gathered_accounts.append(gathered_account)
|
|
|
|
if not self.is_sync_account:
|
|
|
|
return
|
|
|
|
GatheredAccount.sync_accounts(gathered_accounts)
|
2022-12-27 09:45:41 +00:00
|
|
|
|
2022-10-27 10:53:10 +00:00
|
|
|
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:
|
2023-02-06 10:30:17 +00:00
|
|
|
result = self.filter_success_result(asset.type, info)
|
2023-03-23 10:57:22 +00:00
|
|
|
self.update_or_create_accounts(asset, result)
|
2022-10-27 10:53:10 +00:00
|
|
|
else:
|
2022-10-28 10:28:41 +00:00
|
|
|
logger.error("Not found info".format(host))
|