diff --git a/apps/assets/serializers/base.py b/apps/assets/serializers/base.py index 92cc65f19..2c5e579b6 100644 --- a/apps/assets/serializers/base.py +++ b/apps/assets/serializers/base.py @@ -8,7 +8,7 @@ from rest_framework import serializers from common.utils import ssh_pubkey_gen, ssh_private_key_gen, validate_ssh_private_key from common.drf.fields import EncryptedField from assets.models import Type -from .utils import validate_password_contains_left_double_curly_bracket +from .utils import validate_password_for_ansible class AuthSerializer(serializers.ModelSerializer): @@ -35,7 +35,7 @@ class AuthSerializer(serializers.ModelSerializer): class AuthSerializerMixin(serializers.ModelSerializer): password = EncryptedField( label=_('Password'), required=False, allow_blank=True, allow_null=True, max_length=1024, - validators=[validate_password_contains_left_double_curly_bracket] + validators=[validate_password_for_ansible] ) private_key = EncryptedField( label=_('SSH private key'), required=False, allow_blank=True, allow_null=True, max_length=4096 diff --git a/apps/assets/serializers/system_user.py b/apps/assets/serializers/system_user.py index 68ade0ebd..54aff3e82 100644 --- a/apps/assets/serializers/system_user.py +++ b/apps/assets/serializers/system_user.py @@ -9,7 +9,7 @@ from common.drf.serializers import SecretReadableMixin from common.validators import alphanumeric_re, alphanumeric_cn_re, alphanumeric_win_re from orgs.mixins.serializers import BulkOrgResourceModelSerializer from ..models import SystemUser, Asset -from .utils import validate_password_contains_left_double_curly_bracket +from .utils import validate_password_for_ansible from .base import AuthSerializerMixin __all__ = [ @@ -27,7 +27,7 @@ class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer): """ password = EncryptedField( label=_('Password'), required=False, allow_blank=True, allow_null=True, max_length=1024, - trim_whitespace=False, validators=[validate_password_contains_left_double_curly_bracket], + trim_whitespace=False, validators=[validate_password_for_ansible], write_only=True ) auto_generate_key = serializers.BooleanField(initial=True, required=False, write_only=True) diff --git a/apps/assets/serializers/utils.py b/apps/assets/serializers/utils.py index 9110a9978..52527e723 100644 --- a/apps/assets/serializers/utils.py +++ b/apps/assets/serializers/utils.py @@ -2,8 +2,16 @@ from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers -def validate_password_contains_left_double_curly_bracket(password): +def validate_password_for_ansible(password): + """ 校验 Ansible 不支持的特殊字符 """ # validate password contains left double curly bracket # check password not contains `{{` + # Ansible 推送的时候不支持 if '{{' in password: raise serializers.ValidationError(_('Password can not contains `{{` ')) + # Ansible Windows 推送的时候不支持 + if "'" in password: + raise serializers.ValidationError(_("Password can not contains `'` ")) + if '"' in password: + raise serializers.ValidationError(_('Password can not contains `"` ')) +