Light, fluffy, and always free - The AWS Local Emulator alternative
fix(apigateway): import x-amazon-apigateway-any-method in the native image and roll back failed imports (#4473)
Abhigyaa5ded3e162db
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayOpenApiImportCompatibilityTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayOpenApiImportCompatibilityTest.javanew file mode 100644index 0000000000..4f2ba8b538--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayOpenApiImportCompatibilityTest.java@@ -0,0 +1,179 @@+package com.floci.test;++import org.junit.jupiter.api.DisplayName;+import org.junit.jupiter.api.Test;+import software.amazon.awssdk.core.SdkBytes;+import software.amazon.awssdk.services.apigateway.ApiGatewayClient;+import software.amazon.awssdk.services.apigateway.model.BadRequestException;+import software.amazon.awssdk.services.apigateway.model.GetIntegrationResponse;+import software.amazon.awssdk.services.apigateway.model.GetMethodResponse;+import software.amazon.awssdk.services.apigateway.model.ImportRestApiResponse;+import software.amazon.awssdk.services.apigateway.model.PutMode;+import software.amazon.awssdk.services.apigateway.model.Resource;+import software.amazon.awssdk.services.apigateway.model.RestApi;++import java.util.List;++import static org.assertj.core.api.Assertions.assertThat;+import static org.assertj.core.api.Assertions.assertThatThrownBy;++/**+ * OpenAPI import of the {@code x-amazon-apigateway-any-method} pseudo-operation. The JVM test suite+ * cannot see a native-image reflection gap, so this runs the import against the native build.+ */+@DisplayName("API Gateway OpenAPI import")+class ApiGatewayOpenApiImportCompatibilityTest {++ private static final String BROKEN_ANY_METHOD_PATHS = """+ "paths": {+ "/p/{proxy+}": {+ "x-amazon-apigateway-any-method": "not-an-operation-object"+ }+ }+ """;++ @Test+ @DisplayName("ImportRestApi creates an ANY method from x-amazon-apigateway-any-method")+ void importCreatesAnyMethod() {+ String title = TestFixtures.uniqueName("apigw-any-import");+ String spec = """+ {+ "openapi": "3.0.1",+ "info": {"title": "%s", "version": "1"},+ "paths": {+ "/p/{proxy+}": {+ "x-amazon-apigateway-any-method": {+ "parameters": [+ {"name": "proxy", "in": "path", "required": true, "schema": {"type": "string"}}+ ],+ "security": [],+ "x-amazon-apigateway-integration": {+ "type": "http_proxy",+ "httpMethod": "ANY",+ "uri": "http://example.com/{proxy}",+ "requestParameters": {+ "integration.request.path.proxy": "method.request.path.proxy"+ }+ }+ }+ }+ }+ }+ """.formatted(title);++ try (ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient()) {+ ImportRestApiResponse imported = apiGateway.importRestApi(request -> request+ .body(SdkBytes.fromUtf8String(spec)));+ try {+ String resourceId = resourceId(apiGateway, imported.id(), "/p/{proxy+}");++ GetMethodResponse method = apiGateway.getMethod(request -> request+ .restApiId(imported.id())+ .resourceId(resourceId)+ .httpMethod("ANY"));+ assertThat(method.httpMethod()).isEqualTo("ANY");+ assertThat(method.requestParameters()).containsEntry("method.request.path.proxy", true);++ GetIntegrationResponse integration = apiGateway.getIntegration(request -> request+ .restApiId(imported.id())+ .resourceId(resourceId)+ .httpMethod("ANY"));+ assertThat(integration.uri()).isEqualTo("http://example.com/{proxy}");+ assertThat(integration.requestParameters())+ .containsEntry("integration.request.path.proxy", "method.request.path.proxy");+ } finally {+ apiGateway.deleteRestApi(request -> request.restApiId(imported.id()));+ }+ }+ }++ @Test+ @DisplayName("A failed ImportRestApi leaves no REST API behind")+ void failedImportLeavesNothing() {+ String title = TestFixtures.uniqueName("apigw-failed-import");+ String spec = """+ {+ "openapi": "3.0.1",+ "info": {"title": "%s", "version": "1"},+ %s+ }+ """.formatted(title, BROKEN_ANY_METHOD_PATHS);++ try (ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient()) {+ assertThatThrownBy(() -> apiGateway.importRestApi(request -> request+ .body(SdkBytes.fromUtf8String(spec))))+ .isInstanceOf(BadRequestException.class);++ List<String> names = apiGateway.getRestApis(request -> request.limit(500)).items().stream()+ .map(RestApi::name)+ .toList();+ assertThat(names).doesNotContain(title);+ }+ }++ @Test+ @DisplayName("A failed PutRestApi leaves the existing REST API unchanged")+ void failedPutLeavesApiUnchanged() {+ String title = TestFixtures.uniqueName("apigw-failed-put");+ String original = """+ {+ "openapi": "3.0.1",+ "info": {"title": "%s", "version": "1"},+ "paths": {+ "/kept": {+ "get": {+ "x-amazon-apigateway-integration": {+ "type": "http_proxy", "httpMethod": "GET", "uri": "http://example.com/kept"+ }+ }+ }+ }+ }+ """.formatted(title);+ String broken = """+ {+ "openapi": "3.0.1",+ "info": {"title": "%s-renamed", "version": "1"},+ %s+ }+ """.formatted(title, BROKEN_ANY_METHOD_PATHS);++ try (ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient()) {+ ImportRestApiResponse imported = apiGateway.importRestApi(request -> request+ .body(SdkBytes.fromUtf8String(original)));+ try {+ String keptResourceId = resourceId(apiGateway, imported.id(), "/kept");++ assertThatThrownBy(() -> apiGateway.putRestApi(request -> request+ .restApiId(imported.id())+ .mode(PutMode.OVERWRITE)+ .body(SdkBytes.fromUtf8String(broken))))+ .isInstanceOf(BadRequestException.class);++ assertThat(apiGateway.getRestApi(request -> request.restApiId(imported.id())).name())+ .isEqualTo(title);+ List<String> paths = apiGateway.getResources(request -> request.restApiId(imported.id()))+ .items().stream()+ .map(Resource::path)+ .toList();+ assertThat(paths).containsExactlyInAnyOrder("/", "/kept");+ assertThat(resourceId(apiGateway, imported.id(), "/kept")).isEqualTo(keptResourceId);+ GetIntegrationResponse integration = apiGateway.getIntegration(request -> request+ .restApiId(imported.id())+ .resourceId(keptResourceId)+ .httpMethod("GET"));+ assertThat(integration.uri()).isEqualTo("http://example.com/kept");+ } finally {+ apiGateway.deleteRestApi(request -> request.restApiId(imported.id()));+ }+ }+ }++ private static String resourceId(ApiGatewayClient apiGateway, String restApiId, String path) {+ return apiGateway.getResources(request -> request.restApiId(restApiId)).items().stream()+ .filter(resource -> path.equals(resource.path()))+ .map(Resource::id)+ .findFirst()+ .orElseThrow(() -> new AssertionError("No resource with path " + path));+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.javaindex 01a416b259..545896ed24 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java@@ -1,7 +1,9 @@package io.github.hectorvent.floci.services.apigateway;import com.fasterxml.jackson.core.type.TypeReference;+import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.config.TlsCertificateManager;import io.github.hectorvent.floci.core.common.AwsException;@@ -31,12 +33,14 @@ import io.github.hectorvent.floci.services.apigateway.model.Stage;import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;import io.github.hectorvent.floci.services.apigateway.model.UsagePlanKey;import io.github.hectorvent.floci.services.apigateway.model.VpcLink;+import io.swagger.v3.core.util.Json;import io.swagger.v3.oas.models.OpenAPI;import io.swagger.v3.oas.models.Operation;import io.swagger.v3.oas.models.PathItem;import io.swagger.v3.oas.models.security.SecurityRequirement;import io.swagger.v3.oas.models.security.SecurityScheme;import io.swagger.v3.parser.core.models.SwaggerParseResult;+import io.swagger.v3.parser.util.OpenAPIDeserializer;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -55,6 +59,7 @@ import java.util.Objects;import java.util.Optional;import java.util.Set;import java.util.UUID;+import java.util.function.Predicate;import java.util.regex.Pattern;@ApplicationScoped@@ -2643,15 +2648,33 @@ public class ApiGatewayService {request.put("description", description);RestApi api = createRestApi(region, request);- applyOpenApiSpec(region, api.getId(), openAPI);+ try {+ applyOpenApiSpec(region, api.getId(), openAPI);+ } catch (RuntimeException e) {+ // AWS creates nothing when an import fails, and the error carries no id to clean up with.+ restoreRestApi(region, api.getId(), RestApiSnapshot.empty());+ throw e;+ }LOG.infov("Imported REST API from OpenAPI spec: {0} ({1})", name, api.getId());return api;}public RestApi putRestApi(String region, String apiId, String mode, String specBody) {// Note: mode=merge is accepted but treated as overwrite (merge semantics not yet implemented)- RestApi api = getRestApi(region, apiId);+ getRestApi(region, apiId);OpenAPI openAPI = parseOpenApiSpec(specBody);+ RestApiSnapshot snapshot = snapshotRestApi(region, apiId);+ try {+ return overwriteRestApi(region, apiId, openAPI);+ } catch (RuntimeException e) {+ // A failed PutRestApi leaves the API as it was; the overwrite has already cleared it by now.+ restoreRestApi(region, apiId, snapshot);+ throw e;+ }+ }++ private RestApi overwriteRestApi(String region, String apiId, OpenAPI openAPI) {+ RestApi api = getRestApi(region, apiId);// Delete all non-root resourcesList<ApiGatewayResource> existing = getResources(region, apiId);@@ -2687,6 +2710,62 @@ public class ApiGatewayService {return api;}+ /**+ * The API-scoped state an OpenAPI import writes, deep-copied so a failed import can put it back:+ * the stores hand out live objects, and the import mutates some of them in place.+ */+ private record RestApiSnapshot(Map<String, RestApi> apis,+ Map<String, ApiGatewayResource> resources,+ Map<String, Model> models,+ Map<String, RequestValidator> requestValidators,+ Map<String, Authorizer> authorizers,+ Map<String, GatewayResponse> gatewayResponses) {++ static RestApiSnapshot empty() {+ return new RestApiSnapshot(Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), Map.of());+ }+ }++ private RestApiSnapshot snapshotRestApi(String region, String apiId) {+ String prefix = region + "::" + apiId + "::";+ String apiKey = apiKey(region, apiId);+ return new RestApiSnapshot(+ copyEntries(apiStore, apiKey::equals, RestApi.class),+ copyEntries(resourceStore, k -> k.startsWith(prefix), ApiGatewayResource.class),+ copyEntries(modelStore, k -> k.startsWith(prefix), Model.class),+ copyEntries(requestValidatorStore, k -> k.startsWith(prefix), RequestValidator.class),+ copyEntries(authorizerStore, k -> k.startsWith(prefix), Authorizer.class),+ copyEntries(gatewayResponseStore, k -> k.startsWith(prefix), GatewayResponse.class));+ }++ private void restoreRestApi(String region, String apiId, RestApiSnapshot snapshot) {+ String prefix = region + "::" + apiId + "::";+ String apiKey = apiKey(region, apiId);+ replaceEntries(apiStore, apiKey::equals, snapshot.apis());+ replaceEntries(resourceStore, k -> k.startsWith(prefix), snapshot.resources());+ replaceEntries(modelStore, k -> k.startsWith(prefix), snapshot.models());+ replaceEntries(requestValidatorStore, k -> k.startsWith(prefix), snapshot.requestValidators());+ replaceEntries(authorizerStore, k -> k.startsWith(prefix), snapshot.authorizers());+ replaceEntries(gatewayResponseStore, k -> k.startsWith(prefix), snapshot.gatewayResponses());+ }++ private static <T> Map<String, T> copyEntries(StorageBackend<String, T> store, Predicate<String> keyFilter,+ Class<T> type) {+ Map<String, T> copies = new LinkedHashMap<>();+ for (String key : store.keys()) {+ if (keyFilter.test(key)) {+ store.get(key).ifPresent(value -> copies.put(key, JSON.convertValue(value, type)));+ }+ }+ return copies;+ }++ private static <T> void replaceEntries(StorageBackend<String, T> store, Predicate<String> keyFilter,+ Map<String, T> entries) {+ store.keys().stream().filter(keyFilter).toList().forEach(store::delete);+ entries.forEach(store::put);+ }+private OpenAPI parseOpenApiSpec(String specBody) {SwaggerParseResult result = new io.swagger.parser.OpenAPIParser().readContents(specBody, null, null);if (result.getOpenAPI() == null) {@@ -2888,7 +2967,7 @@ public class ApiGatewayService {modelReq.put("contentType", "application/json");try {// Use swagger's own JSON serializer to produce clean JSON Schema- modelReq.put("schema", io.swagger.v3.core.util.Json.mapper().writeValueAsString(schema));+ modelReq.put("schema", Json.mapper().writeValueAsString(schema));} catch (com.fasterxml.jackson.core.JsonProcessingException e) {modelReq.put("schema", "{}");}@@ -3014,29 +3093,39 @@ public class ApiGatewayService {if (pathItem.getExtensions() != null) {Object anyMethodExt = pathItem.getExtensions().get("x-amazon-apigateway-any-method");if (anyMethodExt != null) {+ Operation anyOperation = parseAnyMethodOperation(path, anyMethodExt);try {- Operation anyOperation = io.swagger.v3.core.util.Json.mapper()- .convertValue(anyMethodExt, Operation.class);- try {- applyOperation(region, apiId, resourceId, "ANY", anyOperation, openAPI,- schemeToAuthorizerId, schemeToAuthType, validatorNameToId);- } catch (AwsException e) {- if (PARAMETER_NAME_ERROR.equals(e.getMessage())) {- throw importParameterNameFailure("ANY", path, e);- }- throw e;+ applyOperation(region, apiId, resourceId, "ANY", anyOperation, openAPI,+ schemeToAuthorizerId, schemeToAuthType, validatorNameToId);+ } catch (AwsException e) {+ if (PARAMETER_NAME_ERROR.equals(e.getMessage())) {+ throw importParameterNameFailure("ANY", path, e);}- } catch (IllegalArgumentException e) {- throw new AwsException("BadRequestException",- "Invalid x-amazon-apigateway-any-method definition for path " + path + ": "- + e.getMessage(),- 400);+ throw e;}}}}}+ /**+ * Builds the typed Operation for an ANY pseudo-operation with the swagger parser's own+ * Operation reader, the one that already parses the real verbs. Binding the untyped extension+ * map with Jackson bean deserialization instead needs reflection metadata for the whole+ * swagger model graph, which the native image does not have.+ */+ private Operation parseAnyMethodOperation(String path, Object anyMethodExt) {+ JsonNode node = Json.mapper().valueToTree(anyMethodExt);+ if (!(node instanceof ObjectNode operationNode)) {+ throw new AwsException("BadRequestException",+ "Invalid x-amazon-apigateway-any-method definition for path " + path+ + ": expected an Operation object",+ 400);+ }+ return new OpenAPIDeserializer().getOperation(operationNode,+ "paths.'" + path + "'.x-amazon-apigateway-any-method", new OpenAPIDeserializer.ParseResult());+ }+private void applyOperation(String region, String apiId, String resourceId, String httpMethod,Operation operation, OpenAPI openAPI, Map<String, String> schemeToAuthorizerId,Map<String, String> schemeToAuthType, Map<String, String> validatorNameToId) {diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayOpenApiAnyMethodImportTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayOpenApiAnyMethodImportTest.javaindex 9eaea1a455..8944de30bc 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayOpenApiAnyMethodImportTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayOpenApiAnyMethodImportTest.java@@ -11,6 +11,8 @@ import org.junit.jupiter.api.TestMethodOrder;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.hasItem;+import static org.hamcrest.Matchers.not;import static org.hamcrest.Matchers.notNullValue;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -171,8 +173,8 @@ class ApiGatewayOpenApiAnyMethodImportTest {@Order(6)void import_withMalformedAnyMethodExtension_returnsCleanBadRequest() {// "x-amazon-apigateway-any-method" set to a plain string instead of an operation object.- // Json.mapper().convertValue(..., Operation.class) can't coerce this and must not be allowed- // to escape as an uncaught IllegalArgumentException (which would surface as a raw 500).+ // It is not an Operation object and must be rejected as a BadRequestException, not escape as+ // an uncaught exception (which would surface as a raw 500).String spec = """{"openapi": "3.0.1",@@ -237,4 +239,179 @@ class ApiGatewayOpenApiAnyMethodImportTest {.statusCode(400).body("__type", equalTo("BadRequestException"));}++ @Test+ @Order(8)+ void importedAnyMethod_keepsPathParametersAndIntegrationRequestParameters() throws Exception {+ String spec = """+ {+ "openapi": "3.0.1",+ "info": {"title": "AnyMethodProxyAPI", "version": "1.0"},+ "paths": {+ "/p/{proxy+}": {+ "x-amazon-apigateway-any-method": {+ "parameters": [+ {"name": "proxy", "in": "path", "required": true, "schema": {"type": "string"}}+ ],+ "security": [],+ "x-amazon-apigateway-integration": {+ "type": "http_proxy",+ "httpMethod": "ANY",+ "uri": "http://example.com/{proxy}",+ "requestParameters": {+ "integration.request.path.proxy": "method.request.path.proxy"+ }+ }+ }+ }+ }+ }+ """;++ String proxyApiId = given()+ .contentType(ContentType.JSON)+ .queryParam("mode", "import")+ .body(spec)+ .when().post("/restapis")+ .then()+ .statusCode(201)+ .extract().path("id");++ try {+ String resourceId = resourceIdForPath(proxyApiId, "/p/{proxy+}");+ given()+ .when().get("/restapis/" + proxyApiId + "/resources/" + resourceId + "/methods/ANY")+ .then()+ .statusCode(200)+ .body("httpMethod", equalTo("ANY"))+ .body("requestParameters.'method.request.path.proxy'", equalTo(true));+ given()+ .when().get("/restapis/" + proxyApiId + "/resources/" + resourceId + "/methods/ANY/integration")+ .then()+ .statusCode(200)+ .body("uri", equalTo("http://example.com/{proxy}"))+ .body("requestParameters.'integration.request.path.proxy'",+ equalTo("method.request.path.proxy"));+ } finally {+ given().when().delete("/restapis/" + proxyApiId).then().statusCode(202);+ }+ }++ @Test+ @Order(9)+ void failedImport_leavesNoRestApiBehind() {+ String spec = """+ {+ "openapi": "3.0.1",+ "info": {"title": "FailedImportLeavesNothingAPI", "version": "1.0"},+ "paths": {+ "/p/{proxy+}": {+ "x-amazon-apigateway-any-method": "not-an-operation-object"+ }+ }+ }+ """;++ given()+ .contentType(ContentType.JSON)+ .queryParam("mode", "import")+ .body(spec)+ .when().post("/restapis")+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"));++ given()+ .when().get("/restapis")+ .then()+ .statusCode(200)+ .body("item.name", not(hasItem("FailedImportLeavesNothingAPI")));+ }++ @Test+ @Order(10)+ void failedPutRestApi_leavesExistingApiUnchanged() throws Exception {+ String original = """+ {+ "openapi": "3.0.1",+ "info": {"title": "PutRollbackAPI", "version": "1.0"},+ "paths": {+ "/kept": {+ "get": {+ "x-amazon-apigateway-integration": {+ "type": "http_proxy", "httpMethod": "GET", "uri": "http://example.com/kept"+ }+ }+ }+ }+ }+ """;+ String broken = """+ {+ "openapi": "3.0.1",+ "info": {"title": "PutRollbackAPIRenamed", "version": "1.0"},+ "paths": {+ "/p/{proxy+}": {+ "x-amazon-apigateway-any-method": "not-an-operation-object"+ }+ }+ }+ """;++ String putApiId = given()+ .contentType(ContentType.JSON)+ .queryParam("mode", "import")+ .body(original)+ .when().post("/restapis")+ .then()+ .statusCode(201)+ .extract().path("id");++ try {+ String keptResourceId = resourceIdForPath(putApiId, "/kept");++ given()+ .contentType(ContentType.JSON)+ .queryParam("mode", "overwrite")+ .body(broken)+ .when().put("/restapis/" + putApiId)+ .then()+ .statusCode(400)+ .body("__type", equalTo("BadRequestException"));++ given()+ .when().get("/restapis/" + putApiId)+ .then()+ .statusCode(200)+ .body("name", equalTo("PutRollbackAPI"));+ given()+ .when().get("/restapis/" + putApiId + "/resources")+ .then()+ .statusCode(200)+ .body("item.path", not(hasItem("/p")))+ .body("item.path", not(hasItem("/p/{proxy+}")));+ assertEquals(keptResourceId, resourceIdForPath(putApiId, "/kept"));+ given()+ .when().get("/restapis/" + putApiId + "/resources/" + keptResourceId + "/methods/GET/integration")+ .then()+ .statusCode(200)+ .body("uri", equalTo("http://example.com/kept"));+ } finally {+ given().when().delete("/restapis/" + putApiId).then().statusCode(202);+ }+ }++ private static String resourceIdForPath(String restApiId, String path) throws Exception {+ String body = given()+ .when().get("/restapis/" + restApiId + "/resources")+ .then()+ .statusCode(200)+ .extract().body().asString();+ for (JsonNode resource : mapper.readTree(body).get("item")) {+ if (path.equals(resource.get("path").asText())) {+ return resource.get("id").asText();+ }+ }+ throw new AssertionError("No resource with path " + path + " in " + body);+ }}