weizhouapache commented on code in PR #7693:
URL: https://github.com/apache/cloudstack/pull/7693#discussion_r1244795101


##########
core/src/main/java/org/apache/cloudstack/direct/download/HttpsDirectTemplateDownloader.java:
##########
@@ -0,0 +1,252 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package org.apache.cloudstack.direct.download;
+
+import com.cloud.utils.Pair;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.script.Script;
+import com.cloud.utils.storage.QCOW2Utils;
+import org.apache.cloudstack.utils.security.SSLUtils;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.io.IOUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.commons.collections.MapUtils;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class HttpsDirectTemplateDownloader extends 
DirectTemplateDownloaderImpl {
+
+    protected CloseableHttpClient httpsClient;
+    private HttpUriRequest req;
+
+    protected HttpsDirectTemplateDownloader(String url) {
+        this(url, null, null, null, null, null, null, null, null);
+    }
+
+    public HttpsDirectTemplateDownloader(String url, Long templateId, String 
destPoolPath, String checksum, Map<String, String> headers,
+                                         Integer connectTimeout, Integer 
soTimeout, Integer connectionRequestTimeout, String temporaryDownloadPath) {
+        super(url, destPoolPath, templateId, checksum, temporaryDownloadPath);
+        SSLContext sslcontext = getSSLContext();
+        SSLConnectionSocketFactory factory = new 
SSLConnectionSocketFactory(sslcontext, 
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+        RequestConfig config = RequestConfig.custom()
+                .setConnectTimeout(connectTimeout == null ? 5000 : 
connectTimeout)
+                .setConnectionRequestTimeout(connectionRequestTimeout == null 
? 5000 : connectionRequestTimeout)
+                .setSocketTimeout(soTimeout == null ? 5000 : 
soTimeout).build();
+        httpsClient = 
HttpClients.custom().setSSLSocketFactory(factory).setDefaultRequestConfig(config).build();
+        createUriRequest(url, headers);
+        String downloadDir = getDirectDownloadTempPath(templateId);
+        File tempFile = createTemporaryDirectoryAndFile(downloadDir);
+        setDownloadedFilePath(tempFile.getAbsolutePath());
+    }
+
+    protected void createUriRequest(String downloadUrl, Map<String, String> 
headers) {
+        req = new HttpGet(downloadUrl);
+        if (MapUtils.isNotEmpty(headers)) {
+            for (String headerKey: headers.keySet()) {
+                req.setHeader(headerKey, headers.get(headerKey));
+            }
+        }
+    }
+
+    private SSLContext getSSLContext() {
+        try {
+            KeyStore customKeystore  = KeyStore.getInstance("jks");
+            try (FileInputStream instream = new FileInputStream(new 
File("/etc/cloudstack/agent/cloud.jks"))) {
+                String privatePasswordFormat = "sed -n 
'/keystore.passphrase/p' '%s' 2>/dev/null  | sed 's/keystore.passphrase=//g' 
2>/dev/null";
+                String privatePasswordCmd = 
String.format(privatePasswordFormat, "/etc/cloudstack/agent/agent.properties");
+                String privatePassword = 
Script.runSimpleBashScript(privatePasswordCmd);
+                customKeystore.load(instream, privatePassword.toCharArray());
+            }
+            KeyStore defaultKeystore = 
KeyStore.getInstance(KeyStore.getDefaultType());
+            String relativeCacertsPath = "/lib/security/cacerts".replace("/", 
File.separator);
+            String filename = System.getProperty("java.home") + 
relativeCacertsPath;
+            try (FileInputStream is = new FileInputStream(filename)) {
+                String password = "changeit";
+                defaultKeystore.load(is, password.toCharArray());
+            }
+            TrustManager[] tm = 
HttpsMultiTrustManager.getTrustManagersFromKeyStores(customKeystore, 
defaultKeystore);
+            SSLContext sslContext = SSLUtils.getSSLContext();
+            sslContext.init(null, tm, null);
+            return sslContext;
+        } catch (KeyStoreException | NoSuchAlgorithmException | 
CertificateException | IOException | KeyManagementException e) {
+            s_logger.error(String.format("Failure getting SSL context for 
HTTPS downloader, using default SSL context: %s", e.getMessage()), e);
+            try {
+                return SSLContext.getDefault();
+            } catch (NoSuchAlgorithmException ex) {
+                throw new CloudRuntimeException(String.format("Cannot return 
the default SSL context due to: %s", ex.getMessage()), e);
+            }
+        }

Review Comment:
   as far as I understand, these are the main changes, right ? @nvazquez 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to