2023-02-07 05:27:44 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
2022-10-27 10:53:10 +00:00
|
|
|
from common.utils import get_logger
|
2023-02-07 05:27:44 +00:00
|
|
|
from accounts.models import GatheredAccount
|
2023-01-16 11:02:09 +00:00
|
|
|
from accounts.const import AutomationTypes, Source
|
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 = {}
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2022-12-27 09:45:41 +00:00
|
|
|
@staticmethod
|
2023-02-07 05:27:44 +00:00
|
|
|
def update_or_create_gathered_accounts(asset, result):
|
2022-12-27 09:45:41 +00:00
|
|
|
with tmp_to_org(asset.org_id):
|
2023-02-07 05:27:44 +00:00
|
|
|
GatheredAccount.objects.filter(asset=asset, present=True).update(present=False)
|
2022-12-27 09:45:41 +00:00
|
|
|
for username, data in result.items():
|
2023-02-07 05:27:44 +00:00
|
|
|
d = {'asset': asset, 'username': username, 'present': True}
|
2022-12-27 09:45:41 +00:00
|
|
|
if data.get('date'):
|
2023-02-07 05:27:44 +00:00
|
|
|
d['date_last_login'] = data['date']
|
2022-12-27 09:45:41 +00:00
|
|
|
if data.get('address'):
|
2023-02-07 05:27:44 +00:00
|
|
|
d['address_last_login'] = data['address'][:32]
|
|
|
|
GatheredAccount.objects.update_or_create(
|
|
|
|
defaults=d, asset=asset, username=username,
|
|
|
|
)
|
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-02-07 05:27:44 +00:00
|
|
|
self.update_or_create_gathered_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))
|