Let schema validate image metadata type and key lengths
The check_img_metadata_properties_quota validation method not only checks quota for image metadata but also the type of the metadata object in the request (dict) and the key length of the metadata items in the dict - such that they were between length 1 and 255. The metadata schema property handles all of that validation for us so we don't need to do it in python, which was probably a carry over from the legacy v2 API which didn't use json schema validation. Now that the legacy v2 API code is gone, we can remove the explicit python code checks in check_img_metadata_properties_quota and just let the schema validator do it's job. Change-Id: Ibec92e278887cd06e91687ca91e75f9b7b28098c
This commit is contained in:
parent
7b2cb0dc03
commit
c5915eb0cd
|
@ -290,19 +290,6 @@ def check_img_metadata_properties_quota(context, metadata):
|
|||
expl = _("Image metadata limit exceeded")
|
||||
raise webob.exc.HTTPForbidden(explanation=expl)
|
||||
|
||||
# check the key length.
|
||||
if isinstance(metadata, dict):
|
||||
for key, value in six.iteritems(metadata):
|
||||
if len(key) == 0:
|
||||
expl = _("Image metadata key cannot be blank")
|
||||
raise webob.exc.HTTPBadRequest(explanation=expl)
|
||||
if len(key) > 255:
|
||||
expl = _("Image metadata key too long")
|
||||
raise webob.exc.HTTPBadRequest(explanation=expl)
|
||||
else:
|
||||
expl = _("Invalid image metadata")
|
||||
raise webob.exc.HTTPBadRequest(explanation=expl)
|
||||
|
||||
|
||||
def get_networks_for_instance_from_nw_info(nw_info):
|
||||
networks = collections.OrderedDict()
|
||||
|
|
|
@ -28,9 +28,7 @@ create_backup = {
|
|||
'type': 'string',
|
||||
},
|
||||
'rotation': parameter_types.non_negative_integer,
|
||||
'metadata': {
|
||||
'type': 'object',
|
||||
}
|
||||
'metadata': parameter_types.metadata,
|
||||
},
|
||||
'required': ['name', 'backup_type', 'rotation'],
|
||||
'additionalProperties': False,
|
||||
|
|
|
@ -30,7 +30,6 @@ from nova.compute import vm_states
|
|||
from nova import exception
|
||||
from nova import test
|
||||
from nova.tests.unit.api.openstack import fakes
|
||||
from nova.tests.unit import utils
|
||||
|
||||
|
||||
NS = "{http://docs.openstack.org/compute/api/v1.1}"
|
||||
|
@ -374,41 +373,6 @@ class MiscFunctionsTest(test.TestCase):
|
|||
else:
|
||||
self.fail("webob.exc.HTTPConflict was not raised")
|
||||
|
||||
def test_check_img_metadata_properties_quota_valid_metadata(self):
|
||||
ctxt = utils.get_test_admin_context()
|
||||
metadata1 = {"key": "value"}
|
||||
actual = common.check_img_metadata_properties_quota(ctxt, metadata1)
|
||||
self.assertIsNone(actual)
|
||||
|
||||
metadata2 = {"key": "v" * 260}
|
||||
actual = common.check_img_metadata_properties_quota(ctxt, metadata2)
|
||||
self.assertIsNone(actual)
|
||||
|
||||
metadata3 = {"key": ""}
|
||||
actual = common.check_img_metadata_properties_quota(ctxt, metadata3)
|
||||
self.assertIsNone(actual)
|
||||
|
||||
def test_check_img_metadata_properties_quota_inv_metadata(self):
|
||||
ctxt = utils.get_test_admin_context()
|
||||
metadata1 = {"a" * 260: "value"}
|
||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
||||
common.check_img_metadata_properties_quota, ctxt, metadata1)
|
||||
|
||||
metadata2 = {"": "value"}
|
||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
||||
common.check_img_metadata_properties_quota, ctxt, metadata2)
|
||||
|
||||
metadata3 = "invalid metadata"
|
||||
self.assertRaises(webob.exc.HTTPBadRequest,
|
||||
common.check_img_metadata_properties_quota, ctxt, metadata3)
|
||||
|
||||
metadata4 = None
|
||||
self.assertIsNone(common.check_img_metadata_properties_quota(ctxt,
|
||||
metadata4))
|
||||
metadata5 = {}
|
||||
self.assertIsNone(common.check_img_metadata_properties_quota(ctxt,
|
||||
metadata5))
|
||||
|
||||
def test_status_from_state(self):
|
||||
for vm_state in (vm_states.ACTIVE, vm_states.STOPPED):
|
||||
for task_state in (task_states.RESIZE_PREP,
|
||||
|
|
Loading…
Reference in New Issue