2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-04-25 02:28:19 +00:00
|
|
|
from rest_framework import serializers
|
2023-04-19 02:18:13 +00:00
|
|
|
|
2023-09-26 04:06:54 +00:00
|
|
|
from accounts.const import SecretStrategy, SecretType
|
2023-10-11 08:03:05 +00:00
|
|
|
from accounts.models import AccountTemplate
|
2023-09-26 04:06:54 +00:00
|
|
|
from accounts.utils import SecretGenerator
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers import SecretReadableMixin
|
2023-05-09 07:29:02 +00:00
|
|
|
from common.serializers.fields import ObjectRelatedField
|
2022-09-23 10:59:19 +00:00
|
|
|
from .base import BaseAccountSerializer
|
2022-08-24 08:36:42 +00:00
|
|
|
|
|
|
|
|
2023-09-19 02:59:33 +00:00
|
|
|
class PasswordRulesSerializer(serializers.Serializer):
|
|
|
|
length = serializers.IntegerField(min_value=8, max_value=30, default=16, label=_('Password length'))
|
|
|
|
lowercase = serializers.BooleanField(default=True, label=_('Lowercase'))
|
|
|
|
uppercase = serializers.BooleanField(default=True, label=_('Uppercase'))
|
|
|
|
digit = serializers.BooleanField(default=True, label=_('Digit'))
|
|
|
|
symbol = serializers.BooleanField(default=True, label=_('Special symbol'))
|
2023-12-08 08:34:46 +00:00
|
|
|
exclude_symbols = serializers.CharField(
|
|
|
|
default='', allow_blank=True, max_length=16, label=_('Exclude symbol')
|
|
|
|
)
|
2023-09-19 02:59:33 +00:00
|
|
|
|
|
|
|
|
2022-09-23 10:59:19 +00:00
|
|
|
class AccountTemplateSerializer(BaseAccountSerializer):
|
2023-09-19 02:59:33 +00:00
|
|
|
password_rules = PasswordRulesSerializer(required=False, label=_('Password rules'))
|
2023-05-09 07:29:02 +00:00
|
|
|
su_from = ObjectRelatedField(
|
|
|
|
required=False, queryset=AccountTemplate.objects, allow_null=True,
|
|
|
|
allow_empty=True, label=_('Su from'), attrs=('id', 'name', 'username')
|
|
|
|
)
|
|
|
|
|
2022-09-23 10:59:19 +00:00
|
|
|
class Meta(BaseAccountSerializer.Meta):
|
2022-08-24 08:36:42 +00:00
|
|
|
model = AccountTemplate
|
2023-08-28 07:43:45 +00:00
|
|
|
fields = BaseAccountSerializer.Meta.fields + [
|
2023-09-19 02:59:33 +00:00
|
|
|
'secret_strategy', 'password_rules',
|
2023-08-28 07:43:45 +00:00
|
|
|
'auto_push', 'push_params', 'platforms',
|
2023-10-11 08:03:05 +00:00
|
|
|
'su_from'
|
2023-08-28 07:43:45 +00:00
|
|
|
]
|
|
|
|
extra_kwargs = {
|
2024-05-11 06:45:29 +00:00
|
|
|
**BaseAccountSerializer.Meta.extra_kwargs,
|
2023-08-28 07:43:45 +00:00
|
|
|
'secret_strategy': {'help_text': _('Secret generation strategy for account creation')},
|
|
|
|
'auto_push': {'help_text': _('Whether to automatically push the account to the asset')},
|
2023-09-19 02:59:33 +00:00
|
|
|
'platforms': {
|
|
|
|
'help_text': _(
|
|
|
|
'Associated platform, you can configure push parameters. '
|
|
|
|
'If not associated, default parameters will be used'
|
|
|
|
),
|
|
|
|
'required': False
|
|
|
|
},
|
2023-08-28 07:43:45 +00:00
|
|
|
}
|
2022-08-24 08:36:42 +00:00
|
|
|
|
2023-09-26 04:06:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def generate_secret(attrs):
|
|
|
|
secret_type = attrs.get('secret_type', SecretType.PASSWORD)
|
|
|
|
secret_strategy = attrs.get('secret_strategy', SecretStrategy.custom)
|
|
|
|
password_rules = attrs.get('password_rules')
|
|
|
|
if secret_strategy != SecretStrategy.random:
|
|
|
|
return
|
|
|
|
generator = SecretGenerator(secret_strategy, secret_type, password_rules)
|
|
|
|
attrs['secret'] = generator.get_secret()
|
|
|
|
|
2023-04-25 02:28:19 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
attrs = super().validate(attrs)
|
2023-09-26 04:06:54 +00:00
|
|
|
self.generate_secret(attrs)
|
2023-04-25 02:28:19 +00:00
|
|
|
return attrs
|
2023-04-12 09:59:13 +00:00
|
|
|
|
2022-11-28 07:54:16 +00:00
|
|
|
|
|
|
|
class AccountTemplateSecretSerializer(SecretReadableMixin, AccountTemplateSerializer):
|
|
|
|
class Meta(AccountTemplateSerializer.Meta):
|
2024-05-11 06:45:29 +00:00
|
|
|
fields = AccountTemplateSerializer.Meta.fields + ['spec_info']
|
2022-11-28 07:54:16 +00:00
|
|
|
extra_kwargs = {
|
2024-05-11 06:45:29 +00:00
|
|
|
**AccountTemplateSerializer.Meta.extra_kwargs,
|
2022-11-28 07:54:16 +00:00
|
|
|
'secret': {'write_only': False},
|
2024-05-11 06:45:29 +00:00
|
|
|
'spec_info': {'label': _('Spec info')},
|
2022-11-28 07:54:16 +00:00
|
|
|
}
|