From 4c230df10e8b637c9b0dc6bdc933c21da425d918 Mon Sep 17 00:00:00 2001 From: luqitao Date: Thu, 18 May 2017 01:19:58 -0400 Subject: [PATCH] Check the volume metadata value is a string or not When creating or updating the metadata for a volume, if user give a nested value, cinder-api will raise a 500 error, the root cause is a syntax error in sql statement. According to cinder api ref [1], the metadata value should be a string. [1] https://developer.openstack.org/api-ref/block-storage/v3/?expanded=update-a-volume-s-metadata-detail#update-a-volume-s-metadata Change-Id: I6febfe177fb49372bb6413d3b6478cb762170b2a Closes-Bug: #1690363 --- cinder/tests/unit/test_utils.py | 17 +++++++++++++++++ cinder/utils.py | 15 ++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cinder/tests/unit/test_utils.py b/cinder/tests/unit/test_utils.py index f27da0f7ef4..16446a4df7d 100644 --- a/cinder/tests/unit/test_utils.py +++ b/cinder/tests/unit/test_utils.py @@ -1494,3 +1494,20 @@ class TestLogLevels(test.TestCase): utils.set_log_levels(prefix, 'debug') levels = utils.get_log_levels(prefix) self.assertEqual('DEBUG', levels[prefix]) + + +@ddt.ddt +class TestCheckMetadataProperties(test.TestCase): + @ddt.data( + {'a': {'foo': 'bar'}}, # value is a nested dict + {'a': 123}, # value is an integer + {'a': 123.4}, # value is a float + {'a': True}, # value is a bool + {'a': ('foo', 'bar')}, # value is a tuple + {'a': []}, # value is a list + {'a': None} # value is None + ) + def test_metadata_value_not_string_raise(self, meta): + self.assertRaises(exception.InvalidVolumeMetadata, + utils.check_metadata_properties, + meta) diff --git a/cinder/utils.py b/cinder/utils.py index 42c36022546..c260ec3c771 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -165,22 +165,19 @@ def check_metadata_properties(metadata=None): metadata = {} for k, v in metadata.items(): - if len(k) == 0: - msg = _("Metadata property key blank.") - LOG.debug(msg) - raise exception.InvalidVolumeMetadata(reason=msg) + try: + check_string_length(k, "Metadata key: %s" % k, min_length=1) + check_string_length(v, "Value for metadata key: %s" % k) + except exception.InvalidInput as exc: + raise exception.InvalidVolumeMetadata(reason=exc) + # for backward compatibility if len(k) > 255: msg = _("Metadata property key %s greater than 255 " "characters.") % k - LOG.debug(msg) raise exception.InvalidVolumeMetadataSize(reason=msg) - if v is None: - msg = _("Metadata property key '%s' value is None.") % k - raise exception.InvalidVolumeMetadata(reason=msg) if len(v) > 255: msg = _("Metadata property key %s value greater than " "255 characters.") % k - LOG.debug(msg) raise exception.InvalidVolumeMetadataSize(reason=msg)