17
17
import java .time .Duration ;
18
18
import java .util .Optional ;
19
19
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 ;
20
25
21
26
public class ReportingApiHTTP extends ReportingApi {
22
27
private final Logger logger = LogManager .getLogger (ReportingApiHTTP .class );
@@ -34,6 +39,7 @@ public Optional<APIResponse> fetchNewConfig() {
34
39
try {
35
40
HttpClient httpClient = HttpClient .newBuilder ()
36
41
.connectTimeout (Duration .ofSeconds (timeoutInSec ))
42
+ .sslContext (createDefaultSSLContext ())
37
43
.build ();
38
44
39
45
URI uri = URI .create (reportingUrl + "api/runtime/config" );
@@ -54,6 +60,7 @@ public Optional<APIResponse> report(APIEvent event) {
54
60
try {
55
61
HttpClient httpClient = HttpClient .newBuilder ()
56
62
.connectTimeout (Duration .ofSeconds (timeoutInSec ))
63
+ .sslContext (createDefaultSSLContext ())
57
64
.build ();
58
65
59
66
URI uri = URI .create (reportingUrl + "api/runtime/events" );
@@ -75,25 +82,32 @@ public Optional<APIListsResponse> fetchBlockedLists() {
75
82
return Optional .empty ();
76
83
}
77
84
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 ( );
82
89
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 ();
86
97
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 ) {
88
101
return Optional .empty ();
89
102
}
90
- InputStream inputStream = connection .getInputStream ();
103
+
104
+ InputStream inputStream = httpResponse .body ();
91
105
// Check if the response is gzipped
92
- if ("gzip" .equalsIgnoreCase (connection . getContentEncoding ( ))) {
106
+ if ("gzip" .equalsIgnoreCase (httpResponse . headers (). firstValue ( "Content-Encoding" ). orElse ( "" ))) {
93
107
inputStream = new GZIPInputStream (inputStream );
94
108
}
95
109
96
- // Read the response :
110
+ // Read the response
97
111
APIListsResponse res = gson .fromJson (new InputStreamReader (inputStream ), APIListsResponse .class );
98
112
return Optional .of (res );
99
113
} catch (Exception e ) {
0 commit comments