Skip to content

Commit

Permalink
Raise 400 if bad kepair data is provided
Browse files Browse the repository at this point in the history
Fixes bug 902395

Change-Id: If04a0bf432ad4828ac1fc68f6d33dff5f9f57e24
  • Loading branch information
bcwaldon committed Jan 29, 2012
1 parent 02b8726 commit 9fa7db5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nova/api/openstack/compute/contrib/keypairs.py
Expand Up @@ -99,7 +99,12 @@ def create(self, req, body):

# import if public_key is sent
if 'public_key' in params:
fingerprint = crypto.generate_fingerprint(params['public_key'])
try:
fingerprint = crypto.generate_fingerprint(params['public_key'])
except exception.InvalidKeypair:
msg = _("Keypair data is invalid")
raise webob.exc.HTTPBadRequest(explanation=msg)

keypair['public_key'] = params['public_key']
keypair['fingerprint'] = fingerprint
else:
Expand Down
2 changes: 2 additions & 0 deletions nova/crypto.py
Expand Up @@ -126,6 +126,8 @@ def generate_fingerprint(public_key):
with open(pubfile, 'w') as f:
f.write(public_key)
return _generate_fingerprint(pubfile)
except exception.ProcessExecutionError:
raise exception.InvalidKeypair()
finally:
shutil.rmtree(tmpdir)

Expand Down
4 changes: 4 additions & 0 deletions nova/exception.py
Expand Up @@ -215,6 +215,10 @@ class Invalid(NovaException):
message = _("Unacceptable parameters.")


class InvalidKeypair(Invalid):
message = _("Keypair data is invalid")


class InvalidRequest(Invalid):
message = _("The request is invalid.")

Expand Down
15 changes: 15 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_keypairs.py
Expand Up @@ -133,6 +133,21 @@ def test_keypair_import(self):
self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0)
self.assertFalse('private_key' in res_dict['keypair'])

def test_keypair_import_bad_key(self):
body = {
'keypair': {
'name': 'create_test',
'public_key': 'ssh-what negative',
},
}

req = webob.Request.blank('/v2/fake/os-keypairs')
req.method = 'POST'
req.body = json.dumps(body)
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 400)

def test_keypair_delete(self):
req = webob.Request.blank('/v2/fake/os-keypairs/FAKE')
req.method = 'DELETE'
Expand Down

0 comments on commit 9fa7db5

Please sign in to comment.