Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public static void setRateLimitGroup(String groupId) {

ContextObject currentContext = Context.get();
if (currentContext == null) {
if (Context.isBypassed()) {
return;
}
logger.warn("setRateLimitGroup(...) was called without a context. Make sure to call setRateLimitGroup(...) within an HTTP request.");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static Res report(ContextObject newContext) {
PendingHostnamesStore.clear();

if (config.isIpBypassed(newContext.getRemoteAddress())) {
Context.markBypassed();
return null; // do not set context when the IP address is bypassed (zen = off)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public final class Context {
private Context() {}

static final ThreadLocal<ContextObject> threadLocalContext = new ThreadLocal<>();
// Bypassed requests have no context, so this flag tells them apart from code running outside a request.
static final ThreadLocal<Boolean> threadLocalBypassed = ThreadLocal.withInitial(() -> false);
public static ContextObject get() {
return threadLocalContext.get();
}
Expand All @@ -14,5 +16,12 @@ public static void set(ContextObject contextObject) {
}
public static void reset() {
threadLocalContext.remove();
threadLocalBypassed.remove();
}
public static void markBypassed() {
threadLocalBypassed.set(true);
}
public static boolean isBypassed() {
return threadLocalBypassed.get();
}
}
12 changes: 11 additions & 1 deletion agent_api/src/test/java/SetRateLimitGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void clean() {

@AfterEach
public void tearDown() throws SQLException {
Context.set(null);
Context.reset();
}

@Test
Expand All @@ -58,6 +58,16 @@ public void testWithNoContext(StdOut out) throws SQLException, IOException {
assertTrue(out.capturedString().contains("setRateLimitGroup(...) was called without a context. Make sure to call setRateLimitGroup(...) within an HTTP request."));
}

@Test
@StdIo
public void testWithBypassedRequestDoesNotWarn(StdOut out) {
Context.markBypassed();

SetRateLimitGroup.setRateLimitGroup("id");

assertFalse(out.capturedString().contains("was called without a context"));
}

@Test
@StdIo
public void testEmptyGroupId(StdOut out) throws SQLException {
Expand Down
17 changes: 17 additions & 0 deletions agent_api/src/test/java/collectors/WebRequestCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ void testReport_contextSet() {

}

@Test
void testReport_bypassedIpIsMarkedUntilNextRequest() {
List<String> bypassedIps = List.of("192.168.1.1");
ServiceConfigStore.updateFromAPIResponse(new APIResponse(
true, "", getUnixTimeMS(), List.of(), List.of(), bypassedIps, false, null, true, false, List.of(), 0
));

WebRequestCollector.report(contextObject);
assertNull(Context.get());
assertTrue(Context.isBypassed());

contextObject.setIp("10.0.0.1");
WebRequestCollector.report(contextObject);
assertNotNull(Context.get());
assertFalse(Context.isBypassed());
}

@Test
void testReport_ipNotAllowed() {
setEmptyConfigWithEndpointList(List.of(new Endpoint(
Expand Down
Loading