mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigateway): honor configured execute-api hostname (#2286)
* fix(apigateway): honor configured execute-api hostname * refactor(apigateway): cache execute-api hostname
This commit is contained in:
1 parent
ff0201a13a
commit
3cfa983479
5 files changed
+118
-30
No files matched your search
@@ -59,6 +59,12 @@ function builtinSuffixHost(apiId: string): string {
|
||||
return `${apiId}.execute-api.localhost.floci.io${port}`;
|
||||
}
|
||||
|
||||
function configuredHostnameHost(apiId: string, region: string): string {
|
||||
const endpoint = new URL(ENDPOINT);
|
||||
const port = endpoint.port ? `:${endpoint.port}` : '';
|
||||
return `${apiId}.execute-api.${region}.${endpoint.hostname}${port}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Management API client that presents an execute-api Host header while still connecting to the
|
||||
* real test endpoint. A build-step middleware overrides the `Host` header (before SigV4 signing)
|
||||
@@ -184,6 +190,21 @@ describe('API Gateway v2 execute-api subdomain routing', () => {
|
||||
).rejects.toMatchObject({ name: 'GoneException' });
|
||||
});
|
||||
|
||||
it('routes @connections through the region-bearing configured hostname (not S3)', async () => {
|
||||
const region = 'ap-northeast-2';
|
||||
const stage = 'prod';
|
||||
const apiId = await createWsApiWithStage('configured-host-conn', region, stage);
|
||||
const host = configuredHostnameHost(apiId, region);
|
||||
const mgmt = managementClientForHost(host, stage, region);
|
||||
|
||||
// The Node compatibility container connects to the Compose service at floci:4566, while
|
||||
// FLOCI_HOSTNAME=floci. Present the AWS-shaped region-bearing virtual host through the SDK's
|
||||
// signed Host header so this proves configured-host routing without wildcard DNS.
|
||||
await expect(
|
||||
mgmt.send(new GetConnectionCommand({ ConnectionId: 'does-not-exist' }))
|
||||
).rejects.toMatchObject({ name: 'GoneException' });
|
||||
});
|
||||
|
||||
it('delivers a message to a live connection via the subdomain PostToConnection endpoint', async () => {
|
||||
const stage = 'prod';
|
||||
const region = REGION;
|
||||
|
||||
+47
-25
@@ -1,9 +1,11 @@
|
||||
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.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
@@ -22,31 +24,25 @@ import java.util.regex.Pattern;
|
||||
@Provider
|
||||
@PreMatching
|
||||
@Priority(9)
|
||||
@ApplicationScoped
|
||||
public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ApiGatewayExecuteApiHostFilter.class);
|
||||
// Accepts both the region-bearing AWS shape and Floci's built-in DNS suffixes:
|
||||
// {apiId}.execute-api.{region}.localhost[:port] (local, region-bearing)
|
||||
// {apiId}.execute-api.{region}.amazonaws.com (real AWS shape)
|
||||
// {apiId}.execute-api.localhost.floci.io (built-in suffix, regionless)
|
||||
// {apiId}.execute-api.localhost.localstack.cloud (built-in suffix, regionless)
|
||||
// AWS's invoke URL carries the region in the hostname, so the region-bearing form is the
|
||||
// primary target; the regionless built-in suffixes are kept for local convenience (region is
|
||||
// then recovered from the SigV4 scope or a cross-region apiId lookup). Group 1 = apiId.
|
||||
private static final Pattern EXECUTE_API_HOST = Pattern.compile(
|
||||
"^([a-z0-9-]+)\\.execute-api\\."
|
||||
+ "(?:[a-z]{2}-[a-z-]+-\\d+\\.(?:localhost|amazonaws\\.com)"
|
||||
+ "|localhost\\.(?:floci\\.io|localstack\\.cloud))$",
|
||||
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;
|
||||
private final ApiGatewayExecuteRouteContext routeContext;
|
||||
private final String baseHostname;
|
||||
|
||||
@Inject
|
||||
public ApiGatewayExecuteApiHostFilter(ApiGatewayV2Service apiGatewayV2Service,
|
||||
RegionResolver regionResolver,
|
||||
ApiGatewayExecuteRouteContext routeContext) {
|
||||
ApiGatewayExecuteRouteContext routeContext,
|
||||
EmulatorConfig config) {
|
||||
this(new ApiGatewayLookup() {
|
||||
@Override
|
||||
public String resolveApiRegion(String preferredRegion, String apiId) {
|
||||
@@ -67,18 +63,24 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
|
||||
public void requireStage(String region, String apiId, String stageName) {
|
||||
apiGatewayV2Service.getStage(region, apiId, stageName);
|
||||
}
|
||||
}, regionResolver, routeContext);
|
||||
}, regionResolver, routeContext, config.hostname().orElse("localhost"));
|
||||
}
|
||||
|
||||
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver) {
|
||||
this(apiGatewayLookup, regionResolver, new ApiGatewayExecuteRouteContext());
|
||||
this(apiGatewayLookup, regionResolver, new ApiGatewayExecuteRouteContext(), "localhost");
|
||||
}
|
||||
|
||||
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver,
|
||||
ApiGatewayExecuteRouteContext routeContext) {
|
||||
this(apiGatewayLookup, regionResolver, routeContext, "localhost");
|
||||
}
|
||||
|
||||
ApiGatewayExecuteApiHostFilter(ApiGatewayLookup apiGatewayLookup, RegionResolver regionResolver,
|
||||
ApiGatewayExecuteRouteContext routeContext, String baseHostname) {
|
||||
this.apiGatewayLookup = apiGatewayLookup;
|
||||
this.regionResolver = regionResolver;
|
||||
this.routeContext = routeContext;
|
||||
this.baseHostname = baseHostname;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,12 +90,10 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
Matcher matcher = EXECUTE_API_HOST.matcher(stripPort(host));
|
||||
if (!matcher.matches()) {
|
||||
String apiId = extractApiId(host, baseHostname);
|
||||
if (apiId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String apiId = matcher.group(1).toLowerCase(Locale.ROOT);
|
||||
// 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
|
||||
@@ -186,16 +186,38 @@ 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. Shared with the WebSocket {@code $connect} handler so the host
|
||||
* grammar lives in exactly one place (this filter and the handler must agree on which hosts
|
||||
* are execute-api hosts).
|
||||
* 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.
|
||||
*/
|
||||
public static String extractApiId(String host) {
|
||||
public static String extractApiId(String host, String baseHostname) {
|
||||
if (host == null) {
|
||||
return null;
|
||||
}
|
||||
Matcher matcher = EXECUTE_API_HOST.matcher(stripPort(host));
|
||||
return matcher.matches() ? matcher.group(1).toLowerCase(Locale.ROOT) : null;
|
||||
|
||||
Matcher matcher = EXECUTE_API_PREFIX.matcher(stripPort(host));
|
||||
if (!matcher.matches()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String tail = matcher.group(2);
|
||||
if ("localhost.floci.io".equalsIgnoreCase(tail)
|
||||
|| "localhost.localstack.cloud".equalsIgnoreCase(tail)) {
|
||||
return matcher.group(1).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
int firstDot = tail.indexOf('.');
|
||||
if (firstDot <= 0 || !AWS_REGION.matcher(tail.substring(0, firstDot)).matches()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String endpointHost = tail.substring(firstDot + 1);
|
||||
if ("localhost".equalsIgnoreCase(endpointHost)
|
||||
|| "amazonaws.com".equalsIgnoreCase(endpointHost)
|
||||
|| (baseHostname != null && baseHostname.equalsIgnoreCase(endpointHost))) {
|
||||
return matcher.group(1).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+6
-1
@@ -2,8 +2,10 @@ package io.github.hectorvent.floci.services.apigatewayv2.websocket;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.services.apigateway.ApiGatewayExecuteApiHostFilter;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Api;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Integration;
|
||||
@@ -45,6 +47,7 @@ public class WebSocketHandler {
|
||||
private final WebSocketIntegrationInvoker integrationInvoker;
|
||||
private final WebSocketAuthorizerService authorizerService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final String baseHostname;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final io.vertx.core.Vertx vertx;
|
||||
|
||||
@@ -56,6 +59,7 @@ public class WebSocketHandler {
|
||||
WebSocketIntegrationInvoker integrationInvoker,
|
||||
WebSocketAuthorizerService authorizerService,
|
||||
RegionResolver regionResolver,
|
||||
EmulatorConfig config,
|
||||
ObjectMapper objectMapper,
|
||||
io.vertx.core.Vertx vertx) {
|
||||
this.apiGatewayV2Service = apiGatewayV2Service;
|
||||
@@ -65,6 +69,7 @@ public class WebSocketHandler {
|
||||
this.integrationInvoker = integrationInvoker;
|
||||
this.authorizerService = authorizerService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.baseHostname = config.hostname().orElse("localhost");
|
||||
this.objectMapper = objectMapper;
|
||||
this.vertx = vertx;
|
||||
}
|
||||
@@ -97,7 +102,7 @@ public class WebSocketHandler {
|
||||
*/
|
||||
private void handleSubdomainWebSocketUpgrade(RoutingContext ctx) {
|
||||
String host = resolveRequestHost(ctx);
|
||||
String apiId = io.github.hectorvent.floci.services.apigateway.ApiGatewayExecuteApiHostFilter.extractApiId(host);
|
||||
String apiId = ApiGatewayExecuteApiHostFilter.extractApiId(host, baseHostname);
|
||||
if (apiId == null) {
|
||||
ctx.next();
|
||||
return;
|
||||
|
||||
+18
@@ -181,6 +181,24 @@ class ApiGatewayExecuteApiHostFilterTest {
|
||||
assertEquals("ap-northeast-2", routeContext.httpApiRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void routesRegionBearingConfiguredHost() {
|
||||
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.floci:4566",
|
||||
URI.create("http://abc123.execute-api.ap-northeast-2.floci:4566/accounts"));
|
||||
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(
|
||||
lookup, new RegionResolver(REGION, "000000000000"), routeContext, "floci")
|
||||
.filter(request.context());
|
||||
|
||||
assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath());
|
||||
assertEquals("ap-northeast-2", routeContext.httpApiRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void routesWebSocketConnectionsManagementCall() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
|
||||
+26
-4
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
@@ -13,12 +15,14 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(ApiGatewayExecuteApiHostIntegrationTest.ConfiguredHostnameProfile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
|
||||
@@ -138,6 +142,17 @@ class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void invokesRegionBearingConfiguredHost() {
|
||||
given()
|
||||
.header("Host", apiId + ".execute-api." + REGION + ".floci:4566")
|
||||
.when().get("/accounts")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("path", equalTo("/backend"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void createsRestApiWithSameIdentifier() {
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
@@ -218,7 +233,7 @@ class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
@Order(8)
|
||||
void directPathPrefersRestApiWhenIdentifiersCollide() {
|
||||
given()
|
||||
.when().get("/execute-api/" + apiId + "/dev/accounts")
|
||||
@@ -228,7 +243,7 @@ class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
@Order(9)
|
||||
void hostPathRetainsHttpApiRoutingWhenIdentifiersCollide() {
|
||||
given()
|
||||
.header("Host", apiId + ".execute-api.localhost.floci.io")
|
||||
@@ -239,7 +254,7 @@ class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
@Order(10)
|
||||
void disabledExecuteApiEndpointReturnsNotFoundAndCanBeReenabled() {
|
||||
given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
@@ -281,7 +296,7 @@ class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
* so its direct path resolves to REST and never reaches the v2 dispatch this guards.
|
||||
*/
|
||||
@Test
|
||||
@Order(10)
|
||||
@Order(11)
|
||||
void disabledExecuteApiEndpointAlsoRejectsTheDirectPath() {
|
||||
String isolatedApiId = given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
@@ -314,4 +329,11 @@ class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
public static class ConfiguredHostnameProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.hostname", "floci");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user