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]); } + +