fix(cloudformation): preserve list intrinsics and conditional tags (#2986)

This commit is contained in:
BingEdward authored and GitHub committed 2026-09-07 19:23:33 -05:00
1 parent b9039fef1d
commit 9f442543e3
14 files changed
+207 -54

No files matched your search

@@ -1080,8 +1080,9 @@ public class CloudFormationResourceProvisioner {
private Map<String, String> resolveAsgTags(JsonNode props, CloudFormationTemplateEngine engine) {
Map<String, String> tags = new LinkedHashMap<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
for (JsonNode tag : props.get("Tags")) {
JsonNode tagsNode = props != null ? engine.resolveNode(props.get("Tags")) : null;
if (tagsNode != null && tagsNode.isArray()) {
for (JsonNode tag : tagsNode) {
String key = engine.resolve(tag.path("Key"));
if (!key.isEmpty()) {
tags.put(key, engine.resolve(tag.path("Value")));
@@ -1093,8 +1094,9 @@ public class CloudFormationResourceProvisioner {
private Map<String, Boolean> resolveAsgTagPropagation(JsonNode props, CloudFormationTemplateEngine engine) {
Map<String, Boolean> propagation = new LinkedHashMap<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
for (JsonNode tag : props.get("Tags")) {
JsonNode tagsNode = props != null ? engine.resolveNode(props.get("Tags")) : null;
if (tagsNode != null && tagsNode.isArray()) {
for (JsonNode tag : tagsNode) {
String key = engine.resolve(tag.path("Key"));
if (!key.isEmpty()) {
propagation.put(key, Boolean.parseBoolean(engine.resolve(tag.path("PropagateAtLaunch"))));
@@ -1161,8 +1163,9 @@ public class CloudFormationResourceProvisioner {
}
List<Tag> tags = new ArrayList<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
for (JsonNode tag : props.get("Tags")) {
JsonNode tagsNode = props != null ? engine.resolveNode(props.get("Tags")) : null;
if (tagsNode != null && tagsNode.isArray()) {
for (JsonNode tag : tagsNode) {
String key = engine.resolve(tag.path("Key"));
if (!key.isEmpty()) {
tags.add(new Tag(key, engine.resolve(tag.path("Value"))));
@@ -1856,6 +1859,16 @@ public class CloudFormationResourceProvisioner {
table = dynamoDbService.describeTable(tableName, region);
}
Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, engine);
List<String> staleTags = ProvisionContext.staleTagKeys(
dynamoDbService.listTagsOfResource(table.getTableArn(), region), tags);
if (!staleTags.isEmpty()) {
dynamoDbService.untagResource(table.getTableArn(), staleTags, region);
}
if (!tags.isEmpty()) {
dynamoDbService.tagResource(table.getTableArn(), tags, region);
}
// A template that declares StreamSpecification wants a stream. Unlike the DynamoDB API,
// the CloudFormation property carries no StreamEnabled flag — declaring the block IS the
// request — so its presence alone turns the stream on. Without this the table is created
@@ -4740,6 +4753,7 @@ public class CloudFormationResourceProvisioner {
}
private Map<String, String> parseCfnTags(JsonNode tagsNode, CloudFormationTemplateEngine engine) {
tagsNode = engine.resolveNode(tagsNode);
Map<String, String> out = new HashMap<>();
if (tagsNode == null || tagsNode.isNull() || !tagsNode.isArray()) {
return out;
@@ -129,10 +129,12 @@ public class CloudFormationTemplateEngine {
JsonNode branch = selectIfBranch(node.get("Fn::If"));
return branch == null ? TextNode.valueOf("") : resolveNode(branch);
}
if (node.has("Fn::Split") || node.has("Fn::GetAZs") || node.has("Fn::Cidr")) {
return objectMapper.valueToTree(resolveList(node));
}
if (node.has("Ref") || node.has("Fn::Sub") || node.has("Fn::Join") ||
node.has("Fn::Select") || node.has("Fn::Base64") ||
node.has("Fn::GetAtt") || node.has("Fn::ImportValue") || node.has("Fn::Split") ||
node.has("Fn::GetAZs") || node.has("Fn::Cidr") || node.has("Fn::FindInMap")) {
node.has("Fn::GetAtt") || node.has("Fn::ImportValue") || node.has("Fn::FindInMap")) {
return TextNode.valueOf(resolve(node));
}
// Plain object — resolve each field
@@ -245,15 +247,7 @@ public class CloudFormationTemplateEngine {
return "";
}
String delimiter = join.get(0).asText("");
JsonNode parts = join.get(1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < parts.size(); i++) {
if (i > 0) {
sb.append(delimiter);
}
sb.append(resolve(parts.get(i)));
}
return sb.toString();
return String.join(delimiter, resolveList(join.get(1)));
}
private String resolveSelect(JsonNode select) {
@@ -364,12 +358,8 @@ public class CloudFormationTemplateEngine {
return true;
}
if (node.has("Fn::If")) {
JsonNode ifNode = node.get("Fn::If");
if (ifNode.isArray() && ifNode.size() >= 3) {
String conditionName = ifNode.get(0).asText();
boolean condValue = conditions.getOrDefault(conditionName, false);
return isListValuedIntrinsic(condValue ? ifNode.get(1) : ifNode.get(2));
}
JsonNode branch = selectIfBranch(node.get("Fn::If"));
return branch != null && isListValuedIntrinsic(branch);
}
}
return false;
@@ -130,6 +130,7 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
/** See {@code KmsCfnProvisioner#parseCfnTags} for why this is copied rather than shared. */
private Map<String, String> parseCfnTags(JsonNode tagsNode, ProvisionContext ctx) {
tagsNode = ctx.engine().resolveNode(tagsNode);
Map<String, String> out = new HashMap<>();
if (tagsNode == null || tagsNode.isNull() || !tagsNode.isArray()) {
return out;
@@ -191,7 +191,7 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
private Map<String, String> tags(JsonNode props, ProvisionContext ctx) {
Map<String, String> out = new HashMap<>();
JsonNode tagsNode = props != null ? props.get("Tags") : null;
JsonNode tagsNode = props != null ? ctx.engine().resolveNode(props.get("Tags")) : null;
if (tagsNode == null || !tagsNode.isArray()) {
return out;
}
@@ -59,8 +59,9 @@ public class FirehoseCfnProvisioner implements CfnResourceProvisioner {
}
List<DeliveryStreamDescription.Tag> tags = new ArrayList<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
for (JsonNode tag : props.get("Tags")) {
JsonNode tagsNode = props != null ? ctx.engine().resolveNode(props.get("Tags")) : null;
if (tagsNode != null && tagsNode.isArray()) {
for (JsonNode tag : tagsNode) {
String key = ctx.engine().resolve(tag.path("Key"));
if (!key.isEmpty()) {
tags.add(new DeliveryStreamDescription.Tag(key, ctx.engine().resolve(tag.path("Value"))));
@@ -76,8 +76,9 @@ public class KinesisCfnProvisioner implements CfnResourceProvisioner {
}
}
Map<String, String> tags = new LinkedHashMap<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
for (JsonNode tag : props.get("Tags")) {
JsonNode tagsNode = props != null ? ctx.engine().resolveNode(props.get("Tags")) : null;
if (tagsNode != null && tagsNode.isArray()) {
for (JsonNode tag : tagsNode) {
String key = ctx.engine().resolve(tag.path("Key"));
if (!key.isEmpty()) {
tags.put(key, ctx.engine().resolve(tag.path("Value")));
@@ -69,11 +69,11 @@ public class KmsCfnProvisioner implements CfnResourceProvisioner {
/**
* Copied from {@code CloudFormationResourceProvisioner} rather than delegating to
* {@link ProvisionContext#resolveTags}, which is not equivalent: it skips a blank key this
* keeps, orders entries by insertion rather than hash, and resolves the whole property so an
* {@code Fn::If} around the list works. Adopting it here is a behaviour change and belongs in
* its own PR; this copy dies when the monolith's last caller migrates.
* keeps and orders entries by insertion rather than hash. This copy dies when the monolith's
* last caller migrates.
*/
private Map<String, String> parseCfnTags(JsonNode tagsNode, ProvisionContext ctx) {
tagsNode = ctx.engine().resolveNode(tagsNode);
Map<String, String> out = new HashMap<>();
if (tagsNode == null || tagsNode.isNull() || !tagsNode.isArray()) {
return out;
@@ -77,8 +77,9 @@ public class LogsCfnProvisioner implements CfnResourceProvisioner {
}
}
Map<String, String> tags = new HashMap<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
for (JsonNode tag : props.get("Tags")) {
JsonNode tagsNode = props != null ? ctx.engine().resolveNode(props.get("Tags")) : null;
if (tagsNode != null && tagsNode.isArray()) {
for (JsonNode tag : tagsNode) {
String key = ctx.engine().resolve(tag.path("Key"));
if (!key.isEmpty()) {
tags.put(key, ctx.engine().resolve(tag.path("Value")));
@@ -285,11 +285,12 @@ public class OrganizationsCfnProvisioner implements CfnResourceProvisioner {
/** CloudFormation tags are a list of {@code {Key, Value}} objects. */
private Map<String, String> resolveTags(JsonNode props, ProvisionContext ctx) {
if (props == null || !props.has("Tags") || !props.get("Tags").isArray()) {
JsonNode tagsNode = props != null ? ctx.engine().resolveNode(props.get("Tags")) : null;
if (tagsNode == null || !tagsNode.isArray()) {
return null;
}
Map<String, String> tags = new LinkedHashMap<>();
for (JsonNode tag : ctx.engine().resolveNode(props.get("Tags"))) {
for (JsonNode tag : tagsNode) {
String key = tag.path("Key").asText(null);
if (key != null && !key.isBlank()) {
tags.put(key, tag.path("Value").asText(""));
@@ -498,6 +498,7 @@ public class PipesCfnProvisioner implements CfnResourceProvisioner {
/** See {@code KmsCfnProvisioner#parseCfnTags} for why this is copied rather than shared. */
private Map<String, String> parseCfnTags(JsonNode tagsNode, ProvisionContext ctx) {
tagsNode = ctx.engine().resolveNode(tagsNode);
Map<String, String> out = new HashMap<>();
if (tagsNode == null || tagsNode.isNull() || !tagsNode.isArray()) {
return out;
@@ -42,19 +42,13 @@ public class SchedulerScheduleGroupCfnProvisioner implements CfnResourceProvisio
// it already had across updates instead of getting a fresh random one each time provision
// runs. Read from the context, not the resource: provision assigns the new id as it works.
String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "Name"), r.getLogicalId(), 64, false);
// Tags wrapped in an intrinsic (e.g. Fn::If choosing between two tag lists) is not resolved
// here: the engine collapses a list-valued intrinsic to a string (#2983), so a conditional
// list would not arrive as the chosen array. tagsAreResolvable is true both when Tags is
// absent (the template genuinely wants no tags) and when it is a plain array (the template's
// actual desired tags), and false only when Tags is present but not a plain array, the one
// case where the desired state genuinely cannot be read, so the reconcile path below must
// not treat "couldn't resolve the intended tags" as "the intended tags are empty" and delete
// everything live.
// Preserve live tags when a present property still cannot resolve to a tag list.
boolean hasTagsProperty = props != null && props.has("Tags");
boolean tagsAreResolvable = !hasTagsProperty || props.get("Tags").isArray();
JsonNode tagsNode = hasTagsProperty ? ctx.engine().resolveNode(props.get("Tags")) : null;
boolean tagsAreResolvable = !hasTagsProperty || (tagsNode != null && tagsNode.isArray());
Map<String, String> tags = new HashMap<>();
if (hasTagsProperty && tagsAreResolvable) {
for (JsonNode tag : props.get("Tags")) {
for (JsonNode tag : tagsNode) {
String key = ctx.engine().resolve(tag.path("Key"));
if (!key.isEmpty()) {
tags.put(key, ctx.engine().resolve(tag.path("Value")));