From 3248ee1a3d441d42286a98df9f924abd67d216b4 Mon Sep 17 00:00:00 2001 From: yumaojun <719118794@qq.com> Date: Tue, 22 Dec 2015 22:39:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B7=BB=E5=8A=A0role=20?= =?UTF-8?q?=E6=97=B6=E3=80=80=E5=9B=A0=E7=A7=81=E9=92=A5=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=B8=8D=E5=AF=B9=E8=80=8C=E5=BC=95=E8=B5=B7=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/utils.py | 9 ++++----- jperm/views.py | 6 ++++-- templates/jperm/perm_role_add.html | 9 ++++++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/jperm/utils.py b/jperm/utils.py index 92506cdc1..ecdac7f81 100644 --- a/jperm/utils.py +++ b/jperm/utils.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -import random import os.path import shutil from paramiko import SSHException @@ -8,9 +7,9 @@ from paramiko.rsakey import RSAKey from jumpserver.api import mkdir from uuid import uuid4 from jumpserver.api import CRYPTOR -from os import makedirs -from tempfile import NamedTemporaryFile +from jumpserver.api import logger + from jumpserver.settings import KEY_DIR @@ -55,9 +54,9 @@ def gen_keys(key="", key_path_dir=""): with open(key_file) as f: try: key = RSAKey.from_private_key(f) - except SSHException: + except SSHException, e: shutil.rmtree(key_path_dir, ignore_errors=True) - raise SSHException + raise SSHException(e) os.chmod(private_key, 0644) with open(public_key, 'w') as content_file: diff --git a/jperm/views.py b/jperm/views.py index cb9a7d46e..7af646f9f 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -294,7 +294,10 @@ def perm_role_add(request): # 生成随机密码,生成秘钥对 sudos_obj = [get_object(PermSudo, id=sudo_id) for sudo_id in sudo_ids] if key_content: - key_path = gen_keys(key=key_content) + try: + key_path = gen_keys(key=key_content) + except SSHException, e: + raise ServerError(e) else: key_path = gen_keys() logger.debug('generate role key: %s' % key_path) @@ -305,7 +308,6 @@ def perm_role_add(request): return HttpResponseRedirect(reverse('role_list')) except ServerError, e: error = e - return my_render('jperm/perm_role_add.html', locals(), request) diff --git a/templates/jperm/perm_role_add.html b/templates/jperm/perm_role_add.html index 65a7d8037..ab2ac43b2 100644 --- a/templates/jperm/perm_role_add.html +++ b/templates/jperm/perm_role_add.html @@ -50,7 +50,7 @@
- + 如果不添加密钥,会自动生成
@@ -93,6 +93,7 @@ $('#roleForm').validator({ theme: "yellow_right_effect", rules: { check_name: [/^\w{2,20}$/, '大小写字母数字和下划线,2-20位'], + check_begin: [/^[\-]+BEGIN RSA PRIVATE KEY[\-]+/gm, '请填写正确的RSA Key'], {# either: function(){#} {# return $('#role_password').val() == ''#} {# }#} @@ -105,6 +106,10 @@ $('#roleForm').validator({ ok: "", msg: {required: "系统用户名称必填"} }, + "role_key": { + rule: "required;check_begin", + tip: "请输入正确格式的RSA私钥" + }, {# "role_key": {#} {# rule: "required(either)",#} {# tip: "输入密钥",#} @@ -129,6 +134,8 @@ for (var selector in config) { $(selector).chosen(config[selector]); } + +