feat(eventbridge): provision AWS::Events::Archive (#4818)

* fix(eventbridge): make UpdateArchive a partial update

UpdateArchive reset the event pattern to null and the retention to 0
whenever a request omitted them. On AWS an omitted Description,
EventPattern or RetentionDays keeps the stored value, an empty
Description or EventPattern clears it, and RetentionDays 0 sets 0.

* feat(eventbridge): provision AWS::Events::Archive

CloudFormation stubbed AWS::Events::Archive with a fake ARN and created
nothing. EventsArchiveCfnProvisioner now creates a real archive through
EventBridgeService.

Ref is the archive name and Fn::GetAtt Arn the archive ARN. ArchiveName
is generated when omitted and kept across updates. Description,
EventPattern and RetentionDays update in place, sending only the
properties the template declares, and a failed stack update restores
them from a snapshot. Changing SourceArn or ArchiveName, or dropping an
explicit name, replaces the archive and deletes the displaced one once
the update commits. A replacement that keeps an explicit name is refused
with CloudFormation's custom-named resource error before any change.
KmsKeyIdentifier is ignored.

Closes #4815

* fix(eventbridge): reject an archive source that is not an existing bus of the account and region

CreateArchive now checks EventSourceArn as AWS does, before the archive
name: a bus in another account fails with AccessDeniedException, one in
another region with ValidationException, and a bus that does not exist
with ResourceNotFoundException. The AWS::Events::Archive provisioner goes
through the same service call, so a stack can no longer reach
CREATE_COMPLETE with an archive that could never capture an event.

* test(eventbridge): roll back an in-place archive update when a later resource fails

A stack update changes an archive's description and retention in place,
then fails on a second archive whose source bus does not exist. The stack
ends UPDATE_ROLLBACK_COMPLETE, DescribeArchive shows the original values
and the second archive was never created. The golden case passes on AWS
and is mirrored in the Floci integration test.

* fix(cloudformation): refuse declaring a generated archive name explicitly

Adding ArchiveName with the name CloudFormation generated for an unnamed
archive asks AWS to replace the archive under the same custom name, which
it refuses with the custom-named resource error and rolls the stack back.
Floci took the in-place path and reached UPDATE_COMPLETE; the existing
refusal now also fires when the previous name was generated.

* fix(cloudformation): provision a real archive when updating a stack whose archive was stubbed

A stack created before AWS::Events::Archive had a provisioner holds the dispatcher's stub, whose physical id names no archive. Its next update now creates the archive instead of failing to describe that id, and the stub id is left to the replacement cleanup.

* fix(eventbridge): validate archive description, pattern and retention as CreateArchive and UpdateArchive do

CreateArchive and UpdateArchive now reject a description over 512 characters, an event pattern over 4096 characters and a negative retention with the ValidationException AWS returns, all violations reported together. The check runs before the source, existence and duplicate checks, so it also fails the AWS::Events::Archive resource in a stack.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-04 13:39:49 -04:00
1 parent dc3e9cacec
commit 66f04cde25
13 files changed
+1816 -6

No files matched your search

@@ -0,0 +1,347 @@
package com.floci.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
import software.amazon.awssdk.services.cloudformation.model.Output;
import software.amazon.awssdk.services.cloudformation.model.Parameter;
import software.amazon.awssdk.services.cloudformation.model.Stack;
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
import software.amazon.awssdk.services.eventbridge.model.ArchiveState;
import software.amazon.awssdk.services.eventbridge.model.DescribeArchiveResponse;
import software.amazon.awssdk.services.eventbridge.model.EventBridgeException;
import software.amazon.awssdk.services.eventbridge.model.ResourceNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BooleanSupplier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@DisplayName("CloudFormation AWS::Events::Archive")
class CloudFormationEventsArchiveTest {
private static final String PATTERN = "{\"source\":[\"app.orders\"]}";
private static CloudFormationClient cfn;
private static EventBridgeClient eventBridge;
private String stackName;
private boolean stackCreated;
private final List<String> buses = new ArrayList<>();
@BeforeAll
static void clients() {
cfn = TestFixtures.cloudFormationClient();
eventBridge = TestFixtures.eventBridgeClient();
}
@BeforeEach
void setup() {
stackName = TestFixtures.uniqueName("compat-cfn-archive");
}
@AfterEach
void cleanup() throws InterruptedException {
try {
deleteStack();
} finally {
for (String bus : buses) {
eventBridge.deleteEventBus(r -> r.name(bus));
}
}
}
@AfterAll
static void closeClients() {
cfn.close();
eventBridge.close();
}
@Test
void createsArchiveThatReadsBackThroughDescribeArchiveAndDeletesItWithTheStack()
throws InterruptedException {
String bus = createBus();
String name = TestFixtures.uniqueName("orders");
createStack(template(name, true), parameters(bus, "7"));
assertThat(output("ArchiveRef")).as("Ref is the archive name").isEqualTo(name);
DescribeArchiveResponse archive = eventBridge.describeArchive(r -> r.archiveName(name));
assertThat(output("ArchiveArn")).as("GetAtt Arn is the archive ARN").isEqualTo(archive.archiveArn());
assertThat(archive.eventSourceArn()).isEqualTo(bus);
assertThat(archive.eventPattern()).isEqualTo(PATTERN);
assertThat(archive.retentionDays()).isEqualTo(7);
assertThat(archive.description()).isEqualTo("orders archive");
assertThat(archive.state()).isEqualTo(ArchiveState.ENABLED);
deleteStack();
assertThatThrownBy(() -> eventBridge.describeArchive(r -> r.archiveName(name)))
.isInstanceOf(ResourceNotFoundException.class);
}
@Test
void inPlaceUpdateKeepsTheArchiveAndADescriptionRemovedFromTheTemplate() throws InterruptedException {
String bus = createBus();
String name = TestFixtures.uniqueName("orders");
createStack(template(name, true), parameters(bus, "7"));
String arn = output("ArchiveArn");
cfn.updateStack(r -> r.stackName(stackName).templateBody(template(name, false))
.parameters(parameters(bus, "30")));
awaitStatus("UPDATE_COMPLETE");
assertThat(output("ArchiveRef")).isEqualTo(name);
assertThat(output("ArchiveArn")).isEqualTo(arn);
DescribeArchiveResponse archive = eventBridge.describeArchive(r -> r.archiveName(name));
assertThat(archive.retentionDays()).isEqualTo(30);
assertThat(archive.description())
.as("a property removed from the template keeps its value")
.isEqualTo("orders archive");
assertThat(archive.eventPattern()).isEqualTo(PATTERN);
}
@Test
void changingTheSourceReplacesAGeneratedArchive() throws InterruptedException {
String first = createBus();
String second = createBus();
createStack(template(null, false), parameters(first, "7"));
String original = output("ArchiveRef");
assertThat(original).contains("Archive-").hasSizeLessThanOrEqualTo(48);
cfn.updateStack(r -> r.stackName(stackName).templateBody(template(null, false))
.parameters(parameters(second, "7")));
awaitStatus("UPDATE_COMPLETE");
String replacement = output("ArchiveRef");
assertThat(replacement).contains("Archive-").isNotEqualTo(original).hasSizeLessThanOrEqualTo(48);
assertThat(eventBridge.describeArchive(r -> r.archiveName(replacement)).eventSourceArn())
.isEqualTo(second);
assertThatThrownBy(() -> eventBridge.describeArchive(r -> r.archiveName(original)))
.as("the displaced archive is deleted once the update commits")
.isInstanceOf(ResourceNotFoundException.class);
deleteStack();
assertThatThrownBy(() -> eventBridge.describeArchive(r -> r.archiveName(replacement)))
.isInstanceOf(ResourceNotFoundException.class);
}
@Test
void declaringTheGeneratedNameExplicitlyIsRefusedAsACustomNamedReplacement() throws InterruptedException {
String bus = createBus();
createStack(template(null, false), parameters(bus, "7"));
String generated = cfn.describeStackResource(r -> r.stackName(stackName).logicalResourceId("Archive"))
.stackResourceDetail().physicalResourceId();
cfn.updateStack(r -> r.stackName(stackName).templateBody(template(generated, false))
.parameters(parameters(bus, "7")));
awaitStatus("UPDATE_ROLLBACK_COMPLETE");
String reason = "CloudFormation cannot update a stack when a custom-named resource requires replacing. "
+ "Rename " + generated + " and update the stack again.";
assertThat(cfn.describeStackEvents(r -> r.stackName(stackName)).stackEvents())
.anyMatch(e -> "Archive".equals(e.logicalResourceId())
&& "UPDATE_FAILED".equals(e.resourceStatusAsString())
&& reason.equals(e.resourceStatusReason()));
assertThat(output("ArchiveRef")).isEqualTo(generated);
DescribeArchiveResponse archive = eventBridge.describeArchive(r -> r.archiveName(generated));
assertThat(archive.eventSourceArn()).isEqualTo(bus);
assertThat(archive.retentionDays()).isEqualTo(7);
}
@Test
void failedUpdateRestoresTheInPlaceChangeMadeBeforeALaterResourceFailed() throws InterruptedException {
String name = TestFixtures.uniqueName("orders");
String broken = TestFixtures.uniqueName("broken");
createStack(defaultBusTemplate(name, "orders archive", 7, null), List.of());
cfn.updateStack(r -> r.stackName(stackName)
.templateBody(defaultBusTemplate(name, "changed archive", 30, broken)));
awaitStatus("UPDATE_ROLLBACK_COMPLETE");
DescribeArchiveResponse archive = eventBridge.describeArchive(r -> r.archiveName(name));
assertThat(archive.description()).isEqualTo("orders archive");
assertThat(archive.retentionDays()).isEqualTo(7);
assertThatThrownBy(() -> eventBridge.describeArchive(r -> r.archiveName(broken)))
.as("the archive on a missing bus is never created")
.isInstanceOf(ResourceNotFoundException.class);
}
@Test
void createArchiveAndUpdateArchiveRejectOutOfRangeFieldsBeforeAnyOtherCheck() {
String missingBus = eventBridge.describeEventBus(r -> r.name("default")).arn()
.replace("event-bus/default", "event-bus/" + TestFixtures.uniqueName("compat-archive-missing"));
String name = TestFixtures.uniqueName("compat-archive-invalid");
String description = "d".repeat(513);
String pattern = "{\"source\":[\"" + "a".repeat(4082) + "\"]}";
assertThatThrownBy(() -> eventBridge.createArchive(r -> r.archiveName(name).eventSourceArn(missingBus)
.description(description).eventPattern(pattern).retentionDays(-1)))
.as("validation runs before the source check")
.isInstanceOfSatisfying(EventBridgeException.class, error -> {
assertThat(error.statusCode()).isEqualTo(400);
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
assertThat(error.awsErrorDetails().errorMessage())
.startsWith("3 validation errors detected: Value '-1' at 'retentionDays'")
.contains("at 'description' failed to satisfy constraint: "
+ "Member must have length less than or equal to 512",
"at 'eventPattern' failed to satisfy constraint: "
+ "Member must have length less than or equal to 4096");
});
assertThatThrownBy(() -> eventBridge.updateArchive(r -> r.archiveName(name).retentionDays(-1)))
.as("validation runs before the archive is looked up")
.isInstanceOfSatisfying(EventBridgeException.class, error -> {
assertThat(error.statusCode()).isEqualTo(400);
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
assertThat(error.awsErrorDetails().errorMessage()).isEqualTo("1 validation error detected: "
+ "Value '-1' at 'retentionDays' failed to satisfy constraint: "
+ "Member must have value greater than or equal to 0");
});
}
/**
* An archive on the account's default bus and, when {@code brokenName} is given, a second one
* that depends on it and names a bus that does not exist, so it fails after the first changed.
*/
private static String defaultBusTemplate(String name, String description, int retention, String brokenName) {
String broken = brokenName == null ? "" : """
,
"Broken": {
"Type": "AWS::Events::Archive",
"DependsOn": "Archive",
"Properties": {
"ArchiveName": "%s",
"SourceArn": {"Fn::Sub": "arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/%s"}
}
}""".formatted(brokenName, brokenName + "-missing-bus");
return """
{
"Resources": {
"Archive": {
"Type": "AWS::Events::Archive",
"Properties": {
"ArchiveName": "%s",
"Description": "%s",
"SourceArn": {"Fn::Sub": "arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/default"},
"RetentionDays": %d
}
}%s
}
}
""".formatted(name, description, retention, broken);
}
private static String template(String archiveName, boolean withDescription) {
String name = archiveName == null ? "" : "\"ArchiveName\": \"" + archiveName + "\",";
String description = withDescription ? "\"Description\": \"orders archive\"," : "";
return """
{
"Parameters": {
"Source": {"Type": "String"},
"Retention": {"Type": "Number"}
},
"Resources": {
"Archive": {
"Type": "AWS::Events::Archive",
"Properties": {
%s
%s
"SourceArn": {"Ref": "Source"},
"EventPattern": {"source": ["app.orders"]},
"RetentionDays": {"Ref": "Retention"}
}
}
},
"Outputs": {
"ArchiveRef": {"Value": {"Ref": "Archive"}},
"ArchiveArn": {"Value": {"Fn::GetAtt": ["Archive", "Arn"]}}
}
}
""".formatted(name, description);
}
private static List<Parameter> parameters(String source, String retention) {
return List.of(
Parameter.builder().parameterKey("Source").parameterValue(source).build(),
Parameter.builder().parameterKey("Retention").parameterValue(retention).build());
}
private String createBus() {
String bus = TestFixtures.uniqueName("compat-archive-bus");
String arn = eventBridge.createEventBus(r -> r.name(bus)).eventBusArn();
buses.add(bus);
return arn;
}
private void createStack(String template, List<Parameter> parameters) throws InterruptedException {
cfn.createStack(r -> {
r.stackName(stackName).templateBody(template);
if (!parameters.isEmpty()) {
r.parameters(parameters);
}
});
stackCreated = true;
awaitStatus("CREATE_COMPLETE");
}
private String output(String key) {
return cfn.describeStacks(r -> r.stackName(stackName)).stacks().get(0).outputs().stream()
.filter(o -> key.equals(o.outputKey())).map(Output::outputValue)
.findFirst().orElseThrow();
}
/** Waits for {@code expected}, failing as soon as the stack settles on any other status. */
private void awaitStatus(String expected) throws InterruptedException {
await(() -> {
Stack stack = cfn.describeStacks(r -> r.stackName(stackName)).stacks().get(0);
String status = stack.stackStatusAsString();
if (!expected.equals(status) && !status.endsWith("_IN_PROGRESS")) {
throw new AssertionError(stackName + " reached " + status + ": " + stack.stackStatusReason());
}
return expected.equals(status);
}, expected);
}
private void deleteStack() throws InterruptedException {
if (!stackCreated) {
return;
}
cfn.deleteStack(r -> r.stackName(stackName));
await(() -> {
try {
List<Stack> stacks = cfn.describeStacks(r -> r.stackName(stackName)).stacks();
if (stacks.isEmpty()) {
return true;
}
Stack stack = stacks.get(0);
if ("DELETE_FAILED".equals(stack.stackStatusAsString())) {
throw new AssertionError(stackName + " deletion failed: " + stack.stackStatusReason());
}
return "DELETE_COMPLETE".equals(stack.stackStatusAsString());
} catch (CloudFormationException e) {
if ("ValidationError".equals(e.awsErrorDetails().errorCode())
&& e.getMessage().contains("does not exist")) {
return true;
}
throw e;
}
}, "stack deletion");
stackCreated = false;
}
private void await(BooleanSupplier condition, String expected) throws InterruptedException {
long deadline = System.nanoTime() + 60_000_000_000L;
while (System.nanoTime() < deadline) {
if (condition.getAsBoolean()) {
return;
}
Thread.sleep(100);
}
throw new AssertionError(stackName + " timed out waiting for " + expected);
}
}