fix(cloudformation): use bus|rule as the physical id of an Events rule on a custom bus (#8864)

This commit is contained in:
glorfindel33 authored and GitHub committed 2026-11-18 13:58:05 -04:00
1 parent 2ac4554ea8
commit 9e65c12f8c
4 files changed
+228 -17

No files matched your search

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.cloudformation.model.StackResource;
import io.floci.aws.services.eventbridge.EventBridgeService;
@@ -42,10 +43,10 @@ import java.util.stream.Collectors;
* {@code AWS::Events::EventBus} and {@code AWS::Events::EventBusPolicy}. Extracted from
* the former CloudFormation monolith.
*
* <p>Two of these types need the resource's stored attributes to delete, not just the physical
* id, so they are served by the {@link #delete(StackResource, String)} override rather than the
* id-only form: a rule remembers the custom bus it lives on, and an event bus remembers the
* creation time that proves the stack still owns it.
* <p>Two of these types are served by the {@link #delete(StackResource, String)} override rather
* than the id-only form: an event bus remembers the creation time that proves the stack still owns
* it, and a rule saved before its physical id carried its custom bus finds that bus in the
* {@code EventBusName} attribute it stored.
*/
@ApplicationScoped
public class EventsCfnProvisioner implements CfnResourceProvisioner {
@@ -53,6 +54,11 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
private static final Logger LOG = Logger.getLogger(EventsCfnProvisioner.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String DEFAULT_EVENT_BUS = "default";
private static final String EVENT_BUS_RESOURCE_PREFIX = "event-bus/";
/** Joins a custom bus to the rule name in a rule's physical id; neither name can contain it. */
private static final char RULE_ID_SEPARATOR = '|';
/** The shape AWS::Events::Rule's resource schema declares for its targets. */
private static final String TARGETS = CfnPropertyTypes.propertyShape("AWS::Events::Rule", "Targets");
@@ -92,14 +98,15 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
}
/**
* A rule and an event bus both need attributes the physical id cannot carry: the rule's custom
* bus, and the bus's recorded creation time. Deleting either by id alone would silently target
* the default bus or skip the ownership check.
* An event bus needs its stored creation time, which the physical id cannot carry; deleting
* it by id alone would skip the ownership check. A rule's physical id names its custom bus, but
* a rule saved before it did has only the rule name there and its bus in the stored
* {@code EventBusName}, which deleting by id alone would silently trade for the default bus.
*/
@Override
public void delete(StackResource resource, String region) {
switch (resource.getResourceType()) {
case "AWS::Events::Rule" -> deleteEventBridgeRuleSafe(resource.getPhysicalId(),
case "AWS::Events::Rule" -> deleteRule(resource.getPhysicalId(),
resource.getAttributes() != null ? resource.getAttributes().get("EventBusName") : null,
region);
case "AWS::Events::EventBus" -> deleteEventBusSafe(resource, region);
@@ -113,7 +120,7 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
return;
}
switch (resourceType) {
case "AWS::Events::Rule" -> deleteEventBridgeRuleSafe(physicalId, null, region);
case "AWS::Events::Rule" -> deleteRule(physicalId, null, region);
case "AWS::Events::EventBus" -> deleteEventBusSafe(physicalId, region);
case "AWS::Events::EventBusPolicy" -> removeEventBusPolicySafe(physicalId, region);
case "AWS::Events::ApiDestination" -> deleteApiDestinationSafe(physicalId, region);
@@ -128,11 +135,14 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
// before this run touches a target, so a rollback never puts back a target the rule stopped
// carrying two updates ago.
r.getAttributes().remove(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR);
// Name is createOnly and the physical id is the rule name, so ctx.stablePhysicalName
// applies directly: without it an unnamed rule was given a fresh random name on every
// UpdateStack, creating a second rule and leaving the first behind with its targets.
String ruleName = ctx.stablePhysicalName(
ctx.resolveOptional(props, "Name"), r.getLogicalId(), 64, false);
// Name is createOnly, so an unnamed rule keeps its name across an UpdateStack: without that it
// was given a fresh random name on every update, creating a second rule and leaving the first
// behind with its targets. The prior name comes out of the prior physical id, which for a rule
// on a custom bus also carries the bus.
String explicitName = ctx.resolveOptional(props, "Name");
String ruleName = explicitName != null && !explicitName.isBlank()
? explicitName
: ruleNameOf(ctx.stablePhysicalName(null, r.getLogicalId(), 64, false));
String busName = ctx.resolveOptional(props, "EventBusName");
String description = ctx.resolveOptional(props, "Description");
@@ -154,11 +164,11 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
Rule rule = eventBridgeService.putRule(ruleName, busName, eventPattern, scheduleExpression,
state, description, roleArn, Map.of(), ctx.region());
r.setPhysicalId(ruleName);
r.setPhysicalId(rulePhysicalId(ruleName, busName, ctx.accountId()));
r.getAttributes().put("Arn", rule.getArn());
r.getAttributes().put("RuleName", ruleName);
// A rule on a custom bus is keyed by that bus; remember it so the resource delete can target
// the right bus (the physical id is only the rule name, which resolves to the default bus).
// The bus as the template gave it, name or ARN. The physical id carries it too; this copy is
// what a delete reads for a rule saved before the physical id did.
if (busName != null && !busName.isBlank()) {
r.getAttributes().put("EventBusName", busName);
}
@@ -713,6 +723,53 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
return bus.getCreatedTime() != null ? bus.getCreatedTime().toString() : "";
}
/**
* The physical id CloudFormation gives a rule, which is also what its {@code Ref} returns: the
* rule name for a rule on the default event bus, and {@code <event bus name>|<rule name>} for a
* rule on a custom bus. An event bus given by ARN counts by its name when it is in the stack's own
* account, so the default bus given by ARN still yields the bare rule name; a bus in another
* account keeps its ARN, {@code <event bus ARN>|<rule name>}. This is the format of AWS's resource
* provider for the type. EventBridge's own operations take the rule name and the bus separately,
* so a caller splits the value at the {@code |}.
*/
private static String rulePhysicalId(String ruleName, String busNameOrArn, String stackAccountId) {
if (busNameOrArn == null || busNameOrArn.isBlank() || DEFAULT_EVENT_BUS.equals(busNameOrArn)) {
return ruleName;
}
String busResource = AwsArnUtils.resourceIfArnFor(busNameOrArn, "events").orElse(null);
if (busResource != null && busResource.startsWith(EVENT_BUS_RESOURCE_PREFIX)
&& AwsArnUtils.parse(busNameOrArn).accountId().equals(stackAccountId)) {
String busName = busResource.substring(EVENT_BUS_RESOURCE_PREFIX.length());
return DEFAULT_EVENT_BUS.equals(busName) ? ruleName : busName + RULE_ID_SEPARATOR + ruleName;
}
return busNameOrArn + RULE_ID_SEPARATOR + ruleName;
}
/** The rule name a rule's physical id carries: all of it, or what follows its custom bus. */
private static String ruleNameOf(String physicalId) {
int separator = physicalId.indexOf(RULE_ID_SEPARATOR);
return separator < 0 ? physicalId : physicalId.substring(separator + 1);
}
/** The custom bus, by name or ARN, a rule's physical id carries, or null for the default bus. */
private static String busNameOf(String physicalId) {
int separator = physicalId.indexOf(RULE_ID_SEPARATOR);
return separator < 0 ? null : physicalId.substring(0, separator);
}
/**
* Deletes the rule a physical id names, on the bus the id carries. A rule saved before the id
* carried its custom bus has only the rule name there; {@code storedBus}, the bus it stored at
* creation, then says where it lives.
*/
private void deleteRule(String physicalId, String storedBus, String region) {
if (physicalId == null || physicalId.isBlank()) {
return;
}
String busName = busNameOf(physicalId);
deleteEventBridgeRuleSafe(ruleNameOf(physicalId), busName != null ? busName : storedBus, region);
}
private void deleteEventBridgeRuleSafe(String ruleName, String busName, String region) {
try {
if (!eventBridgeService.ruleExists(ruleName, busName, region)) {