From 40de7d64c669cc1bb4e5c4a80922a8150dfe7f57 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 17 Jul 2019 13:41:26 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9private=E5=88=9B?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/forms/domain.py | 2 +- apps/assets/forms/user.py | 28 +++++----- apps/assets/serializers/base.py | 4 ++ .../assets/templates/assets/_system_user.html | 10 ++-- .../assets/admin_user_create_update.html | 9 ++-- .../assets/gateway_create_update.html | 10 ++-- .../templates/assets/system_user_update.html | 2 +- apps/locale/zh/LC_MESSAGES/django.mo | Bin 77762 -> 77928 bytes apps/locale/zh/LC_MESSAGES/django.po | 49 ++++++++++-------- 9 files changed, 62 insertions(+), 52 deletions(-) diff --git a/apps/assets/forms/domain.py b/apps/assets/forms/domain.py index 5a88a1dd7..90db16fd0 100644 --- a/apps/assets/forms/domain.py +++ b/apps/assets/forms/domain.py @@ -64,7 +64,7 @@ class GatewayForm(PasswordAndKeyAuthForm, OrgModelForm): model = Gateway fields = [ 'name', 'ip', 'port', 'username', 'protocol', 'domain', 'password', - 'private_key_file', 'is_active', 'comment', + 'private_key', 'is_active', 'comment', ] help_texts = { 'protocol': _("SSH gateway support proxy SSH,RDP,VNC") diff --git a/apps/assets/forms/user.py b/apps/assets/forms/user.py index 34a9e3c52..b0096c2f0 100644 --- a/apps/assets/forms/user.py +++ b/apps/assets/forms/user.py @@ -26,39 +26,39 @@ class PasswordAndKeyAuthForm(forms.ModelForm): label=_("Password"), ) # Need use upload private key file except paste private key content - private_key_file = forms.FileField(required=False, label=_("Private key")) + private_key = forms.FileField(required=False, label=_("Private key")) - def clean_private_key_file(self): - private_key_file = self.cleaned_data['private_key_file'] + def clean_private_key(self): + private_key_f = self.cleaned_data['private_key'] password = self.cleaned_data['password'] - if private_key_file: - key_string = private_key_file.read() - private_key_file.seek(0) + if private_key_f: + key_string = private_key_f.read() + private_key_f.seek(0) key_string = key_string.decode() if not validate_ssh_private_key(key_string, password): msg = _('Invalid private key, Only support ' 'RSA/DSA format key') raise forms.ValidationError(msg) - return private_key_file + return private_key_f def validate_password_key(self): password = self.cleaned_data['password'] - private_key_file = self.cleaned_data.get('private_key_file', '') + private_key_f = self.cleaned_data.get('private_key', '') - if not password and not private_key_file: + if not password and not private_key_f: raise forms.ValidationError(_( 'Password and private key file must be input one' )) def gen_keys(self): password = self.cleaned_data.get('password', '') or None - private_key_file = self.cleaned_data['private_key_file'] + private_key_f = self.cleaned_data['private_key'] public_key = private_key = None - if private_key_file: - private_key = private_key_file.read().strip().decode('utf-8') + if private_key_f: + private_key = private_key_f.read().strip().decode('utf-8') public_key = ssh_pubkey_gen(private_key=private_key, password=password) return private_key, public_key @@ -69,7 +69,7 @@ class AdminUserForm(PasswordAndKeyAuthForm): class Meta: model = AdminUser - fields = ['name', 'username', 'password', 'private_key_file', 'comment'] + fields = ['name', 'username', 'password', 'private_key', 'comment'] widgets = { 'name': forms.TextInput(attrs={'placeholder': _('Name')}), 'username': forms.TextInput(attrs={'placeholder': _('Username')}), @@ -87,7 +87,7 @@ class SystemUserForm(OrgModelForm, PasswordAndKeyAuthForm): model = SystemUser fields = [ 'name', 'username', 'protocol', 'auto_generate_key', - 'password', 'private_key_file', 'auto_push', 'sudo', + 'password', 'private_key', 'auto_push', 'sudo', 'comment', 'shell', 'priority', 'login_mode', 'cmd_filters', ] widgets = { diff --git a/apps/assets/serializers/base.py b/apps/assets/serializers/base.py index 257bb95ab..5e853219b 100644 --- a/apps/assets/serializers/base.py +++ b/apps/assets/serializers/base.py @@ -40,6 +40,10 @@ class AuthSerializerMixin: def validate_private_key(self, private_key): if not private_key: return + if 'OPENSSH' in private_key: + msg = _("Not support openssh format key, using " + "ssh-keygen -t rsa -m pem to generate") + raise serializers.ValidationError(msg) password = self.initial_data.get("password") valid = validate_ssh_private_key(private_key, password) if not valid: diff --git a/apps/assets/templates/assets/_system_user.html b/apps/assets/templates/assets/_system_user.html index 5cc3e04ba..1f34f3f55 100644 --- a/apps/assets/templates/assets/_system_user.html +++ b/apps/assets/templates/assets/_system_user.html @@ -53,7 +53,7 @@
{% bootstrap_field form.password layout="horizontal" %} - {% bootstrap_field form.private_key_file layout="horizontal" %} + {% bootstrap_field form.private_key layout="horizontal" %}
@@ -90,7 +90,7 @@ var login_mode_id = '#' + '{{ form.login_mode.id_for_label }}'; var auto_generate_key = '#'+'{{ form.auto_generate_key.id_for_label }}'; var password_id = '#' + '{{ form.password.id_for_label }}'; -var private_key_id = '#' + '{{ form.private_key_file.id_for_label }}'; +var private_key_id = '#' + '{{ form.private_key.id_for_label }}'; var auto_push_id = '#' + '{{ form.auto_push.id_for_label }}'; var sudo_id = '#' + '{{ form.sudo.id_for_label }}'; var shell_id = '#' + '{{ form.shell.id_for_label }}'; @@ -230,7 +230,7 @@ $(document).ready(function () { objectAttrsIsList(data, ['cmd_filters']); objectAttrsIsBool(data, ["auto_generate_key", "auto_push"]); - data["private_key"] = $("#id_private_key_file").data('file'); + data["private_key"] = $("#id_private_key").data('file'); var props = { url: the_url, @@ -240,9 +240,9 @@ $(document).ready(function () { redirect_to: redirect_to }; formSubmit(props); -}).on('change', '#id_private_key_file', function () { +}).on('change', '#id_private_key', function () { readFile($(this)).on("onload", function (evt, data) { - $(this).attr("data-file", data) + $(this).data("file", data) }) }) diff --git a/apps/assets/templates/assets/admin_user_create_update.html b/apps/assets/templates/assets/admin_user_create_update.html index 8d32d16ab..5a2f3d6c9 100644 --- a/apps/assets/templates/assets/admin_user_create_update.html +++ b/apps/assets/templates/assets/admin_user_create_update.html @@ -37,7 +37,7 @@ {% bootstrap_field form.name layout="horizontal" %} {% bootstrap_field form.username layout="horizontal" %} {% bootstrap_field form.password layout="horizontal" %} - {% bootstrap_field form.private_key_file layout="horizontal" %} + {% bootstrap_field form.private_key layout="horizontal" %} {% bootstrap_field form.comment layout="horizontal" %}
@@ -70,8 +70,9 @@ $(document).ready(function () { {% endif %} var form = $("form"); var data = form.serializeObject(); + console.log($("#id_private_key").data("file")); - data["private_key"] = $("#id_private_key_file").data('file'); + data["private_key"] = $("#id_private_key").data('file'); var props = { url: the_url, @@ -82,9 +83,9 @@ $(document).ready(function () { }; formSubmit(props); }) -.on('change', '#id_private_key_file', function () { +.on('change', '#id_private_key', function () { readFile($(this)).on("onload", function (evt, data) { - $(this).attr("data-file", data) + $(this).data("file", data) }) }) diff --git a/apps/assets/templates/assets/gateway_create_update.html b/apps/assets/templates/assets/gateway_create_update.html index a22428087..315302b6f 100644 --- a/apps/assets/templates/assets/gateway_create_update.html +++ b/apps/assets/templates/assets/gateway_create_update.html @@ -46,7 +46,7 @@
{% bootstrap_field form.username layout="horizontal" %} {% bootstrap_field form.password layout="horizontal" %} - {% bootstrap_field form.private_key_file layout="horizontal" %} + {% bootstrap_field form.private_key layout="horizontal" %}
{% endblock %} @@ -70,7 +70,7 @@ {% block custom_foot_js %}