2019-03-18 02:15:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
from django.db import models
|
2022-01-17 07:58:39 +00:00
|
|
|
from django.db.models import F
|
2019-03-25 11:57:18 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-07-08 06:23:18 +00:00
|
|
|
from simple_history.models import HistoricalRecords
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2021-09-15 11:11:19 +00:00
|
|
|
from common.utils import lazyproperty, get_logger
|
2021-07-08 06:23:18 +00:00
|
|
|
from .base import BaseUser, AbsConnectivity
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2021-09-15 11:11:19 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
__all__ = ['AuthBook']
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
class AuthBook(BaseUser, AbsConnectivity):
|
2019-03-18 02:15:33 +00:00
|
|
|
asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_('Asset'))
|
2021-07-08 06:23:18 +00:00
|
|
|
systemuser = models.ForeignKey('assets.SystemUser', on_delete=models.CASCADE, null=True, verbose_name=_("System user"))
|
2019-03-18 02:15:33 +00:00
|
|
|
version = models.IntegerField(default=1, verbose_name=_('Version'))
|
2021-07-08 06:23:18 +00:00
|
|
|
history = HistoricalRecords()
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
auth_attrs = ['username', 'password', 'private_key', 'public_key']
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('AuthBook')
|
2021-07-08 06:23:18 +00:00
|
|
|
unique_together = [('username', 'asset', 'systemuser')]
|
2022-02-17 12:13:31 +00:00
|
|
|
permissions = [
|
2022-03-10 03:25:33 +00:00
|
|
|
('test_authbook', _('Can test asset account connectivity')),
|
2022-02-17 12:13:31 +00:00
|
|
|
('view_assetaccountsecret', _('Can view asset account secret')),
|
2022-07-04 10:54:47 +00:00
|
|
|
('change_assetaccountsecret', _('Can change asset account secret')),
|
|
|
|
('view_assethistoryaccount', _('Can view asset history account')),
|
|
|
|
('view_assethistoryaccountsecret', _('Can view asset history account secret')),
|
2022-02-17 12:13:31 +00:00
|
|
|
]
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.auth_snapshot = {}
|
|
|
|
|
|
|
|
def get_or_systemuser_attr(self, attr):
|
|
|
|
val = getattr(self, attr, None)
|
|
|
|
if val:
|
|
|
|
return val
|
|
|
|
if self.systemuser:
|
|
|
|
return getattr(self.systemuser, attr, '')
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def load_auth(self):
|
|
|
|
for attr in self.auth_attrs:
|
|
|
|
value = self.get_or_systemuser_attr(attr)
|
|
|
|
self.auth_snapshot[attr] = [getattr(self, attr), value]
|
|
|
|
setattr(self, attr, value)
|
|
|
|
|
|
|
|
def unload_auth(self):
|
|
|
|
if not self.systemuser:
|
|
|
|
return
|
|
|
|
|
|
|
|
for attr, values in self.auth_snapshot.items():
|
|
|
|
origin_value, loaded_value = values
|
|
|
|
current_value = getattr(self, attr, '')
|
|
|
|
if current_value == loaded_value:
|
|
|
|
setattr(self, attr, origin_value)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.unload_auth()
|
|
|
|
instance = super().save(*args, **kwargs)
|
|
|
|
self.load_auth()
|
|
|
|
return instance
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
@property
|
2021-07-08 06:23:18 +00:00
|
|
|
def username_display(self):
|
|
|
|
return self.get_or_systemuser_attr('username') or '*'
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2021-07-12 12:11:53 +00:00
|
|
|
@lazyproperty
|
2021-07-12 10:20:32 +00:00
|
|
|
def systemuser_display(self):
|
|
|
|
if not self.systemuser:
|
|
|
|
return ''
|
|
|
|
return str(self.systemuser)
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
@property
|
2021-07-08 06:23:18 +00:00
|
|
|
def smart_name(self):
|
|
|
|
username = self.username_display
|
|
|
|
|
|
|
|
if self.asset:
|
|
|
|
asset = str(self.asset)
|
|
|
|
else:
|
|
|
|
asset = '*'
|
|
|
|
return '{}@{}'.format(username, asset)
|
|
|
|
|
|
|
|
def sync_to_system_user_account(self):
|
|
|
|
if self.systemuser:
|
|
|
|
return
|
|
|
|
matched = AuthBook.objects.filter(
|
|
|
|
asset=self.asset, systemuser__username=self.username
|
|
|
|
)
|
|
|
|
if not matched:
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in matched:
|
|
|
|
i.password = self.password
|
|
|
|
i.private_key = self.private_key
|
|
|
|
i.public_key = self.public_key
|
|
|
|
i.comment = 'Update triggered by account {}'.format(self.id)
|
2021-09-24 03:45:39 +00:00
|
|
|
|
|
|
|
# 不触发post_save信号
|
|
|
|
self.__class__.objects.bulk_update(matched, fields=['password', 'private_key', 'public_key'])
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2021-09-15 11:11:19 +00:00
|
|
|
def remove_asset_admin_user_if_need(self):
|
2021-09-24 03:45:39 +00:00
|
|
|
if not self.asset or not self.systemuser:
|
2021-09-15 11:11:19 +00:00
|
|
|
return
|
2021-09-24 03:45:39 +00:00
|
|
|
if not self.systemuser.is_admin_user or self.asset.admin_user != self.systemuser:
|
2021-09-15 11:11:19 +00:00
|
|
|
return
|
|
|
|
self.asset.admin_user = None
|
|
|
|
self.asset.save()
|
2021-09-24 03:45:39 +00:00
|
|
|
logger.debug('Remove asset admin user: {} {}'.format(self.asset, self.systemuser))
|
2021-09-15 11:11:19 +00:00
|
|
|
|
|
|
|
def update_asset_admin_user_if_need(self):
|
2021-09-24 03:45:39 +00:00
|
|
|
if not self.asset or not self.systemuser:
|
2021-09-15 11:11:19 +00:00
|
|
|
return
|
2021-09-24 03:45:39 +00:00
|
|
|
if not self.systemuser.is_admin_user or self.asset.admin_user == self.systemuser:
|
2021-09-15 11:11:19 +00:00
|
|
|
return
|
|
|
|
self.asset.admin_user = self.systemuser
|
|
|
|
self.asset.save()
|
2021-09-24 03:45:39 +00:00
|
|
|
logger.debug('Update asset admin user: {} {}'.format(self.asset, self.systemuser))
|
2021-09-15 11:11:19 +00:00
|
|
|
|
2022-01-17 07:58:39 +00:00
|
|
|
@classmethod
|
2022-07-13 09:13:02 +00:00
|
|
|
def get_queryset(cls):
|
|
|
|
queryset = cls.objects.all() \
|
2022-01-17 07:58:39 +00:00
|
|
|
.annotate(ip=F('asset__ip')) \
|
|
|
|
.annotate(hostname=F('asset__hostname')) \
|
|
|
|
.annotate(platform=F('asset__platform__name')) \
|
|
|
|
.annotate(protocols=F('asset__protocols'))
|
|
|
|
return queryset
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
def __str__(self):
|
2021-07-08 06:23:18 +00:00
|
|
|
return self.smart_name
|
2019-03-18 02:15:33 +00:00
|
|
|
|