2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-05 02:48:19 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from accounts.const import SecretType
|
|
|
|
from accounts.models import Account
|
2022-12-06 03:03:14 +00:00
|
|
|
from acls.models import CommandGroup, CommandFilterACL
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.models import Asset, Platform, Gateway, Domain
|
2023-04-10 02:57:44 +00:00
|
|
|
from assets.serializers.asset import AssetProtocolsSerializer
|
|
|
|
from assets.serializers.platform import PlatformSerializer
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.fields import LabeledChoiceField
|
|
|
|
from common.serializers.fields import ObjectRelatedField
|
2022-12-06 03:03:14 +00:00
|
|
|
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
2022-12-07 07:09:01 +00:00
|
|
|
from perms.serializers.permission import ActionChoicesField
|
|
|
|
from users.models import User
|
2022-12-06 03:03:14 +00:00
|
|
|
from ..models import ConnectionToken
|
|
|
|
|
2022-12-05 02:48:19 +00:00
|
|
|
__all__ = [
|
2022-12-07 07:09:01 +00:00
|
|
|
'ConnectionTokenSecretSerializer', 'ConnectTokenAppletOptionSerializer'
|
2022-12-05 02:48:19 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class _ConnectionTokenUserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ['id', 'name', 'username', 'email']
|
|
|
|
|
|
|
|
|
|
|
|
class _ConnectionTokenAssetSerializer(serializers.ModelSerializer):
|
|
|
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
2023-04-10 03:23:21 +00:00
|
|
|
info = serializers.DictField()
|
2022-12-05 02:48:19 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = [
|
2023-04-10 02:57:44 +00:00
|
|
|
'id', 'name', 'address', 'protocols', 'category',
|
2023-04-10 08:35:34 +00:00
|
|
|
'type', 'org_id', 'info', 'secret_info', 'spec_info'
|
2022-12-05 02:48:19 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class _SimpleAccountSerializer(serializers.ModelSerializer):
|
2022-12-12 09:30:38 +00:00
|
|
|
secret_type = LabeledChoiceField(choices=SecretType.choices, required=False, label=_('Secret type'))
|
2022-12-05 02:48:19 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Account
|
|
|
|
fields = ['name', 'username', 'secret_type', 'secret']
|
|
|
|
|
|
|
|
|
|
|
|
class _ConnectionTokenAccountSerializer(serializers.ModelSerializer):
|
2023-05-25 10:38:14 +00:00
|
|
|
su_from = serializers.SerializerMethodField(label=_('Su from'))
|
2022-12-12 09:30:38 +00:00
|
|
|
secret_type = LabeledChoiceField(choices=SecretType.choices, required=False, label=_('Secret type'))
|
2022-12-05 02:48:19 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Account
|
|
|
|
fields = [
|
2023-04-10 02:57:44 +00:00
|
|
|
'id', 'name', 'username', 'secret_type',
|
|
|
|
'secret', 'su_from', 'privileged'
|
2022-12-05 02:48:19 +00:00
|
|
|
]
|
|
|
|
|
2023-05-25 10:38:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_su_from(account):
|
2023-11-10 09:16:31 +00:00
|
|
|
if not hasattr(account, 'asset'):
|
|
|
|
return {}
|
2023-05-25 10:38:14 +00:00
|
|
|
su_enabled = account.asset.platform.su_enabled
|
|
|
|
su_from = account.su_from
|
|
|
|
if not su_from or not su_enabled:
|
|
|
|
return
|
|
|
|
return _SimpleAccountSerializer(su_from).data
|
|
|
|
|
2022-12-05 02:48:19 +00:00
|
|
|
|
|
|
|
class _ConnectionTokenGatewaySerializer(serializers.ModelSerializer):
|
2023-01-16 11:02:09 +00:00
|
|
|
account = _SimpleAccountSerializer(
|
|
|
|
required=False, source='select_account', read_only=True
|
2022-12-06 03:03:14 +00:00
|
|
|
)
|
|
|
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
|
|
|
|
2022-12-05 02:48:19 +00:00
|
|
|
class Meta:
|
2022-12-06 03:03:14 +00:00
|
|
|
model = Gateway
|
2022-12-05 02:48:19 +00:00
|
|
|
fields = [
|
2022-12-06 03:03:14 +00:00
|
|
|
'id', 'name', 'address', 'protocols', 'account'
|
2022-12-05 02:48:19 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-12-06 03:03:14 +00:00
|
|
|
class _ConnectionTokenCommandFilterACLSerializer(serializers.ModelSerializer):
|
|
|
|
command_groups = ObjectRelatedField(
|
|
|
|
many=True, required=False, queryset=CommandGroup.objects,
|
|
|
|
attrs=('id', 'name', 'type', 'content', 'ignore_case', 'pattern'),
|
|
|
|
label=_('Command group')
|
|
|
|
)
|
|
|
|
reviewers = ObjectRelatedField(
|
|
|
|
many=True, queryset=User.objects, label=_("Reviewers"), required=False
|
|
|
|
)
|
2022-12-05 02:48:19 +00:00
|
|
|
|
|
|
|
class Meta:
|
2022-12-06 03:03:14 +00:00
|
|
|
model = CommandFilterACL
|
2022-12-05 02:48:19 +00:00
|
|
|
fields = [
|
2023-04-10 02:57:44 +00:00
|
|
|
'id', 'name', 'command_groups', 'action',
|
|
|
|
'reviewers', 'priority', 'is_active'
|
2022-12-05 02:48:19 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class _ConnectionTokenPlatformSerializer(PlatformSerializer):
|
|
|
|
class Meta(PlatformSerializer.Meta):
|
|
|
|
model = Platform
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
names = super().get_field_names(declared_fields, info)
|
|
|
|
names = [n for n in names if n not in ['automation']]
|
|
|
|
return names
|
|
|
|
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
class _ConnectionTokenConnectMethodSerializer(serializers.Serializer):
|
|
|
|
name = serializers.CharField(label=_('Name'))
|
|
|
|
protocol = serializers.CharField(label=_('Protocol'))
|
|
|
|
os = serializers.CharField(label=_('OS'))
|
|
|
|
is_builtin = serializers.BooleanField(label=_('Is builtin'))
|
|
|
|
is_active = serializers.BooleanField(label=_('Is active'))
|
|
|
|
platform = _ConnectionTokenPlatformSerializer(label=_('Platform'))
|
|
|
|
action = ActionChoicesField(label=_('Action'))
|
|
|
|
options = serializers.JSONField(label=_('Options'))
|
|
|
|
|
|
|
|
|
|
|
|
class _ConnectTokenConnectMethodSerializer(serializers.Serializer):
|
|
|
|
label = serializers.CharField(label=_('Label'))
|
|
|
|
value = serializers.CharField(label=_('Value'))
|
|
|
|
type = serializers.CharField(label=_('Type'))
|
|
|
|
component = serializers.CharField(label=_('Component'))
|
|
|
|
|
|
|
|
|
2022-12-05 02:48:19 +00:00
|
|
|
class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin):
|
|
|
|
user = _ConnectionTokenUserSerializer(read_only=True)
|
|
|
|
asset = _ConnectionTokenAssetSerializer(read_only=True)
|
2022-12-05 04:42:15 +00:00
|
|
|
account = _ConnectionTokenAccountSerializer(read_only=True, source='account_object')
|
2022-12-05 02:48:19 +00:00
|
|
|
gateway = _ConnectionTokenGatewaySerializer(read_only=True)
|
|
|
|
platform = _ConnectionTokenPlatformSerializer(read_only=True)
|
2022-12-06 03:03:14 +00:00
|
|
|
domain = ObjectRelatedField(queryset=Domain.objects, required=False, label=_('Domain'))
|
|
|
|
command_filter_acls = _ConnectionTokenCommandFilterACLSerializer(read_only=True, many=True)
|
2022-12-07 07:09:01 +00:00
|
|
|
expire_now = serializers.BooleanField(label=_('Expired now'), write_only=True, default=True)
|
|
|
|
connect_method = _ConnectTokenConnectMethodSerializer(read_only=True, source='connect_method_object')
|
2023-06-05 09:27:44 +00:00
|
|
|
connect_options = serializers.JSONField(read_only=True)
|
2022-12-05 02:48:19 +00:00
|
|
|
actions = ActionChoicesField()
|
|
|
|
expire_at = serializers.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ConnectionToken
|
|
|
|
fields = [
|
|
|
|
'id', 'value', 'user', 'asset', 'account',
|
2022-12-06 03:03:14 +00:00
|
|
|
'platform', 'command_filter_acls', 'protocol',
|
2022-12-07 07:09:01 +00:00
|
|
|
'domain', 'gateway', 'actions', 'expire_at',
|
2023-04-10 02:57:44 +00:00
|
|
|
'from_ticket', 'expire_now', 'connect_method',
|
2023-06-05 09:27:44 +00:00
|
|
|
'connect_options',
|
2022-12-05 02:48:19 +00:00
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
'value': {'read_only': True},
|
|
|
|
}
|
2022-12-05 04:42:15 +00:00
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
class ConnectTokenAppletOptionSerializer(serializers.Serializer):
|
|
|
|
id = serializers.CharField(label=_('ID'))
|
|
|
|
applet = ObjectRelatedField(read_only=True)
|
|
|
|
host = _ConnectionTokenAssetSerializer(read_only=True)
|
|
|
|
account = _ConnectionTokenAccountSerializer(read_only=True)
|
|
|
|
gateway = _ConnectionTokenGatewaySerializer(read_only=True)
|
|
|
|
remote_app_option = serializers.JSONField(read_only=True)
|