Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added validation of name when creating a new keypair
fixes bug 900925
fixes bug 900927

Change-Id: Icef75af7dca299c566c366f41a71cdc49fa1af65
  • Loading branch information
Philip Knouff committed Jan 27, 2012
1 parent cefc979 commit 845c5a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nova/api/openstack/compute/contrib/keypairs.py
Expand Up @@ -84,6 +84,9 @@ def create(self, req, body):
params = body['keypair']
name = params['name']

if not 0 < len(name) < 256:
msg = _('Keypair name must be between 1 and 255 characters long')
raise webob.exc.HTTPBadRequest(explanation=msg)
# NOTE(ja): generation is slow, so shortcut invalid name exception
try:
db.key_pair_get(context, context.user_id, name)
Expand Down
26 changes: 26 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_keypairs.py
Expand Up @@ -80,6 +80,32 @@ def test_keypair_create(self):
self.assertTrue(len(res_dict['keypair']['fingerprint']) > 0)
self.assertTrue(len(res_dict['keypair']['private_key']) > 0)

def test_keypair_create_with_empty_name(self):
body = {'keypair': {'name': ''}}
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_create_with_invalid_name(self):
body = {
'keypair': {
'name': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
}
}
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_import(self):
body = {
'keypair': {
Expand Down

0 comments on commit 845c5a6

Please sign in to comment.