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 4bae1331a9..c86fb2e2f0 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 @@ -2221,6 +2221,25 @@ public class CloudFormationService implements ResourceProvider { && normalizedHost.endsWith("." + normalizedSuffix); } + /** + * Terminal statuses under which {@link #executeTemplate} actually finished creating or + * updating a stack's resources and computed its Outputs, including the two "committed but + * still tidying up" statuses, which still mean Outputs were resolved successfully. + * + *
Every other terminal status ({@code ROLLBACK_COMPLETE}, {@code ROLLBACK_FAILED},
+ * {@code UPDATE_ROLLBACK_COMPLETE}, {@code UPDATE_ROLLBACK_FAILED}, {@code CREATE_FAILED},
+ * {@code UPDATE_FAILED}) means the resource loop failed and rolled back before Outputs were
+ * ever computed: see {@link #executeTemplate}, which only reaches its Outputs block once the
+ * whole resource loop has succeeded. This is deliberately an allow-list rather than a
+ * deny-list of failure strings: a nested stack's own {@code rollbackFailedExecution} rewrites
+ * its status past {@code CREATE_FAILED}/{@code UPDATE_FAILED} into one of the ROLLBACK_*
+ * statuses before this method ever inspects it, so checking for the FAILED strings here can
+ * never match on a create and silently reports a stack that rolled back to nothing as
+ * CREATE_COMPLETE.
+ */
+ private static final Set With the child silently "succeeding", the parent went on to provision a downstream
+ * resource that does {@code Fn::GetAtt} on one of the child's Outputs, an Output that was
+ * never computed, because {@code executeTemplate} only reaches its Outputs block once the whole
+ * resource loop has succeeded. The GetAtt fell back to a garbage literal string, and that
+ * downstream resource (here, an SSM parameter) was created with it, while the parent stack
+ * incorrectly reported CREATE_COMPLETE overall.
+ *
+ * Correct behavior: the child's failure must surface as a failed {@code ChildStack} resource
+ * in the parent, so the parent's own resource loop stops right there, before ever reaching the
+ * downstream GetAtt consumer, and the whole parent rolls back.
+ */
+ @Test
+ void createStack_nestedStackResourceFailsInternally_parentRollsBackBeforeConsumingItsOutputs()
+ throws InterruptedException {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String bucketName = "cfn-nested-outputfail-" + suffix;
+ String paramName = "/test/consumer-bucket-name-" + suffix;
+ given().when().put("/nested-stack-templates").then().statusCode(200);
+ String childTemplate = """
+ {
+ "Resources": {
+ "ChildBucket": {
+ "Type": "AWS::S3::Bucket",
+ "Properties": { "BucketName": "%s" }
+ },
+ "BadSecret": {
+ "Type": "AWS::SecretsManager::Secret",
+ "DependsOn": "ChildBucket",
+ "Properties": {
+ "Name": "bad-secret-outputfail-%s",
+ "SecretString": "explicit",
+ "GenerateSecretString": { "PasswordLength": 32 }
+ }
+ }
+ },
+ "Outputs": {
+ "BucketName": { "Value": { "Ref": "ChildBucket" } }
+ }
+ }
+ """.formatted(bucketName, suffix);
+ given().contentType("application/json").body(childTemplate).when()
+ .put("/nested-stack-templates/child-outputfail-" + suffix + ".json");
+
+ String stackName = "parent-outputfail-" + suffix;
+ String template = """
+ {
+ "Resources": {
+ "ChildStack": {
+ "Type": "AWS::CloudFormation::Stack",
+ "Properties": { "TemplateURL": "http://localhost/nested-stack-templates/child-outputfail-%s.json" }
+ },
+ "ConsumerParam": {
+ "Type": "AWS::SSM::Parameter",
+ "DependsOn": "ChildStack",
+ "Properties": {
+ "Name": "%s",
+ "Type": "String",
+ "Value": { "Fn::GetAtt": ["ChildStack", "Outputs.BucketName"] }
+ }
+ }
+ }
+ }
+ """.formatted(suffix, paramName);
+
+ String parentStackId = createStack(stackName, template);
+ try {
+ awaitStackStatus(parentStackId, "ROLLBACK_COMPLETE");
+ assertBucketDeleted(bucketName);
+
+ // The parent must have stopped at the failed ChildStack resource and never reached
+ // ConsumerParam: proof the child's failure was detected before its (never-computed)
+ // Outputs were consumed downstream.
+ given()
+ .header("X-Amz-Target", "AmazonSSM.GetParameter")
+ .contentType("application/x-amz-json-1.1")
+ .body("""
+ { "Name": "%s" }
+ """.formatted(paramName))
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("ParameterNotFound"));
+ } finally {
+ deleteStack(stackName);
+ }
+ }
+
@Test
void updateStack_rollbackWithNestedStack_deletesNewlyAddedChildStack() throws InterruptedException {
String suffix = Long.toString(System.nanoTime(), 36);