diff --git a/apps/accounts/serializers/automations/change_secret.py b/apps/accounts/serializers/automations/change_secret.py index 40092c1ee..0c27d9f9c 100644 --- a/apps/accounts/serializers/automations/change_secret.py +++ b/apps/accounts/serializers/automations/change_secret.py @@ -42,7 +42,7 @@ class ChangeSecretAutomationSerializer(AuthValidateMixin, BaseAutomationSerializ ) ssh_key_change_strategy = LabeledChoiceField( choices=SSHKeyStrategy.choices, required=False, label=_('SSH Key strategy'), - default="set_jms" + default=SSHKeyStrategy.set_jms ) password_rules = PasswordRulesSerializer(required=False, label=_('Password rules')) secret_type = LabeledChoiceField(choices=get_secret_types(), required=True, label=_('Secret type')) @@ -65,26 +65,6 @@ class ChangeSecretAutomationSerializer(AuthValidateMixin, BaseAutomationSerializ )}, }} - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.set_ssh_key_change_strategy_choices() - - def set_ssh_key_change_strategy_choices(self): - ssh_key_change_strategy = self.fields.get("ssh_key_change_strategy") - if not ssh_key_change_strategy: - return - ssh_key_change_strategy._choices.pop(SSHKeyStrategy.add, None) - - def to_representation(self, instance): - data = super().to_representation(instance) - ssh_strategy_value = data.get('ssh_key_change_strategy', {}).get('value') - if ssh_strategy_value == SSHKeyStrategy.add: - data['ssh_key_change_strategy'] = { - 'label': SSHKeyStrategy.set_jms.label, - 'value': SSHKeyStrategy.set_jms.value - } - return data - @property def model_type(self): return AutomationTypes.change_secret @@ -97,6 +77,19 @@ class ChangeSecretAutomationSerializer(AuthValidateMixin, BaseAutomationSerializ if self.initial_data.get('secret_strategy') == SecretStrategy.custom: return password_rules + 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) + + if length < 6 or length > 30: + msg = _('* Password length range 6-30 bits') + raise serializers.ValidationError(msg) + return password_rules def validate(self, attrs): @@ -136,7 +129,7 @@ class ChangeSecretRecordSerializer(serializers.ModelSerializer): @staticmethod def get_is_success(obj): - return obj.status == ChangeSecretRecordStatusChoice.success.value + return obj.status == ChangeSecretRecordStatusChoice.success class ChangeSecretRecordViewSecretSerializer(serializers.ModelSerializer):