2018-02-07 15:25:15 +00:00
|
|
|
|
from rest_framework import serializers
|
2019-05-21 08:24:01 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from django.db.models import Count
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
from common.mixins.serializers import BulkSerializerMixin
|
2019-08-01 09:10:02 +00:00
|
|
|
|
from common.utils import ssh_pubkey_gen
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2020-03-12 08:24:38 +00:00
|
|
|
|
from ..models import SystemUser, Asset
|
|
|
|
|
from .base import AuthSerializerMixin
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
__all__ = [
|
2021-07-14 08:22:27 +00:00
|
|
|
|
'SystemUserSerializer',
|
2019-12-05 07:09:25 +00:00
|
|
|
|
'SystemUserSimpleSerializer', 'SystemUserAssetRelationSerializer',
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'SystemUserNodeRelationSerializer', 'SystemUserTaskSerializer',
|
|
|
|
|
'SystemUserUserRelationSerializer', 'SystemUserWithAuthInfoSerializer',
|
2021-05-31 07:01:14 +00:00
|
|
|
|
'SystemUserTempAuthSerializer',
|
2019-12-05 07:09:25 +00:00
|
|
|
|
]
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
|
"""
|
|
|
|
|
系统用户
|
|
|
|
|
"""
|
2019-07-08 07:35:20 +00:00
|
|
|
|
auto_generate_key = serializers.BooleanField(initial=True, required=False, write_only=True)
|
2021-07-14 08:22:27 +00:00
|
|
|
|
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display'))
|
2021-07-13 08:57:23 +00:00
|
|
|
|
ssh_key_fingerprint = serializers.ReadOnlyField(label=_('SSH key fingerprint'))
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2021-04-29 11:10:45 +00:00
|
|
|
|
fields_mini = ['id', 'name', 'username']
|
|
|
|
|
fields_write_only = ['password', 'public_key', 'private_key']
|
|
|
|
|
fields_small = fields_mini + fields_write_only + [
|
2021-07-08 06:23:18 +00:00
|
|
|
|
'type', 'type_display', 'protocol', 'login_mode', 'login_mode_display',
|
2021-07-13 08:57:23 +00:00
|
|
|
|
'priority', 'sudo', 'shell', 'sftp_root', 'token', 'ssh_key_fingerprint',
|
2021-04-29 11:10:45 +00:00
|
|
|
|
'home', 'system_groups', 'ad_domain',
|
|
|
|
|
'username_same_with_user', 'auto_push', 'auto_generate_key',
|
|
|
|
|
'date_created', 'date_updated',
|
|
|
|
|
'comment', 'created_by',
|
2019-05-21 08:24:01 +00:00
|
|
|
|
]
|
2021-07-08 06:23:18 +00:00
|
|
|
|
fields_m2m = ['cmd_filters', 'assets_amount']
|
2021-04-29 11:10:45 +00:00
|
|
|
|
fields = fields_small + fields_m2m
|
2019-05-21 08:24:01 +00:00
|
|
|
|
extra_kwargs = {
|
2019-06-25 06:32:25 +00:00
|
|
|
|
'password': {"write_only": True},
|
|
|
|
|
'public_key': {"write_only": True},
|
|
|
|
|
'private_key': {"write_only": True},
|
2020-08-07 03:52:34 +00:00
|
|
|
|
'token': {"write_only": True},
|
2020-11-11 02:27:18 +00:00
|
|
|
|
'nodes_amount': {'label': _('Nodes amount')},
|
|
|
|
|
'assets_amount': {'label': _('Assets amount')},
|
2019-05-21 08:24:01 +00:00
|
|
|
|
'login_mode_display': {'label': _('Login mode display')},
|
2019-06-19 08:45:14 +00:00
|
|
|
|
'created_by': {'read_only': True},
|
2020-11-13 06:46:25 +00:00
|
|
|
|
'ad_domain': {'required': False, 'allow_blank': True, 'label': _('Ad domain')},
|
2019-05-21 08:24:01 +00:00
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_auto_push(self, value):
|
2021-07-13 01:07:57 +00:00
|
|
|
|
login_mode = self.get_initial_value("login_mode")
|
|
|
|
|
protocol = self.get_initial_value("protocol")
|
2019-07-08 07:35:20 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
if login_mode == SystemUser.LOGIN_MANUAL:
|
|
|
|
|
value = False
|
|
|
|
|
elif protocol not in SystemUser.SUPPORT_PUSH_PROTOCOLS:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
value = False
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def validate_auto_generate_key(self, value):
|
2021-07-13 01:07:57 +00:00
|
|
|
|
login_mode = self.get_initial_value("login_mode")
|
|
|
|
|
protocol = self.get_initial_value("protocol")
|
2019-07-08 07:35:20 +00:00
|
|
|
|
|
|
|
|
|
if self.context["request"].method.lower() != "post":
|
|
|
|
|
value = False
|
|
|
|
|
elif self.instance:
|
|
|
|
|
value = False
|
|
|
|
|
elif login_mode == SystemUser.LOGIN_MANUAL:
|
|
|
|
|
value = False
|
2021-07-08 06:23:18 +00:00
|
|
|
|
elif protocol not in SystemUser.SUPPORT_PUSH_PROTOCOLS:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
value = False
|
|
|
|
|
return value
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
def validate_username_same_with_user(self, username_same_with_user):
|
|
|
|
|
if not username_same_with_user:
|
|
|
|
|
return username_same_with_user
|
2021-07-13 01:07:57 +00:00
|
|
|
|
protocol = self.get_initial_value("protocol", "ssh")
|
2020-03-12 08:24:38 +00:00
|
|
|
|
queryset = SystemUser.objects.filter(
|
2021-07-08 06:23:18 +00:00
|
|
|
|
protocol=protocol,
|
|
|
|
|
username_same_with_user=True
|
2020-03-12 08:24:38 +00:00
|
|
|
|
)
|
|
|
|
|
if self.instance:
|
|
|
|
|
queryset = queryset.exclude(id=self.instance.id)
|
|
|
|
|
exists = queryset.exists()
|
|
|
|
|
if not exists:
|
|
|
|
|
return username_same_with_user
|
|
|
|
|
error = _("Username same with user with protocol {} only allow 1").format(protocol)
|
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_username(self, username):
|
|
|
|
|
if username:
|
|
|
|
|
return username
|
2021-07-13 01:07:57 +00:00
|
|
|
|
login_mode = self.get_initial_value("login_mode")
|
|
|
|
|
protocol = self.get_initial_value("protocol")
|
|
|
|
|
username_same_with_user = self.get_initial_value("username_same_with_user")
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2021-07-12 04:46:55 +00:00
|
|
|
|
if username_same_with_user:
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
if login_mode == SystemUser.LOGIN_AUTO and protocol != SystemUser.Protocol.vnc:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
msg = _('* Automatic login mode must fill in the username.')
|
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
return username
|
|
|
|
|
|
2021-05-13 16:17:47 +00:00
|
|
|
|
def validate_home(self, home):
|
2021-07-13 01:07:57 +00:00
|
|
|
|
username_same_with_user = self.get_initial_value("username_same_with_user")
|
2021-05-13 16:17:47 +00:00
|
|
|
|
if username_same_with_user:
|
|
|
|
|
return ''
|
|
|
|
|
return home
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
def validate_sftp_root(self, value):
|
|
|
|
|
if value in ['home', 'tmp']:
|
|
|
|
|
return value
|
|
|
|
|
if not value.startswith('/'):
|
|
|
|
|
error = _("Path should starts with /")
|
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
|
return value
|
|
|
|
|
|
2021-07-13 01:07:57 +00:00
|
|
|
|
def validate_admin_user(self, attrs):
|
|
|
|
|
if self.instance:
|
|
|
|
|
tp = self.instance.type
|
|
|
|
|
else:
|
|
|
|
|
tp = attrs.get('type')
|
2021-07-08 06:23:18 +00:00
|
|
|
|
if tp != SystemUser.Type.admin:
|
|
|
|
|
return attrs
|
|
|
|
|
attrs['protocol'] = SystemUser.Protocol.ssh
|
|
|
|
|
attrs['login_mode'] = SystemUser.LOGIN_AUTO
|
|
|
|
|
attrs['username_same_with_user'] = False
|
|
|
|
|
attrs['auto_push'] = False
|
|
|
|
|
return attrs
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_password(self, password):
|
|
|
|
|
super().validate_password(password)
|
2021-07-13 01:07:57 +00:00
|
|
|
|
auto_gen_key = self.get_initial_value("auto_generate_key", False)
|
|
|
|
|
private_key = self.get_initial_value("private_key")
|
|
|
|
|
login_mode = self.get_initial_value("login_mode")
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2019-08-05 02:01:03 +00:00
|
|
|
|
if not self.instance and not auto_gen_key and not password and \
|
|
|
|
|
not private_key and login_mode == SystemUser.LOGIN_AUTO:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
raise serializers.ValidationError(_("Password or private key required"))
|
|
|
|
|
return password
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
def validate_gen_key(self, attrs):
|
2019-07-08 07:35:20 +00:00
|
|
|
|
username = attrs.get("username", "manual")
|
2019-12-19 07:28:17 +00:00
|
|
|
|
auto_gen_key = attrs.pop("auto_generate_key", False)
|
2019-07-08 07:35:20 +00:00
|
|
|
|
protocol = attrs.get("protocol")
|
2019-12-19 07:28:17 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
if protocol not in SystemUser.SUPPORT_PUSH_PROTOCOLS:
|
2019-12-19 07:28:17 +00:00
|
|
|
|
return attrs
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
# 自动生成
|
|
|
|
|
if auto_gen_key and not self.instance:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
password = SystemUser.gen_password()
|
|
|
|
|
attrs["password"] = password
|
2021-07-08 06:23:18 +00:00
|
|
|
|
if protocol == SystemUser.Protocol.ssh:
|
2019-07-08 07:35:20 +00:00
|
|
|
|
private_key, public_key = SystemUser.gen_key(username)
|
|
|
|
|
attrs["private_key"] = private_key
|
|
|
|
|
attrs["public_key"] = public_key
|
2021-07-08 06:23:18 +00:00
|
|
|
|
# 如果设置了private key,没有设置public key则生成
|
2019-08-01 09:10:02 +00:00
|
|
|
|
elif attrs.get("private_key", None):
|
|
|
|
|
private_key = attrs["private_key"]
|
|
|
|
|
password = attrs.get("password")
|
2021-07-08 06:23:18 +00:00
|
|
|
|
public_key = ssh_pubkey_gen(private_key, password=password, username=username)
|
2019-08-01 09:10:02 +00:00
|
|
|
|
attrs["public_key"] = public_key
|
2019-07-08 07:35:20 +00:00
|
|
|
|
return attrs
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
attrs = self.validate_admin_user(attrs)
|
|
|
|
|
attrs = self.validate_gen_key(attrs)
|
|
|
|
|
return attrs
|
|
|
|
|
|
2019-07-04 07:36:57 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
|
""" Perform necessary eager loading of data. """
|
2020-03-12 08:24:38 +00:00
|
|
|
|
queryset = queryset.annotate(assets_amount=Count("assets"))
|
2019-07-04 07:36:57 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
class SystemUserWithAuthInfoSerializer(SystemUserSerializer):
|
|
|
|
|
class Meta(SystemUserSerializer.Meta):
|
2021-04-29 11:10:45 +00:00
|
|
|
|
fields_mini = ['id', 'name', 'username']
|
|
|
|
|
fields_write_only = ['password', 'public_key', 'private_key']
|
|
|
|
|
fields_small = fields_mini + fields_write_only + [
|
|
|
|
|
'protocol', 'login_mode', 'login_mode_display', 'priority',
|
|
|
|
|
'sudo', 'shell', 'ad_domain', 'sftp_root', 'token',
|
|
|
|
|
"username_same_with_user", 'auto_push', 'auto_generate_key',
|
|
|
|
|
'comment',
|
2020-03-16 08:59:45 +00:00
|
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
|
fields = fields_small
|
2020-03-12 08:24:38 +00:00
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'nodes_amount': {'label': _('Node')},
|
|
|
|
|
'assets_amount': {'label': _('Asset')},
|
|
|
|
|
'login_mode_display': {'label': _('Login mode display')},
|
|
|
|
|
'created_by': {'read_only': True},
|
|
|
|
|
}
|
2020-01-07 07:53:53 +00:00
|
|
|
|
|
2018-03-09 04:53:08 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
class SystemUserSimpleSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""
|
|
|
|
|
系统用户最基本信息的数据结构
|
|
|
|
|
"""
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2018-12-18 09:28:45 +00:00
|
|
|
|
fields = ('id', 'name', 'username')
|
|
|
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
class RelationMixin(BulkSerializerMixin, serializers.Serializer):
|
2021-07-14 03:31:09 +00:00
|
|
|
|
systemuser_display = serializers.ReadOnlyField(label=_("System user name"))
|
2021-07-14 10:51:24 +00:00
|
|
|
|
org_name = serializers.ReadOnlyField(label=_("Org name"))
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
2021-07-14 10:51:24 +00:00
|
|
|
|
fields.extend(['systemuser', "systemuser_display", "org_name"])
|
2019-12-05 07:09:25 +00:00
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserAssetRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
2021-07-14 03:31:09 +00:00
|
|
|
|
asset_display = serializers.ReadOnlyField(label=_('Asset hostname'))
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
class Meta:
|
2019-12-05 07:09:25 +00:00
|
|
|
|
model = SystemUser.assets.through
|
|
|
|
|
fields = [
|
2021-07-12 12:11:53 +00:00
|
|
|
|
"id", "asset", "asset_display", 'systemuser', 'systemuser_display',
|
2021-07-13 09:43:36 +00:00
|
|
|
|
"connectivity", 'date_verified', 'org_id'
|
2019-12-05 07:09:25 +00:00
|
|
|
|
]
|
2021-07-08 06:23:18 +00:00
|
|
|
|
use_model_bulk_create = True
|
|
|
|
|
model_bulk_create_kwargs = {
|
|
|
|
|
'ignore_conflicts': True
|
|
|
|
|
}
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserNodeRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
|
node_display = serializers.SerializerMethodField()
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
class Meta:
|
2019-12-05 07:09:25 +00:00
|
|
|
|
model = SystemUser.nodes.through
|
|
|
|
|
fields = [
|
|
|
|
|
'id', 'node', "node_display",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def get_node_display(self, obj):
|
2020-09-29 09:13:32 +00:00
|
|
|
|
return obj.node.full_value
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserUserRelationSerializer(RelationMixin, serializers.ModelSerializer):
|
|
|
|
|
user_display = serializers.ReadOnlyField()
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
class Meta:
|
2020-03-12 08:24:38 +00:00
|
|
|
|
model = SystemUser.users.through
|
|
|
|
|
fields = [
|
|
|
|
|
'id', "user", "user_display",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserTaskSerializer(serializers.Serializer):
|
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
|
("test", "test"),
|
|
|
|
|
("push", "push"),
|
|
|
|
|
)
|
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
|
|
|
|
asset = serializers.PrimaryKeyRelatedField(
|
|
|
|
|
queryset=Asset.objects, allow_null=True, required=False, write_only=True
|
|
|
|
|
)
|
2020-12-08 09:29:22 +00:00
|
|
|
|
assets = serializers.PrimaryKeyRelatedField(
|
|
|
|
|
queryset=Asset.objects, allow_null=True, required=False, write_only=True,
|
|
|
|
|
many=True
|
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
task = serializers.CharField(read_only=True)
|
2021-05-31 07:01:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserTempAuthSerializer(SystemUserSerializer):
|
|
|
|
|
instance_id = serializers.CharField()
|
|
|
|
|
|
|
|
|
|
class Meta(SystemUserSerializer.Meta):
|
|
|
|
|
fields = ['instance_id', 'username', 'password']
|