Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforcer truststore should trust ca root certs #3088

Merged
merged 8 commits into from
Oct 21, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.choreo.connect.enforcer.config;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -80,14 +81,17 @@
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

/**
* Configuration holder class for Microgateway.
Expand Down Expand Up @@ -362,17 +366,56 @@ private void populateTMBinaryConfig(BinaryPublisher binary) {

private void loadTrustStore() {
try {

trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null);
String truststoreFilePath = getEnvVarConfig().getTrustedAdapterCertsPath();
TLSUtils.addCertsToTruststore(trustStore, truststoreFilePath);

// TODO: enable these with a config, got error when accessing configHolder
// properties since we call loadTrustStore method in ConfigHolder constructor
loadTrustedCertsToTrustStore();
loadDefaultCertsToTrustStore();

trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);

} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
logger.error("Error in loading certs to the trust store.", e);
}
}

private void loadTrustedCertsToTrustStore() throws IOException {
String truststoreFilePath = getEnvVarConfig().getTrustedAdapterCertsPath();
TLSUtils.addCertsToTruststore(trustStore, truststoreFilePath);
}

private void loadDefaultCertsToTrustStore() throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory tmf = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// Using null here initialises the TMF with the default trust store.
tmf.init((KeyStore) null);

// Get hold of the default trust manager
X509TrustManager defaultTm = null;
for (TrustManager tm : tmf.getTrustManagers()) {
if (tm instanceof X509TrustManager) {
defaultTm = (X509TrustManager) tm;
break;
}
}

// Get the certs from defaultTm and add them to our trustStore
X509Certificate[] trustedCerts = defaultTm.getAcceptedIssuers();
Arrays.stream(trustedCerts)
.forEach(cert -> {
try {
trustStore.setCertificateEntry(RandomStringUtils.random(10, true, false),
cert);
} catch (KeyStoreException e) {
logger.error("Error while adding default trusted ca cert", e);
}
});
}

private void loadOpaClientKeyStore() {
String certPath = getEnvVarConfig().getOpaClientPublicKeyPath();
String keyPath = getEnvVarConfig().getOpaClientPrivateKeyPath();
Expand Down