2023-02-21 00:34:31 +00:00
|
|
|
|
from django.db import models
|
2023-03-23 10:57:22 +00:00
|
|
|
|
from django.db.models import Q
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-02-21 00:34:31 +00:00
|
|
|
|
|
2023-03-23 10:57:22 +00:00
|
|
|
|
from accounts.const import AutomationTypes, Source
|
|
|
|
|
from accounts.models import Account
|
2024-10-28 10:57:57 +00:00
|
|
|
|
from common.const import ConfirmOrIgnore
|
2024-10-30 08:10:46 +00:00
|
|
|
|
from common.utils.timezone import is_date_more_than
|
2023-02-23 10:08:56 +00:00
|
|
|
|
from orgs.mixins.models import JMSOrgBaseModel
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from .base import AccountBaseAutomation
|
2022-10-27 10:53:10 +00:00
|
|
|
|
|
2024-12-05 06:27:05 +00:00
|
|
|
|
__all__ = ['GatherAccountsAutomation', 'GatheredAccount']
|
2024-10-30 08:10:46 +00:00
|
|
|
|
|
|
|
|
|
|
2023-02-21 00:34:31 +00:00
|
|
|
|
class GatheredAccount(JMSOrgBaseModel):
|
|
|
|
|
asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_("Asset"))
|
|
|
|
|
username = models.CharField(max_length=32, blank=True, db_index=True, verbose_name=_('Username'))
|
2025-01-09 11:16:26 +00:00
|
|
|
|
address_last_login = models.CharField(null=True, max_length=39, default='', verbose_name=_("Address login"))
|
2024-11-11 03:12:10 +00:00
|
|
|
|
date_last_login = models.DateTimeField(null=True, verbose_name=_("Date login"))
|
|
|
|
|
remote_present = models.BooleanField(default=True, verbose_name=_("Remote present")) # 远端资产上是否还存在
|
|
|
|
|
present = models.BooleanField(default=False, verbose_name=_("Present")) # 系统资产上是否还存在
|
2024-11-12 08:00:41 +00:00
|
|
|
|
date_password_change = models.DateTimeField(null=True, verbose_name=_("Date change password"))
|
2024-11-11 03:12:10 +00:00
|
|
|
|
date_password_expired = models.DateTimeField(null=True, verbose_name=_("Date password expired"))
|
2024-12-05 06:27:05 +00:00
|
|
|
|
status = models.CharField(max_length=32, default=ConfirmOrIgnore.pending, blank=True,
|
|
|
|
|
choices=ConfirmOrIgnore.choices, verbose_name=_("Status"))
|
|
|
|
|
detail = models.JSONField(default=dict, blank=True, verbose_name=_("Detail"))
|
2023-02-21 00:34:31 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def address(self):
|
|
|
|
|
return self.asset.address
|
|
|
|
|
|
2024-10-28 10:57:57 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def update_exists_accounts(cls, gathered_account, accounts):
|
|
|
|
|
if not gathered_account.date_last_login:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for account in accounts:
|
2024-10-30 08:10:46 +00:00
|
|
|
|
# 这里是否可以考虑,标记成未从堡垒机登录风险 ?
|
|
|
|
|
if is_date_more_than(gathered_account.date_last_login, account.date_last_login, '5m'):
|
2024-10-28 10:57:57 +00:00
|
|
|
|
account.date_last_login = gathered_account.date_last_login
|
|
|
|
|
account.login_by = '{}({})'.format('unknown', gathered_account.address_last_login)
|
|
|
|
|
account.save(update_fields=['date_last_login', 'login_by'])
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2024-10-30 08:10:46 +00:00
|
|
|
|
def create_accounts(cls, gathered_account):
|
2023-03-23 10:57:22 +00:00
|
|
|
|
account_objs = []
|
2024-10-28 10:57:57 +00:00
|
|
|
|
asset_id = gathered_account.asset_id
|
|
|
|
|
username = gathered_account.username
|
|
|
|
|
account = Account(
|
|
|
|
|
asset_id=asset_id, username=username,
|
2024-11-06 08:41:01 +00:00
|
|
|
|
name=username, source=Source.DISCOVERY,
|
2024-10-29 11:24:02 +00:00
|
|
|
|
date_last_login=gathered_account.date_last_login,
|
2024-10-28 10:57:57 +00:00
|
|
|
|
)
|
|
|
|
|
account_objs.append(account)
|
|
|
|
|
Account.objects.bulk_create(account_objs)
|
2024-10-30 08:10:46 +00:00
|
|
|
|
gathered_account.status = ConfirmOrIgnore.confirmed
|
|
|
|
|
gathered_account.save(update_fields=['status'])
|
2024-10-28 10:57:57 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2024-10-30 08:10:46 +00:00
|
|
|
|
def sync_accounts(cls, gathered_accounts, auto_create=True):
|
|
|
|
|
"""
|
|
|
|
|
更新为已存在的账号,或者创建新的账号, 原来的 sync 重构了,如果存在则自动更新一些信息
|
|
|
|
|
"""
|
2023-03-23 10:57:22 +00:00
|
|
|
|
for gathered_account in gathered_accounts:
|
|
|
|
|
asset_id = gathered_account.asset_id
|
|
|
|
|
username = gathered_account.username
|
|
|
|
|
accounts = Account.objects.filter(
|
|
|
|
|
Q(asset_id=asset_id, username=username) |
|
|
|
|
|
Q(asset_id=asset_id, name=username)
|
|
|
|
|
)
|
2024-10-30 08:10:46 +00:00
|
|
|
|
|
2023-03-23 10:57:22 +00:00
|
|
|
|
if accounts.exists():
|
2024-10-28 10:57:57 +00:00
|
|
|
|
cls.update_exists_accounts(gathered_account, accounts)
|
2024-10-30 08:10:46 +00:00
|
|
|
|
elif auto_create:
|
|
|
|
|
cls.create_accounts(gathered_account)
|
2023-03-23 10:57:22 +00:00
|
|
|
|
|
2023-02-21 00:34:31 +00:00
|
|
|
|
class Meta:
|
2024-06-21 11:08:37 +00:00
|
|
|
|
verbose_name = _("Gather asset accounts")
|
2023-02-21 00:34:31 +00:00
|
|
|
|
unique_together = [
|
|
|
|
|
('username', 'asset'),
|
|
|
|
|
]
|
|
|
|
|
ordering = ['asset']
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return '{}: {}'.format(self.asset, self.username)
|
2022-10-27 10:53:10 +00:00
|
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
class GatherAccountsAutomation(AccountBaseAutomation):
|
2023-03-23 10:57:22 +00:00
|
|
|
|
is_sync_account = models.BooleanField(
|
|
|
|
|
default=False, blank=True, verbose_name=_("Is sync account")
|
|
|
|
|
)
|
2023-11-07 05:00:09 +00:00
|
|
|
|
recipients = models.ManyToManyField('users.User', verbose_name=_("Recipient"), blank=True)
|
2024-11-18 11:06:04 +00:00
|
|
|
|
check_risk = models.BooleanField(default=True, verbose_name=_("Check risk"))
|
2023-03-23 10:57:22 +00:00
|
|
|
|
|
|
|
|
|
def to_attr_json(self):
|
|
|
|
|
attr_json = super().to_attr_json()
|
|
|
|
|
attr_json.update({
|
|
|
|
|
'is_sync_account': self.is_sync_account,
|
2024-11-18 11:06:04 +00:00
|
|
|
|
'check_risk': self.check_risk,
|
2023-11-07 05:00:09 +00:00
|
|
|
|
'recipients': [
|
|
|
|
|
str(recipient.id) for recipient in self.recipients.all()
|
|
|
|
|
]
|
2023-03-23 10:57:22 +00:00
|
|
|
|
})
|
|
|
|
|
return attr_json
|
|
|
|
|
|
2022-10-27 10:53:10 +00:00
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
self.type = AutomationTypes.gather_accounts
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
2024-06-21 11:08:37 +00:00
|
|
|
|
verbose_name = _('Gather account automation')
|