jumpserver/apps/accounts/automations/remove_account/manager.py

85 lines
2.9 KiB
Python
Raw Normal View History

2023-12-06 10:48:35 +00:00
import os
2024-11-27 11:33:58 +00:00
from collections import defaultdict
2023-12-06 10:48:35 +00:00
from copy import deepcopy
from django.db.models import QuerySet
from accounts.const import AutomationTypes
2024-12-03 09:49:04 +00:00
from accounts.models import Account, GatheredAccount, AccountRisk
2023-12-06 10:48:35 +00:00
from common.utils import get_logger
from ..base.manager import AccountBasePlaybookManager
logger = get_logger(__name__)
class RemoveAccountManager(AccountBasePlaybookManager):
2024-12-03 09:49:04 +00:00
super_accounts = ["root", "administrator"]
2023-12-06 10:48:35 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2024-11-27 11:33:58 +00:00
self.host_account_mapper = dict()
self.host_accounts = defaultdict(list)
2024-12-03 09:49:04 +00:00
snapshot_account = self.execution.snapshot.get("accounts", [])
2024-11-27 11:33:58 +00:00
self.snapshot_asset_account_map = defaultdict(list)
for account in snapshot_account:
2024-12-03 09:49:04 +00:00
self.snapshot_asset_account_map[str(account["asset"])].append(account)
2023-12-06 10:48:35 +00:00
def prepare_runtime_dir(self):
path = super().prepare_runtime_dir()
2024-12-03 09:49:04 +00:00
ansible_config_path = os.path.join(path, "ansible.cfg")
2023-12-06 10:48:35 +00:00
2024-12-03 09:49:04 +00:00
with open(ansible_config_path, "w") as f:
f.write("[ssh_connection]\n")
f.write("ssh_args = -o ControlMaster=no -o ControlPersist=no\n")
2023-12-06 10:48:35 +00:00
return path
@classmethod
def method_type(cls):
return AutomationTypes.remove_account
2024-12-03 09:49:04 +00:00
def host_callback(
self, host, asset=None, account=None, automation=None, path_dir=None, **kwargs
):
if host.get("error"):
2023-12-06 10:48:35 +00:00
return host
inventory_hosts = []
2024-11-27 11:33:58 +00:00
accounts_to_remove = self.snapshot_asset_account_map.get(str(asset.id), [])
2023-12-06 10:48:35 +00:00
2024-11-27 11:33:58 +00:00
for account in accounts_to_remove:
2024-12-03 09:49:04 +00:00
username = account.get("username")
2024-11-27 11:33:58 +00:00
if not username or username.lower() in self.super_accounts:
print("Super account can not be remove: ", username)
continue
2023-12-06 10:48:35 +00:00
h = deepcopy(host)
2024-12-03 09:49:04 +00:00
h["name"] += "(" + username + ")"
self.host_account_mapper[h["name"]] = account
h["account"] = {"username": username}
2023-12-06 10:48:35 +00:00
inventory_hosts.append(h)
return inventory_hosts
def on_host_success(self, host, result):
2024-12-03 09:49:04 +00:00
super().on_host_success(host, result)
account = self.host_account_mapper.get(host)
if not account:
2023-12-06 10:48:35 +00:00
return
2024-12-03 09:49:04 +00:00
try:
Account.objects.filter(
2024-12-03 09:49:04 +00:00
asset_id=account["asset"], username=account["username"]
).delete()
GatheredAccount.objects.filter(
asset_id=account["asset"], username=account["username"]
).delete()
2024-12-03 09:49:04 +00:00
risk = AccountRisk.objects.filter(
asset_id=account["asset"],
username=account["username"],
risk__in=["new_found"],
)
print("Account removed: ", account)
except Exception as e:
2024-12-03 09:49:04 +00:00
logger.error(
f"Failed to delete account {account['username']} on asset {account['asset']}: {e}"
)