Skip to content

feat: ssl bypass #12

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

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<url>https://github.com/clean-arch-enablers-project/cae-utils-http-client/blob/main/README.md</url>
<groupId>com.clean-arch-enablers</groupId>
<artifactId>cae-http-client</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<packaging>jar</packaging>
<licenses>
<license>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cae/http_client/HttpRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface HttpRequestBuilder extends HttpRequestBuilderForHandlers{
HttpRequestBuilder pathVariableOf(String pathVariablePlaceholder, String pathVariableValue);
HttpRequestBuilder queryParameterOf(String queryParameterName, String queryParameterValue);
HttpRequestBuilder proxyAddress(String host, Integer port);
HttpRequestBuilder bypassSsl();
HttpRequestModel buildRequestModel();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class AbstractHttpRequestModel implements HttpRequestModel {
protected BodyPublisher body;
protected HttpRequestMethod method;
protected ProxyAddressModel proxyAddress;
protected Boolean bypassSsl = false;
protected HttpResponseHandler genericResponseHandler;
protected final Map<Integer, HttpResponseHandler> responseHandlersByStatusCode = new HashMap<>();
protected final Map<Class<? extends Exception>, ExceptionHandler> exceptionHandlersByExceptionType = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,27 @@ public HttpResponse<String> execute(HttpRequest finalRequest){

private HttpClient createClient() {
var client = HttpClient.newBuilder();
Optional.ofNullable(this.httpRequestModel.proxyAddress).ifPresent(proxyAddress -> client.proxy(ProxySelector.of(new InetSocketAddress(proxyAddress.getHost(), proxyAddress.getPort()))));
this.handleProxySettings(client);
this.handleSslByPass(client);
return client.build();
}

private void handleProxySettings(HttpClient.Builder client) {
Optional.ofNullable(this.httpRequestModel.proxyAddress).ifPresent(
proxyAddress -> client.proxy(
ProxySelector.of(
new InetSocketAddress(
proxyAddress.getHost(),
proxyAddress.getPort()
)
)
)
);
}

private void handleSslByPass(HttpClient.Builder client) {
if (this.httpRequestModel.bypassSsl)
client.sslContext(SSLBypassSettings.getContext());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public HttpRequestBuilder proxyAddress(String host, Integer port) {
return this;
}

@Override
public HttpRequestBuilder bypassSsl() {
this.httpRequest.bypassSsl = true;
return this;
}

@Override
public HttpRequestModel buildRequestModel() {
return this.httpRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.cae.http_client.implementations;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SSLBypassSettings {

public static SSLContext getContext(){
try{
var allTrustingManager = new TrustManager[] {new CustomTrustManager()};
var sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, allTrustingManager, new SecureRandom());
return sslContext;
} catch (NoSuchAlgorithmException noSuchAlgorithmException){
throw new SSLBypassSettingsException(
"Couldn't set the bypass for SSL. Problem while trying to get the SSL Context instance for TLS. More details:"
+ noSuchAlgorithmException
);
} catch (KeyManagementException keyManagementException){
throw new SSLBypassSettingsException(
"Couldn't set the bypass for SSL. Problem while trying init the SSL Context. More details:"
+ keyManagementException
);
}
}

public static class CustomTrustManager implements X509TrustManager{

@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {}

@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

public static class SSLBypassSettingsException extends RuntimeException{

public SSLBypassSettingsException(String message){
super(message);
}

}

}