Skip to content

Commit 70c6b13

Browse files
Gupta, SuryaGupta, Surya
authored andcommitted
CSTACKEX-46 added valiodation for lun name
1 parent 8980303 commit 70c6b13

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

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
@@ -107,11 +107,15 @@ public void createAsync(DataStore dataStore, DataObject dataObject, AsyncComplet
107107
if (callback == null) {
108108
throw new InvalidParameterValueException("createAsync: callback should not be null");
109109
}
110+
if(!isValidName(dataObject.getName())) {
111+
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.";
112+
s_logger.error(errMsg);
113+
throw new InvalidParameterValueException(errMsg);
114+
}
110115
try {
111116
s_logger.info("createAsync: Started for data store name [{}] and data object name [{}] of type [{}]", dataStore.getName(), dataObject.getName(), dataObject.getType());
112-
113117
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
114-
if(storagePool == null) {
118+
if (storagePool == null) {
115119
s_logger.error("createCloudStackVolume : Storage Pool not found for id: " + dataStore.getId());
116120
throw new CloudRuntimeException("createCloudStackVolume : Storage Pool not found for id: " + dataStore.getId());
117121
}
@@ -133,6 +137,16 @@ public void createAsync(DataStore dataStore, DataObject dataObject, AsyncComplet
133137
}
134138
}
135139

140+
public boolean isValidName(String name) {
141+
// Check for null and length constraint first
142+
if (name == null || name.length() > 200) {
143+
return false;
144+
}
145+
// Regex: Starts with a letter, followed by letters, digits, or underscores
146+
String regex = "^[a-zA-Z][a-zA-Z0-9_]*$";
147+
return name.matches(regex);
148+
}
149+
136150
private String createCloudStackVolumeForTypeVolume(StoragePoolVO storagePool, VolumeInfo volumeInfo) {
137151
Map<String, String> details = storagePoolDetailsDao.listDetailsKeyPairs(storagePool.getId());
138152
StorageStrategy storageStrategy = Utility.getStrategyByStoragePoolDetails(details);
@@ -218,7 +232,7 @@ public boolean grantAccess(DataObject dataObject, Host host, DataStore dataStore
218232
}
219233
try {
220234
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
221-
if(storagePool == null) {
235+
if (storagePool == null) {
222236
s_logger.error("grantAccess : Storage Pool not found for id: " + dataStore.getId());
223237
throw new CloudRuntimeException("grantAccess : Storage Pool not found for id: " + dataStore.getId());
224238
}
@@ -229,7 +243,7 @@ public boolean grantAccess(DataObject dataObject, Host host, DataStore dataStore
229243

230244
if (dataObject.getType() == DataObjectType.VOLUME) {
231245
VolumeVO volumeVO = volumeDao.findById(dataObject.getId());
232-
if(volumeVO == null) {
246+
if (volumeVO == null) {
233247
s_logger.error("grantAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
234248
throw new CloudRuntimeException("grantAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
235249
}
@@ -252,12 +266,12 @@ private void grantAccessForVolume(StoragePoolVO storagePool, VolumeVO volumeVO,
252266
//TODO- verify scopeId DatacenterId is zoneId
253267
long scopeId = (storagePool.getScope() == ScopeType.CLUSTER) ? host.getClusterId() : host.getDataCenterId();
254268

255-
if(ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
269+
if (ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
256270
String accessGroupName = Utility.getIgroupName(svmName, scopeId);
257271
CloudStackVolume cloudStackVolume = getCloudStackVolumeByName(storageStrategy, svmName, volumeVO.getPath());
258272
s_logger.info("grantAccessForVolume: Retrieved LUN [{}] details for volume [{}]", cloudStackVolume.getLun().getName(), volumeVO.getName());
259273
AccessGroup accessGroup = getAccessGroupByName(storageStrategy, svmName, accessGroupName);
260-
if(!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
274+
if (!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
261275
s_logger.error("grantAccess: initiator [{}] is not present in iGroup [{}]", host.getStorageUrl(), accessGroupName);
262276
throw new CloudRuntimeException("grantAccess: initiator [" + host.getStorageUrl() + "] is not present in iGroup [" + accessGroupName + "]");
263277
}
@@ -297,7 +311,7 @@ public void revokeAccess(DataObject dataObject, Host host, DataStore dataStore)
297311
}
298312
try {
299313
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
300-
if(storagePool == null) {
314+
if (storagePool == null) {
301315
s_logger.error("revokeAccess : Storage Pool not found for id: " + dataStore.getId());
302316
throw new CloudRuntimeException("revokeAccess : Storage Pool not found for id: " + dataStore.getId());
303317
}
@@ -308,7 +322,7 @@ public void revokeAccess(DataObject dataObject, Host host, DataStore dataStore)
308322

309323
if (dataObject.getType() == DataObjectType.VOLUME) {
310324
VolumeVO volumeVO = volumeDao.findById(dataObject.getId());
311-
if(volumeVO == null) {
325+
if (volumeVO == null) {
312326
s_logger.error("revokeAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
313327
throw new CloudRuntimeException("revokeAccess : Cloud Stack Volume not found for id: " + dataObject.getId());
314328
}
@@ -329,12 +343,12 @@ private void revokeAccessForVolume(StoragePoolVO storagePool, VolumeVO volumeVO,
329343
String svmName = details.get(Constants.SVM_NAME);
330344
long scopeId = (storagePool.getScope() == ScopeType.CLUSTER) ? host.getClusterId() : host.getDataCenterId();
331345

332-
if(ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
346+
if (ProtocolType.ISCSI.name().equalsIgnoreCase(details.get(Constants.PROTOCOL))) {
333347
String accessGroupName = Utility.getIgroupName(svmName, scopeId);
334348
CloudStackVolume cloudStackVolume = getCloudStackVolumeByName(storageStrategy, svmName, volumeVO.getPath());
335349
AccessGroup accessGroup = getAccessGroupByName(storageStrategy, svmName, accessGroupName);
336350
//TODO check if initiator does exits in igroup, will throw the error ?
337-
if(!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
351+
if (!hostInitiatorFoundInIgroup(host.getStorageUrl(), accessGroup.getIgroup())) {
338352
s_logger.error("revokeAccessForVolume: initiator [{}] is not present in iGroup [{}]", host.getStorageUrl(), accessGroupName);
339353
return;
340354
}
@@ -352,7 +366,7 @@ private CloudStackVolume getCloudStackVolumeByName(StorageStrategy storageStrate
352366
getCloudStackVolumeMap.put(Constants.NAME, cloudStackVolumeName);
353367
getCloudStackVolumeMap.put(Constants.SVM_DOT_NAME, svmName);
354368
CloudStackVolume cloudStackVolume = storageStrategy.getCloudStackVolume(getCloudStackVolumeMap);
355-
if(cloudStackVolume == null || cloudStackVolume.getLun() == null || cloudStackVolume.getLun().getName() == null) {
369+
if (cloudStackVolume == null || cloudStackVolume.getLun() == null || cloudStackVolume.getLun().getName() == null) {
356370
s_logger.error("getCloudStackVolumeByName: Failed to get LUN details [{}]", cloudStackVolumeName);
357371
throw new CloudRuntimeException("getCloudStackVolumeByName: Failed to get LUN [" + cloudStackVolumeName + "]");
358372
}

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
@@ -261,7 +261,7 @@ public boolean attachCluster(DataStore dataStore, ClusterScope scope) {
261261
}
262262
List<String> hostsIdentifier = new ArrayList<>();
263263
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
264-
if(storagePool == null) {
264+
if (storagePool == null) {
265265
s_logger.error("attachCluster : Storage Pool not found for id: " + dataStore.getId());
266266
throw new CloudRuntimeException("attachCluster : Storage Pool not found for id: " + dataStore.getId());
267267
}
@@ -317,7 +317,7 @@ public boolean attachZone(DataStore dataStore, ZoneScope scope, Hypervisor.Hyper
317317
}
318318
List<String> hostsIdentifier = new ArrayList<>();
319319
StoragePoolVO storagePool = storagePoolDao.findById(dataStore.getId());
320-
if(storagePool == null) {
320+
if (storagePool == null) {
321321
s_logger.error("attachZone : Storage Pool not found for id: " + dataStore.getId());
322322
throw new CloudRuntimeException("attachZone : Storage Pool not found for id: " + dataStore.getId());
323323
}

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
@@ -212,7 +212,6 @@ public AccessGroup getAccessGroup(Map<String, String> values) {
212212
public void enableLogicalAccess(Map<String, String> values) {
213213
s_logger.info("enableLogicalAccess : Create LunMap");
214214
s_logger.debug("enableLogicalAccess : Creating LunMap with values {} ", values);
215-
LunMap lunMapRequest = new LunMap();
216215
String svmName = values.get(Constants.SVM_DOT_NAME);
217216
String lunName = values.get(Constants.LUN_DOT_NAME);
218217
String igroupName = values.get(Constants.IGROUP_DOT_NAME);
@@ -224,6 +223,10 @@ public void enableLogicalAccess(Map<String, String> values) {
224223
// Get AuthHeader
225224
String authHeader = Utility.generateAuthHeader(storage.getUsername(), storage.getPassword());
226225
// Create LunMap
226+
LunMap lunMapRequest = new LunMap();
227+
lunMapRequest.getSvm().setName(svmName);
228+
lunMapRequest.getLun().setName(lunName);
229+
lunMapRequest.getIgroup().setName(igroupName);
227230
OntapResponse<LunMap> createdLunMap = sanFeignClient.createLunMap(authHeader, true, lunMapRequest);
228231
if (createdLunMap == null || createdLunMap.getRecords() == null || createdLunMap.getRecords().size() == 0) {
229232
s_logger.error("enableLogicalAccess: LunMap failed for Lun: {} and igroup: {}", lunName, igroupName);
@@ -233,7 +236,7 @@ public void enableLogicalAccess(Map<String, String> values) {
233236
s_logger.debug("enableLogicalAccess: LunMap created successfully, LunMap: {}", lunMap);
234237
s_logger.info("enableLogicalAccess: LunMap created successfully.");
235238
} catch (Exception e) {
236-
s_logger.error("Exception occurred while creating LunMap: {}, Exception: {}", e.getMessage(), e);
239+
s_logger.error("Exception occurred while creating LunMap, Exception: {}", e);
237240
throw new CloudRuntimeException("Failed to create LunMap: " + e.getMessage());
238241
}
239242
}
@@ -254,7 +257,7 @@ public void disableLogicalAccess(Map<String, String> values) {
254257
sanFeignClient.deleteLunMap(authHeader, lunUUID, igroupUUID);
255258
s_logger.info("disableLogicalAccess: LunMap deleted successfully.");
256259
} catch (Exception e) {
257-
s_logger.error("Exception occurred while deleting LunMap: {}, Exception: {}", e.getMessage(), e);
260+
s_logger.error("Exception occurred while deleting LunMap, Exception: {}", e);
258261
throw new CloudRuntimeException("Failed to delete LunMap: " + e.getMessage());
259262
}
260263
}

0 commit comments

Comments
 (0)