fix(apigateway): propagate TOKEN authorizer context to AWS_PROXY Lambda (#581)

* fix(apigateway): propagate TOKEN authorizer context to AWS_PROXY Lambda

REST API Gateway TOKEN authorizers return a principalId + context map that
floci silently dropped, so downstream AWS_PROXY Lambdas saw an empty
event.requestContext.authorizer. LocalStack propagates both; this brings
floci to parity and closes #574.

- Thread authorizer result through invokeAuthorizer via a small
  AuthorizerResult record into buildProxyEvent; emit
  requestContext.authorizer only when a CUSTOM authorizer ran
- Stringify context values at emit time to match AWS's flat-string wire
  contract
- Replace the literal "api/stage/METHOD/path" methodArn in the authorizer
  event with the real arn:aws:execute-api:region:acct:apiId/stage/METHOD/path
  built from the incoming request, so policy-scoping authorizers can match

Adds ApiGatewayAuthorizerContextIntegrationTest covering successful context
propagation + principalId + real methodArn, unprotected routes (no
authorizer injected), and deny returning 403.

Out of scope: v2 HTTP API JWT authorizer context, REST REQUEST authorizer
event-shape parity, authorizerResultTtlInSeconds caching.

* test(apigateway): bump authorizer-context Lambda timeout to 30s

The integration test creates two Node Lambdas back-to-back. Both
inherited the default 3s function timeout, so on CI the proxy
container's cold start ran past 3s while the authorizer was still
warming, returning 502 from the API Gateway invoke path.

Setting Timeout=30 on createNodeLambda gives both functions enough
headroom for the concurrent cold starts. Mirrors the pattern Hector
suggested in the PR review.
This commit is contained in:
TiM authored and GitHub committed 2026-04-26 17:55:43 -05:00
1 parent 9bdcfaf2b1
commit e5848487aa
2 files changed
+381 -18

No files matched your search

@@ -0,0 +1,320 @@
package io.github.hectorvent.floci.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ApiGatewayAuthorizerContextIntegrationTest {
private static final String LAMBDA_BASE_PATH = "/2015-03-31/functions";
private static final String AUTHORIZER_FUNCTION = "apigw-authorizer-context-auth";
private static final String PROXY_FUNCTION = "apigw-authorizer-context-proxy";
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/lambda-role";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static String apiId;
private static String rootId;
private static String securedResourceId;
private static String plainResourceId;
private static String authorizerId;
private static String deploymentId;
@Test
@Order(1)
void createAuthorizerLambda() throws Exception {
createNodeLambda(AUTHORIZER_FUNCTION, """
exports.handler = async (event) => ({
principalId: "test-user",
policyDocument: {
Version: "2012-10-17",
Statement: [{
Action: "execute-api:Invoke",
Effect: event.authorizationToken === "Bearer deny" ? "Deny" : "Allow",
Resource: event.methodArn
}]
},
context: {
org_id: "ORG001",
sub: "test-user",
client_id: "my-client",
methodArn: event.methodArn
}
});
""");
}
@Test
@Order(2)
void createProxyLambda() throws Exception {
createNodeLambda(PROXY_FUNCTION, """
exports.handler = async (event) => ({
statusCode: 200,
body: JSON.stringify({
authorizer: event.requestContext?.authorizer ?? null,
hasAuthorizer: Object.prototype.hasOwnProperty.call(event.requestContext ?? {}, "authorizer")
})
});
""");
}
@Test
@Order(3)
void createRestApi() {
apiId = given()
.contentType(ContentType.JSON)
.body("""
{"name":"authorizer-context-api"}
""")
.when().post("/restapis")
.then()
.statusCode(201)
.extract().path("id");
}
@Test
@Order(4)
void getRootResource() {
rootId = given()
.when().get("/restapis/" + apiId + "/resources")
.then()
.statusCode(200)
.extract().path("item[0].id");
}
@Test
@Order(5)
void createResources() {
securedResourceId = given()
.contentType(ContentType.JSON)
.body("{\"pathPart\":\"secured\"}")
.when().post("/restapis/" + apiId + "/resources/" + rootId)
.then()
.statusCode(201)
.extract().path("id");
plainResourceId = given()
.contentType(ContentType.JSON)
.body("{\"pathPart\":\"plain\"}")
.when().post("/restapis/" + apiId + "/resources/" + rootId)
.then()
.statusCode(201)
.extract().path("id");
}
@Test
@Order(6)
void createAuthorizer() {
String authorizerUri = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/"
+ "arn:aws:lambda:us-east-1:000000000000:function:" + AUTHORIZER_FUNCTION + "/invocations";
authorizerId = given()
.contentType(ContentType.JSON)
.body("""
{
"name":"ctx-authorizer",
"type":"TOKEN",
"authorizerUri":"%s",
"identitySource":"method.request.header.Authorization",
"authorizerResultTtlInSeconds":0
}
""".formatted(authorizerUri))
.when().post("/restapis/" + apiId + "/authorizers")
.then()
.statusCode(201)
.extract().path("id");
}
@Test
@Order(7)
void configureMethodsAndIntegrations() {
given()
.contentType(ContentType.JSON)
.body("""
{
"authorizationType":"CUSTOM",
"authorizerId":"%s",
"requestParameters":{"method.request.header.Authorization":true}
}
""".formatted(authorizerId))
.when().put("/restapis/" + apiId + "/resources/" + securedResourceId + "/methods/PUT")
.then()
.statusCode(201);
given()
.contentType(ContentType.JSON)
.body("""
{"authorizationType":"NONE"}
""")
.when().put("/restapis/" + apiId + "/resources/" + plainResourceId + "/methods/PUT")
.then()
.statusCode(201);
String proxyUri = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/"
+ "arn:aws:lambda:us-east-1:000000000000:function:" + PROXY_FUNCTION + "/invocations";
String integrationBody = """
{
"type":"AWS_PROXY",
"httpMethod":"POST",
"uri":"%s"
}
""".formatted(proxyUri);
given()
.contentType(ContentType.JSON)
.body(integrationBody)
.when().put("/restapis/" + apiId + "/resources/" + securedResourceId + "/methods/PUT/integration")
.then()
.statusCode(201);
given()
.contentType(ContentType.JSON)
.body(integrationBody)
.when().put("/restapis/" + apiId + "/resources/" + plainResourceId + "/methods/PUT/integration")
.then()
.statusCode(201);
}
@Test
@Order(8)
void deployApi() {
deploymentId = given()
.contentType(ContentType.JSON)
.body("{\"description\":\"authorizer-context\"}")
.when().post("/restapis/" + apiId + "/deployments")
.then()
.statusCode(201)
.extract().path("id");
given()
.contentType(ContentType.JSON)
.body("""
{"stageName":"test","deploymentId":"%s"}
""".formatted(deploymentId))
.when().post("/restapis/" + apiId + "/stages")
.then()
.statusCode(201);
}
@Test
@Order(9)
void executeSecuredRoute_propagatesAuthorizerContextAndMethodArn() throws Exception {
String response = given()
.contentType(ContentType.JSON)
.header("Authorization", "Bearer allow")
.body("{\"ok\":true}")
.when().put("/execute-api/" + apiId + "/test/secured")
.then()
.statusCode(200)
.extract().asString();
JsonNode payload = OBJECT_MAPPER.readTree(response);
JsonNode authorizer = payload.path("authorizer");
assertTrue(payload.path("hasAuthorizer").asBoolean());
assertEquals("test-user", authorizer.path("principalId").asText());
assertEquals("ORG001", authorizer.path("org_id").asText());
assertEquals("test-user", authorizer.path("sub").asText());
assertEquals("my-client", authorizer.path("client_id").asText());
assertEquals(
"arn:aws:execute-api:us-east-1:000000000000:" + apiId + "/test/PUT/secured",
authorizer.path("methodArn").asText());
}
@Test
@Order(10)
void executePlainRoute_doesNotInjectAuthorizerContext() throws Exception {
String response = given()
.contentType(ContentType.JSON)
.body("{\"ok\":true}")
.when().put("/execute-api/" + apiId + "/test/plain")
.then()
.statusCode(200)
.extract().asString();
JsonNode payload = OBJECT_MAPPER.readTree(response);
assertFalse(payload.path("hasAuthorizer").asBoolean());
assertTrue(payload.path("authorizer").isNull());
}
@Test
@Order(11)
void executeSecuredRoute_denyStillReturns403() {
given()
.contentType(ContentType.JSON)
.header("Authorization", "Bearer deny")
.body("{\"ok\":true}")
.when().put("/execute-api/" + apiId + "/test/secured")
.then()
.statusCode(403);
}
@Test
@Order(99)
void cleanup() {
if (apiId != null) {
given().when().delete("/restapis/" + apiId).then().statusCode(202);
}
deleteFunction(AUTHORIZER_FUNCTION);
deleteFunction(PROXY_FUNCTION);
}
private static void createNodeLambda(String functionName, String handlerSource) throws Exception {
String zipBase64 = Base64.getEncoder().encodeToString(zipEntries(Map.of(
"index.js", handlerSource
)));
given()
.contentType(ContentType.JSON)
.body("""
{
"FunctionName":"%s",
"Runtime":"nodejs20.x",
"Role":"%s",
"Handler":"index.handler",
"Timeout":30,
"Code":{"ZipFile":"%s"}
}
""".formatted(functionName, ROLE_ARN, zipBase64))
.when().post(LAMBDA_BASE_PATH)
.then()
.statusCode(201);
}
private static byte[] zipEntries(Map<String, String> entries) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
for (Map.Entry<String, String> entry : entries.entrySet()) {
zos.putNextEntry(new ZipEntry(entry.getKey()));
zos.write(entry.getValue().getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
}
}
return baos.toByteArray();
}
private static void deleteFunction(String functionName) {
int statusCode = given()
.when().delete(LAMBDA_BASE_PATH + "/" + functionName)
.then()
.extract().statusCode();
assertTrue(statusCode == 204 || statusCode == 404);
}
}