From 97d83abffe7a21729a56bd954c6f6ea3f0b83520 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 26 Aug 2019 16:01:34 +0530 Subject: [PATCH 1/3] Comment out code that denied adding the same key --- hosting/forms.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/hosting/forms.py b/hosting/forms.py index 1bc99b8f..343ead28 100644 --- a/hosting/forms.py +++ b/hosting/forms.py @@ -9,7 +9,6 @@ from django.contrib.auth import authenticate from django.utils.translation import ugettext_lazy as _ from membership.models import CustomUser -from utils.hosting_utils import get_all_public_keys from .models import UserHostingKey, GenericProduct logger = logging.getLogger(__name__) @@ -193,14 +192,14 @@ class UserHostingKeyForm(forms.ModelForm): KEY_ERROR_MESSAGE = _("Please input a proper SSH key") openssh_pubkey_str = self.data.get('public_key').strip() - if openssh_pubkey_str in get_all_public_keys(self.request.user): - key_name = UserHostingKey.objects.filter( - user_id=self.request.user.id, - public_key=openssh_pubkey_str).first().name - KEY_EXISTS_MESSAGE = _( - "This key exists already with the name \"%(name)s\"") % { - 'name': key_name} - raise forms.ValidationError(KEY_EXISTS_MESSAGE) + # if openssh_pubkey_str in get_all_public_keys(self.request.user): + # key_name = UserHostingKey.objects.filter( + # user_id=self.request.user.id, + # public_key=openssh_pubkey_str).first().name + # KEY_EXISTS_MESSAGE = _( + # "This key exists already with the name \"%(name)s\"") % { + # 'name': key_name} + # raise forms.ValidationError(KEY_EXISTS_MESSAGE) with tempfile.NamedTemporaryFile(delete=True) as tmp_public_key_file: tmp_public_key_file.write(openssh_pubkey_str.encode('utf-8')) From 8b8c93d23ebecca33623971eff1bed574a685a49 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 26 Aug 2019 16:02:08 +0530 Subject: [PATCH 2/3] Filter distinct public keys only --- utils/hosting_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index ec97a320..b3c47e6e 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -18,7 +18,7 @@ def get_all_public_keys(customer): :return: A list of public keys """ return UserHostingKey.objects.filter(user_id=customer.id).values_list( - "public_key", flat=True) + "public_key", flat=True).distinct() def get_or_create_vm_detail(user, manager, vm_id): From 7684687dbcac662b82c723dd5d583dfab491a1c7 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 26 Aug 2019 16:13:31 +0530 Subject: [PATCH 3/3] Remove commented code --- hosting/forms.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/hosting/forms.py b/hosting/forms.py index 343ead28..d98d258f 100644 --- a/hosting/forms.py +++ b/hosting/forms.py @@ -192,15 +192,6 @@ class UserHostingKeyForm(forms.ModelForm): KEY_ERROR_MESSAGE = _("Please input a proper SSH key") openssh_pubkey_str = self.data.get('public_key').strip() - # if openssh_pubkey_str in get_all_public_keys(self.request.user): - # key_name = UserHostingKey.objects.filter( - # user_id=self.request.user.id, - # public_key=openssh_pubkey_str).first().name - # KEY_EXISTS_MESSAGE = _( - # "This key exists already with the name \"%(name)s\"") % { - # 'name': key_name} - # raise forms.ValidationError(KEY_EXISTS_MESSAGE) - with tempfile.NamedTemporaryFile(delete=True) as tmp_public_key_file: tmp_public_key_file.write(openssh_pubkey_str.encode('utf-8')) tmp_public_key_file.flush()