From 418ad3ef2dd52200f6d6c26687e64f023c7ba107 Mon Sep 17 00:00:00 2001 From: Rongze Zhu Date: Wed, 12 Sep 2012 10:18:31 +0000 Subject: [PATCH] Return 400 if create volume snapshot force parameter is invalid Fixes bug #1014689 * Add is_valid_boolstr function in utils.py * Add force parameter check in SnapshotsController.create() * Add unittest for invalid force parameter. Change-Id: I0f64326f33eb4fad1cf384bd825f56f09e935f40 (cherry picked from commit c678b045c9839ed30e13fe5e7e655e3cb2e2c3f9) --- cinder/api/openstack/volume/snapshots.py | 7 ++++++- cinder/tests/api/openstack/volume/test_snapshots.py | 11 +++++++++++ cinder/utils.py | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cinder/api/openstack/volume/snapshots.py b/cinder/api/openstack/volume/snapshots.py index 192dee223..f4f79f54d 100644 --- a/cinder/api/openstack/volume/snapshots.py +++ b/cinder/api/openstack/volume/snapshots.py @@ -25,6 +25,7 @@ from cinder.api.openstack.volume import volumes from cinder import exception from cinder import flags from cinder.openstack.common import log as logging +from cinder import utils from cinder import volume @@ -158,7 +159,11 @@ class SnapshotsController(wsgi.Controller): msg = _("Create snapshot from volume %s") LOG.audit(msg, volume_id, context=context) - if force: + if not utils.is_valid_boolstr(force): + msg = _("Invalid value '%s' for force. ") % force + raise exception.InvalidParameterValue(err=msg) + + if utils.bool_from_str(force): new_snapshot = self.volume_api.create_snapshot_force(context, volume, snapshot.get('display_name'), diff --git a/cinder/tests/api/openstack/volume/test_snapshots.py b/cinder/tests/api/openstack/volume/test_snapshots.py index 9a0e8f24f..f9d61b39e 100644 --- a/cinder/tests/api/openstack/volume/test_snapshots.py +++ b/cinder/tests/api/openstack/volume/test_snapshots.py @@ -118,6 +118,17 @@ class SnapshotApiTest(test.TestCase): self.assertEqual(resp_dict['snapshot']['display_description'], snapshot['display_description']) + snapshot = {"volume_id": "12", + "force": "**&&^^%%$$##@@", + "display_name": "Snapshot Test Name", + "display_description": "Snapshot Test Desc"} + body = dict(snapshot=snapshot) + req = fakes.HTTPRequest.blank('/v1/snapshots') + self.assertRaises(exception.InvalidParameterValue, + self.controller.create, + req, + body) + def test_snapshot_delete(self): self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get) self.stubs.Set(volume.api.API, "delete_snapshot", stub_snapshot_delete) diff --git a/cinder/utils.py b/cinder/utils.py index bfcda366a..0e6038cda 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -691,6 +691,15 @@ def bool_from_str(val): return val.lower() == 'true' +def is_valid_boolstr(val): + """Check if the provided string is a valid bool string or not. """ + val = str(val).lower() + return val == 'true' or val == 'false' or \ + val == 'yes' or val == 'no' or \ + val == 'y' or val == 'n' or \ + val == '1' or val == '0' + + def is_valid_ipv4(address): """valid the address strictly as per format xxx.xxx.xxx.xxx. where xxx is a value between 0 and 255. -- 2.45.2