mirror of https://github.com/jumpserver/jumpserver
fix: 修改 EncryptedField 字段的 write_only 属性 (#8259)
* fix: 修改 EncryptedField 字段的 write_only 属性 fix: 修改 EncryptedField 字段的 write_only 属性 * fix: 修改 EncryptedField 字段的 write_only 属性 Co-authored-by: Jiangjie.Bai <bugatti_it@163.com> Co-authored-by: Jiangjie.Bai <32935519+BaiJiangJie@users.noreply.github.com>pull/8260/head
parent
14710e9c9e
commit
0c71190337
|
@ -5,7 +5,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from assets.serializers.base import AuthSerializerMixin
|
||||
from common.drf.serializers import MethodSerializer
|
||||
from common.drf.serializers import MethodSerializer, SecretReadableMixin
|
||||
from .attrs import (
|
||||
category_serializer_classes_mapping,
|
||||
type_serializer_classes_mapping,
|
||||
|
@ -152,7 +152,7 @@ class AppAccountSerializer(AppSerializerMixin, AuthSerializerMixin, BulkOrgResou
|
|||
return super().to_representation(instance)
|
||||
|
||||
|
||||
class AppAccountSecretSerializer(AppAccountSerializer):
|
||||
class AppAccountSecretSerializer(SecretReadableMixin, AppAccountSerializer):
|
||||
class Meta(AppAccountSerializer.Meta):
|
||||
fields_backup = [
|
||||
'id', 'app_display', 'attrs', 'username', 'password', 'private_key',
|
||||
|
|
|
@ -7,6 +7,7 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|||
from .base import AuthSerializerMixin
|
||||
from .utils import validate_password_contains_left_double_curly_bracket
|
||||
from common.utils.encode import ssh_pubkey_gen
|
||||
from common.drf.serializers import SecretReadableMixin
|
||||
|
||||
|
||||
class AccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
||||
|
@ -70,7 +71,7 @@ class AccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
|||
return super().to_representation(instance)
|
||||
|
||||
|
||||
class AccountSecretSerializer(AccountSerializer):
|
||||
class AccountSecretSerializer(SecretReadableMixin, AccountSerializer):
|
||||
class Meta(AccountSerializer.Meta):
|
||||
fields_backup = [
|
||||
'hostname', 'ip', 'platform', 'protocols', 'username', 'password',
|
||||
|
|
|
@ -8,10 +8,12 @@ from common.mixins import BulkListSerializerMixin
|
|||
from django.utils.functional import cached_property
|
||||
from rest_framework.utils.serializer_helpers import BindingDict
|
||||
from common.mixins.serializers import BulkSerializerMixin
|
||||
from common.drf.fields import EncryptedField
|
||||
|
||||
__all__ = [
|
||||
'MethodSerializer',
|
||||
'EmptySerializer', 'BulkModelSerializer', 'AdaptedBulkListSerializer', 'CeleryTaskSerializer'
|
||||
'EmptySerializer', 'BulkModelSerializer', 'AdaptedBulkListSerializer', 'CeleryTaskSerializer',
|
||||
'SecretReadableMixin'
|
||||
]
|
||||
|
||||
|
||||
|
@ -83,3 +85,20 @@ class CeleryTaskSerializer(serializers.Serializer):
|
|||
task = serializers.CharField(read_only=True)
|
||||
|
||||
|
||||
class SecretReadableMixin(serializers.Serializer):
|
||||
""" 加密字段 (EncryptedField) 可读性 """
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SecretReadableMixin, self).__init__(*args, **kwargs)
|
||||
if not hasattr(self, 'Meta') or not hasattr(self.Meta, 'extra_kwargs'):
|
||||
return
|
||||
extra_kwargs = self.Meta.extra_kwargs
|
||||
for field_name, serializer_field in self.fields.items():
|
||||
if not isinstance(serializer_field, EncryptedField):
|
||||
continue
|
||||
if field_name not in extra_kwargs:
|
||||
continue
|
||||
field_extra_kwargs = extra_kwargs[field_name]
|
||||
if 'write_only' not in field_extra_kwargs:
|
||||
continue
|
||||
serializer_field.write_only = field_extra_kwargs['write_only']
|
||||
|
|
Loading…
Reference in New Issue