Return 400 Bad Request for extra capability keys longer than 64 chars

Length of extra capability keys is limited to 64 characters in the DB
schema. However, the host create API can create a host based on a
request that has a longer extra capability key, only to return a 500
Internal Server Error.

With this patch, the API checks the key length before creating a host
and returns a 400 Bad Request status code if it is too long.

Change-Id: I2cf4ede787e0f65385376832ce410d694df094ff
Closes-Bug: #1709639
This commit is contained in:
Masahito Muroi 2018-06-15 18:12:21 +09:00 committed by Pierre Riteau
parent 4b23129d05
commit 580d4ae470
3 changed files with 22 additions and 0 deletions

View File

@ -153,6 +153,11 @@ class MissingParameter(exceptions.BlazarException):
msg_fmt = _("Missing parameter %(param)s")
class ExtraCapabilityTooLong(exceptions.BlazarException):
code = 400
msg_fmt = _("Extra capability key too long")
class InvalidState(exceptions.BlazarException):
code = 409
msg_fmt = _("Invalid State %(state)s for %(id)s")

View File

@ -341,6 +341,10 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
extra_capabilities = dict(
(key, host_values[key]) for key in extra_capabilities_keys
)
if any([len(key) > 64 for key in extra_capabilities_keys]):
raise manager_ex.ExtraCapabilityTooLong()
pool = nova.ReservationPool()
pool.add_computehost(self.freepool_name,
host_details['service_name'])

View File

@ -221,6 +221,19 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.db_host_extra_capability_create.assert_called_once_with(fake_capa)
self.assertEqual(host, fake_host)
def test_create_host_with_capabilities_too_long(self):
fake_host = self.fake_host.copy()
fake_host.update({'foo': 'bar'})
# NOTE(sbauza): 'id' will be pop'd, we need to keep track of it
fake_request = fake_host.copy()
long_key = ""
for i in range(65):
long_key += "0"
fake_request[long_key] = "foo"
self.assertRaises(manager_exceptions.ExtraCapabilityTooLong,
self.fake_phys_plugin.create_computehost,
fake_request)
def test_create_host_without_trust_id(self):
self.assertRaises(manager_exceptions.MissingTrustId,
self.fake_phys_plugin.create_computehost, {})