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

* fix(appsync): bound the Lambda authorizer result cache

* fix: keep the authorizer cache cap under concurrent inserts
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-24 20:55:27 -04:00
1 parent 09dcbabe2b
commit 6ed4726f52
4 files changed
+147 -5

No files matched your search

@@ -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 {
@@ -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) {