Skip to content

Commit 26cf16e

Browse files
committed
create a default ssl context
1 parent 6414305 commit 26cf16e

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
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

1518
import static dev.aikido.agent_api.helpers.env.Endpoints.getAikidoRealtimeEndpoint;
1619

@@ -25,10 +28,24 @@ public RealtimeAPI(Token token) {
2528
this.token = token;
2629
}
2730
public record ConfigResponse(long configUpdatedAt) {}
31+
32+
private SSLContext createDefaultSSLContext() throws Exception {
33+
// Get the default TrustManagerFactory
34+
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
35+
trustManagerFactory.init((KeyStore) null); // Use the default trust store
36+
37+
// Create an SSLContext with the default TrustManager
38+
SSLContext sslContext = SSLContext.getInstance("TLS");
39+
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
40+
41+
return sslContext;
42+
}
43+
2844
public Optional<ConfigResponse> getConfig() {
2945
try {
3046
HttpClient httpClient = HttpClient.newBuilder()
3147
.connectTimeout(Duration.ofSeconds(timeoutInSec))
48+
.sslContext(createDefaultSSLContext())
3249
.build();
3350
URI uri = URI.create(endpoint + "config");
3451
HttpRequest request = createConfigRequest(token.get(), uri);

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
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;
2023

2124
public class ReportingApiHTTP extends ReportingApi {
2225
private final Logger logger = LogManager.getLogger(ReportingApiHTTP.class);
@@ -30,10 +33,23 @@ public ReportingApiHTTP(String reportingUrl, int timeoutInSec, Token token) {
3033
this.token = token;
3134
}
3235

36+
private SSLContext createDefaultSSLContext() throws Exception {
37+
// Get the default TrustManagerFactory
38+
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
39+
trustManagerFactory.init((KeyStore) null); // Use the default trust store
40+
41+
// Create an SSLContext with the default TrustManager
42+
SSLContext sslContext = SSLContext.getInstance("TLS");
43+
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
44+
45+
return sslContext;
46+
}
47+
3348
public Optional<APIResponse> fetchNewConfig() {
3449
try {
3550
HttpClient httpClient = HttpClient.newBuilder()
3651
.connectTimeout(Duration.ofSeconds(timeoutInSec))
52+
.sslContext(createDefaultSSLContext())
3753
.build();
3854

3955
URI uri = URI.create(reportingUrl + "api/runtime/config");
@@ -54,6 +70,7 @@ public Optional<APIResponse> report(APIEvent event) {
5470
try {
5571
HttpClient httpClient = HttpClient.newBuilder()
5672
.connectTimeout(Duration.ofSeconds(timeoutInSec))
73+
.sslContext(createDefaultSSLContext())
5774
.build();
5875

5976
URI uri = URI.create(reportingUrl + "api/runtime/events");
@@ -75,25 +92,32 @@ public Optional<APIListsResponse> fetchBlockedLists() {
7592
return Optional.empty();
7693
}
7794
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");
95+
HttpClient httpClient = HttpClient.newBuilder()
96+
.connectTimeout(Duration.ofSeconds(timeoutInSec))
97+
.sslContext(createDefaultSSLContext())
98+
.build();
8299

83-
// Set the Accept-Encoding header to gzip
84-
connection.setRequestProperty("Accept-Encoding", "gzip");
85-
connection.setRequestProperty("Authorization", token.get());
100+
URI uri = URI.create(reportingUrl + "api/runtime/firewall/lists");
101+
HttpRequest request = HttpRequest.newBuilder()
102+
.uri(uri)
103+
.timeout(Duration.ofSeconds(timeoutInSec))
104+
.header("Accept-Encoding", "gzip")
105+
.header("Authorization", token.get())
106+
.build();
86107

87-
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
108+
// Send the request and get the response
109+
HttpResponse<InputStream> httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
110+
if (httpResponse.statusCode() != HttpURLConnection.HTTP_OK) {
88111
return Optional.empty();
89112
}
90-
InputStream inputStream = connection.getInputStream();
113+
114+
InputStream inputStream = httpResponse.body();
91115
// Check if the response is gzipped
92-
if ("gzip".equalsIgnoreCase(connection.getContentEncoding())) {
116+
if ("gzip".equalsIgnoreCase(httpResponse.headers().firstValue("Content-Encoding").orElse(""))) {
93117
inputStream = new GZIPInputStream(inputStream);
94118
}
95119

96-
// Read the response :
120+
// Read the response
97121
APIListsResponse res = gson.fromJson(new InputStreamReader(inputStream), APIListsResponse.class);
98122
return Optional.of(res);
99123
} catch (Exception e) {

0 commit comments

Comments
 (0)