|
18 | 18 |
|
19 | 19 | package org.wso2.choreo.connect.enforcer.config;
|
20 | 20 |
|
| 21 | +import org.apache.commons.lang3.RandomStringUtils; |
21 | 22 | import org.apache.commons.lang3.StringUtils;
|
22 | 23 | import org.apache.logging.log4j.LogManager;
|
23 | 24 | import org.apache.logging.log4j.Logger;
|
|
80 | 81 | import java.security.NoSuchAlgorithmException;
|
81 | 82 | import java.security.cert.Certificate;
|
82 | 83 | import java.security.cert.CertificateException;
|
| 84 | +import java.security.cert.X509Certificate; |
83 | 85 | import java.util.ArrayList;
|
84 | 86 | import java.util.Arrays;
|
85 | 87 | import java.util.List;
|
86 | 88 | import java.util.Map;
|
87 | 89 | import java.util.regex.Matcher;
|
88 | 90 | import java.util.regex.Pattern;
|
89 | 91 |
|
| 92 | +import javax.net.ssl.TrustManager; |
90 | 93 | import javax.net.ssl.TrustManagerFactory;
|
| 94 | +import javax.net.ssl.X509TrustManager; |
91 | 95 |
|
92 | 96 | /**
|
93 | 97 | * Configuration holder class for Microgateway.
|
@@ -376,17 +380,58 @@ private void populateTMBinaryConfig(BinaryPublisher binary) {
|
376 | 380 |
|
377 | 381 | private void loadTrustStore() {
|
378 | 382 | try {
|
| 383 | + |
379 | 384 | trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
380 | 385 | trustStore.load(null);
|
381 |
| - String truststoreFilePath = getEnvVarConfig().getTrustedAdapterCertsPath(); |
382 |
| - TLSUtils.addCertsToTruststore(trustStore, truststoreFilePath); |
| 386 | + |
| 387 | + if (getEnvVarConfig().isTrustDefaultCerts()) { |
| 388 | + loadDefaultCertsToTrustStore(); |
| 389 | + } |
| 390 | + loadTrustedCertsToTrustStore(); |
| 391 | + |
383 | 392 | trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
384 | 393 | trustManagerFactory.init(trustStore);
|
| 394 | + |
385 | 395 | } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
|
386 | 396 | logger.error("Error in loading certs to the trust store.", e);
|
387 | 397 | }
|
388 | 398 | }
|
389 | 399 |
|
| 400 | + private void loadTrustedCertsToTrustStore() throws IOException { |
| 401 | + String truststoreFilePath = getEnvVarConfig().getTrustedAdapterCertsPath(); |
| 402 | + TLSUtils.addCertsToTruststore(trustStore, truststoreFilePath); |
| 403 | + } |
| 404 | + |
| 405 | + private void loadDefaultCertsToTrustStore() throws NoSuchAlgorithmException, KeyStoreException { |
| 406 | + TrustManagerFactory tmf = TrustManagerFactory |
| 407 | + .getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 408 | + // Using null here initialises the TMF with the default trust store. |
| 409 | + tmf.init((KeyStore) null); |
| 410 | + |
| 411 | + // Get hold of the default trust manager |
| 412 | + X509TrustManager defaultTm = null; |
| 413 | + for (TrustManager tm : tmf.getTrustManagers()) { |
| 414 | + if (tm instanceof X509TrustManager) { |
| 415 | + defaultTm = (X509TrustManager) tm; |
| 416 | + break; |
| 417 | + } |
| 418 | + } |
| 419 | + |
| 420 | + // Get the certs from defaultTm and add them to our trustStore |
| 421 | + if (defaultTm != null) { |
| 422 | + X509Certificate[] trustedCerts = defaultTm.getAcceptedIssuers(); |
| 423 | + Arrays.stream(trustedCerts) |
| 424 | + .forEach(cert -> { |
| 425 | + try { |
| 426 | + trustStore.setCertificateEntry(RandomStringUtils.random(10, true, false), |
| 427 | + cert); |
| 428 | + } catch (KeyStoreException e) { |
| 429 | + logger.error("Error while adding default trusted ca cert", e); |
| 430 | + } |
| 431 | + }); |
| 432 | + } |
| 433 | + } |
| 434 | + |
390 | 435 | private void loadOpaClientKeyStore() {
|
391 | 436 | String certPath = getEnvVarConfig().getOpaClientPublicKeyPath();
|
392 | 437 | String keyPath = getEnvVarConfig().getOpaClientPrivateKeyPath();
|
|
0 commit comments