Skip to content

Commit c733a23

Browse files
authored
Fix direct download URL checks (#7693)
This PR fixes the URL check for direct downloads, in the case of HTTPS URLs the certificates were not loaded into the SSL context
1 parent e6ef8a5 commit c733a23

File tree

17 files changed

+950
-395
lines changed

17 files changed

+950
-395
lines changed

agent/src/main/java/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java

Lines changed: 0 additions & 131 deletions
This file was deleted.

agent/src/main/java/com/cloud/agent/direct/download/MetalinkDirectTemplateDownloader.java

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
package org.apache.cloudstack.direct.download;
20+
21+
import com.cloud.utils.exception.CloudRuntimeException;
22+
import org.apache.cloudstack.agent.directdownload.DirectDownloadCommand;
23+
import org.apache.cloudstack.agent.directdownload.HttpDirectDownloadCommand;
24+
import org.apache.cloudstack.agent.directdownload.HttpsDirectDownloadCommand;
25+
import org.apache.cloudstack.agent.directdownload.MetalinkDirectDownloadCommand;
26+
import org.apache.cloudstack.agent.directdownload.NfsDirectDownloadCommand;
27+
import org.apache.log4j.Logger;
28+
29+
public class DirectDownloadHelper {
30+
31+
public static final Logger LOGGER = Logger.getLogger(DirectDownloadHelper.class.getName());
32+
33+
/**
34+
* Get direct template downloader from direct download command and destination pool
35+
*/
36+
public static DirectTemplateDownloader getDirectTemplateDownloaderFromCommand(DirectDownloadCommand cmd,
37+
String destPoolLocalPath,
38+
String temporaryDownloadPath) {
39+
if (cmd instanceof HttpDirectDownloadCommand) {
40+
return new HttpDirectTemplateDownloader(cmd.getUrl(), cmd.getTemplateId(), destPoolLocalPath, cmd.getChecksum(), cmd.getHeaders(),
41+
cmd.getConnectTimeout(), cmd.getSoTimeout(), temporaryDownloadPath);
42+
} else if (cmd instanceof HttpsDirectDownloadCommand) {
43+
return new HttpsDirectTemplateDownloader(cmd.getUrl(), cmd.getTemplateId(), destPoolLocalPath, cmd.getChecksum(), cmd.getHeaders(),
44+
cmd.getConnectTimeout(), cmd.getSoTimeout(), cmd.getConnectionRequestTimeout(), temporaryDownloadPath);
45+
} else if (cmd instanceof NfsDirectDownloadCommand) {
46+
return new NfsDirectTemplateDownloader(cmd.getUrl(), destPoolLocalPath, cmd.getTemplateId(), cmd.getChecksum(), temporaryDownloadPath);
47+
} else if (cmd instanceof MetalinkDirectDownloadCommand) {
48+
return new MetalinkDirectTemplateDownloader(cmd.getUrl(), destPoolLocalPath, cmd.getTemplateId(), cmd.getChecksum(), cmd.getHeaders(),
49+
cmd.getConnectTimeout(), cmd.getSoTimeout(), temporaryDownloadPath);
50+
} else {
51+
throw new IllegalArgumentException("Unsupported protocol, please provide HTTP(S), NFS or a metalink");
52+
}
53+
}
54+
55+
public static boolean checkUrlExistence(String url) {
56+
try {
57+
DirectTemplateDownloader checker = getCheckerDownloader(url);
58+
return checker.checkUrl(url);
59+
} catch (CloudRuntimeException e) {
60+
LOGGER.error(String.format("Cannot check URL %s is reachable due to: %s", url, e.getMessage()), e);
61+
return false;
62+
}
63+
}
64+
65+
private static DirectTemplateDownloader getCheckerDownloader(String url) {
66+
if (url.toLowerCase().startsWith("https:")) {
67+
return new HttpsDirectTemplateDownloader(url);
68+
} else if (url.toLowerCase().startsWith("http:")) {
69+
return new HttpDirectTemplateDownloader(url);
70+
} else if (url.toLowerCase().startsWith("nfs:")) {
71+
return new NfsDirectTemplateDownloader(url);
72+
} else if (url.toLowerCase().endsWith(".metalink")) {
73+
return new MetalinkDirectTemplateDownloader(url);
74+
} else {
75+
throw new CloudRuntimeException(String.format("Cannot find a download checker for url: %s", url));
76+
}
77+
}
78+
79+
public static Long getFileSize(String url, String format) {
80+
DirectTemplateDownloader checker = getCheckerDownloader(url);
81+
return checker.getRemoteFileSize(url, format);
82+
}
83+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package org.apache.cloudstack.direct.download;
21+
22+
import com.cloud.utils.Pair;
23+
24+
import java.util.List;
25+
26+
public interface DirectTemplateDownloader {
27+
28+
/**
29+
* Perform template download to pool specified on downloader creation
30+
* @return (true if successful, false if not, download file path)
31+
*/
32+
Pair<Boolean, String> downloadTemplate();
33+
34+
/**
35+
* Perform checksum validation of previously downloaded template
36+
* @return true if successful, false if not
37+
*/
38+
boolean validateChecksum();
39+
40+
/**
41+
* Validate if the URL is reachable and returns HTTP.OK status code
42+
* @return true if the URL is reachable, false if not
43+
*/
44+
boolean checkUrl(String url);
45+
46+
/**
47+
* Obtain the remote file size (and virtual size in case format is qcow2)
48+
*/
49+
Long getRemoteFileSize(String url, String format);
50+
51+
/**
52+
* Get list of urls within metalink content ordered by ascending priority
53+
* (for those which priority tag is not defined, highest priority value is assumed)
54+
*/
55+
List<String> getMetalinkUrls(String metalinkUrl);
56+
57+
/**
58+
* Get the list of checksums within a metalink content
59+
*/
60+
List<String> getMetalinkChecksums(String metalinkUrl);
61+
}

0 commit comments

Comments
 (0)