Fix 500 for 'qos-create' key or value > 255 characters

Currently 'qos-create' raises 500 InternalServerError if
you pass key or value greater than 255 characters. In the
database 'key' and 'value' fields has 255 characters of
maximum limit so if we pass more than that it raises DBError
and finally exits with 500 InternalServerError.

Renamed existing 'validate_extra_specs' from cinder.utils to
reduce code duplication and used it to validate both key and
value.

Change-Id: I35b59fd6fc6cbcbefb112ad5d3470b89641bd3d8
Closes-Bug: #1603325
This commit is contained in:
dineshbhor 2016-07-14 15:13:13 +05:30
parent 6156a45591
commit f5c3bb158e
5 changed files with 21 additions and 9 deletions

View File

@ -85,8 +85,7 @@ class QoSSpecsController(wsgi.Controller):
msg = _("Please specify a name for QoS specs.")
raise webob.exc.HTTPBadRequest(explanation=msg)
self.validate_string_length(name, 'name', min_length=1,
max_length=255, remove_whitespaces=True)
utils.validate_dictionary_string_length(specs)
name = name.strip()
# Remove name from 'specs' since passing it in as separate param
del specs['name']

View File

@ -62,7 +62,7 @@ class VolumeTypeExtraSpecsController(wsgi.Controller):
self._check_type(context, type_id)
specs = body['extra_specs']
self._check_key_names(specs.keys())
utils.validate_extra_specs(specs)
utils.validate_dictionary_string_length(specs)
db.volume_type_extra_specs_update_or_create(context,
type_id,
@ -87,7 +87,7 @@ class VolumeTypeExtraSpecsController(wsgi.Controller):
expl = _('Request body contains too many items')
raise webob.exc.HTTPBadRequest(explanation=expl)
self._check_key_names(body.keys())
utils.validate_extra_specs(body)
utils.validate_dictionary_string_length(body)
db.volume_type_extra_specs_update_or_create(context,
type_id,

View File

@ -58,7 +58,7 @@ class VolumeTypesManageController(wsgi.Controller):
name = vol_type.get('name', None)
description = vol_type.get('description')
specs = vol_type.get('extra_specs', {})
utils.validate_extra_specs(specs)
utils.validate_dictionary_string_length(specs)
is_public = vol_type.get('os-volume-type-access:is_public', True)
if name is None or len(name.strip()) == 0:

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import ddt
import mock
import webob
@ -139,6 +140,7 @@ def return_disassociate_all(context, id):
type_id=None)
@ddt.ddt
class QoSSpecManageApiTest(test.TestCase):
def _create_qos_specs(self, name, values=None):
@ -387,7 +389,7 @@ class QoSSpecManageApiTest(test.TestCase):
@mock.patch('cinder.volume.qos_specs.create',
side_effect=return_qos_specs_create)
@mock.patch('cinder.api.openstack.wsgi.Controller.validate_string_length')
@mock.patch('cinder.utils.validate_dictionary_string_length')
def test_create(self, mock_validate, mock_qos_spec_create):
body = {"qos_specs": {"name": "qos_specs_%s" % fake.QOS_SPEC_ID,
@ -463,6 +465,17 @@ class QoSSpecManageApiTest(test.TestCase):
body = {'qos_specs': 'string'}
self._create_qos_specs_bad_body(body=body)
@ddt.data({'name': 'fake_name', 'a' * 256: 'a'},
{'name': 'fake_name', 'a': 'a' * 256},
{'name': 'fake_name' * 256, 'a': '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,
use_admin_context=True)
req.method = 'POST'
self.assertRaises(exception.InvalidInput,
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):

View File

@ -1011,10 +1011,10 @@ def validate_integer(value, name, min_value=None, max_value=None):
return value
def validate_extra_specs(specs):
"""Validating key and value of extra specs."""
def validate_dictionary_string_length(specs):
"""Check the length of each key and value of dictionary."""
if not isinstance(specs, dict):
msg = _('extra_specs must be a dictionary.')
msg = _('specs must be a dictionary.')
raise exception.InvalidInput(reason=msg)
for key, value in specs.items():