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

Support passing a custom truststore #385

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.moandjiezana.toml.Toml;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
Expand All @@ -45,6 +47,14 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
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.Arrays;
import java.util.List;
Expand Down Expand Up @@ -79,6 +89,17 @@ public class ToolUtil {
System.getenv("TEST_MODE_ACTIVE"));
public static final String DEFAULT_BALLERINA_VERSION = "0.0.0";

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 static final String CA_CERTS_DEFAULT_PATH = System.getProperty("java.home") + "/lib/security/cacerts";
private static final String CA_CERTS_DEFAULT_PASSWORD = "changeit";

private static final String trustStorePath = System.getenv(ENV_TRUSTSTORE_PATH);
private static final String trustStorePassword = System.getenv(ENV_TRUSTSTORE_PASSWORD);
private static final String singleCertPath = System.getenv(ENV_CERT_PATH);


/**
* Provides used Ballerina version.
*
Expand Down Expand Up @@ -110,6 +131,46 @@ public static String getCurrentBallerinaVersion() {
}
}

/**
* Sets custom SSL context for update tool.
*/
public static void setCustomSSLContext() throws IOException {
// Load custom truststore if provided, otherwise use the default truststore
try {
KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
if (trustStorePath != null && trustStorePassword != null) {
try (InputStream keys = Files.newInputStream(Paths.get(trustStorePath))) {
truststore.load(keys, trustStorePassword.toCharArray());
}
} else {
try (InputStream defaultKeys = Files.newInputStream(Paths.get(CA_CERTS_DEFAULT_PATH))) {
truststore.load(defaultKeys, CA_CERTS_DEFAULT_PASSWORD.toCharArray());
}
}

// If there's a single certificate to add
if (singleCertPath != null) {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
try (InputStream certInputStream = Files.newInputStream(Paths.get(singleCertPath))) {
Certificate certificate = certificateFactory.generateCertificate(certInputStream);
truststore.setCertificateEntry("bal-cert", certificate);
}
}

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

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLContext.setDefault(sslContext);
} catch (CertificateException | KeyStoreException | IOException | NoSuchAlgorithmException |
KeyManagementException e) {
System.out.println("Error occurred while loading the custom truststore: " + e.getMessage());
Copy link
Member

@keizer619 keizer619 Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NipunaMadhushan Lets remove System.out.println and use a PrintStream

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed suggestions

throw new IOException(e.getMessage());
}
}

/**
* Provide the installed Ballerina version by the installer.
*
Expand Down Expand Up @@ -215,6 +276,8 @@ public static boolean checkDependencyAvailable(String dependency) {

public static HttpsURLConnection getServerUrlWithProxyAuthentication(URL serverURL) throws IOException {
if (checkProxyConfigsDefinition()) {
setCustomSSLContext();

Map<String, Object> proxyConfigs = getProxyConfigs();
String proxyHost = proxyConfigs.containsKey("host") ? proxyConfigs.get("host").toString() : null;
String proxyPort = proxyConfigs.containsKey("port") ? proxyConfigs.get("port").toString() : null;
Expand Down Expand Up @@ -579,7 +642,7 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
}

private static void downloadDistributionZip(PrintStream printStream, HttpURLConnection conn,
String distribution) {
String distribution) {
try {
String zipFileLocation = getDistributionsPath() + File.separator + distribution + ".zip";
downloadFile(conn, zipFileLocation, distribution, printStream);
Expand Down Expand Up @@ -622,7 +685,7 @@ public static void setupDistribution (String distribution, String dependency) {
}

public static String getDependency(PrintStream printStream, String distribution, String distributionType,
String distributionVersion) {
String distributionVersion) {
HttpsURLConnection conn = null;
String dependencyName = "";
try {
Expand Down