2019-10-12 09:05:49 +00:00
|
|
|
|
import re
|
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 _
|
|
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
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
|
2019-06-19 08:45:14 +00:00
|
|
|
|
from ..models import SystemUser
|
2019-10-12 09:05:49 +00:00
|
|
|
|
from ..const import (
|
2019-10-25 02:55:28 +00:00
|
|
|
|
GENERAL_FORBIDDEN_SPECIAL_CHARACTERS_PATTERN,
|
|
|
|
|
GENERAL_FORBIDDEN_SPECIAL_CHARACTERS_ERROR_MSG
|
2019-10-12 09:05:49 +00:00
|
|
|
|
)
|
2019-07-08 07:35:20 +00:00
|
|
|
|
from .base import AuthSerializer, AuthSerializerMixin
|
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)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2019-04-25 02:11:50 +00:00
|
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2019-05-21 08:24:01 +00:00
|
|
|
|
fields = [
|
2019-06-25 06:32:25 +00:00
|
|
|
|
'id', 'name', 'username', 'password', 'public_key', 'private_key',
|
|
|
|
|
'login_mode', 'login_mode_display', 'priority', 'protocol',
|
2019-10-18 07:12:17 +00:00
|
|
|
|
'auto_push', 'cmd_filters', 'sudo', 'shell', 'comment',
|
|
|
|
|
'assets_amount', 'nodes_amount', 'auto_generate_key'
|
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},
|
2019-10-18 07:12:17 +00:00
|
|
|
|
'nodes_amount': {'label': _('Node')},
|
2019-06-24 12:16:18 +00:00
|
|
|
|
'assets_amount': {'label': _('Asset')},
|
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},
|
2019-05-21 08:24:01 +00:00
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-10-12 09:05:49 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def validate_name(name):
|
2019-10-25 02:55:28 +00:00
|
|
|
|
pattern = GENERAL_FORBIDDEN_SPECIAL_CHARACTERS_PATTERN
|
|
|
|
|
res = re.search(pattern, name)
|
|
|
|
|
if res is not None:
|
|
|
|
|
msg = GENERAL_FORBIDDEN_SPECIAL_CHARACTERS_ERROR_MSG
|
2019-10-12 09:05:49 +00:00
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
return name
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
|
def validate_auto_push(self, value):
|
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
protocol = self.initial_data.get("protocol")
|
|
|
|
|
|
|
|
|
|
if login_mode == SystemUser.LOGIN_MANUAL or \
|
|
|
|
|
protocol in [SystemUser.PROTOCOL_TELNET,
|
|
|
|
|
SystemUser.PROTOCOL_VNC]:
|
|
|
|
|
value = False
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def validate_auto_generate_key(self, value):
|
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
protocol = self.initial_data.get("protocol")
|
|
|
|
|
|
|
|
|
|
if self.context["request"].method.lower() != "post":
|
|
|
|
|
value = False
|
|
|
|
|
elif self.instance:
|
|
|
|
|
value = False
|
|
|
|
|
elif login_mode == SystemUser.LOGIN_MANUAL:
|
|
|
|
|
value = False
|
|
|
|
|
elif protocol in [SystemUser.PROTOCOL_TELNET, SystemUser.PROTOCOL_VNC]:
|
|
|
|
|
value = False
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
def validate_username(self, username):
|
|
|
|
|
if username:
|
|
|
|
|
return username
|
2019-07-08 10:30:18 +00:00
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
protocol = self.initial_data.get("protocol")
|
2019-07-08 07:35:20 +00:00
|
|
|
|
if login_mode == SystemUser.LOGIN_AUTO and \
|
|
|
|
|
protocol != SystemUser.PROTOCOL_VNC:
|
|
|
|
|
msg = _('* Automatic login mode must fill in the username.')
|
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
return username
|
|
|
|
|
|
|
|
|
|
def validate_password(self, password):
|
|
|
|
|
super().validate_password(password)
|
|
|
|
|
auto_gen_key = self.initial_data.get("auto_generate_key", False)
|
|
|
|
|
private_key = self.initial_data.get("private_key")
|
2019-08-05 02:01:03 +00:00
|
|
|
|
login_mode = self.initial_data.get("login_mode")
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
username = attrs.get("username", "manual")
|
|
|
|
|
protocol = attrs.get("protocol")
|
|
|
|
|
auto_gen_key = attrs.get("auto_generate_key", False)
|
|
|
|
|
if auto_gen_key:
|
|
|
|
|
password = SystemUser.gen_password()
|
|
|
|
|
attrs["password"] = password
|
|
|
|
|
if protocol == SystemUser.PROTOCOL_SSH:
|
|
|
|
|
private_key, public_key = SystemUser.gen_key(username)
|
|
|
|
|
attrs["private_key"] = private_key
|
|
|
|
|
attrs["public_key"] = public_key
|
2019-08-01 09:10:02 +00:00
|
|
|
|
# 如果设置了private key,没有设置public key则生成
|
|
|
|
|
elif attrs.get("private_key", None):
|
|
|
|
|
private_key = attrs["private_key"]
|
|
|
|
|
password = attrs.get("password")
|
|
|
|
|
public_key = ssh_pubkey_gen(private_key, password=password,
|
|
|
|
|
username=username)
|
|
|
|
|
attrs["public_key"] = public_key
|
2019-07-08 07:35:20 +00:00
|
|
|
|
attrs.pop("auto_generate_key", None)
|
|
|
|
|
return attrs
|
|
|
|
|
|
2019-07-04 07:36:57 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
|
queryset = queryset.prefetch_related('cmd_filters', 'nodes')
|
|
|
|
|
return queryset
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2018-03-27 10:34:41 +00:00
|
|
|
|
class SystemUserAuthSerializer(AuthSerializer):
|
2018-03-09 04:53:08 +00:00
|
|
|
|
"""
|
|
|
|
|
系统用户认证信息
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
fields = [
|
|
|
|
|
"id", "name", "username", "protocol",
|
2018-07-17 04:06:47 +00:00
|
|
|
|
"login_mode", "password", "private_key",
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|