Skip to content

Commit 2f34f4d

Browse files
committed
Use locks for isIpBlocked & isBlockedUserAgent
1 parent 9173845 commit 2f34f4d

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

agent_api/src/main/java/dev/aikido/agent_api/collectors/WebRequestCollector.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.aikido.agent_api.context.Context;
55
import dev.aikido.agent_api.context.ContextObject;
66
import dev.aikido.agent_api.context.RouteMetadata;
7+
import dev.aikido.agent_api.storage.ServiceConfigStore;
78
import dev.aikido.agent_api.storage.ServiceConfiguration;
89
import dev.aikido.agent_api.storage.statistics.StatisticsStore;
910

@@ -43,11 +44,11 @@ public static Res report(ContextObject newContext) {
4344
if (endpointAllowlistRes != null)
4445
return endpointAllowlistRes;
4546

46-
Res blockedIpsRes = checkBlockedIps(newContext.getRemoteAddress(), config);
47+
Res blockedIpsRes = checkBlockedIps(newContext.getRemoteAddress());
4748
if (blockedIpsRes != null)
4849
return blockedIpsRes;
4950

50-
return checkBlockedUserAgents(newContext.getHeader("user-agent"), config);
51+
return checkBlockedUserAgents(newContext.getHeader("user-agent"));
5152
}
5253

5354
private static Res checkEndpointAllowlist(RouteMetadata routeMetadata, String remoteAddress, ServiceConfiguration config) {
@@ -60,8 +61,8 @@ private static Res checkEndpointAllowlist(RouteMetadata routeMetadata, String re
6061
return null; // not blocked
6162
}
6263

63-
private static Res checkBlockedIps(String remoteAddress, ServiceConfiguration config) {
64-
ServiceConfiguration.BlockedResult ipBlocked = config.isIpBlocked(remoteAddress);
64+
private static Res checkBlockedIps(String remoteAddress) {
65+
ServiceConfiguration.BlockedResult ipBlocked = ServiceConfigStore.isIpBlocked(remoteAddress);
6566
if (ipBlocked.blocked()) {
6667
String msg = "Your IP address is blocked. Reason: " + ipBlocked.description();
6768
msg += " (Your IP: " + remoteAddress + ")";
@@ -70,11 +71,11 @@ private static Res checkBlockedIps(String remoteAddress, ServiceConfiguration co
7071
return null; // not blocked
7172
}
7273

73-
private static Res checkBlockedUserAgents(String userAgent, ServiceConfiguration config) {
74+
private static Res checkBlockedUserAgents(String userAgent) {
7475
if (userAgent == null || userAgent.isEmpty()) {
7576
return null; // not blocked
7677
}
77-
if (config.isBlockedUserAgent(userAgent)) {
78+
if (ServiceConfigStore.isBlockedUserAgent(userAgent)) {
7879
String msg = "You are not allowed to access this resource because you have been identified as a bot.";
7980
return new Res(msg, 403);
8081
}

agent_api/src/main/java/dev/aikido/agent_api/storage/ServiceConfigStore.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import dev.aikido.agent_api.helpers.logging.LogManager;
66
import dev.aikido.agent_api.helpers.logging.Logger;
77

8-
import java.util.Optional;
98
import java.util.concurrent.locks.ReentrantReadWriteLock;
109

1110
public final class ServiceConfigStore {
@@ -24,6 +23,24 @@ public static ServiceConfiguration getConfig() {
2423
}
2524
}
2625

26+
public static ServiceConfiguration.BlockedResult isIpBlocked(String ip) {
27+
mutex.readLock().lock();
28+
try {
29+
return config.isIpBlocked(ip);
30+
} finally {
31+
mutex.readLock().unlock();
32+
}
33+
}
34+
35+
public static boolean isBlockedUserAgent(String userAgent) {
36+
mutex.readLock().lock();
37+
try {
38+
return config.isBlockedUserAgent(userAgent);
39+
} finally {
40+
mutex.readLock().unlock();
41+
}
42+
}
43+
2744
public static void updateFromAPIResponse(APIResponse apiResponse) {
2845
mutex.writeLock().lock();
2946
try {

0 commit comments

Comments
 (0)