fix(apigatewayv2): route requests to API-owning account (#2377)

Resolve HTTP API ownership across account-aware storage before data-plane dispatch, then switch the request context so route, integration, and Lambda lookups use the API owner's account and region. Reject duplicate API IDs across accounts to keep ownership unambiguous.
This commit is contained in:
Saatvik Arya authored and GitHub committed 2026-08-18 16:12:29 -04:00
1 parent bc0f88c9b1
commit ba9ce84242
9 files changed
+217 -24

No files matched your search

@@ -35,7 +35,7 @@ class ApiGatewayExecuteControllerTest {
return new ApiGatewayExecuteController(
null, null, null,
regionResolver, objectMapper, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null);
}
@Test
@@ -235,7 +235,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
apiGatewayService, apiGatewayV2Service, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null);
Response response = controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
@@ -265,7 +265,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
apiGatewayService, apiGatewayV2Service, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null);
controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
@@ -299,7 +299,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
apiGatewayService, apiGatewayV2Service, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null);
controller.dispatch("GET", "restapi1", "prod", "hello", headers, null, null);
@@ -36,7 +36,7 @@ class ApiGatewayProxyMatchTest {
ctrl = new ApiGatewayExecuteController(apiGatewayService, apiGatewayV2Service, lambdaService,
new RegionResolver("us-east-1", "000000000000"),
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager, elbV2Service, null,
new ApiGatewayExecuteRouteContext(), null);
new ApiGatewayExecuteRouteContext(), null, null);
}
private ApiGatewayResource resource(String id, String parentId, String pathPart, String path) {
@@ -50,7 +50,7 @@ class BuildV2ProxyEventBodyEncodingTest {
controller = new ApiGatewayExecuteController(
null, null, null,
regionResolver, objectMapper, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null
);
}
@@ -45,7 +45,7 @@ class BuildV2ProxyEventJwtClaimsTest {
controller = new ApiGatewayExecuteController(
null, null, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null
);
}
@@ -41,7 +41,7 @@ class BuildV2ProxyEventPathParametersTest {
controller = new ApiGatewayExecuteController(
null, null, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null
);
}
@@ -0,0 +1,117 @@
package io.github.hectorvent.floci.services.apigatewayv2;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;
@QuarkusTest
class ApiGatewayV2UnsignedAccountRoutingTest {
private static final String ACCOUNT_A = "123456789012";
private static final String ACCOUNT_B = "210987654321";
@Test
void signedAndUnsignedRequestsResolveTheAccountFromApiId() {
String apiA = createApiGraph(ACCOUNT_A, "account-a-function");
String apiB = createApiGraph(ACCOUNT_B, "account-b-function");
given()
.header("Authorization", authorization(ACCOUNT_B, "execute-api"))
.when().get("/execute-api/" + apiA + "/$default/health")
.then()
.statusCode(404)
.body(containsString("Function not found: account-a-function"));
given()
.header("Host", apiB + ".execute-api.localhost.floci.io")
.when().get("/health")
.then()
.statusCode(404)
.body(containsString("Function not found: account-b-function"));
}
@Test
void rejectsDuplicateApiIdsAcrossAccounts() {
String apiId = "shared-account-api";
createApiWithId(ACCOUNT_A, apiId, 201);
createApiWithId(ACCOUNT_B, apiId, 409);
}
private String createApiGraph(String accountId, String functionName) {
String authorization = authorization(accountId, "apigatewayv2");
String apiId = given()
.contentType(ContentType.JSON)
.header("Authorization", authorization)
.body("""
{"name":"shared-local-api","protocolType":"HTTP"}
""")
.when().post("/v2/apis")
.then()
.statusCode(201)
.body("apiId", notNullValue())
.extract().path("apiId");
String integrationId = given()
.contentType(ContentType.JSON)
.header("Authorization", authorization)
.body("""
{
"integrationType":"AWS_PROXY",
"integrationUri":"arn:aws:lambda:us-east-1:%s:function:%s",
"payloadFormatVersion":"2.0"
}
""".formatted(accountId, functionName))
.when().post("/v2/apis/" + apiId + "/integrations")
.then()
.statusCode(201)
.body("integrationId", notNullValue())
.extract().path("integrationId");
given()
.contentType(ContentType.JSON)
.header("Authorization", authorization)
.body("""
{"routeKey":"GET /health","target":"integrations/%s"}
""".formatted(integrationId))
.when().post("/v2/apis/" + apiId + "/routes")
.then()
.statusCode(201);
given()
.contentType(ContentType.JSON)
.header("Authorization", authorization)
.body("""
{"stageName":"$default","autoDeploy":true}
""")
.when().post("/v2/apis/" + apiId + "/stages")
.then()
.statusCode(201);
return apiId;
}
private String authorization(String accountId, String service) {
return "AWS4-HMAC-SHA256 Credential=" + accountId
+ "/20260803/us-east-1/" + service + "/aws4_request";
}
private void createApiWithId(String accountId, String apiId, int expectedStatus) {
given()
.contentType(ContentType.JSON)
.header("Authorization", authorization(accountId, "apigatewayv2"))
.body("""
{
"name":"account-owned-api",
"protocolType":"HTTP",
"tags":{"floci:override-id":"%s"}
}
""".formatted(apiId))
.when().post("/v2/apis")
.then()
.statusCode(expectedStatus);
}
}