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

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.apigateway;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
@@ -18,6 +19,7 @@ import org.jboss.logging.Logger;
import java.net.URI;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -37,13 +39,20 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
private final RegionResolver regionResolver;
private final ApiGatewayExecuteRouteContext routeContext;
private final String baseHostname;
private final RequestContext requestContext;
@Inject
public ApiGatewayExecuteApiHostFilter(ApiGatewayV2Service apiGatewayV2Service,
RegionResolver regionResolver,
ApiGatewayExecuteRouteContext routeContext,
EmulatorConfig config) {
EmulatorConfig config,
RequestContext requestContext) {
this(new ApiGatewayLookup() {
@Override
public Optional<ApiGatewayV2Service.ApiOwner> findApiOwner(String apiId) {
return apiGatewayV2Service.findApiOwner(apiId);
}
@Override
public String resolveApiRegion(String preferredRegion, String apiId) {
return apiGatewayV2Service.resolveApiRegion(preferredRegion, apiId);
@@ -63,24 +72,31 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
public void requireStage(String region, String apiId, String stageName) {
apiGatewayV2Service.getStage(region, apiId, stageName);
}
}, regionResolver, routeContext, config.hostname().orElse("localhost"));
}, regionResolver, routeContext, config.hostname().orElse("localhost"), requestContext);
}
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver) {
this(apiGatewayLookup, regionResolver, new ApiGatewayExecuteRouteContext(), "localhost");
this(apiGatewayLookup, regionResolver, new ApiGatewayExecuteRouteContext(), "localhost", null);
}
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver,
ApiGatewayExecuteRouteContext routeContext) {
this(apiGatewayLookup, regionResolver, routeContext, "localhost");
this(apiGatewayLookup, regionResolver, routeContext, "localhost", null);
}
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver,
ApiGatewayExecuteRouteContext routeContext, String baseHostname) {
this(apiGatewayLookup, regionResolver, routeContext, baseHostname, null);
}
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver,
ApiGatewayExecuteRouteContext routeContext, String baseHostname,
RequestContext requestContext) {
this.apiGatewayLookup = apiGatewayLookup;
this.regionResolver = regionResolver;
this.routeContext = routeContext;
this.baseHostname = baseHostname;
this.requestContext = requestContext;
}
@Override
@@ -94,15 +110,26 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
if (apiId == null) {
return;
}
// Region: the host label when it is a real AWS region (…execute-api.{region}.…), else the
// SigV4 credential scope. The cross-region apiId scan (resolveApiRegion) is the final
// fallback so an API created outside the default region still resolves — including on the
// regionless built-in suffixes, which carry no region label (issue #1871).
String hostRegion = regionResolver.resolveRegionFromHost(host);
String preferredRegion = hostRegion != null
? hostRegion
: regionResolver.resolveRegionFromAuth(requestContext.getHeaderString("Authorization"));
String region = apiGatewayLookup.resolveApiRegion(preferredRegion, apiId);
Optional<ApiGatewayV2Service.ApiOwner> owner = apiGatewayLookup.findApiOwner(apiId);
String region;
if (owner.isPresent()) {
ApiGatewayV2Service.ApiOwner apiOwner = owner.get();
if (this.requestContext != null) {
this.requestContext.setAccountId(apiOwner.accountId());
this.requestContext.setRegion(apiOwner.region());
}
region = apiOwner.region();
} else {
// Region: the host label when it is a real AWS region (…execute-api.{region}.…), else
// the SigV4 credential scope. The cross-region apiId scan (resolveApiRegion) is the
// final fallback so an API created outside the default region still resolves —
// including on regionless built-in suffixes (issue #1871).
String hostRegion = regionResolver.resolveRegionFromHost(host);
String preferredRegion = hostRegion != null
? hostRegion
: regionResolver.resolveRegionFromAuth(requestContext.getHeaderString("Authorization"));
region = apiGatewayLookup.resolveApiRegion(preferredRegion, apiId);
}
URI originalUri = requestContext.getUriInfo().getRequestUri();
String originalPath = originalUri.getRawPath();
@@ -175,6 +202,10 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
}
interface ApiGatewayLookup {
default Optional<ApiGatewayV2Service.ApiOwner> findApiOwner(String apiId) {
return Optional.empty();
}
String resolveApiRegion(String preferredRegion, String apiId);
String protocolType(String region, String apiId);
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
import io.github.hectorvent.floci.services.apigateway.model.ApiKey;
import io.github.hectorvent.floci.services.apigateway.model.Integration;
@@ -94,6 +95,7 @@ public class ApiGatewayExecuteController {
private final SqsQueryHandler sqsQueryHandler;
private final ApiGatewayExecuteRouteContext routeContext;
private final JwtSignatureVerifier jwtSignatureVerifier;
private final RequestContext requestContext;
@Inject
public ApiGatewayExecuteController(ApiGatewayService apiGatewayService, ApiGatewayV2Service apiGatewayV2Service,
@@ -104,7 +106,8 @@ public class ApiGatewayExecuteController {
ElbV2Service elbV2Service,
SqsQueryHandler sqsQueryHandler,
ApiGatewayExecuteRouteContext routeContext,
JwtSignatureVerifier jwtSignatureVerifier) {
JwtSignatureVerifier jwtSignatureVerifier,
RequestContext requestContext) {
this.apiGatewayService = apiGatewayService;
this.apiGatewayV2Service = apiGatewayV2Service;
this.lambdaService = lambdaService;
@@ -117,6 +120,7 @@ public class ApiGatewayExecuteController {
this.sqsQueryHandler = sqsQueryHandler;
this.routeContext = routeContext;
this.jwtSignatureVerifier = jwtSignatureVerifier;
this.requestContext = requestContext;
}
/** Matches an ELBv2 listener ARN (ALB {@code app/} or NLB {@code net/}); group 1 = region. */
@@ -256,6 +260,11 @@ public class ApiGatewayExecuteController {
String region = regionResolver.resolveRegion(headers);
String httpApiRegion = routeContext.httpApiRegion();
if (httpApiRegion != null) {
Optional<ApiGatewayV2Service.ApiOwner> owner = apiGatewayV2Service.findApiOwner(apiId);
if (owner.isPresent()) {
applyApiOwnerContext(owner.get());
httpApiRegion = owner.get().region();
}
return dispatchV2(httpMethod, apiId, stageName, proxy, headers, uriInfo, body, httpApiRegion);
}
@@ -272,6 +281,12 @@ public class ApiGatewayExecuteController {
try {
apiGatewayService.getRestApi(region, apiId);
} catch (AwsException restApiError) {
Optional<ApiGatewayV2Service.ApiOwner> owner = apiGatewayV2Service.findApiOwner(apiId);
if (owner.isPresent()) {
applyApiOwnerContext(owner.get());
return dispatchV2(httpMethod, apiId, stageName, proxy, headers, uriInfo, body, owner.get().region());
}
String v2Region = apiGatewayV2Service.resolveApiRegion(preferredRegion, apiId);
try {
apiGatewayV2Service.getApi(v2Region, apiId);
@@ -358,6 +373,11 @@ public class ApiGatewayExecuteController {
};
}
private void applyApiOwnerContext(ApiGatewayV2Service.ApiOwner owner) {
requestContext.setAccountId(owner.accountId());
requestContext.setRegion(owner.region());
}
// ──────────────────────────── AWS_PROXY ────────────────────────────
private Response invokeProxy(String region, String apiId, String httpMethod, String path, String proxy,
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -15,6 +16,7 @@ import org.jboss.logging.Logger;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.regex.Pattern;
@@ -23,7 +25,7 @@ public class ApiGatewayV2Service {
private static final Logger LOG = Logger.getLogger(ApiGatewayV2Service.class);
private final StorageBackend<String, Api> apiStore;
private final AccountAwareStorageBackend<Api> apiStore;
private final StorageBackend<String, Route> routeStore;
private final StorageBackend<String, Integration> integrationStore;
private final StorageBackend<String, Authorizer> authorizerStore;
@@ -35,6 +37,8 @@ public class ApiGatewayV2Service {
private final StorageBackend<String, VpcLink> vpcLinkStore;
private final RegionResolver regionResolver;
public record ApiOwner(String accountId, String region) {}
@Inject
public ApiGatewayV2Service(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver) {
this.apiStore = storageFactory.create("apigatewayv2", "apigatewayv2-apis.json",
@@ -86,7 +90,7 @@ public class ApiGatewayV2Service {
Map<String, String> tags = (Map<String, String>) request.get("tags");
String overrideId = ReservedTags.extractOverrideApiId(tags);
String apiId = overrideId != null ? overrideId : shortId(10);
if (apiStore.get(apiKey(region, apiId)).isPresent()) {
if (findApiOwner(apiId).isPresent()) {
throw new AwsException("ConflictException",
"API with id '" + apiId + "' already exists", 409);
}
@@ -144,6 +148,27 @@ public class ApiGatewayV2Service {
.orElse(preferredRegion);
}
/** Resolves the account and region that own an API ID for data-plane requests. */
public Optional<ApiOwner> findApiOwner(String apiId) {
List<AccountAwareStorageBackend.AccountEntry<Api>> matches = apiStore
.scanAllAccountEntries(key -> key.endsWith("::" + apiId));
if (matches.size() > 1) {
throw new AwsException("ConflictException",
"API id '" + apiId + "' is ambiguous across accounts", 409);
}
return matches.stream()
.findFirst()
.map(entry -> new ApiOwner(entry.accountId(), regionFromApiKey(entry.key())));
}
private static String regionFromApiKey(String key) {
int delimiter = key.indexOf("::");
if (delimiter <= 0) {
throw new IllegalStateException("Invalid API storage key: " + key);
}
return key.substring(0, delimiter);
}
public List<Api> getApis(String region) {
String prefix = region + "::";
return apiStore.scan(k -> k.startsWith(prefix));