Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): delete the REST API whose Body import fails on create (#6472)
Jackson Price93f190410644
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.javaindex 17777d2204..3f442ebb95 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java@@ -311,26 +311,35 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {api = apiGatewayService.createRestApi(region, req);}r.setPhysicalId(api.getId());- r.getAttributes().put("RestApiId", api.getId());- // Not the first of getResources, which is the root only on a freshly created API.- r.getAttributes().put("RootResourceId",- apiGatewayService.findRootResourceId(region, api.getId()).orElseThrow());-+ boolean created = existing == null;// putRestApi overwrites Name and Description from the document's info, so the declared Name// and Description (null clearing an undeclared Description) are re-applied immediately after.- if (openApiDocument != null) {- try {- apiGatewayService.putRestApi(region, api.getId(), "overwrite", openApiDocument.toString());- } catch (RuntimeException failure) {- if (existing != null) {- unwind(r, failure);- } else if (ctx.isUpdate()) {- discard(r, api.getId(), region, failure);- }- throw failure;- }- apiGatewayService.updateRestApi(region, api.getId(),- List.of(replacePatchOp("/name", name), replacePatchOp("/description", description)));+ List<Map<String, String>> declaredNameAndDescription = List.of(+ replacePatchOp("/name", name), replacePatchOp("/description", description));+ // A step that fails once the API exists undoes this provision: an API it created is deleted+ // again (or left marked for the stack's rollback when that delete fails too), and a kept API+ // gets back what the update patched. putRestApi leaves the API as it was when it rejects the+ // document.+ CfnRollback.guarded(r, ctx, created,+ () -> delete(REST_API, api.getId(), region),+ () -> restore(api.getId(), readSnapshot(r)),+ () -> {+ r.getAttributes().put("RestApiId", api.getId());+ // Not the first of getResources, which is the root only on a freshly created API.+ r.getAttributes().put("RootResourceId",+ apiGatewayService.findRootResourceId(region, api.getId()).orElseThrow());+ if (openApiDocument != null) {+ apiGatewayService.putRestApi(region, api.getId(), "overwrite", openApiDocument.toString());+ if (created) {+ apiGatewayService.updateRestApi(region, api.getId(), declaredNameAndDescription);+ }+ }+ });+ // On a kept API the re-apply follows the guard: the document putRestApi accepted replaced the+ // API's definition, which the snapshot does not hold, so putting back only the patch would+ // not restore the API.+ if (!created && openApiDocument != null) {+ apiGatewayService.updateRestApi(region, api.getId(), declaredNameAndDescription);}// An API created in place of one removed out of band is a replacement: a rollback deletes it.ReplacementCleanup.record(r, ctx, attributesBefore);@@ -411,45 +420,6 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {apiGatewayService.updateRestApi(region, apiId, operations);}- /**- * An in-place update whose OpenAPI document putRestApi rejected puts back the name, description- * and endpoint configuration it had already patched; putRestApi left the definition as it was.- * CloudFormationService restores the resource the stack held before the attempt, which never- * carried the snapshot, so the rollback walker would leave the patch in place. A restore that- * fails is recorded as a rollback failure, so the stack ends in UPDATE_ROLLBACK_FAILED instead- * of reporting the API intact.- */- private void unwind(StackResource r, RuntimeException failure) {- try {- restore(r.getPhysicalId(), readSnapshot(r));- } catch (RuntimeException unwindFailure) {- r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,- "Could not roll back the update of REST API " + r.getPhysicalId() + ": "- + unwindFailure.getMessage());- failure.addSuppressed(unwindFailure);- }- }-- /**- * Deletes the API an update created in place of one removed out of band, when putRestApi- * rejected its OpenAPI document. CloudFormationService restores the resource the stack held- * before the attempt, so nothing else tracks the new API. An API that cannot be deleted is- * listed for the next cleanup, which the restored resource inherits, and the update reports a- * rollback failure. {@code failure} stays the reported error.- */- private void discard(StackResource r, String apiId, String region, RuntimeException failure) {- try {- delete(REST_API, apiId, region);- } catch (RuntimeException deleteFailure) {- failure.addSuppressed(deleteFailure);- LOG.warnv("Could not remove REST API {0}, created for {1} by a failed update: {2}",- apiId, r.getLogicalId(), deleteFailure.getMessage());- r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR, "Could not remove REST API " + apiId- + ", created for " + r.getLogicalId() + " by a failed update: " + deleteFailure.getMessage());- ReplacementCleanup.recordOrphan(r, apiId, REST_API, region);- }- }-/*** A {@code replace} patch operation for {@link ApiGatewayService#updateRestApi}, allowing a* {@code null} value ({@code Map.of} rejects one) so an undeclared property can still bediff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayRestApiCfnImportRollbackIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayRestApiCfnImportRollbackIntegrationTest.javanew file mode 100644index 0000000000..427992fc35--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayRestApiCfnImportRollbackIntegrationTest.java@@ -0,0 +1,114 @@+package io.github.hectorvent.floci.services.cloudformation;++import io.github.hectorvent.floci.core.common.XmlParser;+import io.github.hectorvent.floci.testing.CfnStackWaits;+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotEquals;++/**+ * A stack whose AWS::ApiGateway::RestApi declares a Body that PutRestApi rejects fails to create.+ * The REST API was created before the import ran, and the rollback removes it, so neither+ * ROLLBACK_COMPLETE nor DeleteStack leaves it behind. A REST API of the same name that the stack+ * did not create is left alone.+ */+@QuarkusTest+class ApiGatewayRestApiCfnImportRollbackIntegrationTest {++ private static final String API_NAME = "cfn-restapi-import-rollback";+ /** A token authorizer whose cache TTL is above the 3600-second maximum, which the import rejects. */+ private static final String TEMPLATE = """+ {"Resources":{"Api":{"Type":"AWS::ApiGateway::RestApi","Properties":{+ "Name":"cfn-restapi-import-rollback",+ "Body":{"openapi":"3.0.1","info":{"title":"cfn-restapi-import-rollback","version":"1"},+ "components":{"securitySchemes":{"token":{"type":"apiKey","name":"Authorization","in":"header",+ "x-amazon-apigateway-authtype":"custom",+ "x-amazon-apigateway-authorizer":{"type":"token","authorizerResultTtlInSeconds":3601,+ "authorizerUri":{"Fn::Sub":"arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:authorizer/invocations"}}}}},+ "paths":{"/items":{"get":{"security":[{"token":[]}],+ "responses":{"200":{"description":"ok"}}}}}}}}}}+ """;++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void rollbackRemovesTheRestApiWhoseBodyWasRejected() {+ String stack = "apigw-cfn-restapi-import-rollback-it";+ String outsideId = given().contentType("application/json")+ .body("{\"name\":\"" + API_NAME + "\",\"description\":\"outside the stack\"}")+ .when().post("/restapis").then().statusCode(201).extract().path("id");+ String createdId = null;+ boolean stackDeleted = false;+ try {+ stackAction(stack, "CreateStack", TEMPLATE);+ CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stack);+ assertEquals("ROLLBACK_COMPLETE", state.status(), state.reason());+ assertThat(CfnStackWaits.failedResourceReason(stack), containsString("authorizerResultTtlInSeconds"));+ createdId = physicalId(stack, "Api");+ assertNotEquals(outsideId, createdId);++ given().when().get("/restapis/" + createdId).then().statusCode(404);+ assertOutsideApiIntact(outsideId);++ deleteStack(stack);+ stackDeleted = true;+ given().when().get("/restapis/" + createdId).then().statusCode(404);+ assertOutsideApiIntact(outsideId);+ } finally {+ try {+ if (!stackDeleted) {+ deleteStack(stack);+ }+ } finally {+ if (createdId != null) {+ given().when().delete("/restapis/" + createdId);+ }+ given().when().delete("/restapis/" + outsideId);+ }+ }+ }++ private static void assertOutsideApiIntact(String apiId) {+ given().when().get("/restapis/" + apiId).then().statusCode(200)+ .body("name", equalTo(API_NAME))+ .body("description", equalTo("outside the stack"));+ }++ private static String physicalId(String stack, String logicalId) {+ String xml = given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStackResources")+ .formParam("StackName", stack)+ .when().post("/").then().statusCode(200)+ .extract().asString();+ return XmlParser.extractGroups(xml, "member").stream()+ .filter(resource -> logicalId.equals(resource.get("LogicalResourceId")))+ .findFirst().orElseThrow().get("PhysicalResourceId");+ }++ private static void stackAction(String stack, String action, String template) {+ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", action)+ .formParam("StackName", stack)+ .formParam("TemplateBody", template)+ .when().post("/").then().statusCode(200);+ }++ private static void deleteStack(String stack) {+ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DeleteStack")+ .formParam("StackName", stack)+ .when().post("/").then().statusCode(200);+ CfnStackWaits.awaitStackDeleted(stack);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.javaindex 61a710b863..ebe4a91f3a 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java@@ -26,6 +26,7 @@ import java.util.Set;import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertSame;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;import static org.mockito.ArgumentMatchers.any;@@ -35,6 +36,7 @@ import static org.mockito.ArgumentMatchers.eq;import static org.mockito.Mockito.doThrow;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.never;+import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;@@ -68,6 +70,66 @@ class ApiGatewayRestApiCfnProvisionerTest {verify(api, never()).putRestApi(anyString(), anyString(), anyString(), anyString());}+ @Test+ void restApiCreateDeletesTheApiItCreatedWhenItsBodyIsRejected() throws Exception {+ when(api.createRestApi(eq("us-east-1"), anyMap())).thenReturn(restApi("api-1", "shop", null, "REGIONAL"));+ when(api.findRootResourceId("us-east-1", "api-1")).thenReturn(Optional.of("root-1"));+ AwsException rejected = new AwsException("BadRequestException", "Invalid OpenAPI input", 400);+ when(api.putRestApi(eq("us-east-1"), eq("api-1"), eq("overwrite"), anyString())).thenThrow(rejected);+ StackResource r = resource("AWS::ApiGateway::RestApi", "Api");++ AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""+ {"Name": "shop", "Body": {"openapi": "3.0.1", "paths": {}}}+ """), ctx()));++ assertSame(rejected, failure);+ verify(api).deleteRestApi("us-east-1", "api-1");+ // Already deleted, so the engine's create rollback has nothing left to delete.+ assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR));+ }++ @Test+ void restApiCreateDeletesTheApiItCreatedWhenReapplyingItsNameFails() throws Exception {+ when(api.createRestApi(eq("us-east-1"), anyMap())).thenReturn(restApi("api-1", "shop", null, "REGIONAL"));+ when(api.findRootResourceId("us-east-1", "api-1")).thenReturn(Optional.of("root-1"));+ AwsException rejected = new AwsException("TooManyRequestsException", "Too Many Requests", 429);+ when(api.updateRestApi(eq("us-east-1"), eq("api-1"), any())).thenThrow(rejected);+ StackResource r = resource("AWS::ApiGateway::RestApi", "Api");++ AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""+ {"Name": "shop", "Body": {"openapi": "3.0.1", "info": {"title": "other"}, "paths": {}}}+ """), ctx()));++ assertSame(rejected, failure);+ verify(api).putRestApi(eq("us-east-1"), eq("api-1"), eq("overwrite"), anyString());+ verify(api).deleteRestApi("us-east-1", "api-1");+ }++ @Test+ void restApiCreateLeavesTheApiToTheStackRollbackWhenItCannotDeleteIt() throws Exception {+ when(api.createRestApi(eq("us-east-1"), anyMap())).thenReturn(restApi("api-1", "shop", null, "REGIONAL"));+ when(api.findRootResourceId("us-east-1", "api-1")).thenReturn(Optional.of("root-1"));+ AwsException rejected = new AwsException("BadRequestException", "Invalid OpenAPI input", 400);+ when(api.putRestApi(eq("us-east-1"), eq("api-1"), eq("overwrite"), anyString())).thenThrow(rejected);+ AwsException cleanupFailure = new AwsException("TooManyRequestsException", "Too Many Requests", 429);+ doThrow(cleanupFailure).doNothing().when(api).deleteRestApi("us-east-1", "api-1");+ StackResource r = resource("AWS::ApiGateway::RestApi", "Api");++ AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""+ {"Name": "shop", "Body": {"openapi": "3.0.1", "paths": {}}}+ """), ctx()));++ assertSame(rejected, failure);+ assertEquals(1, failure.getSuppressed().length);+ assertSame(cleanupFailure, failure.getSuppressed()[0]);+ // Marked as created by this stack, so the create rollback, or DeleteStack, deletes it again.+ assertEquals("api-1", r.getPhysicalId());+ assertEquals("true", r.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR));+ assertFalse(r.getAttributes().containsKey(CfnRollback.REPLACEMENT_CLEANUP_ATTR));+ provisioner.delete(r, "us-east-1");+ verify(api, times(2)).deleteRestApi("us-east-1", "api-1");+ }+@Testvoid restApiUpdateKeepsTheApiAndPatchesItInPlace() throws Exception {existingApi(restApi("api-1", "shop", "v1", "REGIONAL"));@@ -163,6 +225,8 @@ class ApiGatewayRestApiCfnProvisionerTest {verify(api).updateRestApi("us-east-1", "api-1", List.of(op("replace", "/name", "shop"), op("replace", "/description", "v1")));assertFalse(r.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));+ // The stack still holds the API it kept.+ verify(api, never()).deleteRestApi(anyString(), anyString());}@Test