From 3aef51279b36a47ec26b958ec6022da4208cc15e Mon Sep 17 00:00:00 2001 From: zhangyanzi Date: Fri, 22 Nov 2013 16:41:39 +0800 Subject: [PATCH] Ensure 'status' in update_snapshot_status The function os-update_snapshot_status doesn't check whether the param "status" is in request body. It throws 500 error. We should catch the KeyError and return 400 (HTTPBadRequest) instead. Change-Id: If3775e3c0299cf0edbdda5081a6633b38462011b --- cinder/api/contrib/snapshot_actions.py | 6 +++++- cinder/tests/api/contrib/test_snapshot_actions.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cinder/api/contrib/snapshot_actions.py b/cinder/api/contrib/snapshot_actions.py index 781f7a863..527891b5e 100644 --- a/cinder/api/contrib/snapshot_actions.py +++ b/cinder/api/contrib/snapshot_actions.py @@ -46,7 +46,11 @@ class SnapshotActionsController(wsgi.Controller): authorize(context, 'update_snapshot_status') LOG.debug("body: %s" % body) - status = body['os-update_snapshot_status']['status'] + try: + status = body['os-update_snapshot_status']['status'] + except KeyError: + msg = _("'status' must be specified.") + raise webob.exc.HTTPBadRequest(explanation=msg) # Allowed state transitions status_map = {'creating': ['creating', 'available', 'error'], diff --git a/cinder/tests/api/contrib/test_snapshot_actions.py b/cinder/tests/api/contrib/test_snapshot_actions.py index 5ec2fed1d..f8be77f8b 100644 --- a/cinder/tests/api/contrib/test_snapshot_actions.py +++ b/cinder/tests/api/contrib/test_snapshot_actions.py @@ -56,6 +56,17 @@ class SnapshotActionsTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 400) + def test_update_snapshot_status_without_status(self): + self.stubs.Set(db, 'snapshot_get', stub_snapshot_get) + body = {'os-update_snapshot_status': {}} + req = webob.Request.blank('/v2/fake/snapshots/1/action') + req.method = "POST" + req.body = jsonutils.dumps(body) + req.headers["content-type"] = "application/json" + + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 400) + def stub_snapshot_get(context, snapshot_id): snapshot = stubs.stub_snapshot(snapshot_id) -- 2.45.2