Skip to content

Commit 48bb13e

Browse files
committed
SAP: Add affinity UUID validation
This patch adds some basic UUID volume validation for the cinder create volume API request when passing in scheduler hints for volume affinity or anti-affinity. The API now ensures that the UUIDs are valid cinder volumes. The UUID must be a valid cinder volume UUID for the create call to work.
1 parent 767aaad commit 48bb13e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

cinder/volume/api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from oslo_utils import excutils
2727
from oslo_utils import strutils
2828
from oslo_utils import timeutils
29+
from oslo_utils import uuidutils
2930
from oslo_utils import versionutils
3031

3132
from cinder import action_track
@@ -298,6 +299,37 @@ def create(self, context, size, name, description, snapshot=None,
298299
if CONF.storage_availability_zone:
299300
availability_zones.add(CONF.storage_availability_zone)
300301

302+
# Validate the scheduler_hints same_host and different_hosts as
303+
# valid volume UUIDs.
304+
if scheduler_hints:
305+
validate_volume_uuids = []
306+
if 'same_host' in scheduler_hints:
307+
if isinstance(scheduler_hints['same_host'], list):
308+
validate_volume_uuids.extend(scheduler_hints['same_host'])
309+
else:
310+
validate_volume_uuids.append(scheduler_hints['same_host'])
311+
elif 'different_host' in scheduler_hints:
312+
if isinstance(scheduler_hints['different_host'], list):
313+
validate_volume_uuids.extend(
314+
scheduler_hints['different_host'])
315+
else:
316+
validate_volume_uuids.append(
317+
scheduler_hints['different_host'])
318+
319+
for hint_volume_id in validate_volume_uuids:
320+
if not uuidutils.is_uuid_like(hint_volume_id):
321+
msg = _("Invalid UUID(s) '%s' provided in scheduler "
322+
"hints.") % hint_volume_id
323+
raise exception.InvalidInput(reason=msg)
324+
325+
# Now validate that the uuids are valid volumes that exist in
326+
# cinder DB.
327+
for hint_volume_id in validate_volume_uuids:
328+
# All we have to do here is try and fetch the UUID as volume.
329+
# If the UUID doesn't exist in the DB, Cinder will throw
330+
# a VolumeNotFound exception.
331+
objects.Volume.get_by_id(context, hint_volume_id)
332+
301333
# Force the scheduler hints into the volume metadata
302334
if not metadata:
303335
metadata = {}

0 commit comments

Comments
 (0)