Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(appsync): bound the Lambda authorizer result cache (#4345)

Niels Kootstra6ed4726f52a7
diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java b/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java
index 4912116ec3..1a74f5c624 100644
--- a/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/appsync/AppSyncService.java
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker;
import io.github.hectorvent.floci.services.appsync.graphql.SchemaRegistry;
+import io.github.hectorvent.floci.services.appsync.graphql.auth.LambdaAuthorizerCache;
import io.github.hectorvent.floci.services.appsync.model.*;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
@@ -57,6 +58,7 @@ public class AppSyncService {
private final ObjectMapper objectMapper;
private final Clock clock;
private final String baseUrl;
+ private final LambdaAuthorizerCache lambdaAuthorizerCache;
@Inject
public AppSyncService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver,
@@ -64,7 +66,8 @@ public class AppSyncService {
Instance<RequestContext> requestContextInstance, ObjectMapper objectMapper,
AccountAwareStorageBackend<SchemaCreationStatus> schemaStatusStore,
AccountAwareStorageBackend<String> schemaStore,
- Clock clock) {
+ Clock clock,
+ LambdaAuthorizerCache lambdaAuthorizerCache) {
this.apiStore = storageFactory.create("appsync", "appsync-apis.json", new TypeReference<>() {});
this.schemaStore = schemaStore;
this.schemaStatusStore = schemaStatusStore;
@@ -84,6 +87,7 @@ public class AppSyncService {
this.objectMapper = objectMapper;
this.clock = clock;
this.baseUrl = trimTrailingSlash(config.effectiveBaseUrl());
+ this.lambdaAuthorizerCache = lambdaAuthorizerCache;
}
// ──────────────────────────── GraphQL API ────────────────────────────
@@ -228,6 +232,7 @@ public class AppSyncService {
deleteApiKeysForApi(apiId);
deleteChannelNamespacesForApi(apiId);
deleteDomainAssociationsForApi(apiId);
+ lambdaAuthorizerCache.evictApi(apiId);
LOG.infov("Deleted GraphQL API {0}", apiId);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCache.java b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCache.java
index 41a5a65b68..de7e1b67df 100644
--- a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCache.java
+++ b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCache.java
@@ -4,6 +4,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import java.time.Clock;
import java.time.Instant;
+import java.util.Comparator;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@@ -12,6 +13,13 @@ public class LambdaAuthorizerCache {
static final int MAX_CACHEABLE_BYTES = 1_048_576;
+ /**
+ * Caps the number of distinct (API, token) entries held at once. Callers presenting many
+ * distinct authorization tokens would otherwise grow this cache without bound; once the cap
+ * is reached, the entry closest to expiry is evicted to make room for the new one.
+ */
+ static final int MAX_ENTRIES = 1_000;
+
private final ConcurrentHashMap<String, Entry> cache = new ConcurrentHashMap<>();
private final Clock clock;
@@ -35,18 +43,43 @@ public class LambdaAuthorizerCache {
return Optional.of(entry.result);
}
- public void put(String apiId, String token, LambdaAuthorizerResult result, int ttlSeconds) {
+ // Synchronized so the sweep, capacity check, eviction and insert happen as one step;
+ // otherwise concurrent puts all see room (or evict the same entry) and overshoot the cap.
+ public synchronized void put(String apiId, String token, LambdaAuthorizerResult result, int ttlSeconds) {
if (result == null || ttlSeconds <= 0 || result.responseSizeBytes() >= MAX_CACHEABLE_BYTES) {
return;
}
- Instant expiresAt = clock.instant().plusSeconds(ttlSeconds);
- cache.put(key(apiId, token), new Entry(result, expiresAt));
+ Instant now = clock.instant();
+ String key = key(apiId, token);
+ cache.entrySet().removeIf(entry -> !entry.getValue().expiresAt.isAfter(now));
+ if (!cache.containsKey(key) && cache.size() >= MAX_ENTRIES) {
+ evictClosestToExpiry();
+ }
+ cache.put(key, new Entry(result, now.plusSeconds(ttlSeconds)));
+ }
+
+ /**
+ * Drops every cached entry for an API, e.g. when the API itself is deleted.
+ */
+ public void evictApi(String apiId) {
+ String prefix = apiId + "\0";
+ cache.keySet().removeIf(k -> k.startsWith(prefix));
+ }
+
+ private void evictClosestToExpiry() {
+ cache.entrySet().stream()
+ .min(Comparator.comparing(entry -> entry.getValue().expiresAt))
+ .ifPresent(entry -> cache.remove(entry.getKey(), entry.getValue()));
}
static String key(String apiId, String token) {
return apiId + "\0" + token;
}
+ int size() {
+ return cache.size();
+ }
+
private record Entry(LambdaAuthorizerResult result, Instant expiresAt) {
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java
index c2272f103c..05c8584fa4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/appsync/AppSyncServiceTest.java
@@ -10,6 +10,8 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker;
import io.github.hectorvent.floci.services.appsync.graphql.SchemaRegistry;
+import io.github.hectorvent.floci.services.appsync.graphql.auth.LambdaAuthorizerCache;
+import io.github.hectorvent.floci.services.appsync.graphql.auth.LambdaAuthorizerResult;
import io.github.hectorvent.floci.services.appsync.model.ApiKey;
import io.github.hectorvent.floci.services.appsync.model.AuthenticationType;
import io.github.hectorvent.floci.services.appsync.model.GraphqlApi;
@@ -332,7 +334,24 @@ class AppSyncServiceTest {
verify(schemaRegistry, never()).register(any(), any());
}
+ @Test
+ void deleteGraphqlApiEvictsItsLambdaAuthorizerCacheEntries() {
+ GraphqlApi api = service.createGraphqlApi(
+ Map.of("name", "authz", "authenticationType", "AWS_LAMBDA"), "us-east-1");
+ GraphqlApi otherApi = service.createGraphqlApi(
+ Map.of("name", "authz-other", "authenticationType", "AWS_LAMBDA"), "us-east-1");
+ LambdaAuthorizerResult result = new LambdaAuthorizerResult(true, List.of(), Map.of(), 300, 10);
+ lambdaAuthorizerCache.put(api.getApiId(), "tok", result, 300);
+ lambdaAuthorizerCache.put(otherApi.getApiId(), "tok", result, 300);
+
+ service.deleteGraphqlApi(api.getApiId());
+
+ assertTrue(lambdaAuthorizerCache.get(api.getApiId(), "tok").isEmpty());
+ assertTrue(lambdaAuthorizerCache.get(otherApi.getApiId(), "tok").isPresent());
+ }
+
private SchemaRegistry schemaRegistry;
+ private LambdaAuthorizerCache lambdaAuthorizerCache;
@SuppressWarnings("unchecked")
private AppSyncService newService(Clock clock) {
@@ -362,6 +381,7 @@ class AppSyncServiceTest {
};
Instance<RequestContext> requestContext = mock(Instance.class);
schemaRegistry = mock(SchemaRegistry.class);
+ lambdaAuthorizerCache = new LambdaAuthorizerCache();
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.effectiveBaseUrl()).thenReturn(baseUrl);
return new AppSyncService(
@@ -374,7 +394,8 @@ class AppSyncServiceTest {
new ObjectMapper(),
AccountAwareStorageBackend.inMemory("000000000000"),
AccountAwareStorageBackend.inMemory("000000000000"),
- clock);
+ clock,
+ lambdaAuthorizerCache);
}
private static final class MutableClock extends Clock {
diff --git a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCacheTest.java b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCacheTest.java
index 2b2fdac565..213bbaf8b7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCacheTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/appsync/graphql/auth/LambdaAuthorizerCacheTest.java
@@ -6,8 +6,13 @@ import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -52,6 +57,84 @@ class LambdaAuthorizerCacheTest {
assertTrue(cache.get("api-1", "tok").isEmpty());
}
+ @Test
+ void distinctTokensNeverExceedCapacity() {
+ Clock clock = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC);
+ LambdaAuthorizerCache cache = new LambdaAuthorizerCache(clock);
+ LambdaAuthorizerResult result = new LambdaAuthorizerResult(true, List.of(), Map.of(), 300, 10);
+
+ for (int i = 0; i < LambdaAuthorizerCache.MAX_ENTRIES + 50; i++) {
+ cache.put("api-1", "tok-" + i, result, 300);
+ }
+
+ assertTrue(cache.size() <= LambdaAuthorizerCache.MAX_ENTRIES,
+ "cache grew to " + cache.size() + " entries, cap is " + LambdaAuthorizerCache.MAX_ENTRIES);
+ }
+
+ @Test
+ void concurrentDistinctTokensNeverExceedCapacity() throws Exception {
+ Clock clock = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC);
+ LambdaAuthorizerCache cache = new LambdaAuthorizerCache(clock);
+ LambdaAuthorizerResult result = new LambdaAuthorizerResult(true, List.of(), Map.of(), 300, 10);
+ int threads = 16;
+ int putsPerThread = 500;
+ CountDownLatch start = new CountDownLatch(1);
+ ExecutorService pool = Executors.newFixedThreadPool(threads);
+ try {
+ List<Future<?>> puts = new ArrayList<>();
+ for (int t = 0; t < threads; t++) {
+ int thread = t;
+ puts.add(pool.submit(() -> {
+ start.await();
+ for (int i = 0; i < putsPerThread; i++) {
+ cache.put("api-1", "tok-" + thread + "-" + i, result, 300);
+ }
+ return null;
+ }));
+ }
+ start.countDown();
+ for (Future<?> put : puts) {
+ put.get();
+ }
+ } finally {
+ pool.shutdownNow();
+ }
+
+ assertTrue(cache.size() <= LambdaAuthorizerCache.MAX_ENTRIES,
+ "cache grew to " + cache.size() + " entries, cap is " + LambdaAuthorizerCache.MAX_ENTRIES);
+ }
+
+ @Test
+ void expiredEntriesAreSweptOnPutEvenWithoutBeingRead() {
+ MutableClock clock = new MutableClock(Instant.parse("2026-01-01T00:00:00Z"));
+ LambdaAuthorizerCache cache = new LambdaAuthorizerCache(clock);
+ LambdaAuthorizerResult result = new LambdaAuthorizerResult(true, List.of(), Map.of(), 5, 10);
+ for (int i = 0; i < 10; i++) {
+ cache.put("api-1", "tok-" + i, result, 5);
+ }
+ assertEquals(10, cache.size());
+
+ clock.advance(Duration.ofSeconds(6));
+ cache.put("api-1", "tok-new", result, 5);
+
+ // The 10 short-lived entries expired without ever being read again; a put still sweeps
+ // them, so only the freshly-added entry remains.
+ assertEquals(1, cache.size());
+ }
+
+ @Test
+ void evictApiRemovesOnlyThatApisEntries() {
+ LambdaAuthorizerCache cache = new LambdaAuthorizerCache();
+ LambdaAuthorizerResult result = new LambdaAuthorizerResult(true, List.of(), Map.of(), 300, 10);
+ cache.put("api-1", "tok", result, 300);
+ cache.put("api-2", "tok", result, 300);
+
+ cache.evictApi("api-1");
+
+ assertTrue(cache.get("api-1", "tok").isEmpty());
+ assertTrue(cache.get("api-2", "tok").isPresent());
+ }
+
private static final class MutableClock extends Clock {
private Instant instant;
private MutableClock(Instant instant) {