fix(apigateway): route REST execute-api virtual hosts (#4553)

* fix(apigateway): route REST execute-api virtual hosts

* fix(apigateway): preserve REST connection paths

* fix(apigateway): prioritize WebSocket connection routes

* fix(apigateway): honor explicit REST domain routes

* fix(apigateway): preserve WebSocket domain connections

* fix(apigateway): scope base path routes to REST APIs

* fix(apigateway): retain API type for domain mappings

* fix(apigateway): find WebSocket connection routes across accounts

---------

Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
vamsiakandVamsi Akavarapu authored and GitHub committed 2026-09-30 06:07:57 -04:00
1 parent 94ea065cc3
commit af3d387140
12 files changed
+411 -9

No files matched your search

@@ -1636,7 +1636,8 @@ public class ApiGatewayController {
v1Request.put("restApiId", apiId);
v1Request.put("stage", stage);
v1Request.put("basePath", basePath);
BasePathMapping mapping = service.createBasePathMapping(region, domainName, v1Request);
BasePathMapping mapping = service.createBasePathMapping(region, domainName, v1Request,
v2Service.getApi(region, apiId).getProtocolType());
return Response.status(201).entity(toApiMappingNode(basePath, mapping).toString())
.type(MediaType.APPLICATION_JSON).build();
}
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.apigateway;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.services.apigateway.model.BasePathMapping;
import io.github.hectorvent.floci.services.apigateway.model.CustomDomain;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -36,12 +37,15 @@ public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
private static final String REGIONAL_SUFFIX = ".regional.local";
private final ApiGatewayService apiGatewayService;
private final ApiGatewayV2Service apiGatewayV2Service;
private final ApiGatewayExecuteRouteContext routeContext;
@Inject
public ApiGatewayCustomDomainFilter(ApiGatewayService apiGatewayService,
ApiGatewayV2Service apiGatewayV2Service,
ApiGatewayExecuteRouteContext routeContext) {
this.apiGatewayService = apiGatewayService;
this.apiGatewayV2Service = apiGatewayV2Service;
this.routeContext = routeContext;
}
@@ -107,6 +111,14 @@ public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
LOG.debugv("Custom domain routing: {0}{1} -> {2}", host, path, newUri.getPath());
// AWS_IAM dispatch rebuilds the caller's canonical request, which covers the path they
// signed: the custom-domain path, not the /execute-api/... form this rewrite produces.
// Older persisted mappings have no type. Prefer REST if that API exists; otherwise
// retain v2 routing when a v2 API owns the id.
if ("REST".equals(mapping.getApiType())
|| (mapping.getApiType() == null
&& (apiGatewayService.hasRestApi(restApiId)
|| apiGatewayV2Service.findApiOwner(restApiId).isEmpty()))) {
routeContext.routeToRestApi();
}
routeContext.recordSignedRequestPath(path);
requestContext.setRequestUri(newUri);
}
@@ -162,6 +162,12 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
return;
}
} catch (AwsException e) {
// The v2 owner lookup is account-independent. When it found no owner, let the
// REST execute controller resolve the API after account context is established.
if (owner.isEmpty()) {
routeToRestApi(requestContext, originalUri, originalPath, apiId);
return;
}
LOG.debugv(e, "Execute API host did not resolve to a routable API: apiId={0}, region={1}",
apiId, region);
return;
@@ -195,6 +201,18 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
requestContext.setRequestUri(newUri);
}
private void routeToRestApi(ContainerRequestContext requestContext, URI originalUri,
String originalPath, String apiId) {
String path = originalPath == null || originalPath.isEmpty() ? "/" : originalPath;
URI newUri = UriBuilder.fromUri(originalUri)
.replacePath("/execute-api/" + apiId + path)
.buildFromEncoded();
LOG.debugv("REST execute-api host routing: {0} -> {1}", originalPath, newUri.getRawPath());
routeContext.routeToRestApi();
routeContext.recordSignedRequestPath(originalPath);
requestContext.setRequestUri(newUri);
}
private boolean stageExists(String region, String apiId, String stageName) {
try {
apiGatewayLookup.requireStage(region, apiId, stageName);
@@ -168,6 +168,13 @@ public class ApiGatewayExecuteController {
private static final String CONNECTIONS_PREFIX = "@connections/";
private boolean isWebSocketConnectionRequest(String apiId, String proxy) {
if (proxy == null || !proxy.startsWith(CONNECTIONS_PREFIX) || routeContext.isRestApiRoute()) {
return false;
}
return apiGatewayV2Service.hasWebSocketApi(apiId);
}
private String decodeConnectionId(String rawConnectionId) {
return URLDecoder.decode(rawConnectionId, StandardCharsets.UTF_8);
}
@@ -230,7 +237,7 @@ public class ApiGatewayExecuteController {
@PathParam("apiId") String apiId,
@PathParam("stageName") String stageName,
@PathParam("proxy") String proxy) {
if (proxy != null && proxy.startsWith(CONNECTIONS_PREFIX)) {
if (isWebSocketConnectionRequest(apiId, proxy)) {
String connectionId = decodeConnectionId(proxy.substring(CONNECTIONS_PREFIX.length()));
return handleGetConnectionInfo(connectionId);
}
@@ -245,7 +252,7 @@ public class ApiGatewayExecuteController {
@PathParam("stageName") String stageName,
@PathParam("proxy") String proxy,
byte[] body) {
if (proxy != null && proxy.startsWith(CONNECTIONS_PREFIX)) {
if (isWebSocketConnectionRequest(apiId, proxy)) {
String connectionId = decodeConnectionId(proxy.substring(CONNECTIONS_PREFIX.length()));
return handlePostToConnection(connectionId, body);
}
@@ -270,7 +277,7 @@ public class ApiGatewayExecuteController {
@PathParam("apiId") String apiId,
@PathParam("stageName") String stageName,
@PathParam("proxy") String proxy) {
if (proxy != null && proxy.startsWith(CONNECTIONS_PREFIX)) {
if (isWebSocketConnectionRequest(apiId, proxy)) {
String connectionId = decodeConnectionId(proxy.substring(CONNECTIONS_PREFIX.length()));
return handleDeleteConnection(connectionId);
}
@@ -11,6 +11,7 @@ import jakarta.ws.rs.core.UriInfo;
public class ApiGatewayExecuteRouteContext {
private String httpApiRegion;
private boolean restApiRoute;
private String signedRequestPath;
void routeToHttpApi(String region) {
@@ -21,6 +22,14 @@ public class ApiGatewayExecuteRouteContext {
return httpApiRegion;
}
void routeToRestApi() {
this.restApiRoute = true;
}
boolean isRestApiRoute() {
return restApiRoute;
}
/**
* Records the raw path as the client sent it, before a pre-matching filter rewrote the request
* URI onto the internal {@code /execute-api/...} form. SigV4 covers the path the caller signed,
@@ -332,6 +332,11 @@ public class ApiGatewayService {
.orElseThrow(() -> new AwsException("NotFoundException", "Invalid API id specified", 404));
}
public boolean hasRestApi(String apiId) {
return apiStore.keys().stream().anyMatch(key -> key.endsWith("::" + apiId));
}
public String resolveRestApiRegion(String preferredRegion, String apiId) {
if (apiStore.get(apiKey(preferredRegion, apiId)).isPresent()) {
return preferredRegion;
@@ -2122,11 +2127,17 @@ public class ApiGatewayService {
}
public BasePathMapping createBasePathMapping(String region, String domainName, Map<String, Object> request) {
return createBasePathMapping(region, domainName, request, "REST");
}
BasePathMapping createBasePathMapping(String region, String domainName,
Map<String, Object> request, String apiType) {
String basePath = canonicalBasePath((String) request.get("basePath"));
String apiId = (String) request.get("restApiId");
String stage = (String) request.get("stage");
BasePathMapping mapping = new BasePathMapping(basePath, apiId, stage);
mapping.setApiType(apiType);
synchronized (domainNameLock) {
getDomainName(region, domainName);
String key = mappingKey(region, domainName, basePath);
@@ -9,6 +9,7 @@ public class BasePathMapping {
private String basePath;
private String restApiId;
private String stage;
private String apiType;
public BasePathMapping() {
this.basePath = "(none)";
@@ -28,4 +29,7 @@ public class BasePathMapping {
public String getStage() { return stage; }
public void setStage(String stage) { this.stage = stage; }
public String getApiType() { return apiType; }
public void setApiType(String apiType) { this.apiType = apiType; }
}
@@ -193,6 +193,12 @@ public class ApiGatewayV2Service {
.map(entry -> new ApiOwner(entry.accountId(), regionFromApiKey(entry.key())));
}
/** Connection management only needs to know whether any owner uses this ID for WebSocket. */
public boolean hasWebSocketApi(String apiId) {
return apiStore.scanAllAccountEntries(key -> key.endsWith("::" + apiId)).stream()
.anyMatch(entry -> "WEBSOCKET".equals(entry.value().getProtocolType()));
}
private static String regionFromApiKey(String key) {
int delimiter = key.indexOf("::");
if (delimiter <= 0) {