mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): restore EventBridge rule identity on rollback (#9564)
A rule's update rollback snapshot also records its prior physical ID, ARN, name and bus, and restores them once the targets are back, so a rename or bus move that a later resource fails leaves the stack on the original rule. A failed target restore keeps the snapshot for ContinueUpdateRollback, and a rule moved to the default bus no longer keeps a stale EventBusName.
This commit is contained in:
1 parent
e6a9092be8
commit
ce48d54bf6
3 files changed
+249
-2
No files matched your search
+38
-2
@@ -62,6 +62,7 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
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 = '|';
|
||||
private static final List<String> RULE_IDENTITY_ATTRIBUTES = List.of("EventBusName", "Arn", "RuleName");
|
||||
|
||||
/** The shape AWS::Events::Rule's resource schema declares for its targets. */
|
||||
private static final String TARGETS = CfnPropertyTypes.propertyShape("AWS::Events::Rule", "Targets");
|
||||
@@ -151,6 +152,13 @@ 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);
|
||||
ObjectNode priorIdentity = null;
|
||||
if (ctx.isUpdate()) {
|
||||
priorIdentity = MAPPER.createObjectNode().put("physicalId", ctx.priorPhysicalId());
|
||||
for (String attribute : RULE_IDENTITY_ATTRIBUTES) {
|
||||
priorIdentity.put(attribute, r.getAttributes().get(attribute));
|
||||
}
|
||||
}
|
||||
// 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
|
||||
@@ -187,10 +195,12 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
// what a delete reads for a rule saved before the physical id did.
|
||||
if (busName != null && !busName.isBlank()) {
|
||||
r.getAttributes().put("EventBusName", busName);
|
||||
} else {
|
||||
r.getAttributes().remove("EventBusName");
|
||||
}
|
||||
|
||||
if (ctx.isUpdate()) {
|
||||
snapshotTargetsBeforeUpdate(r, ruleName, busName, ctx.region());
|
||||
snapshotTargetsBeforeUpdate(r, ruleName, busName, ctx.region(), priorIdentity);
|
||||
}
|
||||
try {
|
||||
if (!targets.isEmpty()) {
|
||||
@@ -293,11 +303,13 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
* <p>The rule name, its bus and the region travel with the targets because the rollback hook is
|
||||
* handed the stack resource alone, and those three are how targets are addressed.
|
||||
*/
|
||||
private void snapshotTargetsBeforeUpdate(StackResource r, String ruleName, String busName, String region) {
|
||||
private void snapshotTargetsBeforeUpdate(StackResource r, String ruleName, String busName, String region,
|
||||
ObjectNode priorIdentity) {
|
||||
ObjectNode snapshot = MAPPER.createObjectNode();
|
||||
snapshot.put("ruleName", ruleName);
|
||||
snapshot.put("busName", busName);
|
||||
snapshot.put("region", region);
|
||||
snapshot.set("identity", priorIdentity);
|
||||
snapshot.set("targets", MAPPER.valueToTree(
|
||||
eventBridgeService.listTargetsByRule(ruleName, busName, region)));
|
||||
r.getAttributes().put(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR, snapshot.toString());
|
||||
@@ -348,6 +360,18 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
&& resource.getAttributes().containsKey(CfnRollback.EVENT_BUS_UPDATE_SNAPSHOT_ATTR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {
|
||||
if (!"AWS::Events::Rule".equals(attempted.getResourceType())) {
|
||||
return;
|
||||
}
|
||||
String snapshot = attempted.getAttributes().get(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR);
|
||||
previous.getAttributes().remove(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR);
|
||||
if (snapshot != null && attempted.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR)) {
|
||||
previous.getAttributes().put(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR, snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -401,6 +425,18 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (!restored.isEmpty()) {
|
||||
eventBridgeService.restoreAcceptedTargets(ruleName, busName, restored, region);
|
||||
}
|
||||
JsonNode identity = snapshot.path("identity");
|
||||
if (identity.isObject()) {
|
||||
resource.setPhysicalId(snapshotText(identity, "physicalId"));
|
||||
for (String attribute : RULE_IDENTITY_ATTRIBUTES) {
|
||||
String value = snapshotText(identity, attribute);
|
||||
if (value == null) {
|
||||
resource.getAttributes().remove(attribute);
|
||||
} else {
|
||||
resource.getAttributes().put(attribute, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
resource.getAttributes().remove(CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR);
|
||||
}
|
||||
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import io.floci.aws.testing.CfnStackWaits;
|
||||
import io.floci.aws.testing.CfnTestStack;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
|
||||
import software.amazon.awssdk.services.eventbridge.model.ResourceNotFoundException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationRuleRollbackIdentityIntegrationTest {
|
||||
|
||||
@RegisterExtension
|
||||
final CfnTestStack stack = new CfnTestStack("cfn-rule-rollback");
|
||||
|
||||
@Test
|
||||
void movingToDefaultBusRestoresTheOriginalIdentityAfterLaterFailure() {
|
||||
assertRestoredIdentity(false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void renamingOnCustomBusRestoresTheOriginalIdentityAfterLaterFailure() {
|
||||
assertRestoredIdentity(true, true);
|
||||
}
|
||||
|
||||
private void assertRestoredIdentity(boolean customBus, boolean rename) {
|
||||
String original = stack.name() + "-original";
|
||||
String attempted = rename ? stack.name() + "-renamed" : original;
|
||||
EventBridgeClient events = stack.client(EventBridgeClient.builder());
|
||||
try {
|
||||
Stack created = stack.create(template(original, true, false, 30));
|
||||
String originalId = CfnStackWaits.output(created, "RuleRef");
|
||||
String originalArn = CfnStackWaits.output(created, "RuleArn");
|
||||
stack.update(template(attempted, customBus, true, 30), "UPDATE_ROLLBACK_COMPLETE");
|
||||
String physicalId = stack.client(CloudFormationClient.builder())
|
||||
.describeStackResource(r -> r.stackName(stack.name()).logicalResourceId("Rule"))
|
||||
.stackResourceDetail().physicalResourceId();
|
||||
Stack later = stack.update(template(original, true, false, 31), "UPDATE_COMPLETE");
|
||||
assertAll(
|
||||
() -> assertEquals(originalId, physicalId, "rolled-back physical id"),
|
||||
() -> assertEquals(originalId, CfnStackWaits.output(later, "RuleRef"), "subsequent Ref"),
|
||||
() -> assertEquals(originalArn, CfnStackWaits.output(later, "RuleArn"), "subsequent Arn"),
|
||||
() -> assertEquals(originalArn,
|
||||
events.describeRule(r -> r.name(original).eventBusName(bus())).arn()));
|
||||
} finally {
|
||||
deleteRule(events, attempted, customBus ? bus() : "default");
|
||||
deleteRule(events, original, bus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void committedMoveToDefaultBusIsDeletedFromTheDefaultBus() {
|
||||
String name = stack.name() + "-rule";
|
||||
EventBridgeClient events = stack.client(EventBridgeClient.builder());
|
||||
events.createEventBus(r -> r.name(bus()));
|
||||
try {
|
||||
stack.create(singleRuleTemplate(name, true));
|
||||
Stack moved = stack.update(singleRuleTemplate(name, false), "UPDATE_COMPLETE");
|
||||
assertEquals(name, CfnStackWaits.output(moved, "RuleRef"));
|
||||
assertEquals(name, events.describeRule(r -> r.name(name)).name());
|
||||
CfnStackWaits.deleteStack(stack.client(CloudFormationClient.builder()), stack.name());
|
||||
assertThrows(ResourceNotFoundException.class, () -> events.describeRule(r -> r.name(name)));
|
||||
} finally {
|
||||
deleteRule(events, name, "default");
|
||||
deleteRule(events, name, bus());
|
||||
events.deleteEventBus(r -> r.name(bus()));
|
||||
}
|
||||
}
|
||||
|
||||
private String singleRuleTemplate(String name, boolean customBus) {
|
||||
return """
|
||||
{"Resources":{"Rule":{"Type":"AWS::Events::Rule","Properties":{
|
||||
"Name":"%s","EventPattern":{"source":["rollback.test"]}%s}}},
|
||||
"Outputs":{"RuleRef":{"Value":{"Ref":"Rule"}}}}
|
||||
""".formatted(name, customBus ? ",\"EventBusName\":\"" + bus() + "\"" : "");
|
||||
}
|
||||
|
||||
private void deleteRule(EventBridgeClient events, String name, String eventBus) {
|
||||
try {
|
||||
events.deleteRule(r -> r.name(name).eventBusName(eventBus));
|
||||
} catch (ResourceNotFoundException ignored) {
|
||||
// A successful stack delete or a failed create already removed this test-owned rule.
|
||||
}
|
||||
}
|
||||
|
||||
private String bus() {
|
||||
return stack.name() + "-bus";
|
||||
}
|
||||
|
||||
private String template(String ruleName, boolean customBus, boolean fail, int visibilityTimeout) {
|
||||
return """
|
||||
{"Resources":{
|
||||
"Bus":{"Type":"AWS::Events::EventBus","Properties":{"Name":"%s"}},
|
||||
"Queue":{"Type":"AWS::SQS::Queue","Properties":{"VisibilityTimeout":%d}},
|
||||
"Rule":{"Type":"AWS::Events::Rule","DependsOn":"Bus",
|
||||
"Properties":{"Name":"%s","EventPattern":{"source":["rollback.test"]}%s}}%s
|
||||
},"Outputs":{"RuleRef":{"Value":{"Ref":"Rule"}},
|
||||
"RuleArn":{"Value":{"Fn::GetAtt":["Rule","Arn"]}}}}
|
||||
""".formatted(bus(), visibilityTimeout, ruleName,
|
||||
customBus ? ",\"EventBusName\":{\"Ref\":\"Bus\"}" : "",
|
||||
fail ? CfnStackWaits.failingResourceAfter("Rule") : "");
|
||||
}
|
||||
}
|
||||
+101
@@ -38,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -641,6 +642,106 @@ class EventsCfnProvisionerTest {
|
||||
|
||||
private static final String RULE_TARGETS_SNAPSHOT_ATTR = CfnRollback.RULE_TARGETS_SNAPSHOT_ATTR;
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void rollbackRestoresThePriorIdentityIncludingAbsentAttributes(boolean customBus) {
|
||||
stubPutRule();
|
||||
StackResource r = priorRule(customBus);
|
||||
String priorId = r.getPhysicalId();
|
||||
String priorArn = r.getAttributes().get("Arn");
|
||||
ObjectNode props = ruleProps();
|
||||
if (!customBus) {
|
||||
props.put("EventBusName", "new-bus");
|
||||
}
|
||||
provisioner.provision(r, props, ctx(priorId));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
assertEquals(priorId, r.getPhysicalId());
|
||||
assertEquals(priorArn, r.getAttributes().get("Arn"));
|
||||
assertEquals("original", r.getAttributes().get("RuleName"));
|
||||
assertEquals(customBus ? "old-bus" : null, r.getAttributes().get("EventBusName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedTargetRestoreCarriesTheCurrentSnapshotForRetry() {
|
||||
stubPutRule();
|
||||
when(events.listTargetsByRule(anyString(), any(), anyString()))
|
||||
.thenReturn(List.of(storedTarget("keep")));
|
||||
AwsException failure = new AwsException("InternalException", "restore unavailable", 500);
|
||||
doThrow(failure).when(events).putTargets(anyString(), any(), any(), anyString());
|
||||
doThrow(failure).when(events).restoreAcceptedTargets(anyString(), any(), any(), anyString());
|
||||
StackResource previous = priorRule(true);
|
||||
previous.getAttributes().put(RULE_TARGETS_SNAPSHOT_ATTR, "stale committed snapshot");
|
||||
StackResource attempted = priorRule(true);
|
||||
|
||||
assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(attempted, ruleProps("keep"), ctx(previous.getPhysicalId())));
|
||||
String currentSnapshot = attempted.getAttributes().get(RULE_TARGETS_SNAPSHOT_ATTR);
|
||||
provisioner.mergeFailedUpdateResourceTracking(previous, attempted);
|
||||
assertEquals(currentSnapshot, previous.getAttributes().get(RULE_TARGETS_SNAPSHOT_ATTR));
|
||||
|
||||
doReturn(0).when(events).restoreAcceptedTargets(anyString(), any(), any(), anyString());
|
||||
assertTrue(provisioner.rollbackUpdate(previous));
|
||||
assertEquals("old-bus|original", previous.getPhysicalId());
|
||||
assertEquals("old-bus", previous.getAttributes().get("EventBusName"));
|
||||
assertNull(previous.getAttributes().get(RULE_TARGETS_SNAPSHOT_ATTR));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void aFailedUpdateWithNoPendingRestoreDiscardsTheCommittedSnapshot(boolean hasAttemptSnapshot) {
|
||||
StackResource previous = priorRule(true);
|
||||
previous.getAttributes().put(RULE_TARGETS_SNAPSHOT_ATTR, "stale committed snapshot");
|
||||
StackResource attempted = priorRule(true);
|
||||
if (hasAttemptSnapshot) {
|
||||
attempted.getAttributes().put(RULE_TARGETS_SNAPSHOT_ATTR, "spent attempted snapshot");
|
||||
}
|
||||
|
||||
provisioner.mergeFailedUpdateResourceTracking(previous, attempted);
|
||||
|
||||
assertNull(previous.getAttributes().get(RULE_TARGETS_SNAPSHOT_ATTR));
|
||||
assertEquals("old-bus|original", previous.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedRollbackWithoutAnAttemptSnapshotDoesNotReviveACommittedSnapshot() {
|
||||
StackResource previous = priorRule(true);
|
||||
previous.getAttributes().put(RULE_TARGETS_SNAPSHOT_ATTR, "stale committed snapshot");
|
||||
StackResource attempted = priorRule(true);
|
||||
attempted.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR, "failed before snapshot");
|
||||
|
||||
provisioner.mergeFailedUpdateResourceTracking(previous, attempted);
|
||||
|
||||
assertNull(previous.getAttributes().get(RULE_TARGETS_SNAPSHOT_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyTargetSnapshotDoesNotClearIdentityItDidNotCapture() {
|
||||
StackResource r = priorRule(true);
|
||||
r.getAttributes().put(RULE_TARGETS_SNAPSHOT_ATTR, """
|
||||
{"ruleName":"original","busName":"old-bus","region":"us-east-1","targets":[]}
|
||||
""");
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
assertEquals("old-bus|original", r.getPhysicalId());
|
||||
assertEquals("old-bus", r.getAttributes().get("EventBusName"));
|
||||
assertEquals("original", r.getAttributes().get("RuleName"));
|
||||
assertEquals(RULE_ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
private StackResource priorRule(boolean customBus) {
|
||||
StackResource r = resource("AWS::Events::Rule", "Rule");
|
||||
r.setPhysicalId(customBus ? "old-bus|original" : "original");
|
||||
r.getAttributes().put("Arn", RULE_ARN);
|
||||
r.getAttributes().put("RuleName", "original");
|
||||
if (customBus) {
|
||||
r.getAttributes().put("EventBusName", "old-bus");
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRollbackPutsBackATargetTheUpdateDropped() {
|
||||
// Before the snapshot, the reconciliation sweep could delete a live target and a later
|
||||
|
||||
Reference in new issue
Block a user