fix(cloudformation): update the tags of an AWS::Events::EventBus in place (#7799)

* fix(cloudformation): update the tags of an AWS::Events::EventBus in place

A tag-only UpdateStack of an event bus was refused with "Updating
AWS::Events::EventBus Description, Tags, or Policy is not supported until
transactional rollback is available." and rolled back, so a template that
bumps a version tag could not be deployed.

The bus the stack already owns now takes the template's tags: a key the
stack set and the template dropped is untagged, a tag added outside the
stack stays, as measured on AWS. The tags of the keys the update touches
are snapshotted first, and rollbackUpdate puts them and the tracked keys
back when a later resource fails the update. Description and Policy
changes are still refused.

Closes #7796

* fix(cloudformation): keep a failed event bus tag update for its rollback

A tag call that fails after the bus's tags started changing left the
engine restoring the previous resource and marking it rolled back, so
the half-applied tags stayed. retainsFailedUpdateState now keeps an
attempt that carries the tag snapshot, and the rollback walker restores
it through rollbackUpdate, as the SSM parameter provisioner does.

* test(cloudformation): read EventBridge answers through callEventBridge

* refactor(cloudformation): share the tag snapshot of an update with SSM

The event bus recorded and read back the tags of an update the same way
the SSM parameter does. CfnRollback now holds that pair, putTagSnapshot,
snapshotTagKeys and snapshotTags, in the same tagKeys and tags fields, so
a snapshot an SSM update already stored still reads. The bus reconciles
its tags from the managed keys and the desired tags, as SSM's
reconcileTags does, so its update and rollback no longer each work out
the keys to remove.

* fix(cloudformation): read a null tag value back as null from a tag snapshot

* refactor(cloudformation): read the event bus update snapshot through CfnRollback

rollbackEventBusUpdate parsed its snapshot by hand, which is what
CfnRollback.updateSnapshot does; it now uses that and still reports
false when the bus carries no snapshot. The CloudFormation docs say
event bus tags now update in place on UpdateStack, keeping tags added
outside the stack, and go back when a later resource fails the update.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-11-10 08:17:17 -04:00
1 parent aff365d1e5
commit 8b48aafd44
7 files changed
+324 -65

No files matched your search

@@ -3,10 +3,17 @@ package io.floci.aws.services.cloudformation.provisioners;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.services.cloudformation.model.StackResource;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.function.BiConsumer;
/**
@@ -151,6 +158,13 @@ public final class CfnRollback {
*/
public static final String API_DESTINATION_UPDATE_SNAPSHOT_ATTR = "__FlociApiDestinationUpdateSnapshot";
/**
* Holds the tags an EventBridge event bus carried, for the keys an in-place update manages,
* and the managed tag keys it was tracking, so a failed stack update can put them back. Written
* by {@code EventsCfnProvisioner} before its tag calls and spent by its {@code rollbackUpdate}.
*/
public static final String EVENT_BUS_UPDATE_SNAPSHOT_ATTR = "__FlociEventBusUpdateSnapshot";
/**
* Holds the name, description and endpoint configuration a REST API had before an in-place
* update patched them, so a failed stack update can put them back, and a {@code body} flag set
@@ -478,6 +492,38 @@ public final class CfnRollback {
return value == null || value.isNull() ? null : value.asText();
}
/**
* Records the tags an in-place update is about to reconcile under {@code tagKeys} and
* {@code tags} of {@code snapshot}: every key it manages, and the value {@code current} holds
* for it. A managed key the resource did not carry is listed without a value, so the rollback
* removes it. {@link #snapshotTagKeys} and {@link #snapshotTags} read them back.
*/
public static void putTagSnapshot(ObjectNode snapshot, Collection<String> managedKeys,
Map<String, String> current) {
ArrayNode tagKeys = snapshot.putArray("tagKeys");
ObjectNode tags = snapshot.putObject("tags");
for (String key : new TreeSet<>(managedKeys)) {
tagKeys.add(key);
if (current.containsKey(key)) {
tags.put(key, current.get(key));
}
}
}
/** The tag keys {@link #putTagSnapshot} recorded as managed by the update. */
public static List<String> snapshotTagKeys(JsonNode snapshot) {
List<String> keys = new ArrayList<>();
snapshot.path("tagKeys").forEach(key -> keys.add(key.asText()));
return keys;
}
/** The tags {@link #putTagSnapshot} recorded, which are what the rollback puts back. */
public static Map<String, String> snapshotTags(JsonNode snapshot) {
Map<String, String> tags = new LinkedHashMap<>();
snapshot.path("tags").properties().forEach(tag -> tags.put(tag.getKey(), tag.getValue().textValue()));
return tags;
}
/**
* Undoes an update {@link #recordCreatedByUpdate} recorded: deletes the created entity with
* {@code deleteInRegion}, which takes its id and region, names the prior id again as the physical
@@ -25,6 +25,7 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
@@ -306,6 +307,17 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
}
}
/**
* An event bus update that failed after its tag snapshot may already have changed tags, so the
* engine keeps that attempt for {@link #rollbackUpdate} instead of putting the previous resource
* back over it. A failure before the snapshot changed nothing and is left to the engine.
*/
@Override
public boolean retainsFailedUpdateState(StackResource resource) {
return "AWS::Events::EventBus".equals(resource.getResourceType())
&& resource.getAttributes().containsKey(CfnRollback.EVENT_BUS_UPDATE_SNAPSHOT_ATTR);
}
/**
* Puts the rule's targets back to what the snapshot holds and spends it. Exact rather than
* additive: a target the failed update added is removed, and every snapshotted target is put
@@ -323,6 +335,9 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
if ("AWS::Events::ApiDestination".equals(resource.getResourceType())) {
return rollbackApiDestinationUpdate(resource);
}
if ("AWS::Events::EventBus".equals(resource.getResourceType())) {
return rollbackEventBusUpdate(resource);
}
String rawSnapshot = resource.getAttributes().get(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR);
if (rawSnapshot == null) {
return false;
@@ -386,6 +401,8 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
* (not the ARN), so the physical id is the name; {@code Fn::GetAtt "Arn"} exposes the ARN.
*/
private void provisionEventBus(StackResource r, JsonNode props, ProvisionContext ctx) {
// A snapshot describes the update in flight; one an earlier update left behind is stale
r.getAttributes().remove(CfnRollback.EVENT_BUS_UPDATE_SNAPSHOT_ATTR);
validateEventBusProperties(props);
String existingBusName = ctx.isUpdate() ? ctx.priorPhysicalId() : null;
String busName = ctx.resolveOptional(props, "Name");
@@ -424,7 +441,8 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR);
throw e;
}
validateEventBusMutablePropertiesUnchanged(r, bus, description, tags, policy);
validateEventBusMutablePropertiesUnchanged(r, bus, description, policy);
updateEventBusTags(r, bus, tags, ctx.region());
}
// Record identity before applying the policy: rollbackCreatedResources skips any resource
@@ -530,27 +548,11 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
}
private void validateEventBusMutablePropertiesUnchanged(
StackResource resource, EventBus bus, String requestedDescription,
Map<String, String> requestedTags, JsonNode requestedPolicy) {
StackResource resource, EventBus bus, String requestedDescription, JsonNode requestedPolicy) {
if (!Objects.equals(bus.getDescription(), requestedDescription)) {
throw unsupportedEventBusMutableUpdate();
}
// Stacks persisted by the older EventBus provisioner have no managed-key metadata. There
// is no reliable way to distinguish their CloudFormation tags from tags added out of band,
// so allow this one-time adoption and start tracking the requested keys afterwards.
if (resource.getAttributes().containsKey(EVENT_BUS_MANAGED_TAG_KEYS_ATTR)) {
Map<String, String> currentManagedTags = new LinkedHashMap<>();
for (String key : eventBusManagedTagKeys(resource)) {
if (bus.getTags().containsKey(key)) {
currentManagedTags.put(key, bus.getTags().get(key));
}
}
if (!currentManagedTags.equals(requestedTags)) {
throw unsupportedEventBusMutableUpdate();
}
}
String managedPolicy = resource.getAttributes().get(EVENT_BUS_MANAGED_POLICY_ATTR);
JsonNode policyToCompare = managedPolicy != null
? parseEventBusPolicy(managedPolicy, "stored CloudFormation metadata")
@@ -594,10 +596,68 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
private AwsException unsupportedEventBusMutableUpdate() {
return new AwsException("ValidationError",
"Updating AWS::Events::EventBus Description, Tags, or Policy is not supported "
"Updating AWS::Events::EventBus Description or Policy is not supported "
+ "until transactional rollback is available.", 400);
}
/**
* Drives the tags of the bus the stack already owns to the template's, as AWS's update handler
* does: a key the stack set and the template dropped is untagged, and a tag added outside the
* stack stays. A bus provisioned before its keys were tracked has none to drop. The tags of the
* keys this touches are recorded first, so {@link #rollbackUpdate} can put them back.
*/
private void updateEventBusTags(StackResource r, EventBus bus, Map<String, String> tags, String region) {
Set<String> managedKeys = eventBusManagedTagKeys(r);
Set<String> touchedKeys = new HashSet<>(managedKeys);
touchedKeys.addAll(tags.keySet());
ObjectNode snapshot = MAPPER.createObjectNode();
snapshot.put("region", region);
snapshot.put("arn", bus.getArn());
snapshot.put("managedTagKeys", r.getAttributes().get(EVENT_BUS_MANAGED_TAG_KEYS_ATTR));
CfnRollback.putTagSnapshot(snapshot, touchedKeys, bus.getTags());
r.getAttributes().put(CfnRollback.EVENT_BUS_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
reconcileEventBusTags(bus.getArn(), managedKeys, tags, region);
}
/**
* Puts the managed tags and tracked keys the bus had before the failed update back, and spends
* the snapshot. A bus this update did not reach carries no snapshot, which is the false that
* keeps the stack reporting that rollback is not implemented.
*/
private boolean rollbackEventBusUpdate(StackResource resource) {
JsonNode snapshot = CfnRollback.updateSnapshot(resource, CfnRollback.EVENT_BUS_UPDATE_SNAPSHOT_ATTR);
if (snapshot == null) {
return false;
}
reconcileEventBusTags(snapshot.path("arn").asText(), CfnRollback.snapshotTagKeys(snapshot),
CfnRollback.snapshotTags(snapshot), snapshot.path("region").asText());
String managedTagKeys = snapshotText(snapshot, "managedTagKeys");
if (managedTagKeys == null) {
resource.getAttributes().remove(EVENT_BUS_MANAGED_TAG_KEYS_ATTR);
} else {
resource.getAttributes().put(EVENT_BUS_MANAGED_TAG_KEYS_ATTR, managedTagKeys);
}
resource.getAttributes().remove(CfnRollback.EVENT_BUS_UPDATE_SNAPSHOT_ATTR);
return true;
}
/**
* Applies {@code desired} and removes the {@code managedKeys} it does not hold, so a tag added
* outside the stack stays.
*/
private void reconcileEventBusTags(String arn, Collection<String> managedKeys, Map<String, String> desired,
String region) {
List<String> dropped = managedKeys.stream()
.filter(key -> !desired.containsKey(key))
.toList();
if (!dropped.isEmpty()) {
eventBridgeService.untagResource(arn, dropped, region);
}
if (!desired.isEmpty()) {
eventBridgeService.tagResource(arn, desired, region);
}
}
private Set<String> eventBusManagedTagKeys(StackResource resource) {
String value = resource.getAttributes().get(EVENT_BUS_MANAGED_TAG_KEYS_ATTR);
if (value == null || value.isBlank()) {
@@ -3,7 +3,6 @@ package io.floci.aws.services.cloudformation.provisioners;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.services.cloudformation.model.StackResource;
@@ -123,18 +122,9 @@ public class SsmCfnProvisioner implements CfnResourceProvisioner {
if (current.getPolicies() != null) {
snapshot.putArray("policies").addAll(current.getPolicies());
}
// A managed key the parameter did not carry is listed without a value, so the rollback removes it.
Set<String> managedKeys = new TreeSet<>(templateTagKeys(attributesBefore));
managedKeys.addAll(desiredTagKeys);
Map<String, String> currentTags = current.getTags() == null ? Map.of() : current.getTags();
ArrayNode tagKeys = snapshot.putArray("tagKeys");
ObjectNode tags = snapshot.putObject("tags");
for (String key : managedKeys) {
tagKeys.add(key);
if (currentTags.containsKey(key)) {
tags.put(key, currentTags.get(key));
}
}
CfnRollback.putTagSnapshot(snapshot, managedKeys, current.getTags() == null ? Map.of() : current.getTags());
r.getAttributes().put(CfnRollback.SSM_PARAMETER_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
}
@@ -265,10 +255,6 @@ public class SsmCfnProvisioner implements CfnResourceProvisioner {
ssmService.putParameter(name, value, type, description, true, null, keyId, allowedPattern, tier,
policies, region);
}
List<String> managedKeys = new ArrayList<>();
snapshot.path("tagKeys").forEach(key -> managedKeys.add(key.asText()));
Map<String, String> tags = new HashMap<>();
snapshot.path("tags").properties().forEach(tag -> tags.put(tag.getKey(), tag.getValue().asText()));
reconcileTags(name, managedKeys, tags, region);
reconcileTags(name, CfnRollback.snapshotTagKeys(snapshot), CfnRollback.snapshotTags(snapshot), region);
}
}