From 760e59b8632edd8cbf395289e24f380ffd06f047 Mon Sep 17 00:00:00 2001 From: Sirisha Devineni Date: Thu, 13 Sep 2012 00:00:32 +0530 Subject: [PATCH] Validate keypair create request body 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 --- nova/api/openstack/compute/contrib/keypairs.py | 9 +++++++-- .../api/openstack/compute/contrib/test_keypairs.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/nova/api/openstack/compute/contrib/keypairs.py b/nova/api/openstack/compute/contrib/keypairs.py index dc0c74b1147c..e5e1e37fda73 100644 --- a/nova/api/openstack/compute/contrib/keypairs.py +++ b/nova/api/openstack/compute/contrib/keypairs.py @@ -70,8 +70,13 @@ class KeypairController(object): 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: diff --git a/nova/tests/api/openstack/compute/contrib/test_keypairs.py b/nova/tests/api/openstack/compute/contrib/test_keypairs.py index a14a64da5144..d7fc378c6687 100644 --- a/nova/tests/api/openstack/compute/contrib/test_keypairs.py +++ b/nova/tests/api/openstack/compute/contrib/test_keypairs.py @@ -295,6 +295,18 @@ class KeypairsTest(test.TestCase): 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):