From e064a4ea750a6237dabf03202b1dcb6fa435c7f6 Mon Sep 17 00:00:00 2001 From: Unmesh Gurjar Date: Tue, 27 Mar 2012 10:43:02 +0530 Subject: Added img metadata validation. Fixes bug 962117. 1. Added validation for image metadata. 2. Renamed the method 'check_img_metadata_quota_limit' to 'check_img_metadata_quota_limit' since it also validates the image metadata. 3. Added unit test cases. Change-Id: I0e9dad97c03070363d14977897701e146870e41a --- nova/api/openstack/common.py | 15 ++++++++++++++- nova/api/openstack/compute/contrib/admin_actions.py | 2 +- nova/api/openstack/compute/image_metadata.py | 8 +++++--- nova/api/openstack/compute/servers.py | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py index 0d419f205..01787712a 100644 --- a/nova/api/openstack/common.py +++ b/nova/api/openstack/common.py @@ -269,7 +269,7 @@ def get_version_from_href(href): return '2' -def check_img_metadata_quota_limit(context, metadata): +def check_img_metadata_properties_quota(context, metadata): if metadata is None: return num_metadata = len(metadata) @@ -279,6 +279,19 @@ def check_img_metadata_quota_limit(context, metadata): raise webob.exc.HTTPRequestEntityTooLarge(explanation=expl, headers={'Retry-After': 0}) + # check the key length. + if isinstance(metadata, dict): + for key, value in metadata.iteritems(): + 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 dict_to_query_str(params): # TODO(throughnothing): we should just use urllib.urlencode instead of this diff --git a/nova/api/openstack/compute/contrib/admin_actions.py b/nova/api/openstack/compute/contrib/admin_actions.py index f7908f758..f9e661062 100644 --- a/nova/api/openstack/compute/contrib/admin_actions.py +++ b/nova/api/openstack/compute/contrib/admin_actions.py @@ -231,7 +231,7 @@ class AdminActionsController(wsgi.Controller): props = {} metadata = entity.get('metadata', {}) - common.check_img_metadata_quota_limit(context, metadata) + common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: diff --git a/nova/api/openstack/compute/image_metadata.py b/nova/api/openstack/compute/image_metadata.py index d79692f22..145e0395e 100644 --- a/nova/api/openstack/compute/image_metadata.py +++ b/nova/api/openstack/compute/image_metadata.py @@ -64,7 +64,8 @@ class Controller(object): if 'metadata' in body: for key, value in body['metadata'].iteritems(): image['properties'][key] = value - common.check_img_metadata_quota_limit(context, image['properties']) + common.check_img_metadata_properties_quota(context, + image['properties']) self.image_service.update(context, image_id, image, None) return dict(metadata=image['properties']) @@ -88,7 +89,8 @@ class Controller(object): image = self._get_image(context, image_id) image['properties'][id] = meta[id] - common.check_img_metadata_quota_limit(context, image['properties']) + common.check_img_metadata_properties_quota(context, + image['properties']) self.image_service.update(context, image_id, image, None) return dict(meta=meta) @@ -98,7 +100,7 @@ class Controller(object): context = req.environ['nova.context'] image = self._get_image(context, image_id) metadata = body.get('metadata', {}) - common.check_img_metadata_quota_limit(context, metadata) + common.check_img_metadata_properties_quota(context, metadata) image['properties'] = metadata self.image_service.update(context, image_id, image, None) return dict(metadata=metadata) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 635123fff..9aa637cc9 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -1093,7 +1093,7 @@ class Controller(wsgi.Controller): props = {} metadata = entity.get('metadata', {}) - common.check_img_metadata_quota_limit(context, metadata) + common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: -- cgit