feat(appsync): Phase 7 — GraphQL execute authentication (#2380)

* feat(appsync): register GraphQL execute controller in the service catalog

Unsigned POST /v1/apis/{apiId}/graphql was not a registered AppSync resource, so the data-plane path could be claimed by other catch-all routes.

* feat(appsync): default API key expiry and reject duplicate auth providers

CreateApiKey now expires in seven days when omitted, caps expiry at 365 days, and looks keys up by value. Create/UpdateGraphqlApi reject duplicate API_KEY, IAM, and Lambda providers.

* feat(appsync): authenticate GraphQL execute requests

Classify credentials by header shape and require a configured provider before GraphQL runs. Request failures return HTTP 401 UnauthorizedException on the data-plane envelope.

* feat(appsync): enforce SDL field authorization on GraphQL execute

Wrap registered fields with AuthorizationDataFetcher so directive mismatch, Cognito groups, IAM field DENY, and Lambda deniedFields stay HTTP 200 with null data and errors[].

* test(appsync): execute GraphQL over HTTP with an SDK-created API key

Cover data-plane success with x-api-key and 401 without credentials after management CreateGraphqlApi/CreateApiKey.

* fix(appsync): match missing-header 401 text and SigV4 precedence

AWS returns Missing authorization header when credentials are absent, evaluates auth before schema, and routes SigV4 Authorization ahead of x-api-key without falling back to the API key.

* fix(appsync): emit field-level Unauthorized GraphQL errors

AWS AppSync returns HTTP 200 with errorType Unauthorized and
Not Authorized to access {field} on type {type}, not UnauthorizedException.

* fix(appsync): omit sourceIp from OIDC identity

Official AppSync OIDC identity is only sub, issuer, and claims.

* fix(appsync): round API key expiry down to the hour

CreateApiKey and UpdateApiKey store expires as epoch seconds rounded
down to the nearest hour, matching the AWS API Reference.

* fix(appsync): match duplicate auth provider BadRequestException text

AWS names the 1-based additional provider index and the repeated type.

* fix(appsync): reject API key expiry outside 1-365 days

CreateApiKey and UpdateApiKey now raise ApiKeyValidityOutOfBoundsException
when expires is not between 1 and 365 days from now.

* fix(appsync): set ApiKey deletes to expires plus 60 days

AWS keeps expired da2 keys for 60 days and returns deletes as epoch
seconds rounded down to the hour.
This commit is contained in:
AgustinBertagna authored and GitHub committed 2026-08-19 11:46:11 -04:00
1 parent 74da157681
commit 3f12428d97
46 files changed
+3564 -64

No files matched your search

@@ -117,10 +117,11 @@ class AppSyncTest {
// ── API Keys ────────────────────────────────────────────────────────
private static String keyId;
private static String apiKeyValue;
@Test
@Order(20)
void createApiKey() {
void createApiKey() throws Exception {
long expiresEpoch = Instant.parse("2027-01-01T00:00:00Z").getEpochSecond();
CreateApiKeyResponse resp = client.createApiKey(CreateApiKeyRequest.builder()
.apiId(apiId)
@@ -132,6 +133,9 @@ class AppSyncTest {
keyId = resp.apiKey().id();
assertThat(keyId).isNotBlank();
assertThat(resp.apiKey().description()).isEqualTo("sdk-test-key");
// AWS SDK v2 ApiKey has no apiKey() accessor; Floci returns the da2- secret on the wire.
apiKeyValue = fetchApiKeyValue(apiId, keyId);
assertThat(apiKeyValue).startsWith("da2-");
}
@Test
@@ -173,6 +177,50 @@ class AppSyncTest {
assertThat(resp).isNotNull();
}
@Test
@Order(24)
void httpExecuteWithApiKeyReturns200() throws Exception {
String url = TestFixtures.endpoint() + "/v1/apis/" + apiId + "/graphql";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("x-api-key", apiKeyValue)
.POST(HttpRequest.BodyPublishers.ofString("{\"query\":\"{ hello }\"}"))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
assertThat(resp.statusCode()).isEqualTo(200);
@SuppressWarnings("unchecked")
Map<String, Object> body = mapper.readValue(resp.body(), Map.class);
assertThat(body).containsKey("data");
assertThat(body.get("errors")).isNull();
}
@Test
@Order(25)
void httpExecuteWithoutApiKeyReturns401() throws Exception {
String url = TestFixtures.endpoint() + "/v1/apis/" + apiId + "/graphql";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"query\":\"{ hello }\"}"))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
assertThat(resp.statusCode()).isEqualTo(401);
assertThat(resp.headers().firstValue("x-amzn-errortype").orElse(""))
.contains("UnauthorizedException");
@SuppressWarnings("unchecked")
Map<String, Object> body = mapper.readValue(resp.body(), Map.class);
@SuppressWarnings("unchecked")
List<Map<String, Object>> errors = (List<Map<String, Object>>) body.get("errors");
assertThat(errors).isNotEmpty();
assertThat(errors.get(0).get("errorType")).isEqualTo("UnauthorizedException");
assertThat(errors.get(0).get("message")).isEqualTo("Missing authorization header");
assertThat(body.get("data")).isNull();
assertThat(body.get("__type")).isNull();
}
// ── Data Sources ────────────────────────────────────────────────────
@Test
@@ -1265,4 +1313,24 @@ class AppSyncTest {
assertThat(location.get("column")).isNotNull();
assertThat(location.get("span")).isEqualTo(-1);
}
@SuppressWarnings("unchecked")
private static String fetchApiKeyValue(String apiId, String keyId) throws Exception {
String url = TestFixtures.endpoint() + "/v1/apis/" + apiId + "/apikeys";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/appsync/aws4_request")
.GET()
.build();
HttpResponse<String> resp = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
Map<String, Object> body = mapper.readValue(resp.body(), Map.class);
List<Map<String, Object>> keys = (List<Map<String, Object>>) body.get("apiKeys");
assertThat(keys).isNotEmpty();
for (Map<String, Object> key : keys) {
if (keyId.equals(key.get("id"))) {
return String.valueOf(key.get("apiKey"));
}
}
throw new IllegalStateException("API key not listed for id " + keyId);
}
}