Skip to content

Commit

Permalink
Verify requested size in volume.api create
Browse files Browse the repository at this point in the history
Currently we're not checking that the input value requested
for size on volume create is valid, but taskflow portion of
the code expects it to be and as a result when it receives invalid
input we litter the logs with Trace messages.

This patch adds a check that verifies that if we pass in a
size to create_volume in the volume.api that it is in fact
an int or a string representation of an int.

Worst commit ever

Change-Id: I81bf515360eacf63e6f3cdb672ec7f37001aa4fb
Closes-Bug: 1370773
  • Loading branch information
j-griffith committed Sep 18, 2014
1 parent 5aec78f commit 7ed28d1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cinder/tests/test_volume.py
Expand Up @@ -2303,6 +2303,26 @@ def fake_rollback(context, reservations, project_id=None):
'name',
'description')

def test_create_volume_with_float_fails(self):
"""Test volume creation with invalid float size."""
volume_api = cinder.volume.api.API()
self.assertRaises(exception.InvalidInput,
volume_api.create,
self.context,
'1.5',
'name',
'description')

def test_create_volume_with_zero_size_fails(self):
"""Test volume creation with string size."""
volume_api = cinder.volume.api.API()
self.assertRaises(exception.InvalidInput,
volume_api.create,
self.context,
'0',
'name',
'description')

def test_begin_detaching_fails_available(self):
volume_api = cinder.volume.api.API()
volume = tests_utils.create_volume(self.context, **self.volume_params)
Expand Down
8 changes: 8 additions & 0 deletions cinder/utils.py
Expand Up @@ -96,6 +96,14 @@ def as_int(obj, quiet=True):
return obj


def is_int_like(val):
"""Check if a value looks like an int."""
try:
return str(int(val)) == str(val)
except Exception:
return False


def check_exclusive_options(**kwargs):
"""Checks that only one of the provided options is actually not-none.
Expand Down
16 changes: 16 additions & 0 deletions cinder/volume/api.py
Expand Up @@ -155,6 +155,22 @@ def create(self, context, size, name, description, snapshot=None,
scheduler_hints=None, backup_source_volume=None,
source_replica=None, consistencygroup=None):

# NOTE(jdg): we can have a create without size if we're
# doing a create from snap or volume. Currently
# the taskflow api will handle this and pull in the
# size from the source.

# NOTE(jdg): cinderclient sends in a string representation
# of the size value. BUT there is a possbility that somebody
# could call the API directly so the is_int_like check
# handles both cases (string representation or true float or int).
if size and (not utils.is_int_like(size) or int(size) <= 0):
msg = _('Invalid volume size provided for create request '
'(size argument must be an integer (or string '
'represenation or an integer) and greater '
'than zero).')
raise exception.InvalidInput(reason=msg)

if consistencygroup:
if not volume_type:
msg = _("volume_type must be provided when creating "
Expand Down

0 comments on commit 7ed28d1

Please sign in to comment.