2016-10-19 11:30:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-03-17 09:10:10 +00:00
|
|
|
from django.db.models import Q
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-11 07:04:31 +00:00
|
|
|
from rest_framework import serializers
|
2021-04-29 11:10:45 +00:00
|
|
|
|
2023-03-08 10:52:00 +00:00
|
|
|
from accounts.models import AccountTemplate, Account
|
|
|
|
from accounts.tasks import push_accounts_to_assets_task
|
2022-08-17 03:54:18 +00:00
|
|
|
from assets.models import Asset, Node
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import BitChoicesField, ObjectRelatedField
|
2022-11-11 07:04:31 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|
|
|
from perms.models import ActionChoices, AssetPermission
|
2021-02-08 06:59:20 +00:00
|
|
|
from users.models import User, UserGroup
|
2022-09-07 09:35:23 +00:00
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
__all__ = ["AssetPermissionSerializer", "ActionChoicesField"]
|
2022-09-07 09:35:23 +00:00
|
|
|
|
|
|
|
|
2022-11-11 07:04:31 +00:00
|
|
|
class ActionChoicesField(BitChoicesField):
|
2022-09-07 09:35:23 +00:00
|
|
|
def __init__(self, **kwargs):
|
2022-11-15 06:59:22 +00:00
|
|
|
super().__init__(choice_cls=ActionChoices, **kwargs)
|
2019-06-30 12:10:34 +00:00
|
|
|
|
2023-03-13 09:57:50 +00:00
|
|
|
def to_file_representation(self, value):
|
|
|
|
return [v['value'] for v in value]
|
|
|
|
|
|
|
|
def to_file_internal_value(self, data):
|
|
|
|
return data
|
|
|
|
|
2022-09-07 09:35:23 +00:00
|
|
|
|
|
|
|
class AssetPermissionSerializer(BulkOrgResourceModelSerializer):
|
2022-12-26 08:15:44 +00:00
|
|
|
users = ObjectRelatedField(queryset=User.objects, many=True, required=False, label=_('User'))
|
2022-11-11 07:04:31 +00:00
|
|
|
user_groups = ObjectRelatedField(
|
2022-12-26 08:15:44 +00:00
|
|
|
queryset=UserGroup.objects, many=True, required=False, label=_('User group')
|
2022-11-11 07:04:31 +00:00
|
|
|
)
|
2022-12-26 08:15:44 +00:00
|
|
|
assets = ObjectRelatedField(queryset=Asset.objects, many=True, required=False, label=_('Asset'))
|
|
|
|
nodes = ObjectRelatedField(queryset=Node.objects, many=True, required=False, label=_('Node'))
|
2022-11-11 07:04:31 +00:00
|
|
|
actions = ActionChoicesField(required=False, allow_null=True, label=_("Actions"))
|
2021-07-30 11:13:47 +00:00
|
|
|
is_valid = serializers.BooleanField(read_only=True, label=_("Is valid"))
|
2022-11-11 07:04:31 +00:00
|
|
|
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
|
2023-02-15 06:48:31 +00:00
|
|
|
accounts = serializers.ListField(label=_("Account"), required=False)
|
2023-10-26 11:19:09 +00:00
|
|
|
protocols = serializers.ListField(label=_("Protocols"), required=False)
|
2018-04-10 12:29:06 +00:00
|
|
|
|
2023-03-15 08:26:01 +00:00
|
|
|
template_accounts = AccountTemplate.objects.none()
|
2023-03-08 10:52:00 +00:00
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
class Meta:
|
2018-04-08 12:02:40 +00:00
|
|
|
model = AssetPermission
|
2022-11-11 07:04:31 +00:00
|
|
|
fields_mini = ["id", "name"]
|
2023-01-16 11:02:09 +00:00
|
|
|
fields_generic = [
|
2023-10-26 11:19:09 +00:00
|
|
|
"accounts", "protocols", "actions", "created_by", "date_created",
|
2023-05-06 11:52:03 +00:00
|
|
|
"date_start", "date_expired", "is_active", "is_expired",
|
|
|
|
"is_valid", "comment", "from_ticket",
|
2020-05-09 06:51:19 +00:00
|
|
|
]
|
2023-01-16 11:02:09 +00:00
|
|
|
fields_small = fields_mini + fields_generic
|
2023-05-06 11:52:03 +00:00
|
|
|
fields_m2m = ["users", "user_groups", "assets", "nodes"]
|
2023-01-16 11:02:09 +00:00
|
|
|
fields = fields_mini + fields_m2m + fields_generic
|
2022-11-11 07:04:31 +00:00
|
|
|
read_only_fields = ["created_by", "date_created", "from_ticket"]
|
2020-11-11 02:27:18 +00:00
|
|
|
extra_kwargs = {
|
2022-11-11 07:04:31 +00:00
|
|
|
"actions": {"label": _("Actions")},
|
|
|
|
"is_expired": {"label": _("Is expired")},
|
|
|
|
"is_valid": {"label": _("Is valid")},
|
2020-11-11 02:27:18 +00:00
|
|
|
}
|
2020-05-09 06:51:19 +00:00
|
|
|
|
2022-09-29 12:41:40 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_actions_field()
|
|
|
|
|
|
|
|
def set_actions_field(self):
|
2022-11-11 07:04:31 +00:00
|
|
|
actions = self.fields.get("actions")
|
2022-09-29 12:41:40 +00:00
|
|
|
if not actions:
|
|
|
|
return
|
2023-02-02 06:37:21 +00:00
|
|
|
actions.default = list(actions.choices.keys())
|
2022-09-29 12:41:40 +00:00
|
|
|
|
2023-02-03 07:01:31 +00:00
|
|
|
@staticmethod
|
2023-03-08 10:52:00 +00:00
|
|
|
def get_all_assets(nodes, assets):
|
|
|
|
node_asset_ids = Node.get_nodes_all_assets(*nodes).values_list('id', flat=True)
|
|
|
|
direct_asset_ids = [asset.id for asset in assets]
|
|
|
|
asset_ids = set(direct_asset_ids + list(node_asset_ids))
|
|
|
|
return Asset.objects.filter(id__in=asset_ids)
|
|
|
|
|
|
|
|
def create_accounts(self, assets):
|
|
|
|
need_create_accounts = []
|
|
|
|
account_attribute = [
|
2023-05-06 11:52:03 +00:00
|
|
|
'name', 'username', 'secret_type', 'secret',
|
|
|
|
'privileged', 'is_active', 'org_id'
|
2023-03-08 10:52:00 +00:00
|
|
|
]
|
|
|
|
for asset in assets:
|
|
|
|
asset_exist_accounts = Account.objects.none()
|
|
|
|
for template in self.template_accounts:
|
|
|
|
asset_exist_accounts |= asset.accounts.filter(
|
|
|
|
username=template.username,
|
|
|
|
secret_type=template.secret_type,
|
|
|
|
)
|
|
|
|
username_secret_type_dict = asset_exist_accounts.values('username', 'secret_type')
|
|
|
|
for template in self.template_accounts:
|
|
|
|
condition = {
|
|
|
|
'username': template.username,
|
|
|
|
'secret_type': template.secret_type
|
|
|
|
}
|
|
|
|
if condition in username_secret_type_dict:
|
|
|
|
continue
|
|
|
|
account_data = {key: getattr(template, key) for key in account_attribute}
|
2023-05-09 07:29:02 +00:00
|
|
|
account_data['su_from'] = template.get_su_from_account(asset)
|
2023-03-23 07:24:19 +00:00
|
|
|
account_data['name'] = f"{account_data['name']}-{_('Account template')}"
|
2023-03-08 10:52:00 +00:00
|
|
|
need_create_accounts.append(Account(**{'asset_id': asset.id, **account_data}))
|
|
|
|
return Account.objects.bulk_create(need_create_accounts)
|
|
|
|
|
|
|
|
def create_and_push_account(self, nodes, assets):
|
|
|
|
if not self.template_accounts:
|
|
|
|
return
|
|
|
|
assets = self.get_all_assets(nodes, assets)
|
|
|
|
accounts = self.create_accounts(assets)
|
2023-03-17 09:10:10 +00:00
|
|
|
account_ids = [str(account.id) for account in accounts]
|
|
|
|
slice_count = 20
|
|
|
|
for i in range(0, len(account_ids), slice_count):
|
|
|
|
push_accounts_to_assets_task.delay(account_ids[i:i + slice_count])
|
2023-03-08 10:52:00 +00:00
|
|
|
|
2023-04-20 14:32:22 +00:00
|
|
|
def validate_accounts(self, usernames):
|
2023-03-08 10:52:00 +00:00
|
|
|
template_ids = []
|
|
|
|
account_usernames = []
|
|
|
|
for username in usernames:
|
|
|
|
if username.startswith('%'):
|
|
|
|
template_ids.append(username[1:])
|
|
|
|
else:
|
|
|
|
account_usernames.append(username)
|
|
|
|
self.template_accounts = AccountTemplate.objects.filter(id__in=template_ids)
|
|
|
|
template_usernames = list(self.template_accounts.values_list('username', flat=True))
|
|
|
|
return list(set(account_usernames + template_usernames))
|
2023-02-03 07:01:31 +00:00
|
|
|
|
2020-05-09 06:51:19 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
2022-11-11 07:04:31 +00:00
|
|
|
"""Perform necessary eager loading of data."""
|
2021-07-30 11:13:47 +00:00
|
|
|
queryset = queryset.prefetch_related(
|
2023-05-06 11:52:03 +00:00
|
|
|
"users", "user_groups", "assets", "nodes",
|
2021-07-30 11:13:47 +00:00
|
|
|
)
|
2020-05-09 06:51:19 +00:00
|
|
|
return queryset
|
2021-04-28 08:42:54 +00:00
|
|
|
|
2021-07-30 11:13:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def perform_display_create(instance, **kwargs):
|
2021-04-28 08:42:54 +00:00
|
|
|
# 用户
|
|
|
|
users_to_set = User.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
Q(name__in=kwargs.get("users_display"))
|
|
|
|
| Q(username__in=kwargs.get("users_display"))
|
2021-04-28 08:42:54 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.users.add(*users_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 用户组
|
2021-07-30 11:13:47 +00:00
|
|
|
user_groups_to_set = UserGroup.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
name__in=kwargs.get("user_groups_display")
|
2021-07-30 11:13:47 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.user_groups.add(*user_groups_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 资产
|
|
|
|
assets_to_set = Asset.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
Q(address__in=kwargs.get("assets_display"))
|
|
|
|
| Q(name__in=kwargs.get("assets_display"))
|
2021-04-28 08:42:54 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.assets.add(*assets_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 节点
|
2021-07-30 11:13:47 +00:00
|
|
|
nodes_to_set = Node.objects.filter(
|
2022-11-11 07:04:31 +00:00
|
|
|
full_value__in=kwargs.get("nodes_display")
|
2021-07-30 11:13:47 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.nodes.add(*nodes_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
|
2023-03-08 10:52:00 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
self.create_and_push_account(
|
|
|
|
attrs.get("nodes", []),
|
|
|
|
attrs.get("assets", [])
|
|
|
|
)
|
|
|
|
return super().validate(attrs)
|
|
|
|
|
2021-04-28 08:42:54 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
display = {
|
2022-11-11 07:04:31 +00:00
|
|
|
"users_display": validated_data.pop("users_display", ""),
|
|
|
|
"user_groups_display": validated_data.pop("user_groups_display", ""),
|
|
|
|
"assets_display": validated_data.pop("assets_display", ""),
|
|
|
|
"nodes_display": validated_data.pop("nodes_display", ""),
|
2021-04-28 08:42:54 +00:00
|
|
|
}
|
|
|
|
instance = super().create(validated_data)
|
|
|
|
self.perform_display_create(instance, **display)
|
|
|
|
return instance
|