mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(cloudformation): provision AWS::Events::EventBus as a real custom bus (#1807)
* feat(cloudformation): provision EventBridge event buses * fix(cloudformation): restore main's provisioner changes lost in the EventBus merge The previous conflict resolution took this branch's whole CloudFormationResourceProvisioner, which silently reverted five commits main had landed on that file since the merge base — including the DynamoDB/Lambda delete idempotency from #1803 and AWS::EC2::LaunchTemplate from #1973. Re-resolve from main's file and apply only this branch's EventBus additions on top: the ownership-tracking provisioner, its validation helpers, and the typed rethrow in deleteEventBridgeRuleSafe. main's provisionEventBusPolicy and inline Policy support are kept, with Policy added to EVENT_BUS_SUPPORTED_PROPERTIES so the inline form is no longer rejected. * fix(cloudformation): delete EventBus resources created before ownership tracking deleteEventBusSafe(StackResource) refused to delete whenever the FlociEventBusCreatedTime attribute was absent, treating "ownership not tracked" the same as "ownership changed". Stacks provisioned by an earlier Floci build are restored from cloudformation-stacks.json without that attribute, so the first DeleteStack after upgrading left them permanently in DELETE_FAILED. Only refuse when a recorded created-time actually disagrees with the live bus. A missing attribute falls back to the pre-tracking behaviour of deleting what the stack recorded it created. * fix(cloudformation): adopt untracked EventBus on update and record identity before the policy call Two provision-side defects, both surfaced in review: UpdateStack broke for stacks provisioned before FlociEventBusCreatedTime existed. Those resources are restored without the attribute, so the adoption guard's 'existingCreatedTime == null' arm re-threw ResourceAlreadyExists on every update, including no-ops. Same inverted guard the delete path already uses: only a recorded time that actually disagrees means the bus was recreated out of band. The bus also leaked when Policy application failed. r.setPhysicalId ran after putPermission, and rollbackCreatedResources skips resources whose physicalId is null, so a malformed policy left a live bus rollback could not find. Identity and ROLLBACK_OWNED_ATTR are now recorded before the policy call, matching how provisionIamRole marks a freshly created role. The attribute is cleared on the adoption path so rollback never deletes a bus this stack did not create. * chore: re-run CI Build and Test failed on RuntimeApiServerTest's extension/stop race, a pre-existing flake unrelated to this change (no CloudFormation test failed). Root-caused separately: test-timing half in #2141, server-side shutdown-ordering half filed as an issue. * fix(cloudformation): adopt tagged pre-tracking event buses * test(dynamodb): retain Quarkus port for cleanup * chore: rerun CI after download outage * fix(cloudformation): reject unsafe EventBus policy updates --------- Co-authored-by: Alexander Banna <abanna@users.noreply.github.com>
This commit is contained in:
1 parent
33ba5ac59c
commit
bc21367cc4
10 files changed
+1824
-69
No files matched your search
+195
@@ -0,0 +1,195 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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.CreateStackRequest;
|
||||
import software.amazon.awssdk.services.cloudformation.model.DeleteStackRequest;
|
||||
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Output;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
|
||||
import software.amazon.awssdk.services.eventbridge.model.DescribeEventBusRequest;
|
||||
import software.amazon.awssdk.services.eventbridge.model.DescribeEventBusResponse;
|
||||
import software.amazon.awssdk.services.eventbridge.model.ListTagsForResourceRequest;
|
||||
import software.amazon.awssdk.services.eventbridge.model.ResourceNotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("CloudFormation AWS::Events::EventBus")
|
||||
class CloudFormationEventBusTest {
|
||||
|
||||
private static CloudFormationClient cloudFormation;
|
||||
private static EventBridgeClient eventBridge;
|
||||
private static String stackName;
|
||||
private static String busName;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
cloudFormation = TestFixtures.cloudFormationClient();
|
||||
eventBridge = TestFixtures.eventBridgeClient();
|
||||
stackName = TestFixtures.uniqueName("compat-cfn-eventbus");
|
||||
busName = TestFixtures.uniqueName("compat-eventbus");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (cloudFormation != null) {
|
||||
try {
|
||||
cloudFormation.deleteStack(
|
||||
DeleteStackRequest.builder().stackName(stackName).build());
|
||||
} catch (Exception e) {
|
||||
System.err.println("CloudFormation EventBus cleanup skipped: " + e.getMessage());
|
||||
}
|
||||
cloudFormation.close();
|
||||
}
|
||||
if (eventBridge != null) {
|
||||
eventBridge.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsValidatesUpdatesAndDeletesEventBusThroughAwsSdk() throws InterruptedException {
|
||||
cloudFormation.createStack(CreateStackRequest.builder()
|
||||
.stackName(stackName)
|
||||
.templateBody(template("before", "old", "remove"))
|
||||
.build());
|
||||
assertThat(waitForTerminal(stackName, 30)).isEqualTo("CREATE_COMPLETE");
|
||||
|
||||
Stack createdStack = cloudFormation.describeStacks(
|
||||
DescribeStacksRequest.builder().stackName(stackName).build())
|
||||
.stacks().get(0);
|
||||
Map<String, String> outputs = createdStack.outputs().stream()
|
||||
.collect(Collectors.toMap(Output::outputKey, Output::outputValue));
|
||||
String busArn = "arn:aws:events:us-east-1:000000000000:event-bus/" + busName;
|
||||
assertThat(outputs)
|
||||
.containsEntry("BusRef", busName)
|
||||
.containsEntry("BusName", busName)
|
||||
.containsEntry("BusArn", busArn);
|
||||
|
||||
DescribeEventBusResponse created = eventBridge.describeEventBus(
|
||||
DescribeEventBusRequest.builder().name(busName).build());
|
||||
assertThat(created.name()).isEqualTo(busName);
|
||||
assertThat(created.arn()).isEqualTo(busArn);
|
||||
assertThat(created.description()).isEqualTo("before");
|
||||
assertThat(tags(busArn))
|
||||
.containsEntry("keep", "old")
|
||||
.containsEntry("remove", "old");
|
||||
|
||||
cloudFormation.updateStack(r -> r
|
||||
.stackName(stackName)
|
||||
.templateBody(template("before", "old", "remove")));
|
||||
assertThat(waitForTerminal(stackName, 30)).isEqualTo("UPDATE_COMPLETE");
|
||||
|
||||
cloudFormation.updateStack(r -> r
|
||||
.stackName(stackName)
|
||||
.templateBody(template("after", "new", "add")));
|
||||
assertThat(waitForTerminal(stackName, 30)).isEqualTo("UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
DescribeEventBusResponse afterRejectedUpdate = eventBridge.describeEventBus(
|
||||
DescribeEventBusRequest.builder().name(busName).build());
|
||||
assertThat(afterRejectedUpdate.description()).isEqualTo("before");
|
||||
assertThat(tags(busArn))
|
||||
.containsEntry("keep", "old")
|
||||
.containsEntry("remove", "old")
|
||||
.doesNotContainKey("add");
|
||||
|
||||
cloudFormation.deleteStack(DeleteStackRequest.builder().stackName(stackName).build());
|
||||
waitForDeleted(stackName, 30);
|
||||
assertThatThrownBy(() -> eventBridge.describeEventBus(
|
||||
DescribeEventBusRequest.builder().name(busName).build()))
|
||||
.isInstanceOf(ResourceNotFoundException.class);
|
||||
}
|
||||
|
||||
private static Map<String, String> tags(String busArn) {
|
||||
return eventBridge.listTagsForResource(ListTagsForResourceRequest.builder()
|
||||
.resourceARN(busArn)
|
||||
.build())
|
||||
.tags().stream()
|
||||
.collect(Collectors.toMap(
|
||||
software.amazon.awssdk.services.eventbridge.model.Tag::key,
|
||||
software.amazon.awssdk.services.eventbridge.model.Tag::value));
|
||||
}
|
||||
|
||||
private static String template(String description, String keepValue, String secondTagKey) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Bus": {
|
||||
"Type": "AWS::Events::EventBus",
|
||||
"Properties": {
|
||||
"Name": "%s",
|
||||
"Description": "%s",
|
||||
"Tags": [
|
||||
{"Key": "keep", "Value": "%s"},
|
||||
{"Key": "%s", "Value": "%s"}
|
||||
]
|
||||
}
|
||||
},
|
||||
"Rule": {
|
||||
"Type": "AWS::Events::Rule",
|
||||
"Properties": {
|
||||
"Name": "%s",
|
||||
"EventBusName": {"Ref": "Bus"},
|
||||
"EventPattern": {"source": ["com.example.orders"]},
|
||||
"State": "ENABLED"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"BusRef": {"Value": {"Ref": "Bus"}},
|
||||
"BusArn": {"Value": {"Fn::GetAtt": ["Bus", "Arn"]}},
|
||||
"BusName": {"Value": {"Fn::GetAtt": ["Bus", "Name"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(
|
||||
busName, description, keepValue, secondTagKey, keepValue, busName + "-rule");
|
||||
}
|
||||
|
||||
private static String waitForTerminal(String name, int maxSeconds) throws InterruptedException {
|
||||
long deadline = System.currentTimeMillis() + maxSeconds * 1000L;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
List<Stack> stacks = cloudFormation.describeStacks(
|
||||
DescribeStacksRequest.builder().stackName(name).build()).stacks();
|
||||
if (!stacks.isEmpty()) {
|
||||
String status = stacks.get(0).stackStatusAsString();
|
||||
if (!status.endsWith("_IN_PROGRESS")) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
Thread.sleep(500);
|
||||
}
|
||||
throw new AssertionError(
|
||||
"Stack " + name + " did not reach a terminal state within " + maxSeconds + "s");
|
||||
}
|
||||
|
||||
private static void waitForDeleted(String name, int maxSeconds) throws InterruptedException {
|
||||
long deadline = System.currentTimeMillis() + maxSeconds * 1000L;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
try {
|
||||
List<Stack> stacks = cloudFormation.describeStacks(
|
||||
DescribeStacksRequest.builder().stackName(name).build()).stacks();
|
||||
if (stacks.isEmpty()
|
||||
|| "DELETE_COMPLETE".equals(stacks.get(0).stackStatusAsString())) {
|
||||
return;
|
||||
}
|
||||
} catch (CloudFormationException e) {
|
||||
if (e.getMessage() != null && e.getMessage().contains("does not exist")) {
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
Thread.sleep(500);
|
||||
}
|
||||
throw new AssertionError(
|
||||
"Stack " + name + " was not deleted within " + maxSeconds + "s");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user