Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Andre Kuraitb5532028e106
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java
index e25ffa9b50..764b745839 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java
@@ -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);
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRdsDynamicReferenceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRdsDynamicReferenceIntegrationTest.java
index e50ddc63e4..948a937cf0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRdsDynamicReferenceIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRdsDynamicReferenceIntegrationTest.java
@@ -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);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngineTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngineTest.java
index b62080a2ef..012c1fc62f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngineTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngineTest.java
@@ -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.