Skip to content

Commit f067b50

Browse files
Gupta, SuryaLocharla, Sandeep
authored andcommitted
CSTACKEX-46 added valiodation for lun name
1 parent e9ba8b3 commit f067b50

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriver.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ public void createAsync(DataStore dataStore, DataObject dataObject, AsyncComplet
106106
if (callback == null) {
107107
throw new InvalidParameterValueException("createAsync: callback should not be null");
108108
}
109+
if(!isValidName(dataObject.getName())) {
110+
errMsg = "createAsync: Invalid dataObject name [" + dataObject.getName() + "]. It must start with a letter and can only contain letters, digits, and underscores, and be up to 200 characters long.";
111+
s_logger.error(errMsg);
112+
throw new InvalidParameterValueException(errMsg);
113+
}
109114
try {
110115
s_logger.info("createAsync: Started for data store name [{}] and data object name [{}] of type [{}]", dataStore.getName(), dataObject.getName(), dataObject.getType());
111-
112116
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
113-
if(storagePool == null) {
117+
if (storagePool == null) {
114118
s_logger.error("createCloudStackVolume : Storage Pool not found for id: " + dataStore.getId());
115119
throw new CloudRuntimeException("createCloudStackVolume : Storage Pool not found for id: " + dataStore.getId());
116120
}
@@ -135,6 +139,16 @@ public void createAsync(DataStore dataStore, DataObject dataObject, AsyncComplet
135139
}
136140
}
137141

142+
public boolean isValidName(String name) {
143+
// Check for null and length constraint first
144+
if (name == null || name.length() > 200) {
145+
return false;
146+
}
147+
// Regex: Starts with a letter, followed by letters, digits, or underscores
148+
String regex = "^[a-zA-Z][a-zA-Z0-9_]*$";
149+
return name.matches(regex);
150+
}
151+
138152
private String createCloudStackVolumeForTypeVolume(DataStore dataStore, VolumeInfo volumeObject) {
139153
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
140154
if(storagePool == null) {
@@ -248,7 +262,7 @@ public boolean grantAccess(DataObject dataObject, Host host, DataStore dataStore
248262
}
249263
try {
250264
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
251-
if(storagePool == null) {
265+
if (storagePool == null) {
252266
s_logger.error("grantAccess : Storage Pool not found for id: " + dataStore.getId());
253267
throw new CloudRuntimeException("grantAccess : Storage Pool not found for id: " + dataStore.getId());
254268
}
@@ -259,7 +273,7 @@ public boolean grantAccess(DataObject dataObject, Host host, DataStore dataStore
259273

260274
if (dataObject.getType() == DataObjectType.VOLUME) {
261275
VolumeVO volumeVO = volumeDao.findById(dataObject.getId());
262-
if(volumeVO == null) {
276+
if (volumeVO == null) {
263277
s_logger.error("grantAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
264278
throw new CloudRuntimeException("grantAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
265279
}
@@ -281,12 +295,12 @@ private void grantAccessForVolume(StoragePoolVO storagePool, VolumeVO volumeVO,
281295
String svmName = details.get(Constants.SVM_NAME);
282296
long scopeId = (storagePool.getScope() == ScopeType.CLUSTER) ? host.getClusterId() : host.getDataCenterId();
283297

284-
if(ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
298+
if (ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
285299
String accessGroupName = Utility.getIgroupName(svmName, scopeId);
286300
CloudStackVolume cloudStackVolume = getCloudStackVolumeByName(storageStrategy, svmName, volumeVO.getPath());
287301
s_logger.info("grantAccessForVolume: Retrieved LUN [{}] details for volume [{}]", cloudStackVolume.getLun().getName(), volumeVO.getName());
288302
AccessGroup accessGroup = getAccessGroupByName(storageStrategy, svmName, accessGroupName);
289-
if(!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
303+
if (!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
290304
s_logger.error("grantAccess: initiator [{}] is not present in iGroup [{}]", host.getStorageUrl(), accessGroupName);
291305
throw new CloudRuntimeException("grantAccess: initiator [" + host.getStorageUrl() + "] is not present in iGroup [" + accessGroupName + "]");
292306
}
@@ -326,7 +340,7 @@ public void revokeAccess(DataObject dataObject, Host host, DataStore dataStore)
326340
}
327341
try {
328342
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
329-
if(storagePool == null) {
343+
if (storagePool == null) {
330344
s_logger.error("revokeAccess : Storage Pool not found for id: " + dataStore.getId());
331345
throw new CloudRuntimeException("revokeAccess : Storage Pool not found for id: " + dataStore.getId());
332346
}
@@ -337,7 +351,7 @@ public void revokeAccess(DataObject dataObject, Host host, DataStore dataStore)
337351

338352
if (dataObject.getType() == DataObjectType.VOLUME) {
339353
VolumeVO volumeVO = volumeDao.findById(dataObject.getId());
340-
if(volumeVO == null) {
354+
if (volumeVO == null) {
341355
s_logger.error("revokeAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
342356
throw new CloudRuntimeException("revokeAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
343357
}
@@ -358,12 +372,12 @@ private void revokeAccessForVolume(StoragePoolVO storagePool, VolumeVO volumeVO,
358372
String svmName = details.get(Constants.SVM_NAME);
359373
long scopeId = (storagePool.getScope() == ScopeType.CLUSTER) ? host.getClusterId() : host.getDataCenterId();
360374

361-
if(ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
375+
if (ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
362376
String accessGroupName = Utility.getIgroupName(svmName, scopeId);
363377
CloudStackVolume cloudStackVolume = getCloudStackVolumeByName(storageStrategy, svmName, volumeVO.getPath());
364378
AccessGroup accessGroup = getAccessGroupByName(storageStrategy, svmName, accessGroupName);
365379
//TODO check if initiator does exits in igroup, will throw the error ?
366-
if(!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
380+
if (!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
367381
s_logger.error("revokeAccessForVolume: initiator [{}] is not present in iGroup [{}]", host.getStorageUrl(), accessGroupName);
368382
return;
369383
}
@@ -381,7 +395,7 @@ private CloudStackVolume getCloudStackVolumeByName(StorageStrategy storageStrate
381395
getCloudStackVolumeMap.put(Constants.NAME, cloudStackVolumeName);
382396
getCloudStackVolumeMap.put(Constants.SVM_DOT_NAME, svmName);
383397
CloudStackVolume cloudStackVolume = storageStrategy.getCloudStackVolume(getCloudStackVolumeMap);
384-
if(cloudStackVolume == null || cloudStackVolume.getLun() == null || cloudStackVolume.getLun().getName() == null) {
398+
if (cloudStackVolume == null || cloudStackVolume.getLun() == null || cloudStackVolume.getLun().getName() == null) {
385399
s_logger.error("getCloudStackVolumeByName: Failed to get LUN details [{}]", cloudStackVolumeName);
386400
throw new CloudRuntimeException("getCloudStackVolumeByName: Failed to get LUN [" + cloudStackVolumeName + "]");
387401
}

plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/lifecycle/OntapPrimaryDatastoreLifecycle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public boolean attachCluster(DataStore dataStore, ClusterScope scope) {
301301
}
302302
List<String> hostsIdentifier = new ArrayList<>();
303303
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
304-
if(storagePool == null) {
304+
if (storagePool == null) {
305305
s_logger.error("attachCluster : Storage Pool not found for id: " + dataStore.getId());
306306
throw new CloudRuntimeException("attachCluster : Storage Pool not found for id: " + dataStore.getId());
307307
}
@@ -373,7 +373,7 @@ public boolean attachZone(DataStore dataStore, ZoneScope scope, Hypervisor.Hyper
373373
}
374374
List<String> hostsIdentifier = new ArrayList<>();
375375
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
376-
if(storagePool == null) {
376+
if (storagePool == null) {
377377
s_logger.error("attachZone : Storage Pool not found for id: " + dataStore.getId());
378378
throw new CloudRuntimeException("attachZone : Storage Pool not found for id: " + dataStore.getId());
379379
}

plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedSANStrategy.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ public AccessGroup getAccessGroup(Map<String, String> values) {
377377
public void enableLogicalAccess(Map<String, String> values) {
378378
s_logger.info("enableLogicalAccess : Create LunMap");
379379
s_logger.debug("enableLogicalAccess : Creating LunMap with values {} ", values);
380-
LunMap lunMapRequest = new LunMap();
381380
String svmName = values.get(Constants.SVM_DOT_NAME);
382381
String lunName = values.get(Constants.LUN_DOT_NAME);
383382
String igroupName = values.get(Constants.IGROUP_DOT_NAME);
@@ -389,6 +388,10 @@ public void enableLogicalAccess(Map<String, String> values) {
389388
// Get AuthHeader
390389
String authHeader = Utility.generateAuthHeader(storage.getUsername(), storage.getPassword());
391390
// Create LunMap
391+
LunMap lunMapRequest = new LunMap();
392+
lunMapRequest.getSvm().setName(svmName);
393+
lunMapRequest.getLun().setName(lunName);
394+
lunMapRequest.getIgroup().setName(igroupName);
392395
OntapResponse<LunMap> createdLunMap = sanFeignClient.createLunMap(authHeader, true, lunMapRequest);
393396
if (createdLunMap == null || createdLunMap.getRecords() == null || createdLunMap.getRecords().size() == 0) {
394397
s_logger.error("enableLogicalAccess: LunMap failed for Lun: {} and igroup: {}", lunName, igroupName);
@@ -398,7 +401,7 @@ public void enableLogicalAccess(Map<String, String> values) {
398401
s_logger.debug("enableLogicalAccess: LunMap created successfully, LunMap: {}", lunMap);
399402
s_logger.info("enableLogicalAccess: LunMap created successfully.");
400403
} catch (Exception e) {
401-
s_logger.error("Exception occurred while creating LunMap: {}, Exception: {}", e.getMessage(), e);
404+
s_logger.error("Exception occurred while creating LunMap, Exception: {}", e);
402405
throw new CloudRuntimeException("Failed to create LunMap: " + e.getMessage());
403406
}
404407
}
@@ -419,7 +422,7 @@ public void disableLogicalAccess(Map<String, String> values) {
419422
sanFeignClient.deleteLunMap(authHeader, lunUUID, igroupUUID);
420423
s_logger.info("disableLogicalAccess: LunMap deleted successfully.");
421424
} catch (Exception e) {
422-
s_logger.error("Exception occurred while deleting LunMap: {}, Exception: {}", e.getMessage(), e);
425+
s_logger.error("Exception occurred while deleting LunMap, Exception: {}", e);
423426
throw new CloudRuntimeException("Failed to delete LunMap: " + e.getMessage());
424427
}
425428
}

0 commit comments

Comments
 (0)