Removed new lines when importing a keypair
Fixes bug 988139 All new lines (\r, \n) are removed from input when importing a keypair. This is done as sometimes a user copies the public key from terminal, with new lines in it. As new lines are not allowed in a public key, so they are stripped. Change-Id: If526bf081448e315f3518d7ecd2689ec816b77a7
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
import re
|
||||
|
||||
from django import shortcuts
|
||||
from django.contrib import messages
|
||||
@@ -31,6 +32,7 @@ from horizon import forms
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
NEW_LINES = re.compile(r"\r|\n")
|
||||
|
||||
|
||||
class CreateKeypair(forms.SelfHandlingForm):
|
||||
@@ -60,6 +62,8 @@ class ImportKeypair(forms.SelfHandlingForm):
|
||||
def handle(self, request, data):
|
||||
try:
|
||||
LOG.info('Importing keypair "%s"' % data['name'])
|
||||
# Remove any new lines in the public key
|
||||
data['public_key'] = NEW_LINES.sub("", data['public_key'])
|
||||
api.keypair_import(request, data['name'], data['public_key'])
|
||||
messages.success(request, _('Successfully imported public key: %s')
|
||||
% data['name'])
|
||||
|
||||
@@ -109,6 +109,24 @@ class KeyPairViewTests(test.TestCase):
|
||||
|
||||
self.assertTrue(res.has_header('content-disposition'))
|
||||
|
||||
@test.create_stubs({api: ("keypair_import",)})
|
||||
def test_import_keypair(self):
|
||||
key1_name = "new key pair"
|
||||
public_key = "ssh-rsa ABCDEFGHIJKLMNOPQR\r\n" \
|
||||
"STUVWXYZ1234567890\r" \
|
||||
"XXYYZZ user@computer\n\n"
|
||||
api.keypair_import(IsA(http.HttpRequest), key1_name,
|
||||
public_key.replace("\r", "")
|
||||
.replace("\n", ""))
|
||||
self.mox.ReplayAll()
|
||||
|
||||
formData = {'method': 'ImportKeypair',
|
||||
'name': key1_name,
|
||||
'public_key': public_key}
|
||||
url = reverse('horizon:nova:access_and_security:keypairs:import')
|
||||
self.client.post(url, formData)
|
||||
self.assertMessageCount(success=1)
|
||||
|
||||
def test_generate_keypair_exception(self):
|
||||
keypair = self.keypairs.first()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user