From 4c8e8af3ab7f4b0946779ab4d1d8b1a3ae59edc9 Mon Sep 17 00:00:00 2001 From: Vipin Balachandran Date: Fri, 29 Jul 2016 11:37:47 +0530 Subject: [PATCH] Validate name in qos-spec Commit f5c3bb158e4e600379301281cb630d655c46d7e0 changed the method to validate the qos-spec name. The new validation method does not remove whitespace and check for zero-length name like the method it replaced. This patch restores the old validation to disallow empty qos-spec name. Closes-bug: #1607641 Change-Id: I3c617362d6d1b53c308ee382223e6de2cd843481 --- cinder/api/contrib/qos_specs_manage.py | 7 ++++++- .../unit/api/contrib/test_qos_specs_manage.py | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cinder/api/contrib/qos_specs_manage.py b/cinder/api/contrib/qos_specs_manage.py index 88f1baa8dbe..e5c26c43411 100644 --- a/cinder/api/contrib/qos_specs_manage.py +++ b/cinder/api/contrib/qos_specs_manage.py @@ -85,10 +85,15 @@ class QoSSpecsController(wsgi.Controller): msg = _("Please specify a name for QoS specs.") raise webob.exc.HTTPBadRequest(explanation=msg) - utils.validate_dictionary_string_length(specs) + self.validate_string_length(name, 'name', min_length=1, + max_length=255, remove_whitespaces=True) name = name.strip() # Remove name from 'specs' since passing it in as separate param del specs['name'] + + # Validate the key-value pairs in the qos spec. + utils.validate_dictionary_string_length(specs) + try: spec = qos_specs.create(context, name, specs) notifier_info = dict(name=name, specs=specs) diff --git a/cinder/tests/unit/api/contrib/test_qos_specs_manage.py b/cinder/tests/unit/api/contrib/test_qos_specs_manage.py index d0749d7398f..0091dc5fdf6 100644 --- a/cinder/tests/unit/api/contrib/test_qos_specs_manage.py +++ b/cinder/tests/unit/api/contrib/test_qos_specs_manage.py @@ -457,8 +457,7 @@ class QoSSpecManageApiTest(test.TestCase): @ddt.data({'name': 'fake_name', 'a' * 256: 'a'}, {'name': 'fake_name', 'a': 'a' * 256}, - {'name': 'fake_name' * 256, 'a': 'a'}, - {'name': 'fake_name' * 256, '': 'a'}) + {'name': 'fake_name', '': 'a'}) def test_create_qos_with_invalid_specs(self, value): body = {'qos_specs': value} req = fakes.HTTPRequest.blank('/v2/%s/qos-specs' % fake.PROJECT_ID, @@ -467,6 +466,18 @@ class QoSSpecManageApiTest(test.TestCase): self.assertRaises(exception.InvalidInput, self.controller.create, req, body) + @ddt.data({'name': None}, + {'name': 'n' * 256}, + {'name': ''}, + {'name': ' '}) + def test_create_qos_with_invalid_spec_name(self, value): + body = {'qos_specs': value} + req = fakes.HTTPRequest.blank('/v2/%s/qos-specs' % fake.PROJECT_ID, + use_admin_context=True) + req.method = 'POST' + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, req, body) + @mock.patch('cinder.volume.qos_specs.update', side_effect=return_qos_specs_update) def test_update(self, mock_qos_update):