Skip to content

Commit

Permalink
Add validation for OSAPI server name length.
Browse files Browse the repository at this point in the history
Fixes LP Bug #962515.

Change-Id: Iee895604f8e9101a341a5909fc5ba2dd8e708b4b
  • Loading branch information
dprince committed Mar 29, 2012
1 parent 998e57b commit c7f526f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nova/api/openstack/compute/servers.py
Expand Up @@ -505,6 +505,10 @@ def _validate_server_name(self, value):
msg = _("Server name is an empty string")
raise exc.HTTPBadRequest(explanation=msg)

if not len(value) < 256:
msg = _("Server name must be less than 256 characters.")
raise exc.HTTPBadRequest(explanation=msg)

def _get_injected_files(self, personality):
"""
Create a list of injected files from the personality attribute
Expand Down
42 changes: 42 additions & 0 deletions nova/tests/api/openstack/compute/test_servers.py
Expand Up @@ -891,6 +891,17 @@ def test_update_server_name(self):
self.assertEqual(res_dict['server']['id'], FAKE_UUID)
self.assertEqual(res_dict['server']['name'], 'server_test')

def test_update_server_name_too_long(self):
self.stubs.Set(nova.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': 'x' * 256}}
req.body = json.dumps(body)
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
req, FAKE_UUID, body)

def test_update_server_access_ipv4(self):
self.stubs.Set(nova.db, 'instance_get',
fakes.fake_instance_get(access_ipv4='0.0.0.0'))
Expand Down Expand Up @@ -1643,6 +1654,37 @@ def test_create_instance_bad_format_access_ip_v6(self):
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create,
req, body)

def test_create_instance_name_too_long(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': 'X' * 256,
'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 = json.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'
Expand Down

0 comments on commit c7f526f

Please sign in to comment.