fix(apigateway): map authorizer context in HTTP integrations (#4992)

* fix(apigateway): map authorizer context in HTTP integrations

HTTP backends need verified authorizer context from parameter mappings. Resolve scalar context values and replace mapped headers regardless of casing so client claims cannot remain beside verified values.

* fix(apigateway): drop unresolved context headers

Remove inbound context-mapped headers even when verified context is absent or non-scalar. Cover missing authorizer claims, forged repeated headers, OPTIONS and unresolved method mappings.

* fix(apigateway): preserve original mapping inputs

Keep HTTP proxy header, query and path mapping sources separate from outgoing destinations. Cover both mapping orders with present and missing authorizer claims through both REST execution endpoints.
This commit is contained in:
Abhigya authored and GitHub committed 2026-10-02 23:17:57 -04:00
1 parent b437e1cc32
commit 036f55b196
3 files changed
+439 -15

No files matched your search

+25
View File
@@ -309,6 +309,31 @@ A greedy resource captures only the path segments after its parent. For example,
`/api/v1/auth/signup/{proxy}` forwards the prefix once. Greedy parameter names other
than `proxy` are supported, including in `integration.request.path.*` mappings.
REST `HTTP_PROXY` and `HTTP` integrations also accept `context.*` sources in header, query
and path parameter mappings. Supported request fields are `accountId`, `apiId`, `deploymentId`,
`httpMethod`, `path` (including the stage), `protocol`, `requestId`, `resourceId`, `resourcePath`,
`stage`, `identity.sourceIp` and `identity.userAgent`. `context.requestId` uses the same ID as
the HTTP integration's VTL templates. `context.authorizer.principalId` and
`context.authorizer.<property>` come from the successful authorizer result; authenticated Cognito
claims are available as `context.authorizer.claims.<property>`. String, number and boolean values
are forwarded as strings. Missing values and objects are not mapped. An explicit header mapping
replaces all inbound values of that header, regardless of casing.
For a header mapped from `context.*`, inbound values are also removed when the context value is
missing or is not a scalar. The backend therefore cannot receive a client-supplied value in place
of an absent authorizer claim. An unresolved `method.request.*` mapping retains the existing
HTTP proxy passthrough behavior.
`method.request.*` sources always read the original inbound headers, query parameters and path
parameters. Mapped destinations do not change another mapping's source, regardless of mapping order.
Use alphanumeric or underscore authorizer context keys, as required by the
[AWS mapping contract](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html).
For example, return `userClaims` in the authorizer context and map
`integration.request.header.X-User-Claims` from `context.authorizer.userClaims`. The HTTP header
name can contain hyphens. REST Lambda authorizer result caching is not implemented; setting
`authorizerResultTtlInSeconds` does not currently suppress repeat Lambda invocations.
A backend response body larger than the 10 MB API Gateway payload quota yields `413` with `{"message":"Request Entity Too Large"}`. The same limit applies to HTTP API `HTTP_PROXY` integrations.
Passthrough keeps repeated values repeated, in both directions: `?tag=a&tag=b` reaches the backend as two `tag` parameters rather than one `tag=a,b`, a header sent twice arrives twice, and a backend that returns two `Set-Cookie` headers relays two to the caller. Comma-joining them would not be reversible, since a cookie's `Expires` attribute contains a comma of its own. An explicit `integration.request.header.X` or `integration.request.querystring.X` mapping overwrites, so it replaces any repeated inbound values with the single mapped one.
@@ -468,7 +468,7 @@ public class ApiGatewayExecuteController {
case "AWS" -> invokeAwsIntegration(scope, region, httpMethod, path, stageName,
matched, integration, headers, uriInfo, body, authorizerResult);
case "HTTP_PROXY" -> invokeHttpProxy(scope, apiId, httpMethod, path, stageName,
matched, integration, headers, uriInfo, body, iamIdentity);
matched, integration, headers, uriInfo, body, authorizerResult, iamIdentity);
case "HTTP" -> invokeHttpIntegration(scope, region, apiId, httpMethod, path, proxy, stageName,
matched, integration, headers, uriInfo, body, authorizerResult);
case "MOCK" -> invokeMock(scope, region, httpMethod, path, stageName,
@@ -545,6 +545,7 @@ public class ApiGatewayExecuteController {
String stageName, ApiGatewayResource resource,
Integration integration, HttpHeaders headers,
UriInfo uriInfo, byte[] body,
AuthorizerResult authorizerResult,
ExecuteApiSigV4Authorizer.CallerIdentity iamIdentity) {
String uri = integration.getUri();
if (uri == null || uri.isBlank()) {
@@ -552,6 +553,9 @@ public class ApiGatewayExecuteController {
"No integration URI configured");
}
String requestId = UUID.randomUUID().toString();
Map<String, Object> mappingContext = httpRequestMappingContext(scope, requestId, authorizerResult);
// Two views of the same inbound data. The multi-value maps are what gets forwarded: a
// proxy integration passes the request through, so "?tag=a&tag=b" has to arrive as two
// tag parameters and not as "tag=a,b". The joined single-value maps are only the lookup
@@ -573,26 +577,41 @@ public class ApiGatewayExecuteController {
pathMap.putAll(extractPathParams(resource.getPath(), path));
pathMap.putAll(greedyPathParam(resource.getPath(), path));
// integration.request.{header,querystring,path}.X ← method.request.*, applied here rather
// Mapping sources always read the original request, independent of destination order.
Map<String, String> outHeaders = new LinkedHashMap<>(headerMap);
Map<String, String> outQuery = new LinkedHashMap<>(queryMap);
Map<String, String> outPath = new LinkedHashMap<>(pathMap);
// REST integration.request.* mappings are applied here rather
// than by the v2 RequestParameterMapper: that mapper reads the unrelated v2 syntax
// ("append:header.x" → "$request.header.y") and would silently ignore these REST mappings.
Map<String, String> requestParameters = integration.getRequestParameters();
if (requestParameters != null) {
for (Map.Entry<String, String> param : requestParameters.entrySet()) {
String dest = param.getKey();
String resolved = resolveRequestParameter(param.getValue(), queryMap, pathMap, headerMap);
if (resolved == null) continue;
String source = param.getValue();
String resolved = resolveRequestParameter(source, queryMap, pathMap, headerMap, mappingContext);
boolean isContextHeader = dest.startsWith("integration.request.header.")
&& source != null && source.startsWith("context.");
if (resolved == null && !isContextHeader) {
continue;
}
// An explicit mapping overwrites, so it replaces any repeated inbound values too.
if (dest.startsWith("integration.request.header.")) {
String name = dest.substring("integration.request.header.".length());
headerMap.put(name, resolved);
multiValueHeaders.put(name, List.of(resolved));
// A missing context value must not fall back to a client-supplied trusted header.
outHeaders.keySet().removeIf(existing -> existing.equalsIgnoreCase(name));
multiValueHeaders.keySet().removeIf(existing -> existing.equalsIgnoreCase(name));
if (resolved != null) {
outHeaders.put(name, resolved);
multiValueHeaders.put(name, List.of(resolved));
}
} else if (dest.startsWith("integration.request.querystring.")) {
String name = dest.substring("integration.request.querystring.".length());
queryMap.put(name, resolved);
outQuery.put(name, resolved);
multiValueQuery.put(name, List.of(resolved));
} else if (dest.startsWith("integration.request.path.")) {
pathMap.put(dest.substring("integration.request.path.".length()), resolved);
outPath.put(dest.substring("integration.request.path.".length()), resolved);
}
}
}
@@ -609,10 +628,10 @@ public class ApiGatewayExecuteController {
io.github.hectorvent.floci.services.apigatewayv2.proxy.RequestContext ctx =
new io.github.hectorvent.floci.services.apigatewayv2.proxy.RequestContext(
apiId, stageName, httpMethod, path,
pathMap.getOrDefault("proxy", ""), resource.getPath(),
UUID.randomUUID().toString(),
outPath.getOrDefault("proxy", ""), resource.getPath(),
requestId,
headerMap.getOrDefault("X-Forwarded-For", "127.0.0.1"),
headerMap, queryMap, pathMap, body,
outHeaders, outQuery, outPath, body,
Map.of(), Map.of(),
multiValueHeaders, multiValueQuery);
@@ -847,6 +866,7 @@ public class ApiGatewayExecuteController {
}
String requestId = UUID.randomUUID().toString();
Map<String, Object> mappingContext = httpRequestMappingContext(scope, requestId, authorizerResult);
boolean binaryRequest = isBinaryMediaType(region, apiId,
headers.getHeaderString(HttpHeaders.CONTENT_TYPE));
String bodyStr = templateBody(integration, body, binaryRequest);
@@ -876,14 +896,14 @@ public class ApiGatewayExecuteController {
// Only explicitly mapped parameters reach the backend — the defining difference from
// HTTP_PROXY, which seeds the outgoing request with every inbound header and query param.
Map<String, String> outHeaders = new LinkedHashMap<>();
Map<String, String> outHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
Map<String, String> outQuery = new LinkedHashMap<>();
Map<String, String> outPath = new LinkedHashMap<>(pathMap);
Map<String, String> requestParameters = integration.getRequestParameters();
if (requestParameters != null) {
for (Map.Entry<String, String> param : requestParameters.entrySet()) {
String dest = param.getKey();
String resolved = resolveRequestParameter(param.getValue(), queryMap, pathMap, headerMap);
String resolved = resolveRequestParameter(param.getValue(), queryMap, pathMap, headerMap, mappingContext);
if (resolved == null) continue;
if (dest.startsWith("integration.request.header.")) {
outHeaders.put(dest.substring("integration.request.header.".length()), resolved);
@@ -1626,7 +1646,7 @@ public class ApiGatewayExecuteController {
for (Map.Entry<String, String> param : integrationReqParams.entrySet()) {
String dest = param.getKey(); // integration.request.header.X-Foo or integration.request.querystring.bar
String source = param.getValue(); // method.request.querystring.q or method.request.header.Auth or method.request.path.id
String resolvedValue = resolveRequestParameter(source, queryMap, pathMap, headerMap);
String resolvedValue = resolveRequestParameter(source, queryMap, pathMap, headerMap, Map.of());
if (resolvedValue != null) {
if (dest.startsWith("integration.request.header.")) {
headerMap.put(dest.substring("integration.request.header.".length()), resolvedValue);
@@ -1987,8 +2007,39 @@ public class ApiGatewayExecuteController {
return null;
}
private Map<String, Object> httpRequestMappingContext(GatewayResponseScope scope, String requestId,
AuthorizerResult authorizerResult) {
Map<String, Object> context = new LinkedHashMap<>();
context.put("accountId", regionResolver.getAccountId());
context.put("apiId", scope.apiId());
context.put("deploymentId", scope.stage().getDeploymentId());
context.put("httpMethod", scope.httpMethod());
context.put("path", "/" + scope.stageName() + scope.path());
context.put("protocol", "HTTP/1.1");
context.put("requestId", requestId);
context.put("resourceId", scope.resource().getId());
context.put("resourcePath", scope.resource().getPath());
context.put("stage", scope.stageName());
Map<String, Object> identity = new LinkedHashMap<>();
identity.put("sourceIp", "127.0.0.1");
identity.put("userAgent", scope.headers().getHeaderString("User-Agent"));
context.put("identity", identity);
Map<String, Object> authorizer = new LinkedHashMap<>();
if (authorizerResult.context() != null) {
authorizer.putAll(authorizerResult.context());
}
if (authorizerResult.principalId() != null) {
authorizer.put("principalId", authorizerResult.principalId());
}
context.put("authorizer", authorizer);
return context;
}
private String resolveRequestParameter(String source, Map<String, String> queryParams,
Map<String, String> pathParams, Map<String, String> headers) {
Map<String, String> pathParams, Map<String, String> headers,
Map<String, Object> context) {
if (source == null) return null;
if (source.startsWith("method.request.querystring.")) {
return queryParams.get(source.substring("method.request.querystring.".length()));
@@ -1999,6 +2050,11 @@ public class ApiGatewayExecuteController {
if (source.startsWith("method.request.header.")) {
return headers.get(source.substring("method.request.header.".length()));
}
if (source.startsWith("context.")) {
Object value = contextValue(context, source.substring("context.".length()));
return value instanceof String || value instanceof Number || value instanceof Boolean
? value.toString() : null;
}
// Static value
if (source.startsWith("'") && source.endsWith("'")) {
return source.substring(1, source.length() - 1);
@@ -0,0 +1,343 @@
package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@QuarkusTest
class ApiGatewayHttpContextMappingIntegrationTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String FUNCTION = "http-context-authorizer";
private static final String AUTHORIZER_URI = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/"
+ "arn:aws:lambda:us-east-1:000000000000:function:" + FUNCTION + "/invocations";
private static final AtomicInteger backendRequests = new AtomicInteger();
private static HttpServer backend;
@InjectMock
LambdaService lambdaService;
private String apiId;
private String resourceId;
private String methodPath;
@BeforeAll
static void startBackend() throws IOException {
backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
backend.createContext("/", ApiGatewayHttpContextMappingIntegrationTest::echo);
backend.start();
}
@AfterAll
static void stopBackend() {
backend.stop(0);
}
private static void echo(HttpExchange exchange) throws IOException {
backendRequests.incrementAndGet();
Map<String, Object> response = new LinkedHashMap<>();
Map<String, List<String>> headers = new LinkedHashMap<>();
exchange.getRequestHeaders().forEach((name, values) -> headers.put(name.toLowerCase(Locale.ROOT), values));
response.put("headers", headers);
response.put("path", exchange.getRequestURI().getPath());
response.put("query", exchange.getRequestURI().getQuery());
response.put("body", new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8));
byte[] payload = MAPPER.writeValueAsBytes(response);
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, payload.length);
exchange.getResponseBody().write(payload);
exchange.close();
}
@BeforeEach
void createApi() {
backendRequests.set(0);
apiId = given().contentType(ContentType.JSON).body(Map.of("name", "http-context-mapping"))
.post("/restapis").then().statusCode(201).extract().path("id");
String rootId = given().get("/restapis/" + apiId + "/resources")
.then().statusCode(200).extract().path("item[0].id");
resourceId = given().contentType(ContentType.JSON).body(Map.of("pathPart", "{proxy+}"))
.post("/restapis/" + apiId + "/resources/" + rootId)
.then().statusCode(201).extract().path("id");
methodPath = "/restapis/" + apiId + "/resources/" + resourceId + "/methods/";
}
@AfterEach
void deleteApi() {
if (apiId != null) {
given().delete("/restapis/" + apiId).then().statusCode(202);
}
}
@ParameterizedTest
@ValueSource(strings = {"HTTP_PROXY", "HTTP"})
void forwardsVerifiedAuthorizerScalarsAndRequestContext(String type) throws Exception {
configureAuthorizer(0);
configureIntegration(type, "GET");
deploy();
for (String endpoint : List.of("/execute-api/" + apiId + "/test/",
"/restapis/" + apiId + "/test/_user_request_/")) {
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer allowed")
.header("x-user-claims", "forged", "also-forged")
.header("x-missing", "forged-missing")
.header("x-object", "forged-object")
.header("x-request-id", "client-id")
.header("X-Source", "client-value")
.get(endpoint + "orders/42?client=acme")
.then().statusCode(200).extract().asByteArray());
assertMappedContext(response);
assertEquals("/test/orders/42", response.path("headers").path("x-context-path").get(0).asText());
assertEquals("GET", response.path("headers").path("x-context-method").get(0).asText());
assertEquals(apiId, response.path("headers").path("x-context-api").get(0).asText());
assertEquals(resourceId, response.path("headers").path("x-context-resource").get(0).asText());
assertEquals("/users/verified-user/orders/42", response.path("path").asText());
assertTrue(response.path("query").asText().contains("claims=verified-claims"));
assertTrue(response.path("query").asText().contains("client=acme"));
assertEquals("client-value", response.path("headers").path("x-from-client").get(0).asText());
assertEquals("static-value", response.path("headers").path("x-static").get(0).asText());
if ("HTTP".equals(type)) {
assertEquals(response.path("headers").path("x-request-id").get(0).asText(),
MAPPER.readTree(response.path("body").asText()).path("requestId").asText());
}
}
}
@ParameterizedTest
@ValueSource(strings = {"HTTP_PROXY", "HTTP"})
void missingAuthorizerClaimDoesNotFallBackToClientHeader(String type) throws Exception {
configureAuthorizer(0, Map.of());
configureIntegration(type, "GET");
deploy();
for (String endpoint : List.of("/execute-api/" + apiId + "/test/",
"/restapis/" + apiId + "/test/_user_request_/")) {
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer allowed")
.header("x-user-claims", "forged", "also-forged")
.get(endpoint + "orders/42")
.then().statusCode(200).extract().asByteArray());
assertFalse(response.path("headers").has("x-user-claims"));
assertEquals(MAPPER.valueToTree(List.of("verified-user")), response.path("headers").path("x-principal"));
}
}
@Test
void missingMethodRequestMappingPreservesInboundProxyHeader() throws Exception {
configureAuthorizer(0);
configureIntegration("HTTP_PROXY", "GET");
deploy();
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer allowed")
.header("X-Unresolved", "client-value")
.get("/execute-api/" + apiId + "/test/orders/42")
.then().statusCode(200).extract().asByteArray());
assertEquals(MAPPER.valueToTree(List.of("client-value")), response.path("headers").path("x-unresolved"));
}
@ParameterizedTest
@CsvSource({"HTTP_PROXY,true,true", "HTTP_PROXY,true,false", "HTTP_PROXY,false,true", "HTTP_PROXY,false,false",
"HTTP,true,true", "HTTP,true,false", "HTTP,false,true", "HTTP,false,false"})
void requestMappingsReadOriginalInputs(String type, boolean hasClaim, boolean copiesFirst) throws Exception {
configureAuthorizer(0, hasClaim ? Map.of("userClaims", "verified-claims") : Map.of());
Map<String, String> replacements = new LinkedHashMap<>();
replacements.put("integration.request.header.X-User-Claims", "context.authorizer.userClaims");
replacements.put("integration.request.header.X-Principal", "context.authorizer.principalId");
replacements.put("integration.request.querystring.claims", "'mapped-query'");
replacements.put("integration.request.path.principal", "context.authorizer.principalId");
replacements.put("integration.request.path.proxy", "'mapped-path'");
Map<String, String> copies = new LinkedHashMap<>();
copies.put("integration.request.header.X-Original-Claims", "method.request.header.x-user-claims");
copies.put("integration.request.header.X-Original-Query", "method.request.querystring.claims");
copies.put("integration.request.header.X-Original-Path", "method.request.path.proxy");
Map<String, String> parameters = new LinkedHashMap<>();
parameters.putAll(copiesFirst ? copies : replacements);
parameters.putAll(copiesFirst ? replacements : copies);
configureIntegration(type, "GET", parameters);
deploy();
for (String endpoint : List.of("/execute-api/" + apiId + "/test/",
"/restapis/" + apiId + "/test/_user_request_/")) {
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer allowed")
.header("x-user-claims", "client-claims")
.get(endpoint + "orders/42?claims=client-query")
.then().statusCode(200).extract().asByteArray());
JsonNode headers = response.path("headers");
assertEquals("client-claims", headers.path("x-original-claims").path(0).asText());
assertEquals("client-query", headers.path("x-original-query").path(0).asText());
assertEquals("orders/42", headers.path("x-original-path").path(0).asText());
assertEquals("/users/verified-user/mapped-path", response.path("path").asText());
assertEquals("claims=mapped-query", response.path("query").asText());
if (hasClaim) {
assertEquals(MAPPER.valueToTree(List.of("verified-claims")), headers.path("x-user-claims"));
} else {
assertFalse(headers.has("x-user-claims"));
}
}
}
@ParameterizedTest
@ValueSource(strings = {"HTTP_PROXY", "HTTP"})
void retainsContextOnRepeatedRequestsWithAuthorizerTtl(String type) throws Exception {
configureAuthorizer(300);
configureIntegration(type, "GET");
deploy();
String previousRequestId = null;
for (int request = 0; request < 2; request++) {
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer allowed")
.header("X-User-Claims", "forged")
.get("/execute-api/" + apiId + "/test/orders/42")
.then().statusCode(200).extract().asByteArray());
assertMappedContext(response);
String requestId = response.path("headers").path("x-request-id").get(0).asText();
assertNotEquals(previousRequestId, requestId);
previousRequestId = requestId;
}
}
@ParameterizedTest
@ValueSource(strings = {"HTTP_PROXY", "HTTP"})
void rejectedTokensNeverReachBackend(String type) throws Exception {
configureAuthorizer(0);
configureIntegration(type, "GET");
deploy();
for (String token : List.of("", "Bearer forged")) {
RequestSpecification request = given().header("X-User-Claims", "forged");
if (!token.isEmpty()) {
request.header("Authorization", token);
}
request.get("/execute-api/" + apiId + "/test/orders/42").then().statusCode(403);
}
assertEquals(0, backendRequests.get());
}
@ParameterizedTest
@ValueSource(strings = {"HTTP_PROXY", "HTTP"})
void unauthenticatedOptionsHasRequestContextWithoutBearerClaims(String type) throws Exception {
configureAuthorizer(0);
configureIntegration(type, "GET");
given().contentType(ContentType.JSON).body(Map.of("authorizationType", "NONE"))
.put(methodPath + "OPTIONS").then().statusCode(201);
configureIntegration(type, "OPTIONS");
deploy();
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer forged")
.header("x-user-claims", "forged", "also-forged")
.options("/execute-api/" + apiId + "/test/orders/42")
.then().statusCode(200).extract().asByteArray());
assertFalse(response.path("headers").has("x-user-claims"));
assertFalse(response.path("headers").has("x-principal"));
assertFalse(response.path("headers").has("x-missing"));
assertFalse(response.path("headers").has("x-object"));
assertEquals("OPTIONS", response.path("headers").path("x-context-method").path(0).asText());
assertDoesNotThrow(() -> UUID.fromString(response.path("headers").path("x-request-id").path(0).asText()));
}
private void assertMappedContext(JsonNode response) {
JsonNode headers = response.path("headers");
assertEquals(MAPPER.valueToTree(List.of("verified-claims")), headers.path("x-user-claims"));
assertEquals(MAPPER.valueToTree(List.of("verified-user")), headers.path("x-principal"));
assertEquals(MAPPER.valueToTree(List.of("123")), headers.path("x-number"));
assertEquals(MAPPER.valueToTree(List.of("true")), headers.path("x-boolean"));
assertEquals(MAPPER.valueToTree(List.of("test")), headers.path("x-context-stage"));
assertFalse(headers.has("x-missing"));
assertFalse(headers.has("x-object"));
assertEquals(1, headers.path("x-request-id").size());
assertDoesNotThrow(() -> UUID.fromString(headers.path("x-request-id").get(0).asText()));
}
private void configureAuthorizer(int ttl) throws Exception {
configureAuthorizer(ttl, Map.of("userClaims", "verified-claims", "principalId", "forged-principal",
"numberKey", 123, "booleanKey", true, "objectKey", Map.of("nested", "value")));
}
private void configureAuthorizer(int ttl, Map<String, Object> context) throws Exception {
String authorizerId = given().contentType(ContentType.JSON)
.body(Map.of("name", "claims", "type", "TOKEN", "authorizerUri", AUTHORIZER_URI,
"identitySource", "method.request.header.Authorization", "authorizerResultTtlInSeconds", ttl))
.post("/restapis/" + apiId + "/authorizers")
.then().statusCode(201).extract().path("id");
given().contentType(ContentType.JSON)
.body(Map.of("authorizationType", "CUSTOM", "authorizerId", authorizerId))
.put(methodPath + "GET").then().statusCode(201);
when(lambdaService.invoke(eq("us-east-1"), eq(FUNCTION), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
JsonNode event = MAPPER.readTree((byte[]) invocation.getArgument(2));
String effect = "Bearer allowed".equals(event.path("authorizationToken").asText()) ? "Allow" : "Deny";
byte[] payload = MAPPER.writeValueAsBytes(Map.of("principalId", "verified-user",
"policyDocument", Map.of("Version", "2012-10-17", "Statement", List.of(Map.of(
"Action", "execute-api:Invoke", "Effect", effect, "Resource", "*"))),
"context", context));
return new InvokeResult(200, null, payload, null, "authorizer-request");
});
}
private void configureIntegration(String type, String method) {
Map<String, String> parameters = new LinkedHashMap<>();
parameters.put("integration.request.header.X-User-Claims", "context.authorizer.userClaims");
parameters.put("integration.request.header.X-Principal", "context.authorizer.principalId");
parameters.put("integration.request.header.X-Number", "context.authorizer.numberKey");
parameters.put("integration.request.header.X-Boolean", "context.authorizer.booleanKey");
parameters.put("integration.request.header.X-Missing", "context.authorizer.missing");
parameters.put("integration.request.header.X-Object", "context.authorizer.objectKey");
parameters.put("integration.request.header.X-Request-Id", "context.requestId");
parameters.put("integration.request.header.X-Context-Stage", "context.stage");
parameters.put("integration.request.header.X-Context-Path", "context.path");
parameters.put("integration.request.header.X-Context-Method", "context.httpMethod");
parameters.put("integration.request.header.X-Context-Api", "context.apiId");
parameters.put("integration.request.header.X-Context-Resource", "context.resourceId");
parameters.put("integration.request.header.X-From-Client", "method.request.header.X-Source");
parameters.put("integration.request.header.X-Unresolved", "method.request.querystring.missing");
parameters.put("integration.request.header.X-Static", "'static-value'");
parameters.put("integration.request.querystring.claims", "context.authorizer.userClaims");
parameters.put("integration.request.querystring.client", "method.request.querystring.client");
parameters.put("integration.request.path.principal", "context.authorizer.principalId");
parameters.put("integration.request.path.proxy", "method.request.path.proxy");
configureIntegration(type, method, parameters);
}
private void configureIntegration(String type, String method, Map<String, String> parameters) {
// POST makes the HTTP integration's rendered request body observable at the echo backend.
String integrationMethod = "HTTP".equals(type) && "GET".equals(method) ? "POST" : method;
given().contentType(ContentType.JSON)
.body(Map.of("type", type, "httpMethod", integrationMethod,
"uri", "http://127.0.0.1:" + backend.getAddress().getPort() + "/users/{principal}/{proxy}",
"requestParameters", parameters, "requestTemplates", Map.of("application/json",
"{\"requestId\":\"$context.requestId\"}")))
.put(methodPath + method + "/integration").then().statusCode(201);
}
private void deploy() {
given().contentType(ContentType.JSON).body(Map.of("stageName", "test"))
.post("/restapis/" + apiId + "/deployments").then().statusCode(201);
}
}