Skip to content

Commit

Permalink
Support adding a single cert
Browse files Browse the repository at this point in the history
  • Loading branch information
azinneera committed Nov 25, 2024
1 parent b274bd2 commit 8b48602
Showing 1 changed file with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -161,8 +163,9 @@ public class CentralAPIClient {
private static final int MAX_RETRY = 1;
public static final String CONNECTION_RESET = "Connection reset";
private static final String ENV_CENTRAL_VERBOSE_ENABLED = "CENTRAL_VERBOSE_ENABLED";
private static final String ENV_TRUSTSTORE_PATH = "BALLERINA_TRUSTSTORE_PATH";
private static final String ENV_TRUSTSTORE_PASSWORD = "BALLERINA_TRUSTSTORE_PASSWORD";
private static final String ENV_TRUSTSTORE_PATH = "BALLERINA_CA_BUNDLE";
private static final String ENV_TRUSTSTORE_PASSWORD = "BALLERINA_CA_PASSWORD";
private static final String ENV_CERT_PATH = "BALLERINA_CA_CERT";

private final String baseUrl;
private final Proxy proxy;
Expand All @@ -178,6 +181,7 @@ public class CentralAPIClient {
private final int maxRetries;
private final String trustStorePath;
private final String trustStorePassword;
private final String singleCertPath;

public CentralAPIClient(String baseUrl, Proxy proxy, String accessToken) {
this.outStream = System.out;
Expand All @@ -194,6 +198,7 @@ public CentralAPIClient(String baseUrl, Proxy proxy, String accessToken) {
this.maxRetries = MAX_RETRY;
this.trustStorePath = System.getenv(ENV_TRUSTSTORE_PATH);
this.trustStorePassword = System.getenv(ENV_TRUSTSTORE_PASSWORD);
this.singleCertPath = System.getenv(ENV_CERT_PATH);

Check warning on line 201 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L199-L201

Added lines #L199 - L201 were not covered by tests
}

public CentralAPIClient(String baseUrl, Proxy proxy, String accessToken, boolean verboseEnabled, int maxRetries,
Expand All @@ -212,6 +217,7 @@ public CentralAPIClient(String baseUrl, Proxy proxy, String accessToken, boolean
this.maxRetries = maxRetries;
this.trustStorePath = System.getenv(ENV_TRUSTSTORE_PATH);
this.trustStorePassword = System.getenv(ENV_TRUSTSTORE_PASSWORD);
this.singleCertPath = System.getenv(ENV_CERT_PATH);
}

public CentralAPIClient(String baseUrl, Proxy proxy, String proxyUsername, String proxyPassword,
Expand All @@ -231,6 +237,7 @@ public CentralAPIClient(String baseUrl, Proxy proxy, String proxyUsername, Strin
this.maxRetries = maxRetries;
this.trustStorePath = System.getenv(ENV_TRUSTSTORE_PATH);
this.trustStorePassword = System.getenv(ENV_TRUSTSTORE_PASSWORD);
this.singleCertPath = System.getenv(ENV_CERT_PATH);
}

/**
Expand Down Expand Up @@ -1609,26 +1616,45 @@ protected OkHttpClient getClient() throws CentralClientException {
.retryOnConnectionFailure(true)
.proxy(this.proxy)
.addInterceptor(new CustomRetryInterceptor(this.maxRetries));
if (this.trustStorePath != null && this.trustStorePassword != null) {
try {
KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());

// Load custom truststore if provided, otherwise use the default truststore
try {
KeyStore truststore;
if (this.trustStorePath != null && this.trustStorePassword != null) {
truststore = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream keys = new FileInputStream(trustStorePath)) {
truststore.load(keys, trustStorePassword.toCharArray());

Check warning on line 1626 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1624-L1626

Added lines #L1624 - L1626 were not covered by tests
}
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(truststore);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLContext.setDefault(sslContext);
} else {
truststore = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream defaultKeys = new FileInputStream(System.getProperty("java.home") +
"/lib/security/cacerts")) {
truststore.load(defaultKeys, "changeit".toCharArray()); // Default password for cacerts
}
}

builder.sslSocketFactory(sslContext.getSocketFactory(),
(X509TrustManager) trustManagerFactory.getTrustManagers()[0]);
} catch (CertificateException | KeyStoreException | IOException | NoSuchAlgorithmException |
KeyManagementException e) {
throw new CentralClientException(e.getMessage());
// If there's a single certificate to add
if (this.singleCertPath != null) {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
try (InputStream certInputStream = new FileInputStream(singleCertPath)) {
Certificate certificate = certificateFactory.generateCertificate(certInputStream);
truststore.setCertificateEntry("bal-cert", certificate);

Check warning on line 1641 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1638-L1641

Added lines #L1638 - L1641 were not covered by tests
}
}

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

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLContext.setDefault(sslContext);

builder.sslSocketFactory(sslContext.getSocketFactory(),
(X509TrustManager) trustManagerFactory.getTrustManagers()[0]);
} catch (CertificateException | KeyStoreException | IOException | NoSuchAlgorithmException |

Check warning on line 1655 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1655

Added line #L1655 was not covered by tests
KeyManagementException e) {
throw new CentralClientException(e.getMessage());

Check warning on line 1657 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1657

Added line #L1657 was not covered by tests
}

OkHttpClient okHttpClient = builder.build();
Expand Down

0 comments on commit 8b48602

Please sign in to comment.