2023-11-07 05:00:09 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
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-11-07 05:00:09 +00:00
|
|
|
from assets.models import Asset
|
2023-02-21 12:06:45 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from orgs.utils import tmp_to_org
|
2023-11-07 05:00:09 +00:00
|
|
|
from users.models import User
|
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
|
2023-11-07 05:00:09 +00:00
|
|
|
from ...notifications import GatherAccountChangeMsg
|
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-11-15 08:44:35 +00:00
|
|
|
self.asset_account_info = {}
|
|
|
|
|
2023-11-07 05:00:09 +00:00
|
|
|
self.asset_username_mapper = defaultdict(set)
|
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
|
2023-11-07 05:00:09 +00:00
|
|
|
|
|
|
|
def generate_data(self, asset, result):
|
2023-03-23 10:57:22 +00:00
|
|
|
data = []
|
|
|
|
for username, info in result.items():
|
2023-11-07 05:00:09 +00:00
|
|
|
self.asset_username_mapper[str(asset.id)].add(username)
|
2023-03-23 10:57:22 +00:00
|
|
|
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
|
|
|
|
|
2023-11-15 08:44:35 +00:00
|
|
|
def collect_asset_account_info(self, asset, result):
|
2023-03-23 10:57:22 +00:00
|
|
|
data = self.generate_data(asset, result)
|
2023-11-15 08:44:35 +00:00
|
|
|
self.asset_account_info[asset] = data
|
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-11-15 08:44:35 +00:00
|
|
|
self.collect_asset_account_info(asset, result)
|
2022-10-27 10:53:10 +00:00
|
|
|
else:
|
2023-11-07 05:00:09 +00:00
|
|
|
logger.error(f'Not found {host} info')
|
|
|
|
|
2023-11-15 08:44:35 +00:00
|
|
|
def update_or_create_accounts(self):
|
|
|
|
for asset, data in self.asset_account_info.items():
|
|
|
|
with tmp_to_org(asset.org_id):
|
|
|
|
gathered_accounts = []
|
|
|
|
GatheredAccount.objects.filter(asset=asset, present=True).update(present=False)
|
|
|
|
for d in data:
|
|
|
|
username = d['username']
|
|
|
|
gathered_account, __ = GatheredAccount.objects.update_or_create(
|
|
|
|
defaults=d, asset=asset, username=username,
|
|
|
|
)
|
|
|
|
gathered_accounts.append(gathered_account)
|
|
|
|
if not self.is_sync_account:
|
|
|
|
return
|
|
|
|
GatheredAccount.sync_accounts(gathered_accounts)
|
|
|
|
|
2023-11-07 05:00:09 +00:00
|
|
|
def run(self, *args, **kwargs):
|
|
|
|
super().run(*args, **kwargs)
|
2023-11-15 08:44:35 +00:00
|
|
|
users, change_info = self.generate_send_users_and_change_info()
|
|
|
|
self.update_or_create_accounts()
|
|
|
|
self.send_email_if_need(users, change_info)
|
2023-11-07 05:00:09 +00:00
|
|
|
|
2023-11-15 08:44:35 +00:00
|
|
|
def generate_send_users_and_change_info(self):
|
2023-11-07 05:00:09 +00:00
|
|
|
recipients = self.execution.recipients
|
|
|
|
if not self.asset_username_mapper or not recipients:
|
2023-11-15 08:44:35 +00:00
|
|
|
return None, None
|
2023-11-07 05:00:09 +00:00
|
|
|
|
|
|
|
users = User.objects.filter(id__in=recipients)
|
|
|
|
if not users:
|
2023-11-15 08:44:35 +00:00
|
|
|
return users, None
|
2023-11-07 05:00:09 +00:00
|
|
|
|
|
|
|
asset_ids = self.asset_username_mapper.keys()
|
|
|
|
assets = Asset.objects.filter(id__in=asset_ids)
|
2023-11-15 08:44:35 +00:00
|
|
|
gather_accounts = GatheredAccount.objects.filter(asset_id__in=asset_ids, present=True)
|
2023-11-07 05:00:09 +00:00
|
|
|
asset_id_map = {str(asset.id): asset for asset in assets}
|
2023-11-15 08:44:35 +00:00
|
|
|
asset_id_username = list(assets.values_list('id', 'accounts__username'))
|
|
|
|
asset_id_username.extend(list(gather_accounts.values_list('asset_id', 'username')))
|
2023-11-07 05:00:09 +00:00
|
|
|
|
2023-11-15 08:44:35 +00:00
|
|
|
system_asset_username_mapper = defaultdict(set)
|
|
|
|
for asset_id, username in asset_id_username:
|
2023-11-07 05:00:09 +00:00
|
|
|
system_asset_username_mapper[str(asset_id)].add(username)
|
|
|
|
|
|
|
|
change_info = {}
|
|
|
|
for asset_id, usernames in self.asset_username_mapper.items():
|
|
|
|
system_usernames = system_asset_username_mapper.get(asset_id)
|
|
|
|
|
|
|
|
if not system_usernames:
|
|
|
|
continue
|
|
|
|
|
|
|
|
add_usernames = usernames - system_usernames
|
|
|
|
remove_usernames = system_usernames - usernames
|
|
|
|
k = f'{asset_id_map[asset_id]}[{asset_id}]'
|
|
|
|
|
2023-11-14 06:50:33 +00:00
|
|
|
if not add_usernames and not remove_usernames:
|
|
|
|
continue
|
|
|
|
|
2023-11-07 05:00:09 +00:00
|
|
|
change_info[k] = {
|
|
|
|
'add_usernames': ', '.join(add_usernames),
|
|
|
|
'remove_usernames': ', '.join(remove_usernames),
|
|
|
|
}
|
|
|
|
|
2023-11-15 08:44:35 +00:00
|
|
|
return users, change_info
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def send_email_if_need(users, change_info):
|
|
|
|
if not users or not change_info:
|
2023-11-07 05:00:09 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
for user in users:
|
|
|
|
GatherAccountChangeMsg(user, change_info).publish_async()
|