Skip to content

Commit

Permalink
TS-38828 Rework and adapt test
Browse files Browse the repository at this point in the history
  • Loading branch information
sewe committed May 22, 2024
1 parent 1b3fbf7 commit fb71676
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/main/java/com/teamscale/upload/utils/OkHttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ public static OkHttpClient createClient(boolean validateSsl, String trustStorePa
setTimeouts(builder, timeoutInSeconds);
builder.followRedirects(false).followSslRedirects(false);

if (validateSsl) {
if (validateSsl || !disableSslValidation(builder)) {
configureTrustStore(builder, trustStorePath, trustStorePassword);
}
if (!validateSsl) {
disableSslValidation(builder);
}

return builder.build();
}
Expand All @@ -94,7 +91,7 @@ private static void configureTrustStore(OkHttpClient.Builder builder, String tru

MultiTrustManager multiTrustManager = new MultiTrustManager(trustManagers);

sslContext.init(null, new TrustManager[]{multiTrustManager}, new SecureRandom());
sslContext.init(null, new TrustManager[] {multiTrustManager}, new SecureRandom());
builder.sslSocketFactory(sslContext.getSocketFactory(), multiTrustManager);
} catch (NoSuchAlgorithmException e) {
LogUtils.failWithStackTrace(e, "Failed to instantiate an SSLContext or TrustManagerFactory.");
Expand Down Expand Up @@ -222,19 +219,21 @@ private static Collection<X509Certificate> getCustomOsTrustedCertificates() {
}
}

private static void disableSslValidation(OkHttpClient.Builder builder) {
/** Tries to disable SSL validation. Returns {@code true} if validation was successfully disabled. */
private static boolean disableSslValidation(OkHttpClient.Builder builder) {
SSLSocketFactory sslSocketFactory;
try {
SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
sslContext.init(null, new TrustManager[] { TrustAllCertificatesManager.INSTANCE }, new SecureRandom());
sslSocketFactory = sslContext.getSocketFactory();
} catch (GeneralSecurityException e) {
LogUtils.warn("Could not disable SSL certificate validation. Leaving it enabled", e);
return;
return false;
}

builder.sslSocketFactory(sslSocketFactory, TrustAllCertificatesManager.INSTANCE);
builder.hostnameVerifier((hostName, session) -> true);
return true;
}

private static void setTimeouts(okhttp3.OkHttpClient.Builder builder, long timeoutInSeconds) {
Expand Down Expand Up @@ -277,7 +276,6 @@ public X509Certificate[] getAcceptedIssuers() {
return trustManagers.stream().flatMap(manager -> Arrays.stream(manager.getAcceptedIssuers())).toArray(X509Certificate[]::new);
}


@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
checkAll(manager -> manager.checkClientTrusted(chain, authType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ public void selfSignedCertificateShouldBeAcceptedWithInsecureFlag() {
new TeamscaleUploadArguments().withUrl("https://localhost:" + MOCK_TEAMSCALE_PORT).withInsecure());
assertThat(result.exitCode).describedAs("Stderr and stdout: " + result.getOutputAndErrorOutput()).isZero();
assertThat(server.sessions).hasSize(1);
assertThatOSCertificatesWereImported(result);
}
}

Expand Down

0 comments on commit fb71676

Please sign in to comment.