Skip to content

Commit 0ec4268

Browse files
authored
Adding in the default trust store entries to the ssl context for direct downloads so that it will work with normal public ssl sites. (#36)
1 parent 8d0e207 commit 0ec4268

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import org.apache.http.ssl.SSLContexts;
3636

3737
import javax.net.ssl.SSLContext;
38+
import javax.net.ssl.TrustManager;
39+
import javax.net.ssl.TrustManagerFactory;
40+
import javax.net.ssl.X509TrustManager;
3841
import java.io.File;
3942
import java.io.FileInputStream;
4043
import java.io.IOException;
@@ -46,6 +49,7 @@
4649
import java.security.KeyStoreException;
4750
import java.security.NoSuchAlgorithmException;
4851
import java.security.cert.CertificateException;
52+
import java.security.cert.X509Certificate;
4953
import java.util.Map;
5054

5155
public class HttpsDirectTemplateDownloader extends HttpDirectTemplateDownloader {
@@ -90,6 +94,16 @@ private SSLContext getSSLContext() throws KeyStoreException, NoSuchAlgorithmExce
9094
} finally {
9195
instream.close();
9296
}
97+
// Load the default trust store entries to ensure we have everything
98+
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
99+
factory.init((KeyStore) null);
100+
for (TrustManager manager : factory.getTrustManagers()) {
101+
if (manager instanceof X509TrustManager) {
102+
for (X509Certificate acceptedIssuer : ((X509TrustManager) manager).getAcceptedIssuers()) {
103+
trustStore.setCertificateEntry(acceptedIssuer.getSubjectDN().getName(), acceptedIssuer);
104+
}
105+
}
106+
}
93107
return SSLContexts.custom()
94108
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
95109
.build();
@@ -101,6 +115,7 @@ public boolean downloadTemplate() {
101115
try {
102116
response = httpsClient.execute(req);
103117
} catch (IOException e) {
118+
s_logger.error("Error on execute", e);
104119
throw new CloudRuntimeException("Error on HTTPS request: " + e.getMessage());
105120
}
106121
return consumeResponse(response);

0 commit comments

Comments
 (0)