Skip to content

Commit

Permalink
Remove trust all certs
Browse files Browse the repository at this point in the history
  • Loading branch information
keizer619 committed Mar 16, 2021
1 parent 59cc065 commit 0ca5445
Showing 1 changed file with 6 additions and 47 deletions.
53 changes: 6 additions & 47 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ public class ToolUtil {
System.getenv("BALLERINA_DEV_UPDATE"));
public static final String LATEST_PULL_INPUT = "latest";

private static TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
//No need to implement.
}

public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
//No need to implement.
}
}
};

/**
* Provides used Ballerina version.
*
Expand Down Expand Up @@ -206,10 +190,6 @@ public static List<Channel> getDistributions() {
List<Channel> channels = new ArrayList<>();
List<Distribution> distributions = new ArrayList<>();
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(getServerURL() + "/distributions");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
Expand Down Expand Up @@ -264,7 +244,7 @@ public static List<Channel> getDistributions() {
channel.getDistributions().add(distribution);
}
}
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
} finally {
if (conn != null) {
Expand All @@ -277,10 +257,6 @@ public static List<Channel> getDistributions() {
public static String getLatest(String currentVersion, String type) {
HttpURLConnection conn = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(getServerURL()
+ "/distributions/latest?version=" + currentVersion + "&type=" + type);
conn = (HttpURLConnection) url.openConnection();
Expand All @@ -296,7 +272,7 @@ public static String getLatest(String currentVersion, String type) {
return null;
}
throw ErrorUtil.createCommandException(getServerRequestFailedErrorMessage(conn));
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
} finally {
if (conn != null) {
Expand All @@ -317,12 +293,7 @@ private static String getValue(String key, String json) {
public static String getLatestToolVersion() {
HttpURLConnection conn = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(getServerURL() + "/versions/latest");

conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("user-agent", OSUtils.getUserAgent(getCurrentBallerinaVersion(),
Expand All @@ -343,7 +314,7 @@ public static String getLatestToolVersion() {
return null;
}
throw ErrorUtil.createCommandException(getServerRequestFailedErrorMessage(conn));
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
} finally {
if (conn != null) {
Expand Down Expand Up @@ -455,10 +426,6 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
try {
if (!ToolUtil.checkDistributionAvailable(distribution)) {
printStream.println("Fetching the '" + distribution + "' distribution from the remote server...");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(ToolUtil.getServerURL() + "/distributions/" + distributionVersion);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
Expand All @@ -484,7 +451,7 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
printStream.println("'" + distribution + "' is already available locally");
return true;
}
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
} finally {
if (conn != null) {
Expand Down Expand Up @@ -531,10 +498,6 @@ public static void getDependency(PrintStream printStream, String distribution, S
HttpURLConnection conn = null;
try {
printStream.println("\nFetching the dependencies for '" + distribution + "' from the remote server...");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(ToolUtil.getServerURL() + "/distributions");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
Expand Down Expand Up @@ -567,7 +530,7 @@ public static void getDependency(PrintStream printStream, String distribution, S
}
}
}
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
} finally {
if (conn != null) {
Expand Down Expand Up @@ -623,10 +586,6 @@ private static void downloadAndSetupDependency(HttpURLConnection conn, PrintStre
public static void downloadTool(PrintStream printStream, String toolVersion) {
HttpURLConnection conn = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

URL url = new URL(ToolUtil.getServerURL() + "/versions/" + toolVersion);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
Expand All @@ -643,7 +602,7 @@ public static void downloadTool(PrintStream printStream, String toolVersion) {
} else {
throw ErrorUtil.createCommandException("tool version '" + toolVersion + "' not found ");
}
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
} finally {
if (conn != null) {
Expand Down

0 comments on commit 0ca5445

Please sign in to comment.