diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index e87c7b10cb..5cf36d4676 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -286,6 +286,7 @@ These management-plane operations have no handler in v1. Calls will return `404` The execute plane (actual proxied HTTP traffic via `/restapis/{id}/{stage}/_user_request_/…`) is implemented separately and is not counted as management-plane operations. A deployed REST API is also available at `http://{apiId}.execute-api.localhost.floci.io:4566/{stage}/{path}` +(or with the region label, `{apiId}.execute-api.{region}.localhost.floci.io`) and `/execute-api/{apiId}/{stage}/{path}`. All three forms use the same method authorization and mappings. It supports these integration types; others return an error: @@ -482,6 +483,13 @@ When an API has a `$default` stage, callers may omit the stage segment: curl http://{apiId}.execute-api.localhost.floci.io:4566/{path} ``` +The region-bearing form a client derives from a stage's invoke URL is accepted on the +same domain, as it is on `localhost` and the configured `FLOCI_HOSTNAME`: + +```bash +curl http://{apiId}.execute-api.{region}.localhost.floci.io:4566/{stageName}/{path} +``` + APIs created or updated with `disableExecuteApiEndpoint` reject requests to this default hostname with `404 Not Found`, matching AWS HTTP API behavior. diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilter.java index 4df1b9c296..7d1fe41423 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilter.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilter.java @@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsPartitions; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.RequestContext; import io.github.hectorvent.floci.core.common.RequestHost; +import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import jakarta.annotation.Priority; import jakarta.enterprise.context.ApplicationScoped; @@ -34,8 +35,6 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter { private static final Logger LOG = Logger.getLogger(ApiGatewayExecuteApiHostFilter.class); private static final Pattern EXECUTE_API_PREFIX = Pattern.compile("^([a-z0-9-]+)\\.execute-api\\.(.+)$", Pattern.CASE_INSENSITIVE); - private static final Pattern AWS_REGION = Pattern.compile("^[a-z]{2}-[a-z-]+-\\d+$", - Pattern.CASE_INSENSITIVE); private final ApiGatewayLookup apiGatewayLookup; private final RegionResolver regionResolver; @@ -241,8 +240,9 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter { /** * Extracts the {@code apiId} from an execute-api virtual host, or {@code null} when the host * is not an execute-api host. Region-bearing hosts accept the configured Floci hostname, the - * local {@code localhost} form, and AWS's {@code amazonaws.com} form. The public built-in - * suffixes remain regionless convenience forms. + * local {@code localhost} form, the public built-in suffixes, and AWS's + * {@code amazonaws.com} form. The built-in suffixes are also accepted regionless, as a + * convenience form. */ public static String extractApiId(String host, String baseHostname) { if (host == null) { @@ -255,18 +255,18 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter { } String tail = matcher.group(2); - if ("localhost.floci.io".equalsIgnoreCase(tail) - || "localhost.localstack.cloud".equalsIgnoreCase(tail)) { + if (isBuiltInSuffix(tail)) { return matcher.group(1).toLowerCase(Locale.ROOT); } int firstDot = tail.indexOf('.'); - if (firstDot <= 0 || !AWS_REGION.matcher(tail.substring(0, firstDot)).matches()) { + if (firstDot <= 0 || !RegionResolver.isKnownRegion(tail.substring(0, firstDot))) { return null; } String endpointHost = tail.substring(firstDot + 1); if ("localhost".equalsIgnoreCase(endpointHost) + || isBuiltInSuffix(endpointHost) || AwsPartitions.isDnsSuffix(endpointHost) || (baseHostname != null && baseHostname.equalsIgnoreCase(endpointHost))) { return matcher.group(1).toLowerCase(Locale.ROOT); @@ -274,6 +274,15 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter { return null; } + private static boolean isBuiltInSuffix(String host) { + for (String suffix : EmbeddedDnsServer.BUILTIN_SUFFIXES) { + if (suffix.equalsIgnoreCase(host)) { + return true; + } + } + return false; + } + /** * True when the path is a WebSocket {@code @connections} management route, * {@code /{stage}/@connections[/{connectionId}]} (raw or percent-encoded {@code @}). diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilterTest.java index 7ab540b0a8..8e9b383a75 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilterTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteApiHostFilterTest.java @@ -199,6 +199,104 @@ class ApiGatewayExecuteApiHostFilterTest { assertEquals("ap-northeast-2", routeContext.httpApiRegion()); } + /** + * The regionless built-in suffix is a convenience form; the region-bearing one is what a + * client derives from a stage's invoke URL. Unclaimed, the request fell through to S3's path + * routes and answered NoSuchBucket for the stage segment. + */ + @Test + void routesRegionBearingBuiltInSuffixHost() { + FakeApiGatewayLookup lookup = new FakeApiGatewayLookup(); + lookup.addApi("ap-northeast-2", API_ID, "HTTP"); + lookup.addStage("ap-northeast-2", API_ID, "$default"); + RecordingRequest request = new RecordingRequest( + "abc123.execute-api.ap-northeast-2.localhost.floci.io:4566", + URI.create("http://abc123.execute-api.ap-northeast-2.localhost.floci.io:4566/accounts")); + ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext(); + + new ApiGatewayExecuteApiHostFilter( + lookup, new RegionResolver(REGION, "000000000000"), routeContext) + .filter(request.context()); + + assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath()); + assertEquals("ap-northeast-2", routeContext.httpApiRegion()); + } + + @Test + void routesRestApiOnRegionBearingBuiltInSuffixHost() { + FakeApiGatewayLookup lookup = new FakeApiGatewayLookup(); + RecordingRequest request = new RecordingRequest( + "abc123.execute-api.us-east-1.localhost.floci.io:4566", + URI.create("http://abc123.execute-api.us-east-1.localhost.floci.io:4566/prod/users")); + ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext(); + + new ApiGatewayExecuteApiHostFilter( + lookup, new RegionResolver(REGION, "000000000000"), routeContext) + .filter(request.context()); + + assertEquals("/execute-api/abc123/prod/users", request.routedUri().getRawPath()); + assertNull(routeContext.httpApiRegion()); + } + + @Test + void extractsApiIdFromRegionBearingBuiltInSuffixHosts() { + assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.us-east-1.localhost.floci.io:4566", null)); + assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.us-east-1.localhost.localstack.cloud", null)); + assertNull(ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.us-east-1.example.test", null)); + } + + /** + * A region label is any region a partition recognizes, not a two-letter geo prefix: the EUSC + * partition's {@code eusc-de-east-1} was rejected before the suffix was even looked at, on + * every tail. + */ + @Test + void extractsApiIdFromEuscRegionHosts() { + assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.eusc-de-east-1.localhost.floci.io:4566", null)); + assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.eusc-de-east-1.localhost:4566", null)); + assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.eusc-de-east-1.amazonaws.eu", null)); + } + + /** + * A region launched after the vendored partition data was generated matches its partition's + * region regex without being in the published list; signed requests already accept it. + */ + @Test + void extractsApiIdFromRegionNotYetInTheCatalog() { + assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.eu-south-9.localhost.floci.io", null)); + } + + @Test + void ignoresRegionShapedLabelThatIsNotAKnownRegion() { + assertNull(ApiGatewayExecuteApiHostFilter.extractApiId( + "abc123.execute-api.zz-nowhere-9.localhost.floci.io", null)); + } + + @Test + void routesHttpApiOnEuscRegionBuiltInSuffixHost() { + FakeApiGatewayLookup lookup = new FakeApiGatewayLookup(); + lookup.addApi("eusc-de-east-1", API_ID, "HTTP"); + lookup.addStage("eusc-de-east-1", API_ID, "$default"); + RecordingRequest request = new RecordingRequest( + "abc123.execute-api.eusc-de-east-1.localhost.floci.io:4566", + URI.create("http://abc123.execute-api.eusc-de-east-1.localhost.floci.io:4566/accounts")); + ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext(); + + new ApiGatewayExecuteApiHostFilter( + lookup, new RegionResolver(REGION, "000000000000"), routeContext) + .filter(request.context()); + + assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath()); + assertEquals("eusc-de-east-1", routeContext.httpApiRegion()); + } + @Test void routesRegionBearingConfiguredHost() { FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();