From f4906f07cd49e4deae79018d7f586b16266859eb Mon Sep 17 00:00:00 2001 From: Sirisha Devineni Date: Fri, 14 Sep 2012 23:04:37 +0530 Subject: Raise BadRequest while creating server with invalid personality Handle UnicodeDecodeError raises from compute api while trying to create server with invalid personality content and throw it as HTTPBadRequest Fixed bug 1050409 Change-Id: I27d47bbc9ed89abfa9827512fbfb3b16a0d87160 --- nova/api/openstack/compute/servers.py | 3 ++ nova/tests/api/openstack/compute/test_servers.py | 37 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index c949ad661..d06ebc1e6 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -823,6 +823,9 @@ class Controller(wsgi.Controller): msg = "%(err_type)s: %(err_msg)s" % {'err_type': err.exc_type, 'err_msg': err.value} raise exc.HTTPBadRequest(explanation=msg) + except UnicodeDecodeError as error: + msg = "UnicodeError: %s" % unicode(error) + raise exc.HTTPBadRequest(explanation=msg) # Let the caller deal with unhandled exceptions. # If the caller wanted a reservation_id, return it diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py index aa8f6132b..d5f1170e6 100644 --- a/nova/tests/api/openstack/compute/test_servers.py +++ b/nova/tests/api/openstack/compute/test_servers.py @@ -2803,6 +2803,43 @@ class ServersControllerCreateTest(test.TestCase): # The fact that the action doesn't raise is enough validation self.controller.create(req, body) + def test_create_instance_invalid_personality(self): + + def fake_create(*args, **kwargs): + codec = 'utf8' + content = 'b25zLiINCg0KLVJpY2hhcmQgQ$$%QQmFjaA==' + start_position = 19 + end_position = 20 + msg = 'invalid start byte' + raise UnicodeDecodeError(codec, content, start_position, + end_position, msg) + + self.stubs.Set(nova.compute.api.API, + 'create', + fake_create) + image_uuid = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/v2/flavors/3' + body = { + 'server': { + 'name': 'server_test', + 'imageRef': image_uuid, + 'flavorRef': flavor_ref, + 'personality': [ + { + "path": "/etc/banner.txt", + "contents": "b25zLiINCg0KLVJpY2hhcmQgQ$$%QQmFjaA==", + }, + ], + }, + } + + req = fakes.HTTPRequest.blank('/v2/fake/servers') + req.method = 'POST' + req.body = jsonutils.dumps(body) + req.headers["content-type"] = "application/json" + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, req, body) + def test_create_location(self): selfhref = 'http://localhost/v2/fake/servers/%s' % FAKE_UUID bookhref = 'http://localhost/fake/servers/%s' % FAKE_UUID -- cgit