fix(assets): 网关的密码不能包含特殊字符

pull/4894/head
xinwen 2020-10-27 19:54:41 +08:00 committed by 老广
parent b4a8cb768b
commit e2f540a1f4
6 changed files with 70 additions and 47 deletions

View File

@ -9,6 +9,7 @@ import paramiko
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from common.utils.strings import no_special_chars
from orgs.mixins.models import OrgModelMixin from orgs.mixins.models import OrgModelMixin
from .base import BaseUser from .base import BaseUser
@ -64,8 +65,8 @@ class Gateway(BaseUser):
def test_connective(self, local_port=None): def test_connective(self, local_port=None):
if local_port is None: if local_port is None:
local_port = self.port local_port = self.port
if self.password and not re.match(r'\w+$', self.password): if self.password and not no_special_chars(self.password):
return False, _("Password should not contain special characters") return False, _("Password should not contains special characters")
client = paramiko.SSHClient() client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

View File

@ -4,7 +4,7 @@ from rest_framework import serializers
from common.serializers import AdaptedBulkListSerializer from common.serializers import AdaptedBulkListSerializer
from orgs.mixins.serializers import BulkOrgResourceModelSerializer from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from common.validators import NoSpecialChars
from ..models import Domain, Gateway from ..models import Domain, Gateway
from .base import AuthSerializerMixin from .base import AuthSerializerMixin
@ -47,6 +47,9 @@ class GatewaySerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
'private_key', 'public_key', 'domain', 'is_active', 'date_created', 'private_key', 'public_key', 'domain', 'is_active', 'date_created',
'date_updated', 'created_by', 'comment', 'date_updated', 'created_by', 'comment',
] ]
extra_kwargs = {
'password': {'validators': [NoSpecialChars()]}
}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

View File

@ -0,0 +1,5 @@
import re
def no_special_chars(s):
return bool(re.match(r'\w+$', s))

View File

@ -1,11 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext as _
from rest_framework.validators import ( from rest_framework.validators import (
UniqueTogetherValidator, ValidationError UniqueTogetherValidator, ValidationError
) )
from rest_framework import serializers
from common.utils.strings import no_special_chars
alphanumeric = RegexValidator(r'^[0-9a-zA-Z_@\-\.]*$', _('Special char not allowed')) alphanumeric = RegexValidator(r'^[0-9a-zA-Z_@\-\.]*$', _('Special char not allowed'))
@ -22,3 +24,11 @@ class ProjectUniqueValidator(UniqueTogetherValidator):
continue continue
errors[field] = _('This field must be unique.') errors[field] = _('This field must be unique.')
raise ValidationError(errors) raise ValidationError(errors)
class NoSpecialChars:
def __call__(self, value):
if not no_special_chars(value):
raise serializers.ValidationError(
_("Should not contains special characters")
)

