diff --git a/agent_api/src/main/java/dev/aikido/agent_api/background/BackgroundProcess.java b/agent_api/src/main/java/dev/aikido/agent_api/background/BackgroundProcess.java index a98bb33a..f3ddbc8c 100644 --- a/agent_api/src/main/java/dev/aikido/agent_api/background/BackgroundProcess.java +++ b/agent_api/src/main/java/dev/aikido/agent_api/background/BackgroundProcess.java @@ -21,7 +21,8 @@ import static dev.aikido.agent_api.helpers.env.Endpoints.getAikidoAPIEndpoint; public class BackgroundProcess extends Thread { - private static final int API_TIMEOUT = 10; // 10 seconds + private static final int API_TIMEOUT = 30; // GET config and lists + private static final int EVENTS_API_TIMEOUT = 60; // POST events private static final int POLLING_INTERVAL_SECONDS = 60; // Check for realtime config changes every 1 minute private static final int FIRST_HEARTBEAT_INTERVAL_SECONDS = 30; private static final int SECOND_HEARTBEAT_INTERVAL_SECONDS = 120; @@ -41,7 +42,7 @@ public void run() { } logger.trace("Starting agent thread: %s", Thread.currentThread()); - ReportingApiHTTP api = new ReportingApiHTTP(getAikidoAPIEndpoint(token), API_TIMEOUT, token); + ReportingApiHTTP api = new ReportingApiHTTP(getAikidoAPIEndpoint(token), API_TIMEOUT, EVENTS_API_TIMEOUT, token); RealtimeAPI realtimeApi = new RealtimeAPI(token); // make api calls on start diff --git a/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/RealtimeAPI.java b/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/RealtimeAPI.java index 118a52f7..7a42d78e 100644 --- a/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/RealtimeAPI.java +++ b/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/RealtimeAPI.java @@ -17,7 +17,7 @@ public class RealtimeAPI { private static final Logger logger = LogManager.getLogger(RealtimeAPI.class); - private static final int timeoutInSec = 3; // 3 sec timeout for requests to the realtime endpoint + private static final int timeoutInSec = 30; private final String endpoint; private final Token token; private HttpClient httpClient; @@ -63,6 +63,7 @@ public Optional toConfigResponse(HttpResponse res) { private static HttpRequest createConfigRequest(String token, URI uri) { HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() .uri(uri) // Change to your target URL + .timeout(Duration.ofSeconds(timeoutInSec)) .header("Content-Type", "application/json") // Set Content-Type header .header("Authorization", token); // Set Authorization header AgentHeaders.get().forEach(requestBuilder::header); diff --git a/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/api/ReportingApiHTTP.java b/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/api/ReportingApiHTTP.java index 3a0be419..5404ece7 100644 --- a/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/api/ReportingApiHTTP.java +++ b/agent_api/src/main/java/dev/aikido/agent_api/background/cloud/api/ReportingApiHTTP.java @@ -26,9 +26,11 @@ public class ReportingApiHTTP extends ReportingApi { private final Token token; private final Gson gson = new Gson(); private HttpClient httpClient; - public ReportingApiHTTP(String reportingUrl, int timeoutInSec, Token token) { + private final int eventsTimeoutInSec; + public ReportingApiHTTP(String reportingUrl, int timeoutInSec, int eventsTimeoutInSec, Token token) { // Reporting URL should end with trailing slash for now. super(timeoutInSec); + this.eventsTimeoutInSec = eventsTimeoutInSec; this.reportingUrl = reportingUrl; this.token = token; } @@ -125,9 +127,10 @@ public APIResponse toApiResponse(HttpResponse res) { return getUnsuccessfulAPIResponse("unknown_error"); } private HttpRequest createHttpRequest(Optional event, URI uri) { + int requestTimeoutInSec = event.isPresent() ? eventsTimeoutInSec : timeoutInSec; HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() .uri(uri) // Change to your target URL - .timeout(Duration.ofSeconds(timeoutInSec)) + .timeout(Duration.ofSeconds(requestTimeoutInSec)) .header("Content-Type", "application/json") // Set Content-Type header .header("Authorization", token.get()); // Set Authorization header AgentHeaders.get().forEach(requestBuilder::header); diff --git a/agent_api/src/test/java/background/cloud/ReportingAPIResourceTest.java b/agent_api/src/test/java/background/cloud/ReportingAPIResourceTest.java index a2dd4595..c3a9d505 100644 --- a/agent_api/src/test/java/background/cloud/ReportingAPIResourceTest.java +++ b/agent_api/src/test/java/background/cloud/ReportingAPIResourceTest.java @@ -36,7 +36,7 @@ public void testReusesConnectionsAcrossCloudRequests() throws Exception { }); server.start(); try { - ReportingApiHTTP api = new ReportingApiHTTP("http://127.0.0.1:" + server.getAddress().getPort() + "/", 2, new Token("token")); + ReportingApiHTTP api = new ReportingApiHTTP("http://127.0.0.1:" + server.getAddress().getPort() + "/", 2, 2, new Token("token")); record Event(String type) implements APIEvent {} for (int i = 0; i < 10; i++) { assertTrue(api.fetchNewConfig().isPresent()); @@ -80,7 +80,7 @@ private void assertClosesUnfinishedResponse(String status, String headers, Strin return socket.getInputStream().read(); } }); - ReportingApiHTTP api = new ReportingApiHTTP("http://localhost:" + server.getLocalPort() + "/", 1, new Token("token")); + ReportingApiHTTP api = new ReportingApiHTTP("http://localhost:" + server.getLocalPort() + "/", 1, 1, new Token("token")); assertTrue(api.fetchBlockedLists().isEmpty()); assertEquals(-1, disconnected.get(4, TimeUnit.SECONDS)); } finally { @@ -106,7 +106,7 @@ public void testReadsGzippedLists() throws Exception { }); server.start(); try { - ReportingApiHTTP api = new ReportingApiHTTP("http://127.0.0.1:" + server.getAddress().getPort() + "/", 2, new Token("token")); + ReportingApiHTTP api = new ReportingApiHTTP("http://127.0.0.1:" + server.getAddress().getPort() + "/", 2, 2, new Token("token")); assertEquals("bot", api.fetchBlockedLists().orElseThrow().blockedUserAgents()); } finally { server.stop(0); diff --git a/agent_api/src/test/java/background/cloud/ReportingAPITest.java b/agent_api/src/test/java/background/cloud/ReportingAPITest.java index e00cc43e..3bd3ed64 100644 --- a/agent_api/src/test/java/background/cloud/ReportingAPITest.java +++ b/agent_api/src/test/java/background/cloud/ReportingAPITest.java @@ -25,12 +25,12 @@ public class ReportingAPITest { ReportingApiHTTP api; @BeforeEach public void setup() { - api = new ReportingApiHTTP("http://localhost:5000/", 2, new Token("token")); + api = new ReportingApiHTTP("http://localhost:5000/", 2, 2, new Token("token")); } @Test public void testTimeoutValid() { - api = new ReportingApiHTTP("http://localhost:5000/delayed/2/", 3, new Token("token")); // Allowed delay + api = new ReportingApiHTTP("http://localhost:5000/delayed/2/", 3, 3, new Token("token")); // Allowed delay Optional res = api.fetchNewConfig(); assertTrue(res.isPresent()); assertTrue(res.get().block()); @@ -38,7 +38,7 @@ public void testTimeoutValid() { } @Test public void testTimeoutInvalid() { - api = new ReportingApiHTTP("http://localhost:5000/delayed/4/", 3, new Token("token")); // Allowed delay + api = new ReportingApiHTTP("http://localhost:5000/delayed/4/", 3, 3, new Token("token")); // Allowed delay Optional res = api.fetchNewConfig(); assertFalse(res.isPresent()); } @@ -53,7 +53,7 @@ public void testFetchNewConfig() { @Test @StdIo public void testFetchNewConfigInvalidEndpoint(StdOut out) { - this.api = new ReportingApiHTTP("http://unknown.app.here:1234/", 2, new Token("token")); + this.api = new ReportingApiHTTP("http://unknown.app.here:1234/", 2, 2, new Token("token")); Optional res = api.fetchNewConfig(); assertEquals(Optional.empty(), res); assertTrue( @@ -65,7 +65,7 @@ public void testFetchNewConfigInvalidEndpoint(StdOut out) { @Test @StdIo public void testListsResponseWithWrongEndpoint(StdOut out) { - this.api = new ReportingApiHTTP("http://unknown.app.here:1234/", 2, new Token("token")); + this.api = new ReportingApiHTTP("http://unknown.app.here:1234/", 2, 2, new Token("token")); Optional res = api.fetchBlockedLists(); assertEquals(Optional.empty(), res); assertTrue( @@ -114,7 +114,7 @@ public void testSendsAgentHeaders() throws Exception { }); server.start(); try { - api = new ReportingApiHTTP("http://localhost:" + server.getAddress().getPort() + "/", 2, new Token("token")); + api = new ReportingApiHTTP("http://localhost:" + server.getAddress().getPort() + "/", 2, 2, new Token("token")); api.fetchNewConfig(); api.fetchBlockedLists(); } finally {