jumpserver/apps/accounts/risk_handlers.py

129 lines
3.9 KiB
Python
Raw Normal View History

2024-11-27 11:33:58 +00:00
from django.utils.translation import gettext_lazy as _
2024-12-09 09:11:03 +00:00
from accounts.const import AutomationTypes
2024-12-03 09:49:04 +00:00
from common.const import ConfirmOrIgnore
2024-12-09 09:11:03 +00:00
from accounts.models import (
GatheredAccount,
AccountRisk,
SecretType,
AutomationExecution,
)
2024-12-03 09:49:04 +00:00
from django.utils import timezone
from common.const import ConfirmOrIgnore
2024-11-27 11:33:58 +00:00
TYPE_CHOICES = [
("ignore", _("Ignore")),
("disable_remote", _("Disable remote")),
("delete_remote", _("Delete remote")),
("delete_both", _("Delete remote")),
("add_account", _("Add account")),
("change_password_add", _("Change password and Add")),
("change_password", _("Change password")),
]
class RiskHandler:
2024-12-09 09:11:03 +00:00
def __init__(self, asset, username, request=None, risk=""):
2024-11-27 11:33:58 +00:00
self.asset = asset
self.username = username
2024-12-03 09:49:04 +00:00
self.request = request
self.risk = risk
2024-11-27 11:33:58 +00:00
2024-12-09 09:11:03 +00:00
def handle(self, tp, risk=""):
2024-12-03 09:49:04 +00:00
self.risk = risk
2024-11-27 11:33:58 +00:00
attr = f"handle_{tp}"
if hasattr(self, attr):
2024-12-03 09:49:04 +00:00
ret = getattr(self, attr)()
self.update_risk_if_need(tp)
return ret
2024-11-27 11:33:58 +00:00
else:
raise ValueError(f"Invalid risk type: {tp}")
2024-12-03 09:49:04 +00:00
def update_risk_if_need(self, tp):
r = self.get_risk()
if not r:
return
2024-12-09 09:11:03 +00:00
status = (
ConfirmOrIgnore.ignored if tp == "ignore" else ConfirmOrIgnore.confirmed
)
r.details.append({**self.process_detail, "action": tp, "status": status})
2024-12-03 09:49:04 +00:00
r.status = status
r.save()
def get_risk(self):
r = AccountRisk.objects.filter(asset=self.asset, username=self.username)
if self.risk:
r = r.filter(risk=self.risk)
return r.first()
def handle_ignore(self):
GatheredAccount.objects.filter(asset=self.asset, username=self.username).update(status=ConfirmOrIgnore.ignored)
self.risk = 'ignored'
2024-12-03 09:49:04 +00:00
def handle_review(self):
2024-11-27 11:33:58 +00:00
pass
2024-12-03 09:49:04 +00:00
@property
def process_detail(self):
return {
2024-12-09 09:11:03 +00:00
"datetime": timezone.now().isoformat(),
"type": "process",
"processor": str(self.request.user),
2024-12-03 09:49:04 +00:00
}
def handle_add_account(self):
2024-11-27 11:33:58 +00:00
data = {
"username": self.username,
"name": self.username,
"secret_type": SecretType.PASSWORD,
"source": "collected",
}
self.asset.accounts.get_or_create(defaults=data, username=self.username)
GatheredAccount.objects.filter(asset=self.asset, username=self.username).update(
2024-12-03 09:49:04 +00:00
present=True, status=ConfirmOrIgnore.confirmed
2024-11-27 11:33:58 +00:00
)
2024-12-09 09:11:03 +00:00
self.risk = "new_found"
2024-11-27 11:33:58 +00:00
2024-12-03 09:49:04 +00:00
def handle_disable_remote(self):
2024-11-27 11:33:58 +00:00
pass
2024-12-03 09:49:04 +00:00
def handle_delete_remote(self):
2024-12-09 09:11:03 +00:00
self._handle_delete(delete="remote")
def _handle_delete(self, delete="both"):
2024-11-27 11:33:58 +00:00
asset = self.asset
execution = AutomationExecution()
execution.snapshot = {
"assets": [str(asset.id)],
"accounts": [{"asset": str(asset.id), "username": self.username}],
"type": "remove_account",
"name": "Remove remote account: {}@{}".format(self.username, asset.name),
2024-12-09 09:11:03 +00:00
"delete": delete,
"risk": self.risk
2024-11-27 11:33:58 +00:00
}
execution.save()
execution.start()
2024-12-03 09:49:04 +00:00
return execution.summary
2024-11-27 11:33:58 +00:00
2024-12-03 09:49:04 +00:00
def handle_delete_both(self):
2024-12-09 09:11:03 +00:00
self._handle_delete(delete="both")
2024-11-27 11:33:58 +00:00
2024-12-03 09:49:04 +00:00
def handle_change_password_add(self):
2024-11-27 11:33:58 +00:00
pass
2024-12-03 09:49:04 +00:00
def handle_change_password(self):
2024-12-09 09:11:03 +00:00
asset = self.asset
execution = AutomationExecution()
execution.snapshot = {
"assets": [str(asset.id)],
"accounts": [self.username],
"type": AutomationTypes.change_secret,
"secret_type": "password",
"secret_strategy": "random",
"name": "Change account password: {}@{}".format(self.username, asset.name),
}
execution.save()
execution.start()
return execution.summary