fix(appsync): use ApiKey.id as the API key value (#2645)

CreateApiKey returned a short random id and kept the value the execute
endpoint validates in a non-model apiKey field, which the AWS CLI and SDKs
drop. On AWS the id itself is the da2- key value sent as x-api-key, so make
id that value, validate against it, and drop the extra field. Keys persisted
by earlier builds keep their short id, stay listable and deletable, and never
authenticate (as before).
This commit is contained in:
武田隆志 authored and GitHub committed 2026-08-28 09:59:42 -04:00
1 parent 3b0e31b888
commit 29325006bb
9 files changed
+70 -42

No files matched your search

@@ -265,7 +265,7 @@ class AppSyncAuthIntegrationTest {
.post("/v1/apis/" + apiId + "/apikeys")
.then()
.statusCode(200)
.extract().path("apiKey.apiKey");
.extract().path("apiKey.id");
}
private static void startSchema(String apiId, String definition) {
@@ -356,7 +356,7 @@ class AppSyncExecutionIntegrationTest {
.post("/v1/apis/" + apiId + "/apikeys")
.then()
.statusCode(200)
.extract().path("apiKey.apiKey");
.extract().path("apiKey.id");
}
private static void startSchema(String apiId, String definition) {
@@ -229,8 +229,8 @@ class AppSyncIntegrationTest {
.post("/v1/apis/" + apiId + "/apikeys")
.then()
.statusCode(200)
.body("apiKey.id", notNullValue())
.body("apiKey.apiKey", startsWith("da2-"))
.body("apiKey.id", matchesPattern("da2-[a-z0-9]{26}"))
.body("apiKey", not(hasKey("apiKey")))
.body("apiKey.description", equalTo("test-key"))
.extract().path("apiKey.id");
}
@@ -43,6 +43,7 @@ class AppSyncServiceTest {
private static final Instant NOW = Instant.parse("2026-01-01T00:00:00Z");
private AppSyncService service;
private AccountAwareStorageBackend<ApiKey> apiKeyStoreOverride;
@BeforeEach
void setUp() {
@@ -53,7 +54,36 @@ class AppSyncServiceTest {
void validateApiKeyLooksUpByValue() {
GraphqlApi api = service.createGraphqlApi(Map.of("name", "a", "authenticationType", "API_KEY"), "us-east-1");
ApiKey created = service.createApiKey(api.getApiId(), Map.of());
assertTrue(service.validateApiKey(api.getApiId(), created.getApiKey()).isPresent());
assertTrue(service.validateApiKey(api.getApiId(), created.getId()).isPresent());
}
@Test
void createApiKeyIdIsTheUsableKeyValue() {
GraphqlApi api = service.createGraphqlApi(Map.of("name", "fmt", "authenticationType", "API_KEY"), "us-east-1");
ApiKey created = service.createApiKey(api.getApiId(), Map.of());
assertTrue(created.getId().matches("da2-[a-z0-9]{26}"), created.getId());
assertTrue(service.validateApiKey(api.getApiId(), created.getId()).isPresent());
assertTrue(service.validateApiKey(api.getApiId(), "da2-" + "x".repeat(26)).isEmpty());
assertEquals(created.getId(), service.getApiKey(api.getApiId(), created.getId()).getId());
}
@Test
void legacyShortApiKeyIdIsListedButNeverAuthenticates() {
// Keys persisted by builds before ApiKey.id became the key value have a 7-character id.
AccountAwareStorageBackend<ApiKey> keyStore = AccountAwareStorageBackend.inMemory("000000000000");
apiKeyStoreOverride = keyStore;
AppSyncService svc = newService(Clock.fixed(NOW, ZoneOffset.UTC));
GraphqlApi api = svc.createGraphqlApi(Map.of("name", "legacy", "authenticationType", "API_KEY"), "us-east-1");
ApiKey legacy = new ApiKey();
legacy.setId("ad6c9b6");
legacy.setApiId(api.getApiId());
legacy.setExpires(NOW.getEpochSecond() + Duration.ofDays(7).getSeconds());
keyStore.put(api.getApiId() + "::ad6c9b6", legacy);
assertEquals(1, svc.listApiKeys(api.getApiId(), null, null).items().size());
assertTrue(svc.validateApiKey(api.getApiId(), "ad6c9b6").isEmpty());
svc.deleteApiKey(api.getApiId(), "ad6c9b6");
assertEquals(0, svc.listApiKeys(api.getApiId(), null, null).items().size());
}
@Test
@@ -63,9 +93,9 @@ class AppSyncServiceTest {
GraphqlApi api = svc.createGraphqlApi(Map.of("name", "b", "authenticationType", "API_KEY"), "us-east-1");
long expires = NOW.getEpochSecond() + Duration.ofDays(1).getSeconds();
ApiKey created = svc.createApiKey(api.getApiId(), Map.of("expires", expires));
assertTrue(svc.validateApiKey(api.getApiId(), created.getApiKey()).isPresent());
assertTrue(svc.validateApiKey(api.getApiId(), created.getId()).isPresent());
clock.set(NOW.plus(Duration.ofDays(1)));
assertTrue(svc.validateApiKey(api.getApiId(), created.getApiKey()).isEmpty());
assertTrue(svc.validateApiKey(api.getApiId(), created.getId()).isEmpty());
}
@Test
@@ -104,7 +134,7 @@ class AppSyncServiceTest {
GraphqlApi a = service.createGraphqlApi(Map.of("name", "c", "authenticationType", "API_KEY"), "us-east-1");
GraphqlApi b = service.createGraphqlApi(Map.of("name", "d", "authenticationType", "API_KEY"), "us-east-1");
ApiKey created = service.createApiKey(a.getApiId(), Map.of());
assertTrue(service.validateApiKey(b.getApiId(), created.getApiKey()).isEmpty());
assertTrue(service.validateApiKey(b.getApiId(), created.getId()).isEmpty());
}
@Test
@@ -307,6 +337,9 @@ class AppSyncServiceTest {
if ("appsync-apis.json".equals(fileName)) {
return (AccountAwareStorageBackend<V>) apiStore;
}
if ("appsync-apikeys.json".equals(fileName) && apiKeyStoreOverride != null) {
return (AccountAwareStorageBackend<V>) apiKeyStoreOverride;
}
return AccountAwareStorageBackend.inMemory("000000000000");
}
};
@@ -23,12 +23,12 @@ class ApiKeyAuthValidatorTest {
@Test
void validKeyReturnsStoredKey() {
ApiKey stored = new ApiKey();
stored.setApiKey("da2-abc");
stored.setId("da2-abc");
when(appSyncService.validateApiKey("api-1", "da2-abc")).thenReturn(Optional.of(stored));
ApiKey result = new ApiKeyAuthValidator(appSyncService).validate("api-1", "da2-abc");
assertEquals("da2-abc", result.getApiKey());
assertEquals("da2-abc", result.getId());
}
@Test