mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(apigateway): route execute-api hostnames for HTTP APIs (#2054)
* feat(apigateway): route execute-api hostnames for HTTP APIs
* fix(apigateway): disambiguate execute-api routing
* fix(apigateway): make the execute-api host filter idempotent
Host matching alone cannot tell whether a request has already been rewritten,
because the Host header still names the execute-api subdomain afterwards. If any
other filter rewrites first, this one rewrites again and produces
/execute-api/{apiId}/execute-api/{apiId}/... which routes to nothing.
Bail out when the path is already under /execute-api/. This keeps the filter
correct regardless of where it sits in the provider chain.
* fix(apigateway): enforce disableExecuteApiEndpoint on the direct execute-api path
The setting was only checked in ApiGatewayExecuteApiHostFilter, which sees the
host-based route (*.execute-api.localhost.*). A request to the direct
/execute-api/{apiId}/{stageName}/... form reaches dispatchV2 without passing the
filter, so the same disabled HTTP API answered 403 by host and 200 by path.
Move the check into dispatchV2, the single choke point both entry points funnel
through, so they cannot disagree. A missing API is left to findMatchingRoute's 404
rather than reported from here.
* test(dynamodb): retain Quarkus port for cleanup
* fix(apigateway): match disabled HTTP API response
---------
Co-authored-by: Alexander Banna <abanna@users.noreply.github.com>
This commit is contained in:
1 parent
e1f389f61b
commit
33ba5ac59c
19 files changed
+958
-34
No files matched your search
@@ -229,7 +229,9 @@ class TlsCertificateHostnameTest {
|
||||
List<String> sans = extractSansFromCertificate(cert);
|
||||
|
||||
Set<String> expectedSans = Set.of("localhost", "127.0.0.1", "0.0.0.0", "*.localhost",
|
||||
"localhost.floci.io", "*.localhost.floci.io", "host.docker.internal");
|
||||
"localhost.floci.io", "*.localhost.floci.io",
|
||||
"*.execute-api.localhost.floci.io",
|
||||
"*.execute-api.localhost.localstack.cloud", "host.docker.internal");
|
||||
Set<String> actualSans = new HashSet<>(sans);
|
||||
|
||||
assertEquals(expectedSans, actualSans,
|
||||
@@ -252,7 +254,9 @@ class TlsCertificateHostnameTest {
|
||||
List<String> sans = extractSansFromCertificate(cert);
|
||||
|
||||
Set<String> expectedSans = Set.of("localhost", "127.0.0.1", "0.0.0.0", "*.localhost",
|
||||
"localhost.floci.io", "*.localhost.floci.io", "host.docker.internal");
|
||||
"localhost.floci.io", "*.localhost.floci.io",
|
||||
"*.execute-api.localhost.floci.io",
|
||||
"*.execute-api.localhost.localstack.cloud", "host.docker.internal");
|
||||
Set<String> actualSans = new HashSet<>(sans);
|
||||
|
||||
assertEquals(expectedSans, actualSans,
|
||||
|
||||
+6
-2
@@ -160,10 +160,14 @@ class TlsConfigSourceCertificateGenerationTest {
|
||||
|
||||
assertTrue(sans.contains("host.docker.internal"),
|
||||
"Certificate SANs should include default 'host.docker.internal'");
|
||||
assertTrue(sans.contains("*.execute-api.localhost.floci.io"),
|
||||
"Certificate SANs should include API Gateway execution hosts");
|
||||
assertTrue(sans.contains("*.execute-api.localhost.localstack.cloud"),
|
||||
"Certificate SANs should include LocalStack-compatible API Gateway execution hosts");
|
||||
|
||||
// Should not contain any custom hostnames
|
||||
assertEquals(7, sans.size(),
|
||||
"Certificate SANs should contain exactly 7 default entries (localhost, 127.0.0.1, 0.0.0.0, *.localhost, localhost.floci.io, *.localhost.floci.io, host.docker.internal)");
|
||||
assertEquals(9, sans.size(),
|
||||
"Certificate SANs should contain exactly 9 default entries, including API Gateway execution hosts");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+298
@@ -0,0 +1,298 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class ApiGatewayExecuteApiHostFilterTest {
|
||||
|
||||
private static final String API_ID = "abc123";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Test
|
||||
void routesStageLessRequestToDefaultStageAndPreservesQuery() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
lookup.addStage(API_ID, "$default");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io:4566",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io:4566/accounts?tenant=alpha"));
|
||||
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("tenant=alpha", request.routedUri().getQuery());
|
||||
assertEquals(REGION, routeContext.httpApiRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void routesExplicitStageAndRemovesItFromRemainingPath() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
lookup.addStage(API_ID, "dev");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.localstack.cloud",
|
||||
URI.create("http://abc123.execute-api.localhost.localstack.cloud/dev/accounts"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertEquals("/execute-api/abc123/dev/accounts", request.routedUri().getRawPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preservesEncodedPathAndQueryComponents() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
lookup.addStage(API_ID, "$default");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/files/a%2Fb?value=a%2Fb"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertEquals("/execute-api/abc123/$default/files/a%2Fb", request.routedUri().getRawPath());
|
||||
assertEquals("value=a%2Fb", request.routedUri().getRawQuery());
|
||||
}
|
||||
|
||||
@Test
|
||||
void routesStageRootWithTrailingSlash() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
lookup.addStage(API_ID, "dev");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/dev"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertEquals("/execute-api/abc123/dev/", request.routedUri().getRawPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesUnsignedApiOutsideDefaultRegion() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi("us-west-2", API_ID, "HTTP");
|
||||
lookup.addStage("us-west-2", API_ID, "$default");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/accounts"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizesMixedCaseHostApiId() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
lookup.addStage(API_ID, "$default");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"AbC123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/accounts"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsDisabledExecuteApiEndpoint() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
lookup.addStage(API_ID, "$default");
|
||||
lookup.disableExecuteApiEndpoint(API_ID);
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/accounts"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertNull(request.routedUri());
|
||||
assertEquals(404, request.abortedResponse().getStatus());
|
||||
assertEquals("{\"message\":\"Not Found\"}", request.abortedResponse().getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresWebSocketApi() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(REGION, API_ID, "WEBSOCKET");
|
||||
lookup.addStage(API_ID, "$default");
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/accounts"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertNull(request.routedUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotMarkHttpRouteWhenNoStageMatches() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
lookup.addApi(API_ID);
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"abc123.execute-api.localhost.floci.io",
|
||||
URI.create("http://abc123.execute-api.localhost.floci.io/accounts"));
|
||||
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(
|
||||
lookup, new RegionResolver(REGION, "000000000000"), routeContext)
|
||||
.filter(request.context());
|
||||
|
||||
assertNull(request.routedUri());
|
||||
assertNull(routeContext.httpApiRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresNonExecuteApiHost() {
|
||||
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
|
||||
RecordingRequest request = new RecordingRequest(
|
||||
"localhost:4566",
|
||||
URI.create("http://localhost:4566/accounts"));
|
||||
|
||||
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
|
||||
.filter(request.context());
|
||||
|
||||
assertNull(request.routedUri());
|
||||
assertEquals(0, lookup.apiLookups);
|
||||
}
|
||||
|
||||
private static final class FakeApiGatewayLookup implements ApiGatewayExecuteApiHostFilter.ApiGatewayLookup {
|
||||
private final Map<String, String> protocols = new HashMap<>();
|
||||
private final Map<String, Boolean> stages = new HashMap<>();
|
||||
private final Map<String, Boolean> disabledExecuteApiEndpoints = new HashMap<>();
|
||||
private int apiLookups;
|
||||
|
||||
void addApi(String apiId) {
|
||||
addApi(REGION, apiId, "HTTP");
|
||||
}
|
||||
|
||||
void addApi(String region, String apiId, String protocolType) {
|
||||
protocols.put(region + "/" + apiId, protocolType);
|
||||
}
|
||||
|
||||
void addStage(String apiId, String stageName) {
|
||||
addStage(REGION, apiId, stageName);
|
||||
}
|
||||
|
||||
void addStage(String region, String apiId, String stageName) {
|
||||
stages.put(region + "/" + apiId + "/" + stageName, true);
|
||||
}
|
||||
|
||||
void disableExecuteApiEndpoint(String apiId) {
|
||||
disabledExecuteApiEndpoints.put(REGION + "/" + apiId, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String resolveApiRegion(String preferredRegion, String apiId) {
|
||||
if (protocols.containsKey(preferredRegion + "/" + apiId)) {
|
||||
return preferredRegion;
|
||||
}
|
||||
String suffix = "/" + apiId;
|
||||
return protocols.keySet().stream()
|
||||
.filter(key -> key.endsWith(suffix))
|
||||
.map(key -> key.substring(0, key.indexOf('/')))
|
||||
.findFirst()
|
||||
.orElse(preferredRegion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String protocolType(String region, String apiId) {
|
||||
apiLookups++;
|
||||
String protocolType = protocols.get(region + "/" + apiId);
|
||||
if (protocolType == null) {
|
||||
throw new AwsException("NotFoundException", "Invalid API id specified", 404);
|
||||
}
|
||||
return protocolType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean executeApiEndpointDisabled(String region, String apiId) {
|
||||
return disabledExecuteApiEndpoints.getOrDefault(region + "/" + apiId, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requireStage(String region, String apiId, String stageName) {
|
||||
if (!stages.containsKey(region + "/" + apiId + "/" + stageName)) {
|
||||
throw new AwsException("NotFoundException", "Stage not found", 404);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingRequest {
|
||||
private final String host;
|
||||
private final URI requestUri;
|
||||
private URI routedUri;
|
||||
private Response abortedResponse;
|
||||
|
||||
private RecordingRequest(String host, URI requestUri) {
|
||||
this.host = host;
|
||||
this.requestUri = requestUri;
|
||||
}
|
||||
|
||||
private ContainerRequestContext context() {
|
||||
return (ContainerRequestContext) Proxy.newProxyInstance(
|
||||
ContainerRequestContext.class.getClassLoader(),
|
||||
new Class<?>[] { ContainerRequestContext.class },
|
||||
(proxy, method, args) -> switch (method.getName()) {
|
||||
case "getHeaderString" -> header((String) args[0]);
|
||||
case "getUriInfo" -> uriInfo();
|
||||
case "setRequestUri" -> {
|
||||
routedUri = (URI) args[0];
|
||||
yield null;
|
||||
}
|
||||
case "abortWith" -> {
|
||||
abortedResponse = (Response) args[0];
|
||||
yield null;
|
||||
}
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
|
||||
private URI routedUri() {
|
||||
return routedUri;
|
||||
}
|
||||
|
||||
private Response abortedResponse() {
|
||||
return abortedResponse;
|
||||
}
|
||||
|
||||
private String header(String name) {
|
||||
return "Host".equals(name) ? host : null;
|
||||
}
|
||||
|
||||
private UriInfo uriInfo() {
|
||||
return (UriInfo) Proxy.newProxyInstance(
|
||||
UriInfo.class.getClassLoader(),
|
||||
new Class<?>[] { UriInfo.class },
|
||||
(proxy, method, args) -> {
|
||||
if ("getRequestUri".equals(method.getName())) {
|
||||
return requestUri;
|
||||
}
|
||||
throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayExecuteApiHostIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-west-2";
|
||||
private static final String AUTHORIZATION =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260730/" + REGION + "/apigateway/aws4_request";
|
||||
|
||||
private static HttpServer backend;
|
||||
private static int backendPort;
|
||||
private static String apiId;
|
||||
private static String integrationId;
|
||||
|
||||
@BeforeAll
|
||||
static void startBackend() throws IOException {
|
||||
backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
backend.createContext("/", exchange -> {
|
||||
String query = exchange.getRequestURI().getRawQuery();
|
||||
byte[] response = """
|
||||
{"path":"%s","query":"%s"}
|
||||
""".formatted(exchange.getRequestURI().getRawPath(), query == null ? "" : query)
|
||||
.strip()
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().add("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
exchange.getResponseBody().write(response);
|
||||
exchange.close();
|
||||
});
|
||||
backend.start();
|
||||
backendPort = backend.getAddress().getPort();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopBackend() {
|
||||
if (backend != null) {
|
||||
backend.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createsHttpApiOutsideDefaultRegion() {
|
||||
apiId = given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"execute-api-host-test","protocolType":"HTTP"}
|
||||
""")
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("apiId", notNullValue())
|
||||
.body("disableExecuteApiEndpoint", equalTo(false))
|
||||
.extract().path("apiId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void configuresDefaultAndExplicitStages() {
|
||||
createStage("$default");
|
||||
createStage("dev");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void configuresHttpProxyRoute() {
|
||||
integrationId = given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"integrationType":"HTTP_PROXY",
|
||||
"integrationUri":"http://127.0.0.1:%d/backend",
|
||||
"integrationMethod":"GET",
|
||||
"payloadFormatVersion":"1.0"
|
||||
}
|
||||
""".formatted(backendPort))
|
||||
.when().post("/v2/apis/" + apiId + "/integrations")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("integrationId", notNullValue())
|
||||
.extract().path("integrationId");
|
||||
|
||||
given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"routeKey":"GET /accounts","target":"integrations/%s"}
|
||||
""".formatted(integrationId))
|
||||
.when().post("/v2/apis/" + apiId + "/routes")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void invokesDefaultStageByHostWithoutAuthorizationAndPreservesQuery() {
|
||||
given()
|
||||
.header("Host", apiId + ".execute-api.localhost.floci.io:4566")
|
||||
.queryParam("tenant", "alpha")
|
||||
.when().get("/accounts")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("path", equalTo("/backend"))
|
||||
.body("query", equalTo("tenant=alpha"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void invokesExplicitStageByCompatibleHost() {
|
||||
given()
|
||||
.header("Host", apiId + ".execute-api.localhost.localstack.cloud")
|
||||
.when().get("/dev/accounts")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("path", equalTo("/backend"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void createsRestApiWithSameIdentifier() {
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"rest-routing-collision","tags":{"floci:override-id":"%s"}}
|
||||
""".formatted(apiId))
|
||||
.when().post("/restapis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("id", equalTo(apiId));
|
||||
|
||||
String rootId = given()
|
||||
.when().get("/restapis/" + apiId + "/resources")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("item[0].id");
|
||||
|
||||
String resourceId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"pathPart\":\"accounts\"}")
|
||||
.when().post("/restapis/" + apiId + "/resources/" + rootId)
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("id");
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"authorizationType\":\"NONE\"}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"responseParameters\":{}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET/responses/200")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"type":"MOCK","requestTemplates":{"application/json":"{\\"statusCode\\":200}"}}
|
||||
""")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET/integration")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"selectionPattern":"",
|
||||
"responseTemplates":{"application/json":"{\\"source\\":\\"rest\\"}"}
|
||||
}
|
||||
""")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + resourceId
|
||||
+ "/methods/GET/integration/responses/200")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
String deploymentId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"description\":\"routing collision regression\"}")
|
||||
.when().post("/restapis/" + apiId + "/deployments")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("id");
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"stageName":"dev","deploymentId":"%s"}
|
||||
""".formatted(deploymentId))
|
||||
.when().post("/restapis/" + apiId + "/stages")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void directPathPrefersRestApiWhenIdentifiersCollide() {
|
||||
given()
|
||||
.when().get("/execute-api/" + apiId + "/dev/accounts")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("source", equalTo("rest"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void hostPathRetainsHttpApiRoutingWhenIdentifiersCollide() {
|
||||
given()
|
||||
.header("Host", apiId + ".execute-api.localhost.floci.io")
|
||||
.when().get("/dev/accounts")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("path", equalTo("/backend"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void disabledExecuteApiEndpointReturnsNotFoundAndCanBeReenabled() {
|
||||
given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"disableExecuteApiEndpoint":true}
|
||||
""")
|
||||
.when().patch("/v2/apis/" + apiId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("disableExecuteApiEndpoint", equalTo(true));
|
||||
|
||||
given()
|
||||
.header("Host", apiId + ".execute-api.localhost.floci.io")
|
||||
.when().get("/accounts")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("message", equalTo("Not Found"));
|
||||
|
||||
given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"disableExecuteApiEndpoint":false}
|
||||
""")
|
||||
.when().patch("/v2/apis/" + apiId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("disableExecuteApiEndpoint", equalTo(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* The direct {@code /execute-api/{apiId}/{stage}/...} form is the same execute-api endpoint as
|
||||
* the {@code *.execute-api.localhost.*} host, so {@code disableExecuteApiEndpoint} has to reject
|
||||
* there too — otherwise the two entry points disagree about whether the API is invokable.
|
||||
*
|
||||
* <p>Uses its own HTTP API rather than the shared {@code apiId}: by this point the shared one has
|
||||
* a REST API with a colliding identifier (see {@link #directPathPrefersRestApiWhenIdentifiersCollide}),
|
||||
* so its direct path resolves to REST and never reaches the v2 dispatch this guards.
|
||||
*/
|
||||
@Test
|
||||
@Order(10)
|
||||
void disabledExecuteApiEndpointAlsoRejectsTheDirectPath() {
|
||||
String isolatedApiId = given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"execute-api-direct-disable-test","protocolType":"HTTP",
|
||||
"disableExecuteApiEndpoint":true}
|
||||
""")
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("disableExecuteApiEndpoint", equalTo(true))
|
||||
.extract().path("apiId");
|
||||
|
||||
given()
|
||||
.when().get("/execute-api/" + isolatedApiId + "/dev/accounts")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("message", equalTo("Not Found"));
|
||||
}
|
||||
|
||||
private static void createStage(String stageName) {
|
||||
given()
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"stageName":"%s","autoDeploy":true}
|
||||
""".formatted(stageName))
|
||||
.when().post("/v2/apis/" + apiId + "/stages")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -28,7 +28,7 @@ class ApiGatewayExecuteControllerTest {
|
||||
return new ApiGatewayExecuteController(
|
||||
null, null, null,
|
||||
regionResolver, objectMapper, null,
|
||||
null, null, null, null);
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-1
@@ -35,7 +35,8 @@ class ApiGatewayProxyMatchTest {
|
||||
void setUp() {
|
||||
ctrl = new ApiGatewayExecuteController(apiGatewayService, apiGatewayV2Service, lambdaService,
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager, elbV2Service, null);
|
||||
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager, elbV2Service, null,
|
||||
new ApiGatewayExecuteRouteContext());
|
||||
}
|
||||
|
||||
private ApiGatewayResource resource(String id, String parentId, String pathPart, String path) {
|
||||
|
||||
+2
-2
@@ -50,7 +50,7 @@ class BuildV2ProxyEventBodyEncodingTest {
|
||||
controller = new ApiGatewayExecuteController(
|
||||
null, null, null,
|
||||
regionResolver, objectMapper, null,
|
||||
null, null, null, null
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ class BuildV2ProxyEventBodyEncodingTest {
|
||||
|
||||
String json = controller.buildV2ProxyEvent(
|
||||
"POST", "/echo", "POST /echo",
|
||||
"abc123", "$default", headers, uriInfo, requestBody, "req-1");
|
||||
"abc123", "us-east-1", "$default", headers, uriInfo, requestBody, "req-1");
|
||||
|
||||
JsonNode event = objectMapper.readTree(json);
|
||||
if (expectedBody == null) {
|
||||
|
||||
+6
-4
@@ -41,7 +41,7 @@ class BuildV2ProxyEventPathParametersTest {
|
||||
controller = new ApiGatewayExecuteController(
|
||||
null, null, null,
|
||||
regionResolver, new ObjectMapper(), null,
|
||||
null, null, null, null
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -49,10 +49,12 @@ class BuildV2ProxyEventPathParametersTest {
|
||||
void greedyProxyRoutePopulatesPathParameters() throws Exception {
|
||||
String json = controller.buildV2ProxyEvent(
|
||||
"POST", "/trpc/health", "ANY /{proxy+}",
|
||||
"abc123", "$default", headers, uriInfo, null, "req-1");
|
||||
"abc123", "us-west-2", "$default", headers, uriInfo, null, "req-1");
|
||||
JsonNode event = new ObjectMapper().readTree(json);
|
||||
assertTrue(event.has("pathParameters"), "pathParameters must be present");
|
||||
assertEquals("trpc/health", event.get("pathParameters").get("proxy").asText());
|
||||
assertEquals("abc123.execute-api.us-west-2.amazonaws.com",
|
||||
event.get("requestContext").get("domainName").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,7 +62,7 @@ class BuildV2ProxyEventPathParametersTest {
|
||||
when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/api/stage/users/42"));
|
||||
String json = controller.buildV2ProxyEvent(
|
||||
"GET", "/users/42", "GET /users/{id}",
|
||||
"abc123", "$default", headers, uriInfo, null, "req-2");
|
||||
"abc123", "us-east-2", "$default", headers, uriInfo, null, "req-2");
|
||||
JsonNode event = new ObjectMapper().readTree(json);
|
||||
assertTrue(event.has("pathParameters"), "pathParameters must be present");
|
||||
assertEquals("42", event.get("pathParameters").get("id").asText());
|
||||
@@ -71,7 +73,7 @@ class BuildV2ProxyEventPathParametersTest {
|
||||
when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/api/stage/anything"));
|
||||
String json = controller.buildV2ProxyEvent(
|
||||
"GET", "/anything", "$default",
|
||||
"abc123", "$default", headers, uriInfo, null, "req-3");
|
||||
"abc123", "us-east-1", "$default", headers, uriInfo, null, "req-3");
|
||||
JsonNode event = new ObjectMapper().readTree(json);
|
||||
assertFalse(event.has("pathParameters"), "pathParameters must be absent for $default route");
|
||||
}
|
||||
|
||||
+18
-1
@@ -48,7 +48,11 @@ class ApiGatewayV2JsonHandlerTest {
|
||||
.header("X-Amz-Target", TARGET_PREFIX + "CreateApi")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{"Name":"json11-test","ProtocolType":"HTTP"}
|
||||
{
|
||||
"Name":"json11-test",
|
||||
"ProtocolType":"HTTP",
|
||||
"DisableExecuteApiEndpoint":true
|
||||
}
|
||||
""")
|
||||
.when().post("/")
|
||||
.then()
|
||||
@@ -59,7 +63,20 @@ class ApiGatewayV2JsonHandlerTest {
|
||||
// AWS defaults must be populated
|
||||
.body("RouteSelectionExpression", equalTo("${request.method} ${request.path}"))
|
||||
.body("ApiKeySelectionExpression", equalTo("$request.header.x-api-key"))
|
||||
.body("DisableExecuteApiEndpoint", equalTo(true))
|
||||
.extract().path("ApiId");
|
||||
|
||||
given()
|
||||
.contentType(AMZ_JSON)
|
||||
.header("X-Amz-Target", TARGET_PREFIX + "UpdateApi")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{"ApiId":"%s","DisableExecuteApiEndpoint":false}
|
||||
""".formatted(apiId))
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DisableExecuteApiEndpoint", equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user