diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java index 6ef1f403b0..7b18376e44 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java @@ -167,6 +167,38 @@ public class CloudFormationService implements ResourceProvider { return stack != null ? stack.getParameters() : Map.of(); } + /** + * The status of a stack, or {@code null} when no stack of that name exists in the region. + * Unlike {@link #describeStacks}, asking about a stack that is not there is not an error: + * StackSet deployment uses this to decide what to do with an instance before it acts on it. + */ + String stackStatus(String stackName, String region) { + Stack stack = resolveStack(stackName, region); + return stack != null ? stack.getStatus() : null; + } + + /** + * Whether a stack in this status refuses an update, on the two grounds AWS refuses one. + * + *
A status ending in {@code _IN_PROGRESS} says an operation owns the stack: a create, an + * update, a rollback or the cleanup phase of a committed update. AWS refuses to start a second + * one over it, and offers nothing that finishes a phase whose process is gone - DeleteStack is + * the only way out of one. Refusing keeps every abandoned phase out of the next update's + * transaction. + * + *
{@code ROLLBACK_COMPLETE} is terminal for a different reason: a create that failed rolled
+ * its resources back, so the stack holds the name and nothing else, and only DeleteStack frees
+ * it. The CDK CLI keys on this state and tells the user to delete the stack; a client that is
+ * accepted here instead proceeds against a stack AWS would have rejected. It is matched exactly
+ * rather than by suffix, because {@code UPDATE_ROLLBACK_COMPLETE} ends the same way and is a
+ * perfectly updatable stack: it is where an update that failed and rolled back settles, and
+ * retrying the update is how it is repaired.
+ */
+ static boolean refusesUpdate(String status) {
+ return status != null
+ && (status.endsWith("_IN_PROGRESS") || "ROLLBACK_COMPLETE".equals(status));
+ }
+
// ── CreateChangeSet ───────────────────────────────────────────────────────
public ChangeSet createChangeSet(String stackName, String changeSetName, String changeSetType,
@@ -293,17 +325,10 @@ public class CloudFormationService implements ResourceProvider {
throw new AwsException("AlreadyExistsException",
"Stack [" + stackName + "] already exists", 400);
}
- // A status ending in _IN_PROGRESS says an operation owns the stack: a create, an
- // update, a rollback or the cleanup phase of a committed update. AWS refuses to
- // start a second one over it, and offers nothing that finishes a phase whose
- // process is gone - DeleteStack is the only way out of one. Refusing here keeps
- // every abandoned phase out of the next update's transaction.
- //
// The message is the one real CloudFormation emits, down to its own "can not"
// spelling and the stack id carried as "Stack:ValidationError"))
+ .body(containsString(
+ "Stack:arn:aws:cloudformation:us-east-1:000000000000:stack/" + stackName + "/"))
+ .body(containsString(" is in ROLLBACK_COMPLETE state and can not be updated."));
+
+ // The refusal changes nothing: the stack keeps its status and gains no resource.
+ assertStackStatus(stackName, "ROLLBACK_COMPLETE");
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStackResources")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(not(containsString(topicName)));
+
+ deleteStack(stackName);
+ }
+
+ /**
+ * CreateChangeSet with ChangeSetType=UPDATE is refused on the same terms as UpdateStack: both
+ * reach the same guard, so a client cannot route around the refusal by building the change set
+ * itself.
+ */
+ @Test
+ void aChangeSetIsRefusedOnARollbackCompleteStack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "rollback-complete-changeset-" + suffix;
+ String topicName = "rollback-complete-changeset-t-" + suffix;
+
+ createRolledBackStack(stackName, suffix);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateChangeSet")
+ .formParam("StackName", stackName)
+ .formParam("ChangeSetName", "rollback-complete-cs-" + suffix)
+ .formParam("ChangeSetType", "UPDATE")
+ .formParam("TemplateBody", topicTemplate(topicName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body(containsString("ValidationError"))
+ .body(containsString(" is in ROLLBACK_COMPLETE state and can not be updated."));
+
+ assertStackStatus(stackName, "ROLLBACK_COMPLETE");
+ deleteStack(stackName);
+ }
+
+ /**
+ * DeleteStack is the way out of ROLLBACK_COMPLETE, so refusing the update must not touch it:
+ * the stack goes away and its name is free for a fresh CreateStack.
+ */
+ @Test
+ void deleteStackStillClearsARollbackCompleteStack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "rollback-complete-delete-" + suffix;
+ String topicName = "rollback-complete-delete-t-" + suffix;
+
+ createRolledBackStack(stackName, suffix);
+ deleteStack(stackName);
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", topicTemplate(topicName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ assertStackStatus(stackName, "CREATE_COMPLETE");
+ deleteStack(stackName);
+ }
+
+ /**
+ * UPDATE_ROLLBACK_COMPLETE ends in the same eleven characters and means the opposite: an update
+ * failed over a stack that is still live and intact, and retrying the update is how it is
+ * repaired. Refusing it would strand every stack whose update ever failed.
+ */
+ @Test
+ void updateStaysAllowedAfterAnUpdateRolledBack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "update-rollback-complete-" + suffix;
+ String topicName = "update-rollback-complete-t-" + suffix;
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", topicTemplate(topicName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ assertStackStatus(stackName, "CREATE_COMPLETE");
+
+ // An update whose added resource cannot provision rolls back over a live stack.
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", topicTemplate(topicName, unprovisionableNestedStack()))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ assertStackStatus(stackName, "UPDATE_ROLLBACK_COMPLETE");
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "UpdateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", topicTemplate(topicName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ assertStackStatus(stackName, "UPDATE_COMPLETE");
+
+ deleteStack(stackName);
+ }
+
+ // ── Helpers ───────────────────────────────────────────────────────────────
+
+ /** Creates a stack whose only resource fails, so it settles in ROLLBACK_COMPLETE. */
+ private static void createRolledBackStack(String stackName, String suffix) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", "{\"Resources\":{" + unprovisionableNestedStack() + "}}")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ assertStackStatus(stackName, "ROLLBACK_COMPLETE");
+ }
+
+ /** A nested stack with no TemplateURL cannot provision, and its rollback deletes nothing. */
+ private static String unprovisionableNestedStack() {
+ return "\"Nested\":{\"Type\":\"AWS::CloudFormation::Stack\",\"Properties\":{}}";
+ }
+
+ private static String topicTemplate(String topicName) {
+ return topicTemplate(topicName, null);
+ }
+
+ /**
+ * The topic depends on the added resource, so an update that adds a resource which cannot
+ * provision fails before the topic is reached. That keeps the rollback to the one resource the
+ * failed update created and lands the stack in UPDATE_ROLLBACK_COMPLETE, rather than in
+ * UPDATE_ROLLBACK_FAILED over a topic no provisioner knows how to restore.
+ */
+ private static String topicTemplate(String topicName, String extraResource) {
+ return "{\"Resources\":{"
+ + "\"Topic\":{\"Type\":\"AWS::SNS::Topic\""
+ + (extraResource == null ? "" : ",\"DependsOn\":\"Nested\"")
+ + ",\"Properties\":{\"TopicName\":\"" + topicName + "\"}}"
+ + (extraResource == null ? "" : "," + extraResource)
+ + "}}";
+ }
+
+ private static void assertStackStatus(String stackName, String status) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("