Skip to content

Commit

Permalink
SAP: Add affinity UUID validation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hemna committed Oct 19, 2023
1 parent 6dd928f commit 1a6d03d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cinder/volume/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from oslo_utils import excutils
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from oslo_utils import versionutils

from cinder import action_track
Expand Down Expand Up @@ -204,6 +205,36 @@ def _is_encrypted(self, volume_type):
return False
return specs.get('encryption', {}) is not {}

def _validate_scheduler_hints(self, scheduler_hints):
if scheduler_hints:
validate_volume_uuids = []
if 'same_host' in scheduler_hints:
if isinstance(scheduler_hints['same_host'], list):
validate_volume_uuids.extend(scheduler_hints['same_host'])
else:
validate_volume_uuids.append(scheduler_hints['same_host'])
elif 'different_host' in scheduler_hints:
if isinstance(scheduler_hints['different_host'], list):
validate_volume_uuids.extend(
scheduler_hints['different_host'])
else:
validate_volume_uuids.append(
scheduler_hints['different_host'])

for hint_volume_id in validate_volume_uuids:
if not uuidutils.is_uuid_like(hint_volume_id):
msg = _("Invalid UUID(s) '%s' provided in scheduler "
"hints.") % hint_volume_id
raise exception.InvalidInput(reason=msg)

# Now validate that the uuids are valid volumes that exist in
# cinder DB.
for hint_volume_id in validate_volume_uuids:
# All we have to do here is try and fetch the UUID as volume.
# If the UUID doesn't exist in the DB, Cinder will throw
# a VolumeNotFound exception.
objects.Volume.get_by_id(context, hint_volume_id)

@action_track.track_decorator(action_track.ACTION_VOLUME_CREATE)
def create(self, context, size, name, description, snapshot=None,
image_id=None, volume_type=None, metadata=None,
Expand Down Expand Up @@ -298,6 +329,10 @@ def create(self, context, size, name, description, snapshot=None,
if CONF.storage_availability_zone:
availability_zones.add(CONF.storage_availability_zone)

# Validate the scheduler_hints same_host and different_hosts as
# valid volume UUIDs.
self._validate_scheduler_hints(scheduler_hints)

# Force the scheduler hints into the volume metadata
if not metadata:
metadata = {}
Expand Down

0 comments on commit 1a6d03d

Please sign in to comment.