fix(cloudformation): preserve nested dynamic reference opt-out (#6574)

* fix(cloudformation): preserve nested dynamic reference opt-out

Keep nested intrinsics on the RDS credential path outside the general dynamic-reference stage. Reuse the existing suspension counter and restore it after every exit.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* test(cloudformation): preserve joined-reference test documentation

Move the nested opt-out test above the joined-string Javadoc so the comment documents its original test. Preserve both test bodies unchanged.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-25 22:53:18 -04:00
1 parent f1816dbb44
commit b5532028e1
3 files changed
+50 -13

No files matched your search

@@ -80,8 +80,8 @@ public class CloudFormationTemplateEngine {
private final AttributeValueResolver attributeValueResolver;
private final List<String> unresolvedIntrinsics = new ArrayList<>();
/**
* Above zero while {@code Fn::GetStackOutput}'s arguments resolve: CloudFormation hands them on
* with their dynamic references unresolved.
* Above zero while an intrinsic-only property or {@code Fn::GetStackOutput}'s arguments resolve,
* including nested intrinsic calls.
*/
private int dynamicReferencesSuspended;
@@ -234,7 +234,12 @@ public class CloudFormationTemplateEngine {
* only that property is allowed.
*/
public String resolveWithoutDynamicReferences(JsonNode node) {
return resolveIntrinsic(node);
dynamicReferencesSuspended++;
try {
return resolveIntrinsic(node);
} finally {
dynamicReferencesSuspended--;
}
}
private String resolveIntrinsic(JsonNode node) {
@@ -1041,12 +1046,7 @@ public class CloudFormationTemplateEngine {
if (value == null || value.isNull()) {
return "";
}
dynamicReferencesSuspended++;
try {
return resolveIntrinsic(value);
} finally {
dynamicReferencesSuspended--;
}
return resolveWithoutDynamicReferences(value);
}
/**
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.RdsMockProfile;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
@@ -12,6 +13,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static io.github.hectorvent.floci.testing.RdsRequests.rds;
@@ -109,8 +111,9 @@ class CloudFormationRdsDynamicReferenceIntegrationTest {
* {@code CREATE_COMPLETE}. If the credential properties stop opting out of the general stage,
* that stage rejects the reference and the stack rolls back instead.
*/
@Test
void createStack_rdsMasterPasswordResolvesSsmSecureDynamicReference() throws Exception {
@ParameterizedTest
@ValueSource(strings = {"literal", "if", "select", "sub"})
void createStack_rdsMasterPasswordResolvesSsmSecureDynamicReference(String form) throws Exception {
String suffix = Long.toString(System.nanoTime(), 36);
String parameterName = "/cfn-rds-dynref/secure-password-" + suffix;
String stackName = "cfn-rds-dynref-secure-stack-" + suffix;
@@ -119,8 +122,19 @@ class CloudFormationRdsDynamicReferenceIntegrationTest {
boolean stackCreated = false;
try {
createStack(stackName, template(databaseId,
"{{resolve:ssm-secure:" + parameterName + "}}"));
ObjectMapper mapper = new ObjectMapper();
String reference = "{{resolve:ssm-secure:" + parameterName + "}}";
Object password = switch (form) {
case "if" -> Map.of("Fn::If", List.of("Chosen", reference, "unused"));
case "select" -> Map.of("Fn::Select", List.of(0, List.of(reference)));
case "sub" -> Map.of("Fn::Sub", List.of("${Password}", Map.of("Password", reference)));
default -> reference;
};
ObjectNode document = (ObjectNode) mapper.readTree(template(databaseId, "placeholder"));
document.set("Conditions", mapper.valueToTree(Map.of("Chosen", Map.of("Fn::Equals", List.of("yes", "yes")))));
((ObjectNode) document.path("Resources").path("Database").path("Properties"))
.set("MasterUserPassword", mapper.valueToTree(password));
createStack(stackName, mapper.writeValueAsString(document));
stackCreated = true;
String describeXml = describeStack(stackName);
@@ -687,6 +687,29 @@ class CloudFormationTemplateEngineTest {
+ "secret:creds-AbC123:SecretString:password::}}"), resolverInputs);
}
@Test
void nestedOptOutPreservesReferencesAndRestoresResolution() {
List<String> inputs = new ArrayList<>();
CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine("000000000000",
"us-east-1", "stack", "stack/id", Map.of(), Map.of(), Map.of(),
Map.of("Chosen", true), Map.of(), mapper, name -> null, value -> {
inputs.add(value);
return "resolved";
});
String reference = "{{resolve:ssm-secure:/password}}";
for (String expression : List.of(
"\"%s\"",
"{\"Fn::If\":[\"Chosen\",\"%s\",\"unused\"]}",
"{\"Fn::Select\":[0,[\"%s\",\"unused\"]]}",
"{\"Fn::Sub\":[\"${Value}\",{\"Value\":\"%s\"}]}",
"{\"Fn::Join\":[\"\",[{\"Fn::If\":[\"Chosen\",\"%s\",\"unused\"]}]]}")) {
assertEquals(reference, engine.resolveWithoutDynamicReferences(json(expression.formatted(reference))), expression);
assertTrue(inputs.isEmpty(), expression);
}
assertEquals("resolved", engine.resolve(json("\"" + reference + "\"")));
assertEquals(List.of(reference), inputs);
}
/**
* The RDS credential path opts out of the general dynamic-reference stage and resolves the
* reference itself, so the joined string must survive intact with the reference untouched.