Binary file not shown.

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: JumpServer 0.3.3\n" "Project-Id-Version: JumpServer 0.3.3\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-21 14:31+0800\n" "POT-Creation-Date: 2020-10-27 20:00+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: ibuler <ibuler@qq.com>\n" "Last-Translator: ibuler <ibuler@qq.com>\n"
"Language-Team: JumpServer team<ibuler@qq.com>\n" "Language-Team: JumpServer team<ibuler@qq.com>\n"
@ -24,7 +24,7 @@ msgstr "自定义"
#: applications/models/database_app.py:18 applications/models/k8s_app.py:11 #: applications/models/database_app.py:18 applications/models/k8s_app.py:11
#: applications/models/remote_app.py:21 assets/models/asset.py:149 #: applications/models/remote_app.py:21 assets/models/asset.py:149
#: assets/models/base.py:232 assets/models/cluster.py:18 #: assets/models/base.py:232 assets/models/cluster.py:18
#: assets/models/cmd_filter.py:21 assets/models/domain.py:20 #: assets/models/cmd_filter.py:21 assets/models/domain.py:21
#: assets/models/group.py:20 assets/models/label.py:18 ops/mixin.py:24 #: assets/models/group.py:20 assets/models/label.py:18 ops/mixin.py:24
#: orgs/models.py:23 perms/models/base.py:48 settings/models.py:27 #: orgs/models.py:23 perms/models/base.py:48 settings/models.py:27
#: terminal/models.py:27 terminal/models.py:348 terminal/models.py:380 #: terminal/models.py:27 terminal/models.py:348 terminal/models.py:380
@ -59,7 +59,7 @@ msgid "Host"
msgstr "主机" msgstr "主机"
#: applications/models/database_app.py:27 assets/models/asset.py:195 #: applications/models/database_app.py:27 assets/models/asset.py:195
#: assets/models/domain.py:51 #: assets/models/domain.py:52
msgid "Port" msgid "Port"
msgstr "端口" msgstr "端口"
@ -74,8 +74,8 @@ msgstr "数据库"
#: applications/models/remote_app.py:45 assets/models/asset.py:154 #: applications/models/remote_app.py:45 assets/models/asset.py:154
#: assets/models/asset.py:230 assets/models/base.py:237 #: assets/models/asset.py:230 assets/models/base.py:237
#: assets/models/cluster.py:29 assets/models/cmd_filter.py:23 #: assets/models/cluster.py:29 assets/models/cmd_filter.py:23
#: assets/models/cmd_filter.py:57 assets/models/domain.py:21 #: assets/models/cmd_filter.py:57 assets/models/domain.py:22
#: assets/models/domain.py:54 assets/models/group.py:23 #: assets/models/domain.py:55 assets/models/group.py:23
#: assets/models/label.py:23 ops/models/adhoc.py:37 orgs/models.py:26 #: assets/models/label.py:23 ops/models/adhoc.py:37 orgs/models.py:26
#: perms/models/base.py:56 settings/models.py:32 terminal/models.py:37 #: perms/models/base.py:56 settings/models.py:32 terminal/models.py:37
#: terminal/models.py:387 terminal/models.py:424 tickets/models/ticket.py:43 #: terminal/models.py:387 terminal/models.py:424 tickets/models/ticket.py:43
@ -125,7 +125,7 @@ msgstr "Kubernetes应用"
#: users/templates/users/user_asset_permission.html:40 #: users/templates/users/user_asset_permission.html:40
#: users/templates/users/user_asset_permission.html:70 #: users/templates/users/user_asset_permission.html:70
#: users/templates/users/user_granted_remote_app.html:36 #: users/templates/users/user_granted_remote_app.html:36
#: xpack/plugins/change_auth_plan/models.py:282 #: xpack/plugins/change_auth_plan/models.py:283
#: xpack/plugins/cloud/models.py:275 #: xpack/plugins/cloud/models.py:275
msgid "Asset" msgid "Asset"
msgstr "资产" msgstr "资产"
@ -159,7 +159,7 @@ msgstr "创建者"
# msgstr "创建者" # msgstr "创建者"
#: applications/models/remote_app.py:42 assets/models/asset.py:229 #: applications/models/remote_app.py:42 assets/models/asset.py:229
#: assets/models/base.py:238 assets/models/cluster.py:26 #: assets/models/base.py:238 assets/models/cluster.py:26
#: assets/models/domain.py:23 assets/models/gathered_user.py:19 #: assets/models/domain.py:24 assets/models/gathered_user.py:19
#: assets/models/group.py:22 assets/models/label.py:25 common/db/models.py:69 #: assets/models/group.py:22 assets/models/label.py:25 common/db/models.py:69
#: common/mixins/models.py:50 ops/models/adhoc.py:38 ops/models/command.py:27 #: common/mixins/models.py:50 ops/models/adhoc.py:38 ops/models/command.py:27
#: orgs/models.py:25 orgs/models.py:398 perms/models/base.py:55 #: orgs/models.py:25 orgs/models.py:398 perms/models/base.py:55
@ -222,7 +222,7 @@ msgstr "内部的"
msgid "Platform" msgid "Platform"
msgstr "系统平台" msgstr "系统平台"
#: assets/models/asset.py:190 assets/models/domain.py:50 #: assets/models/asset.py:190 assets/models/domain.py:51
#: assets/serializers/asset_user.py:46 settings/serializers/settings.py:52 #: assets/serializers/asset_user.py:46 settings/serializers/settings.py:52
#: users/templates/users/_granted_assets.html:26 #: users/templates/users/_granted_assets.html:26
#: users/templates/users/user_asset_permission.html:156 #: users/templates/users/user_asset_permission.html:156
@ -237,7 +237,7 @@ msgstr "IP"
msgid "Hostname" msgid "Hostname"
msgstr "主机名" msgstr "主机名"
#: assets/models/asset.py:194 assets/models/domain.py:52 #: assets/models/asset.py:194 assets/models/domain.py:53
#: assets/models/user.py:97 terminal/serializers/session.py:29 #: assets/models/user.py:97 terminal/serializers/session.py:29
msgid "Protocol" msgid "Protocol"
msgstr "协议" msgstr "协议"
@ -247,8 +247,8 @@ msgstr "协议"
msgid "Protocols" msgid "Protocols"
msgstr "协议组" msgstr "协议组"
#: assets/models/asset.py:198 assets/models/domain.py:26 #: assets/models/asset.py:198 assets/models/domain.py:27
#: assets/models/domain.py:53 #: assets/models/domain.py:54
msgid "Domain" msgid "Domain"
msgstr "网域" msgstr "网域"
@ -260,7 +260,7 @@ msgid "Nodes"
msgstr "节点" msgstr "节点"
#: assets/models/asset.py:200 assets/models/cmd_filter.py:22 #: assets/models/asset.py:200 assets/models/cmd_filter.py:22
#: assets/models/domain.py:55 assets/models/label.py:22 #: assets/models/domain.py:56 assets/models/label.py:22
#: authentication/models.py:48 #: authentication/models.py:48
msgid "Is active" msgid "Is active"
msgstr "激活" msgstr "激活"
@ -365,7 +365,7 @@ msgstr ""
#: users/templates/users/user_list.html:15 #: users/templates/users/user_list.html:15
#: users/templates/users/user_profile.html:47 #: users/templates/users/user_profile.html:47
#: xpack/plugins/change_auth_plan/models.py:47 #: xpack/plugins/change_auth_plan/models.py:47
#: xpack/plugins/change_auth_plan/models.py:278 #: xpack/plugins/change_auth_plan/models.py:279
msgid "Username" msgid "Username"
msgstr "用户名" msgstr "用户名"
@ -381,20 +381,20 @@ msgstr "用户名"
#: users/templates/users/user_pubkey_update.html:41 #: users/templates/users/user_pubkey_update.html:41
#: users/templates/users/user_update.html:20 #: users/templates/users/user_update.html:20
#: xpack/plugins/change_auth_plan/models.py:68 #: xpack/plugins/change_auth_plan/models.py:68
#: xpack/plugins/change_auth_plan/models.py:190 #: xpack/plugins/change_auth_plan/models.py:191
#: xpack/plugins/change_auth_plan/models.py:285 #: xpack/plugins/change_auth_plan/models.py:286
msgid "Password" msgid "Password"
msgstr "密码" msgstr "密码"
#: assets/models/base.py:235 xpack/plugins/change_auth_plan/models.py:72 #: assets/models/base.py:235 xpack/plugins/change_auth_plan/models.py:72
#: xpack/plugins/change_auth_plan/models.py:197 #: xpack/plugins/change_auth_plan/models.py:198
#: xpack/plugins/change_auth_plan/models.py:292 #: xpack/plugins/change_auth_plan/models.py:293
msgid "SSH private key" msgid "SSH private key"
msgstr "SSH密钥" msgstr "SSH密钥"
#: assets/models/base.py:236 xpack/plugins/change_auth_plan/models.py:75 #: assets/models/base.py:236 xpack/plugins/change_auth_plan/models.py:75
#: xpack/plugins/change_auth_plan/models.py:193 #: xpack/plugins/change_auth_plan/models.py:194
#: xpack/plugins/change_auth_plan/models.py:288 #: xpack/plugins/change_auth_plan/models.py:289
msgid "SSH public key" msgid "SSH public key"
msgstr "SSH公钥" msgstr "SSH公钥"
@ -506,13 +506,13 @@ msgstr "动作"
msgid "Command filter rule" msgid "Command filter rule"
msgstr "命令过滤规则" msgstr "命令过滤规则"
#: assets/models/domain.py:62 #: assets/models/domain.py:63
msgid "Gateway" msgid "Gateway"
msgstr "网关" msgstr "网关"
#: assets/models/domain.py:68 #: assets/models/domain.py:69
msgid "Password should not contain special characters" msgid "Password should not contains special characters"
msgstr "不能包含特殊字符" msgstr "密码不能包含特殊字符"
#: assets/models/gathered_user.py:16 #: assets/models/gathered_user.py:16
msgid "Present" msgid "Present"
@ -937,7 +937,7 @@ msgstr "成功"
#: audits/models.py:43 ops/models/command.py:28 perms/models/base.py:52 #: audits/models.py:43 ops/models/command.py:28 perms/models/base.py:52
#: terminal/models.py:200 tickets/serializers/request_asset_perm.py:29 #: terminal/models.py:200 tickets/serializers/request_asset_perm.py:29
#: xpack/plugins/change_auth_plan/models.py:177 #: xpack/plugins/change_auth_plan/models.py:177
#: xpack/plugins/change_auth_plan/models.py:307 #: xpack/plugins/change_auth_plan/models.py:308
#: xpack/plugins/gathered_user/models.py:76 #: xpack/plugins/gathered_user/models.py:76
msgid "Date start" msgid "Date start"
msgstr "开始日期" msgstr "开始日期"
@ -1021,7 +1021,7 @@ msgstr "Agent"
msgid "MFA" msgid "MFA"
msgstr "多因子认证" msgstr "多因子认证"
#: audits/models.py:105 xpack/plugins/change_auth_plan/models.py:303 #: audits/models.py:105 xpack/plugins/change_auth_plan/models.py:304
#: xpack/plugins/cloud/models.py:223 #: xpack/plugins/cloud/models.py:223
msgid "Reason" msgid "Reason"
msgstr "原因" msgstr "原因"
@ -1491,14 +1491,18 @@ msgstr ""
msgid "Invalid ip" msgid "Invalid ip"
msgstr "无效IP" msgstr "无效IP"
#: common/validators.py:11 #: common/validators.py:13
msgid "Special char not allowed" msgid "Special char not allowed"
msgstr "不能包含特殊字符" msgstr "不能包含特殊字符"
#: common/validators.py:23 #: common/validators.py:25
msgid "This field must be unique." msgid "This field must be unique."
msgstr "字段必须唯一" msgstr "字段必须唯一"
#: common/validators.py:33
msgid "Should not contains special characters"
msgstr "不能包含特殊字符"
#: jumpserver/conf.py:471 templates/_base_only_msg_content.html:27 #: jumpserver/conf.py:471 templates/_base_only_msg_content.html:27
#: xpack/plugins/interface/api.py:18 xpack/plugins/interface/models.py:36 #: xpack/plugins/interface/api.py:18 xpack/plugins/interface/models.py:36
msgid "Welcome to the JumpServer open source fortress" msgid "Welcome to the JumpServer open source fortress"
@ -1628,7 +1632,7 @@ msgid "End time"
msgstr "完成时间" msgstr "完成时间"
#: ops/models/adhoc.py:242 xpack/plugins/change_auth_plan/models.py:180 #: ops/models/adhoc.py:242 xpack/plugins/change_auth_plan/models.py:180
#: xpack/plugins/change_auth_plan/models.py:310 #: xpack/plugins/change_auth_plan/models.py:311
#: xpack/plugins/gathered_user/models.py:79 #: xpack/plugins/gathered_user/models.py:79
msgid "Time" msgid "Time"
msgstr "时间" msgstr "时间"
@ -2590,16 +2594,16 @@ msgstr "请先确认系统用户"
msgid "Confirmed system-users changed" msgid "Confirmed system-users changed"
msgstr "确认的系统用户变更了" msgstr "确认的系统用户变更了"
#: tickets/api/request_asset_perm.py:108 tickets/api/request_asset_perm.py:115 #: tickets/api/request_asset_perm.py:111 tickets/api/request_asset_perm.py:118
#: xpack/plugins/cloud/models.py:211 #: xpack/plugins/cloud/models.py:211
msgid "Succeed" msgid "Succeed"
msgstr "成功" msgstr "成功"
#: tickets/api/request_asset_perm.py:123 #: tickets/api/request_asset_perm.py:125
msgid "From request ticket: {} {}" msgid "From request ticket: {} {}"
msgstr "来自工单申请: {} {}" msgstr "来自工单申请: {} {}"
#: tickets/api/request_asset_perm.py:125 #: tickets/api/request_asset_perm.py:127
msgid "{} request assets, approved by {}" msgid "{} request assets, approved by {}"
msgstr "{} 申请资产,通过人 {}" msgstr "{} 申请资产,通过人 {}"
@ -3858,44 +3862,44 @@ msgstr "所有资产使用不同的随机密码"
msgid "Password rules" msgid "Password rules"
msgstr "密码规则" msgstr "密码规则"
#: xpack/plugins/change_auth_plan/models.py:187 #: xpack/plugins/change_auth_plan/models.py:188
msgid "Change auth plan snapshot" msgid "Change auth plan snapshot"
msgstr "改密计划快照" msgstr "改密计划快照"
#: xpack/plugins/change_auth_plan/models.py:202 #: xpack/plugins/change_auth_plan/models.py:203
#: xpack/plugins/change_auth_plan/models.py:296 #: xpack/plugins/change_auth_plan/models.py:297
msgid "Change auth plan execution" msgid "Change auth plan execution"
msgstr "改密计划执行" msgstr "改密计划执行"
#: xpack/plugins/change_auth_plan/models.py:269 #: xpack/plugins/change_auth_plan/models.py:270
msgid "Ready" msgid "Ready"
msgstr "" msgstr ""
#: xpack/plugins/change_auth_plan/models.py:270 #: xpack/plugins/change_auth_plan/models.py:271
msgid "Preflight check" msgid "Preflight check"
msgstr "" msgstr ""
#: xpack/plugins/change_auth_plan/models.py:271 #: xpack/plugins/change_auth_plan/models.py:272
msgid "Change auth" msgid "Change auth"
msgstr "" msgstr ""
#: xpack/plugins/change_auth_plan/models.py:272 #: xpack/plugins/change_auth_plan/models.py:273
msgid "Verify auth" msgid "Verify auth"
msgstr "" msgstr ""
#: xpack/plugins/change_auth_plan/models.py:273 #: xpack/plugins/change_auth_plan/models.py:274
msgid "Keep auth" msgid "Keep auth"
msgstr "" msgstr ""
#: xpack/plugins/change_auth_plan/models.py:274 #: xpack/plugins/change_auth_plan/models.py:275
msgid "Finished" msgid "Finished"
msgstr "结束" msgstr "结束"
#: xpack/plugins/change_auth_plan/models.py:300 #: xpack/plugins/change_auth_plan/models.py:301
msgid "Step" msgid "Step"
msgstr "步骤" msgstr "步骤"
#: xpack/plugins/change_auth_plan/models.py:317 #: xpack/plugins/change_auth_plan/models.py:318
msgid "Change auth plan task" msgid "Change auth plan task"
msgstr "改密计划任务" msgstr "改密计划任务"