Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): resolve dynamic references after Fn::Join (#3610)
Chris Jovanovskie5bee4ac22bf
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.javaindex 103577d11e..a48be90335 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@@ -315,7 +315,11 @@ public class CloudFormationTemplateEngine {return "";}String delimiter = join.get(0).asText("");- return String.join(delimiter, resolveList(join.get(1)));+ // CDK splices a dynamic reference across fragments, for example+ // ["{{resolve:secretsmanager:", {"Ref": "Secret"}, ":SecretString:password::}}"], so the+ // fragments are only intrinsic-resolved here and the caller resolves dynamic references on+ // the concatenated string. Resolving them per fragment rejects every fragment as unclosed.+ return String.join(delimiter, resolveList(join.get(1), false));}private String resolveSelect(JsonNode select) {@@ -336,12 +340,21 @@ public class CloudFormationTemplateEngine {* two such lists, or a comma-delimited scalar (e.g. a {@code Ref} to a {@code List<>} parameter).*/private List<String> resolveList(JsonNode node) {+ return resolveList(node, true);+ }++ /**+ * @param resolveDynamicReferences whether scalar elements pass through the dynamic-reference+ * stage individually; {@code Fn::Join} passes {@code false}+ * because a reference may span several fragments.+ */+ private List<String> resolveList(JsonNode node, boolean resolveDynamicReferences) {List<String> out = new ArrayList<>();if (node == null || node.isNull() || node.isMissingNode()) {return out;}if (node.isArray()) {- return resolveListElements(node);+ return resolveListElements(node, resolveDynamicReferences);}if (node.isObject()) {if (node.has("Fn::If")) {@@ -350,7 +363,7 @@ public class CloudFormationTemplateEngine {// to the scalar branch below, which would stringify a list-shaped branch instead// of splitting it.JsonNode branch = selectIfBranch(node.get("Fn::If"));- return branch == null ? out : resolveList(branch);+ return branch == null ? out : resolveList(branch, resolveDynamicReferences);}if (node.has("Fn::GetAZs")) {return resolveAvailabilityZones(node.get("Fn::GetAZs"));@@ -362,7 +375,7 @@ public class CloudFormationTemplateEngine {return resolveSplit(node.get("Fn::Split"));}}- String scalar = resolve(node);+ String scalar = resolveDynamicReferences ? resolve(node) : resolveIntrinsic(node);if (!scalar.isEmpty()) {out.addAll(Arrays.asList(scalar.split(",", -1)));}@@ -378,12 +391,16 @@ public class CloudFormationTemplateEngine {* so dropping a blank element ahead of the selected index would shift every later index.*/private List<String> resolveListElements(JsonNode node) {+ return resolveListElements(node, true);+ }++ private List<String> resolveListElements(JsonNode node, boolean resolveDynamicReferences) {List<String> out = new ArrayList<>();for (JsonNode element : node) {if (isListValuedIntrinsic(element)) {- out.addAll(resolveList(element));+ out.addAll(resolveList(element, resolveDynamicReferences));} else {- out.add(resolve(element));+ out.add(resolveDynamicReferences ? resolve(element) : resolveIntrinsic(element));}}return out;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.javaindex 2592380dce..05f7fca3e0 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@@ -7,6 +7,7 @@ import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.CsvSource;import org.junit.jupiter.params.provider.ValueSource;+import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.function.Function;@@ -379,6 +380,50 @@ class CloudFormationTemplateEngineTest {* literal text because {@code resolveNode}, the general-purpose path every non-RDS property goes* through, had no dynamic-reference stage.*/+ /**+ * CDK emits an RDS master credential as an {@code Fn::Join} whose fragments split one dynamic+ * reference: {@code ["{{resolve:secretsmanager:", {"Ref": "Secret"}, ":SecretString:password::}}"]}.+ * Resolving each fragment on its own rejects the opening fragment as an unclosed reference, so+ * the reference must be resolved once, on the concatenated string.+ */+ @Test+ void resolveNodeResolvesDynamicReferenceSplicedAcrossJoinFragments() {+ List<String> resolverInputs = new ArrayList<>();+ UnaryOperator<String> resolver = value -> {+ resolverInputs.add(value);+ return "resolved-password";+ };+ CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",+ "us-east-1", "my-stack", "stack/id", Map.of(),+ Map.of("Secret", "arn:aws:secretsmanager:us-east-1:000000000000:secret:creds-AbC123"),+ Map.of(), Map.of(), Map.of(), mapper, (Function<String, String>) name -> null, resolver);++ String resolved = e.resolveNode(json("{\"Fn::Join\":[\"\",[\"{{resolve:secretsmanager:\","+ + "{\"Ref\":\"Secret\"},\":SecretString:password::}}\"]]}")).asText();++ assertEquals("resolved-password", resolved);+ assertEquals(List.of("{{resolve:secretsmanager:arn:aws:secretsmanager:us-east-1:000000000000:"+ + "secret:creds-AbC123:SecretString:password::}}"), resolverInputs);+ }++ /**+ * 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.+ */+ @Test+ void resolveWithoutDynamicReferencesKeepsJoinSplicedReferenceIntact() {+ UnaryOperator<String> resolver = value -> fail("dynamic reference must not be resolved here: " + value);+ CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",+ "us-east-1", "my-stack", "stack/id", Map.of(),+ Map.of("Secret", "arn:aws:secretsmanager:us-east-1:000000000000:secret:creds-AbC123"),+ Map.of(), Map.of(), Map.of(), mapper, (Function<String, String>) name -> null, resolver);++ assertEquals("{{resolve:secretsmanager:arn:aws:secretsmanager:us-east-1:000000000000:"+ + "secret:creds-AbC123:SecretString:password::}}",+ e.resolveWithoutDynamicReferences(json("{\"Fn::Join\":[\"\",[\"{{resolve:secretsmanager:\","+ + "{\"Ref\":\"Secret\"},\":SecretString:password::}}\"]]}")));+ }+@Testvoid resolveNodeResolvesDynamicReferenceInPlainStringValue() {UnaryOperator<String> resolver = value -> {