Skip to content

Commit b85183e

Browse files
committed
CLOUDSTACK-7678:volumes are getting uploaded successfully with wrong
url.
1 parent 93b2b3a commit b85183e

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ public class VolumeServiceImpl implements VolumeService {
125125
@Inject
126126
VMTemplatePoolDao _tmpltPoolDao;
127127
@Inject
128-
VolumeDao _volumeDao;
129-
@Inject
130128
EndPointSelector _epSelector;
131129
@Inject
132130
HostDao _hostDao;
@@ -1021,7 +1019,7 @@ protected Void copyVolumeCallBack(AsyncCallbackDispatcher<VolumeServiceImpl, Cop
10211019
}
10221020
srcVolume.processEvent(Event.OperationSuccessed);
10231021
destVolume.processEvent(Event.OperationSuccessed, result.getAnswer());
1024-
_volumeDao.updateUuid(srcVolume.getId(), destVolume.getId());
1022+
volDao.updateUuid(srcVolume.getId(), destVolume.getId());
10251023
try {
10261024
destroyVolume(srcVolume.getId());
10271025
srcVolume = volFactory.getVolume(srcVolume.getId());
@@ -1226,10 +1224,16 @@ public AsyncCallFuture<VolumeApiResult> registerVolume(VolumeInfo volume, DataSt
12261224

12271225
protected Void registerVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> callback, CreateVolumeContext<VolumeApiResult> context) {
12281226
CreateCmdResult result = callback.getResult();
1227+
VolumeObject vo = (VolumeObject)context.volume;
12291228
try {
1230-
VolumeObject vo = (VolumeObject)context.volume;
12311229
if (result.isFailed()) {
12321230
vo.processEvent(Event.OperationFailed);
1231+
// delete the volume entry from volumes table in case of failure
1232+
VolumeVO vol = volDao.findById(vo.getId());
1233+
if (vol != null) {
1234+
volDao.remove(vo.getId());
1235+
}
1236+
12331237
} else {
12341238
vo.processEvent(Event.OperationSuccessed, result.getAnswer());
12351239

@@ -1268,6 +1272,11 @@ protected Void registerVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl,
12681272

12691273
} catch (Exception e) {
12701274
s_logger.error("register volume failed: ", e);
1275+
// delete the volume entry from volumes table in case of failure
1276+
VolumeVO vol = volDao.findById(vo.getId());
1277+
if (vol != null) {
1278+
volDao.remove(vo.getId());
1279+
}
12711280
VolumeApiResult res = new VolumeApiResult(null);
12721281
context.future.complete(res);
12731282
return null;
@@ -1303,7 +1312,7 @@ public void resizeVolumeOnHypervisor(long volumeId, long newSize, long destHostI
13031312
EndPoint ep = RemoteHostEndPoint.getHypervisorHostEndPoint(destHost);
13041313

13051314
if (ep != null) {
1306-
VolumeVO volume = _volumeDao.findById(volumeId);
1315+
VolumeVO volume = volDao.findById(volumeId);
13071316
PrimaryDataStore primaryDataStore = this.dataStoreMgr.getPrimaryDataStore(volume.getPoolId());
13081317
ResizeVolumeCommand resizeCmd = new ResizeVolumeCommand(volume.getPath(), new StorageFilerTO(primaryDataStore), volume.getSize(), newSize, true, instanceName);
13091318

@@ -1375,7 +1384,7 @@ public void handleVolumeSync(DataStore store) {
13751384
List<VolumeDataStoreVO> dbVolumes = _volumeStoreDao.listByStoreId(storeId);
13761385
List<VolumeDataStoreVO> toBeDownloaded = new ArrayList<VolumeDataStoreVO>(dbVolumes);
13771386
for (VolumeDataStoreVO volumeStore : dbVolumes) {
1378-
VolumeVO volume = _volumeDao.findById(volumeStore.getVolumeId());
1387+
VolumeVO volume = volDao.findById(volumeStore.getVolumeId());
13791388
if (volume == null) {
13801389
s_logger.warn("Volume_store_ref shows that volume " + volumeStore.getVolumeId() + " is on image store " + storeId +
13811390
", but the volume is not found in volumes table, potentially some bugs in deleteVolume, so we just treat this volume to be deleted and mark it as destroyed");
@@ -1420,7 +1429,7 @@ public void handleVolumeSync(DataStore store) {
14201429
if (volume.getSize() == 0) {
14211430
// Set volume size in volumes table
14221431
volume.setSize(volInfo.getSize());
1423-
_volumeDao.update(volumeStore.getVolumeId(), volume);
1432+
volDao.update(volumeStore.getVolumeId(), volume);
14241433
}
14251434

14261435
if (volInfo.getSize() > 0) {

server/src/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ private boolean validateVolume(Account caller, long ownerId, Long zoneId, String
298298

299299
UriUtils.validateUrl(format, url);
300300

301+
// check URL existence
302+
UriUtils.checkUrlExistence(url);
301303

302304
// Check that the resource limit for secondary storage won't be exceeded
303305
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.secondary_storage, UriUtils.getRemoteSize(url));

utils/src/com/cloud/utils/UriUtils.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@
3838

3939
import org.apache.commons.httpclient.Credentials;
4040
import org.apache.commons.httpclient.HttpClient;
41+
import org.apache.commons.httpclient.HttpException;
4142
import org.apache.commons.httpclient.HttpStatus;
4243
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
4344
import org.apache.commons.httpclient.UsernamePasswordCredentials;
4445
import org.apache.commons.httpclient.auth.AuthScope;
4546
import org.apache.commons.httpclient.methods.GetMethod;
47+
import org.apache.commons.httpclient.methods.HeadMethod;
4648
import org.apache.commons.httpclient.util.URIUtil;
4749
import org.apache.http.NameValuePair;
4850
import org.apache.http.client.utils.URIBuilder;
4951
import org.apache.http.client.utils.URLEncodedUtils;
52+
5053
import org.apache.http.message.BasicNameValuePair;
5154
import org.apache.log4j.Logger;
5255

@@ -273,12 +276,28 @@ public static Pair<String, Integer> validateUrl(String format, String url) throw
273276
checkFormat(format, uripath);
274277
}
275278
return new Pair<String, Integer>(host, port);
276-
277279
} catch (URISyntaxException use) {
278280
throw new IllegalArgumentException("Invalid URL: " + url);
279281
}
280282
}
281283

284+
// use http HEAD method to validate url
285+
public static void checkUrlExistence(String url) {
286+
if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) {
287+
HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
288+
HeadMethod httphead = new HeadMethod(url);
289+
try {
290+
if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) {
291+
throw new IllegalArgumentException("Invalid URL: " + url);
292+
}
293+
} catch (HttpException hte) {
294+
throw new IllegalArgumentException("Cannot reach URL: " + url);
295+
} catch (IOException ioe) {
296+
throw new IllegalArgumentException("Cannot reach URL: " + url);
297+
}
298+
}
299+
}
300+
282301
// verify if a URI path is compliance with the file format given
283302
private static void checkFormat(String format, String uripath) {
284303
if ((!uripath.toLowerCase().endsWith("vhd")) && (!uripath.toLowerCase().endsWith("vhd.zip")) && (!uripath.toLowerCase().endsWith("vhd.bz2")) &&

0 commit comments

Comments
 (0)