2022-11-08 09:54:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-08 09:54:51 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from accounts.const import (
|
2023-09-19 02:59:33 +00:00
|
|
|
AutomationTypes, SecretType, SecretStrategy, SSHKeyStrategy
|
2023-01-16 11:02:09 +00:00
|
|
|
)
|
|
|
|
from accounts.models import (
|
|
|
|
Account, ChangeSecretAutomation,
|
2023-02-21 10:31:42 +00:00
|
|
|
ChangeSecretRecord, AutomationExecution
|
2023-01-16 11:02:09 +00:00
|
|
|
)
|
2023-09-19 02:59:33 +00:00
|
|
|
from accounts.serializers import AuthValidateMixin, PasswordRulesSerializer
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.models import Asset
|
|
|
|
from common.serializers.fields import LabeledChoiceField, ObjectRelatedField
|
2022-11-08 09:54:51 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from .base import BaseAutomationSerializer
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'ChangeSecretAutomationSerializer',
|
|
|
|
'ChangeSecretRecordSerializer',
|
2023-01-16 11:02:09 +00:00
|
|
|
'ChangeSecretRecordBackUpSerializer',
|
|
|
|
'ChangeSecretUpdateAssetSerializer',
|
|
|
|
'ChangeSecretUpdateNodeSerializer',
|
2022-11-08 09:54:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
def get_secret_types():
|
|
|
|
return [
|
|
|
|
(SecretType.PASSWORD, _('Password')),
|
|
|
|
(SecretType.SSH_KEY, _('SSH key')),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
class ChangeSecretAutomationSerializer(AuthValidateMixin, BaseAutomationSerializer):
|
2022-11-09 10:15:21 +00:00
|
|
|
secret_strategy = LabeledChoiceField(
|
|
|
|
choices=SecretStrategy.choices, required=True, label=_('Secret strategy')
|
2022-11-08 09:54:51 +00:00
|
|
|
)
|
2022-11-09 10:15:21 +00:00
|
|
|
ssh_key_change_strategy = LabeledChoiceField(
|
|
|
|
choices=SSHKeyStrategy.choices, required=False, label=_('SSH Key strategy')
|
2022-11-08 09:54:51 +00:00
|
|
|
)
|
2023-09-19 02:59:33 +00:00
|
|
|
password_rules = PasswordRulesSerializer(required=False, label=_('Password rules'))
|
2023-01-16 11:02:09 +00:00
|
|
|
secret_type = LabeledChoiceField(choices=get_secret_types(), required=True, label=_('Secret type'))
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ChangeSecretAutomation
|
2022-11-09 10:15:21 +00:00
|
|
|
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
|
2022-11-08 09:54:51 +00:00
|
|
|
fields = BaseAutomationSerializer.Meta.fields + read_only_fields + [
|
|
|
|
'secret_type', 'secret_strategy', 'secret', 'password_rules',
|
2023-07-04 09:34:31 +00:00
|
|
|
'ssh_key_change_strategy', 'passphrase', 'recipients', 'params'
|
2022-11-08 09:54:51 +00:00
|
|
|
]
|
|
|
|
extra_kwargs = {**BaseAutomationSerializer.Meta.extra_kwargs, **{
|
2023-02-21 09:54:59 +00:00
|
|
|
'accounts': {'required': True},
|
2022-11-08 09:54:51 +00:00
|
|
|
'recipients': {'label': _('Recipient'), 'help_text': _(
|
|
|
|
"Currently only mail sending is supported"
|
|
|
|
)},
|
|
|
|
}}
|
2023-04-14 10:31:09 +00:00
|
|
|
|
2023-02-21 10:31:42 +00:00
|
|
|
@property
|
|
|
|
def model_type(self):
|
|
|
|
return AutomationTypes.change_secret
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
def validate_password_rules(self, password_rules):
|
2023-02-16 06:27:54 +00:00
|
|
|
secret_type = self.initial_data['secret_type']
|
2022-11-08 09:54:51 +00:00
|
|
|
if secret_type != SecretType.PASSWORD:
|
|
|
|
return password_rules
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
if self.initial_data.get('secret_strategy') == SecretStrategy.custom:
|
|
|
|
return password_rules
|
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
length = password_rules.get('length')
|
|
|
|
|
|
|
|
try:
|
|
|
|
length = int(length)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
|
|
|
msg = _("* Please enter the correct password length")
|
|
|
|
raise serializers.ValidationError(msg)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
if length < 6 or length > 30:
|
|
|
|
msg = _('* Password length range 6-30 bits')
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
|
|
|
return password_rules
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
secret_type = attrs.get('secret_type')
|
|
|
|
secret_strategy = attrs.get('secret_strategy')
|
|
|
|
if secret_type == SecretType.PASSWORD:
|
|
|
|
attrs.pop('ssh_key_change_strategy', None)
|
|
|
|
if secret_strategy == SecretStrategy.custom:
|
|
|
|
attrs.pop('password_rules', None)
|
|
|
|
else:
|
|
|
|
attrs.pop('secret', None)
|
|
|
|
elif secret_type == SecretType.SSH_KEY:
|
|
|
|
attrs.pop('password_rules', None)
|
|
|
|
if secret_strategy != SecretStrategy.custom:
|
|
|
|
attrs.pop('secret', None)
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeSecretRecordSerializer(serializers.ModelSerializer):
|
|
|
|
is_success = serializers.SerializerMethodField(label=_('Is success'))
|
2022-11-09 10:15:21 +00:00
|
|
|
asset = ObjectRelatedField(queryset=Asset.objects, label=_('Asset'))
|
|
|
|
account = ObjectRelatedField(queryset=Account.objects, label=_('Account'))
|
|
|
|
execution = ObjectRelatedField(
|
|
|
|
queryset=AutomationExecution.objects, label=_('Automation task execution')
|
|
|
|
)
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ChangeSecretRecord
|
|
|
|
fields = [
|
2023-10-30 06:24:34 +00:00
|
|
|
'id', 'asset', 'account', 'date_finished',
|
|
|
|
'status', 'is_success', 'error', 'execution',
|
2022-11-08 09:54:51 +00:00
|
|
|
]
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_is_success(obj):
|
2023-01-16 11:02:09 +00:00
|
|
|
return obj.status == 'success'
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeSecretRecordBackUpSerializer(serializers.ModelSerializer):
|
|
|
|
asset = serializers.SerializerMethodField(label=_('Asset'))
|
|
|
|
account = serializers.SerializerMethodField(label=_('Account'))
|
|
|
|
is_success = serializers.SerializerMethodField(label=_('Is success'))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ChangeSecretRecord
|
|
|
|
fields = [
|
|
|
|
'id', 'asset', 'account', 'old_secret', 'new_secret',
|
|
|
|
'status', 'error', 'is_success'
|
|
|
|
]
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_asset(instance):
|
|
|
|
return str(instance.asset)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_account(instance):
|
|
|
|
return str(instance.account)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_is_success(obj):
|
|
|
|
if obj.status == 'success':
|
|
|
|
return _("Success")
|
|
|
|
return _("Failed")
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeSecretUpdateAssetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ChangeSecretAutomation
|
|
|
|
fields = ['id', 'assets']
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeSecretUpdateNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ChangeSecretAutomation
|
|
|
|
fields = ['id', 'nodes']
|