Skip to content

Commit 1165a3a

Browse files
Merge pull request #192 from AikidoSec/update-http-client-ssl-context
Update way API works: use custom SSL Context
2 parents 6414305 + 3eb85f4 commit 1165a3a

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

agent_api/src/main/java/dev/aikido/agent_api/background/cloud/RealtimeAPI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import java.net.http.HttpResponse;
1212
import java.time.Duration;
1313
import java.util.Optional;
14+
import javax.net.ssl.SSLContext;
15+
import javax.net.ssl.TrustManagerFactory;
16+
import java.security.KeyStore;
1417

18+
import static dev.aikido.agent_api.background.cloud.SSLContextHelper.createDefaultSSLContext;
1519
import static dev.aikido.agent_api.helpers.env.Endpoints.getAikidoRealtimeEndpoint;
1620

1721
public class RealtimeAPI {
@@ -25,10 +29,12 @@ public RealtimeAPI(Token token) {
2529
this.token = token;
2630
}
2731
public record ConfigResponse(long configUpdatedAt) {}
32+
2833
public Optional<ConfigResponse> getConfig() {
2934
try {
3035
HttpClient httpClient = HttpClient.newBuilder()
3136
.connectTimeout(Duration.ofSeconds(timeoutInSec))
37+
.sslContext(createDefaultSSLContext())
3238
.build();
3339
URI uri = URI.create(endpoint + "config");
3440
HttpRequest request = createConfigRequest(token.get(), uri);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.aikido.agent_api.background.cloud;
2+
3+
import javax.net.ssl.SSLContext;
4+
import javax.net.ssl.TrustManagerFactory;
5+
import java.security.KeyStore;
6+
7+
public final class SSLContextHelper {
8+
private SSLContextHelper() {}
9+
public static SSLContext createDefaultSSLContext() throws Exception {
10+
// Get the default TrustManagerFactory
11+
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
12+
trustManagerFactory.init((KeyStore) null); // Use the default trust store
13+
14+
// Create an SSLContext with the default TrustManager
15+
SSLContext sslContext = SSLContext.getInstance("TLS");
16+
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
17+
18+
return sslContext;
19+
}
20+
}

agent_api/src/main/java/dev/aikido/agent_api/background/cloud/api/ReportingApiHTTP.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
import java.time.Duration;
1818
import java.util.Optional;
1919
import java.util.zip.GZIPInputStream;
20+
import javax.net.ssl.SSLContext;
21+
import javax.net.ssl.TrustManagerFactory;
22+
import java.security.KeyStore;
23+
24+
import static dev.aikido.agent_api.background.cloud.SSLContextHelper.createDefaultSSLContext;
2025

2126
public class ReportingApiHTTP extends ReportingApi {
2227
private final Logger logger = LogManager.getLogger(ReportingApiHTTP.class);
@@ -34,6 +39,7 @@ public Optional<APIResponse> fetchNewConfig() {
3439
try {
3540
HttpClient httpClient = HttpClient.newBuilder()
3641
.connectTimeout(Duration.ofSeconds(timeoutInSec))
42+
.sslContext(createDefaultSSLContext())
3743
.build();
3844

3945
URI uri = URI.create(reportingUrl + "api/runtime/config");
@@ -54,6 +60,7 @@ public Optional<APIResponse> report(APIEvent event) {
5460
try {
5561
HttpClient httpClient = HttpClient.newBuilder()
5662
.connectTimeout(Duration.ofSeconds(timeoutInSec))
63+
.sslContext(createDefaultSSLContext())
5764
.build();
5865

5966
URI uri = URI.create(reportingUrl + "api/runtime/events");
@@ -75,25 +82,32 @@ public Optional<APIListsResponse> fetchBlockedLists() {
7582
return Optional.empty();
7683
}
7784
try {
78-
// Make a GET request to api/runtime/firewall/lists
79-
URL url = new URL(reportingUrl + "api/runtime/firewall/lists");
80-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
81-
connection.setRequestMethod("GET");
85+
HttpClient httpClient = HttpClient.newBuilder()
86+
.connectTimeout(Duration.ofSeconds(timeoutInSec))
87+
.sslContext(createDefaultSSLContext())
88+
.build();
8289

83-
// Set the Accept-Encoding header to gzip
84-
connection.setRequestProperty("Accept-Encoding", "gzip");
85-
connection.setRequestProperty("Authorization", token.get());
90+
URI uri = URI.create(reportingUrl + "api/runtime/firewall/lists");
91+
HttpRequest request = HttpRequest.newBuilder()
92+
.uri(uri)
93+
.timeout(Duration.ofSeconds(timeoutInSec))
94+
.header("Accept-Encoding", "gzip")
95+
.header("Authorization", token.get())
96+
.build();
8697

87-
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
98+
// Send the request and get the response
99+
HttpResponse<InputStream> httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
100+
if (httpResponse.statusCode() != HttpURLConnection.HTTP_OK) {
88101
return Optional.empty();
89102
}
90-
InputStream inputStream = connection.getInputStream();
103+
104+
InputStream inputStream = httpResponse.body();
91105
// Check if the response is gzipped
92-
if ("gzip".equalsIgnoreCase(connection.getContentEncoding())) {
106+
if ("gzip".equalsIgnoreCase(httpResponse.headers().firstValue("Content-Encoding").orElse(""))) {
93107
inputStream = new GZIPInputStream(inputStream);
94108
}
95109

96-
// Read the response :
110+
// Read the response
97111
APIListsResponse res = gson.fromJson(new InputStreamReader(inputStream), APIListsResponse.class);
98112
return Optional.of(res);
99113
} catch (Exception e) {

0 commit comments

Comments
 (0)