From 580d4ae470520e6c72f34b11943dec475d0af8e9 Mon Sep 17 00:00:00 2001 From: Masahito Muroi Date: Fri, 15 Jun 2018 18:12:21 +0900 Subject: [PATCH] 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 --- blazar/manager/exceptions.py | 5 +++++ blazar/plugins/oshosts/host_plugin.py | 4 ++++ blazar/tests/plugins/test_physical_host_plugin.py | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/blazar/manager/exceptions.py b/blazar/manager/exceptions.py index 704af673..dd35fdb8 100644 --- a/blazar/manager/exceptions.py +++ b/blazar/manager/exceptions.py @@ -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") diff --git a/blazar/plugins/oshosts/host_plugin.py b/blazar/plugins/oshosts/host_plugin.py index 107f0f0f..b529178c 100644 --- a/blazar/plugins/oshosts/host_plugin.py +++ b/blazar/plugins/oshosts/host_plugin.py @@ -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']) diff --git a/blazar/tests/plugins/test_physical_host_plugin.py b/blazar/tests/plugins/test_physical_host_plugin.py index 1de0fb5f..35654c59 100644 --- a/blazar/tests/plugins/test_physical_host_plugin.py +++ b/blazar/tests/plugins/test_physical_host_plugin.py @@ -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, {})