From 98ff29d13af9e0c1fa96e49e68939634c24ad2c3 Mon Sep 17 00:00:00 2001 From: Prem Karat Date: Fri, 3 May 2013 22:36:04 +0530 Subject: Fix to disallow server name with all blank spaces * Nova CLI allows server name to be created with all blank characters. This fix will disallow a server name with all blank spaces during server/instance creation as well as while updating/renaming a server * Adding unit tests to test the above scenarios (create & rename server with blank spaces) Fixes bug 1175956 Change-Id: I58af207c4a6b8579bb172d4bb612e0aa2aa6b498 --- nova/api/openstack/compute/servers.py | 2 ++ nova/tests/api/openstack/compute/test_servers.py | 42 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 41c3b92a5..f47ac56a7 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -580,6 +580,8 @@ class Controller(wsgi.Controller): def _check_string_length(self, value, name, max_length=None): try: + if isinstance(value, basestring): + value = value.strip() utils.check_string_length(value, name, min_length=1, max_length=max_length) except exception.InvalidInput as e: diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py index 6cdad1294..f6ca7a043 100644 --- a/nova/tests/api/openstack/compute/test_servers.py +++ b/nova/tests/api/openstack/compute/test_servers.py @@ -1093,6 +1093,17 @@ class ServersControllerTest(test.TestCase): self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, req, FAKE_UUID, body) + def test_update_server_name_all_blank_spaces(self): + self.stubs.Set(db, 'instance_get', + fakes.fake_instance_get(name='server_test')) + req = fakes.HTTPRequest.blank('/v2/fake/servers/%s' % FAKE_UUID) + req.method = 'PUT' + req.content_type = 'application/json' + body = {'server': {'name': ' ' * 64}} + req.body = jsonutils.dumps(body) + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + req, FAKE_UUID, body) + def test_update_server_access_ipv4(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get(access_ipv4='0.0.0.0')) @@ -2898,6 +2909,37 @@ class ServersControllerCreateTest(test.TestCase): self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, req, body) + def test_create_instance_name_all_blank_spaces(self): + # proper local hrefs must start with 'http://localhost/v2/' + image_uuid = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + image_href = 'http://localhost/v2/images/%s' % image_uuid + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'name': ' ' * 64, + 'imageRef': image_href, + 'flavorRef': flavor_ref, + 'metadata': { + 'hello': 'world', + 'open': 'stack', + }, + 'personality': [ + { + "path": "/etc/banner.txt", + "contents": "MQ==", + }, + + ], + }, + } + + 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_instance(self): # proper local hrefs must start with 'http://localhost/v2/' image_uuid = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' -- cgit