Skip to content

Commit

Permalink
Validate keypair create request body
Browse files Browse the repository at this point in the history
Check if keypair create request body has manditory keys like keypair, name
and raise HTTPBadRequest if the required keys are missing.

Fixes bug 1049619

Change-Id: Ia6129e1b04e910fbea81b62b921a228aafa32f64
  • Loading branch information
sirisha-devineni committed Sep 13, 2012
1 parent e72db9f commit 760e59b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions nova/api/openstack/compute/contrib/keypairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ def create(self, req, body):

context = req.environ['nova.context']
authorize(context)
params = body['keypair']
name = params['name']

try:
params = body['keypair']
name = params['name']
except KeyError:
msg = _("Invalid request body")
raise webob.exc.HTTPBadRequest(explanation=msg)

try:
if 'public_key' in params:
Expand Down
12 changes: 12 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_keypairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ def test_detail_servers(self):
self.assertTrue('key_name' in server_dict)
self.assertEquals(server_dict['key_name'], '')

def test_keypair_create_with_invalid_keypairBody(self):
body = {'alpha': {'name': 'create_test'}}
req = webob.Request.blank('/v1.1/fake/os-keypairs')
req.method = 'POST'
req.body = jsonutils.dumps(body)
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app())
res_dict = jsonutils.loads(res.body)
self.assertEqual(res.status_int, 400)
self.assertEqual(res_dict['badRequest']['message'],
"Invalid request body")


class KeypairsXMLSerializerTest(test.TestCase):
def setUp(self):
Expand Down

0 comments on commit 760e59b

Please sign in to comment.