mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(cloudformation): support AWS::Events::EventBus and EventBusPolicy (#1794)
* feat(cloudformation): support AWS::Events::EventBus provisioning and deletion * fix(cloudformation): delete EventBridge rules against their own event bus on stack teardown * test(cloudformation): verify custom event bus rule delivers to SQS end to end * feat(cloudformation): support AWS::Events::EventBusPolicy (Action/Principal form) * feat(cloudformation): support AWS::Events::EventBusPolicy Statement form with merge by Sid * fix(cloudformation): route CREATE rollback through resource-aware delete so custom-bus rules are cleaned up * chore(cloudformation): warn instead of debug when a stack event bus cannot be deleted
This commit is contained in:
1 parent
58743487a7
commit
76f602fafa
2 files changed
+545
-5
No files matched your search
+148
-5
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnResour
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.BatchParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.EventBus;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.SqsParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
@@ -293,6 +294,8 @@ public class CloudFormationResourceProvisioner {
|
||||
case "AWS::Route53::HostedZone" -> provisionRoute53HostedZone(resource, properties, engine);
|
||||
case "AWS::Route53::RecordSet" -> provisionRoute53RecordSet(resource, properties, engine);
|
||||
case "AWS::Events::Rule" -> provisionEventBridgeRule(resource, properties, engine, region, stackName);
|
||||
case "AWS::Events::EventBus" -> provisionEventBus(resource, properties, engine, region, stackName);
|
||||
case "AWS::Events::EventBusPolicy" -> provisionEventBusPolicy(resource, properties, engine, region);
|
||||
case "AWS::ApiGateway::RestApi" -> provisionApiGatewayRestApi(resource, properties, engine, region, accountId, stackName);
|
||||
case "AWS::ApiGateway::Resource" -> provisionApiGatewayResource(resource, properties, engine, region);
|
||||
case "AWS::ApiGateway::Authorizer" -> provisionApiGatewayAuthorizer(resource, properties, engine, region);
|
||||
@@ -411,6 +414,14 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Rule deletion needs the rule's event bus (stored as an attribute at provision time),
|
||||
// which the type/physicalId delete path can't provide; a custom-bus rule looked up under
|
||||
// the default bus would silently no-op and leave the rule (and its bus) live.
|
||||
if ("AWS::Events::Rule".equals(resourceType)) {
|
||||
deleteEventBridgeRuleSafe(resource.getPhysicalId(),
|
||||
resource.getAttributes().get("EventBusName"), region);
|
||||
return;
|
||||
}
|
||||
// AWS::IAM::Policy is an inline policy; detaching it needs the principals it was attached to,
|
||||
// which the type/physicalId delete path can't provide (only the delete-stack path has them).
|
||||
if ("AWS::IAM::Policy".equals(resourceType)) {
|
||||
@@ -458,7 +469,9 @@ public class CloudFormationResourceProvisioner {
|
||||
} // KMS keys can't be immediately deleted; skip
|
||||
case "AWS::KMS::Alias" -> kmsService.deleteAlias(physicalId, region);
|
||||
case "AWS::SecretsManager::Secret" -> deleteSecretSafe(physicalId, region);
|
||||
case "AWS::Events::Rule" -> deleteEventBridgeRuleSafe(physicalId, region);
|
||||
case "AWS::Events::Rule" -> deleteEventBridgeRuleSafe(physicalId, null, region);
|
||||
case "AWS::Events::EventBus" -> deleteEventBusSafe(physicalId, region);
|
||||
case "AWS::Events::EventBusPolicy" -> removeEventBusPolicySafe(physicalId, region);
|
||||
case "AWS::ApiGateway::RestApi" -> apiGatewayService.deleteRestApi(region, physicalId);
|
||||
case "AWS::ApiGatewayV2::Api" -> apiGatewayV2Service.deleteApi(region, physicalId);
|
||||
case "AWS::ECR::Repository" ->
|
||||
@@ -2366,6 +2379,9 @@ public class CloudFormationResourceProvisioner {
|
||||
state, description, roleArn, Map.of(), region);
|
||||
r.setPhysicalId(ruleName);
|
||||
r.getAttributes().put("Arn", rule.getArn());
|
||||
if (busName != null && !busName.isBlank()) {
|
||||
r.getAttributes().put("EventBusName", busName);
|
||||
}
|
||||
|
||||
// Provision inline targets
|
||||
if (props != null && props.has("Targets")) {
|
||||
@@ -2410,20 +2426,147 @@ public class CloudFormationResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteEventBridgeRuleSafe(String ruleName, String region) {
|
||||
private void deleteEventBridgeRuleSafe(String ruleName, String busName, String region) {
|
||||
try {
|
||||
// Remove all targets before deleting the rule
|
||||
var targets = eventBridgeService.listTargetsByRule(ruleName, null, region);
|
||||
var targets = eventBridgeService.listTargetsByRule(ruleName, busName, region);
|
||||
if (!targets.isEmpty()) {
|
||||
List<String> targetIds = targets.stream().map(Target::getId).toList();
|
||||
eventBridgeService.removeTargets(ruleName, null, targetIds, region);
|
||||
eventBridgeService.removeTargets(ruleName, busName, targetIds, region);
|
||||
}
|
||||
eventBridgeService.deleteRule(ruleName, null, region);
|
||||
eventBridgeService.deleteRule(ruleName, busName, region);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not delete EventBridge rule {0}: {1}", ruleName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void provisionEventBus(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String region, String stackName) {
|
||||
String name = resolveOptional(props, "Name", engine);
|
||||
if (name == null || name.isBlank()) {
|
||||
name = generatePhysicalName(stackName, r.getLogicalId(), 256, false);
|
||||
}
|
||||
String description = resolveOptional(props, "Description", engine);
|
||||
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
|
||||
for (JsonNode tag : props.get("Tags")) {
|
||||
String key = engine.resolve(tag.path("Key"));
|
||||
if (!key.isEmpty()) {
|
||||
tags.put(key, engine.resolve(tag.path("Value")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EventBus bus;
|
||||
try {
|
||||
bus = eventBridgeService.createEventBus(name, description, tags, region);
|
||||
} catch (AwsException e) {
|
||||
if ("ResourceAlreadyExistsException".equals(e.getErrorCode())) {
|
||||
bus = eventBridgeService.describeEventBus(name, region);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Optional inline resource policy (rare for CDK EventBus constructs).
|
||||
if (props != null && props.has("Policy") && !props.get("Policy").isNull()) {
|
||||
String policyJson = engine.resolveNode(props.get("Policy")).toString();
|
||||
eventBridgeService.putPermission(name, null, null, null, null, policyJson, region);
|
||||
}
|
||||
|
||||
r.setPhysicalId(name);
|
||||
r.getAttributes().put("Arn", bus.getArn());
|
||||
r.getAttributes().put("Name", name);
|
||||
}
|
||||
|
||||
private void deleteEventBusSafe(String name, String region) {
|
||||
try {
|
||||
eventBridgeService.deleteEventBus(name, region);
|
||||
} catch (Exception e) {
|
||||
// Warn (not debug): the stack still records the resource as DELETE_COMPLETE, so a bus
|
||||
// left alive here (e.g. rules deleted out of dependency order) is a silent divergence
|
||||
// that must stay diagnosable.
|
||||
LOG.warnv("Could not delete event bus {0}: {1}", name, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void provisionEventBusPolicy(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String region) {
|
||||
String busName = resolveOrDefault(props, "EventBusName", engine, "default");
|
||||
String statementId = resolveOptional(props, "StatementId", engine);
|
||||
if (statementId == null || statementId.isBlank()) {
|
||||
throw new AwsException("ValidationException", "EventBusPolicy StatementId is required.", 400);
|
||||
}
|
||||
|
||||
if (props != null && props.has("Statement") && props.get("Statement").isObject()) {
|
||||
// Statement form: merge the full statement into the bus policy, keyed by Sid,
|
||||
// so multiple EventBusPolicy resources on the same bus coexist.
|
||||
try {
|
||||
ObjectNode statement = (ObjectNode) engine.resolveNode(props.get("Statement")).deepCopy();
|
||||
statement.put("Sid", statementId);
|
||||
|
||||
EventBus bus = eventBridgeService.describeEventBus(busName, region);
|
||||
ObjectNode policy;
|
||||
String current = bus.getPolicy();
|
||||
if (current != null && !current.isBlank()) {
|
||||
policy = (ObjectNode) objectMapper.readTree(current);
|
||||
} else {
|
||||
policy = objectMapper.createObjectNode();
|
||||
policy.put("Version", "2012-10-17");
|
||||
policy.putArray("Statement");
|
||||
}
|
||||
ArrayNode statements = policy.withArray("Statement");
|
||||
for (int i = 0; i < statements.size(); i++) {
|
||||
if (statementId.equals(statements.get(i).path("Sid").asText(null))) {
|
||||
statements.remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
statements.add(statement);
|
||||
eventBridgeService.putPermission(busName, null, null, statementId, null,
|
||||
objectMapper.writeValueAsString(policy), region);
|
||||
} catch (AwsException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid EventBusPolicy Statement: " + e.getMessage(), 400);
|
||||
}
|
||||
r.setPhysicalId(busName + "|" + statementId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Individual form: Action + Principal (+ optional Condition {Type, Key, Value}).
|
||||
String action = resolveOptional(props, "Action", engine);
|
||||
String principal = resolveOptional(props, "Principal", engine);
|
||||
String conditionJson = null;
|
||||
if (props != null && props.has("Condition") && !props.get("Condition").isNull()) {
|
||||
JsonNode c = engine.resolveNode(props.get("Condition"));
|
||||
String type = c.path("Type").asText(null);
|
||||
String key = c.path("Key").asText(null);
|
||||
String value = c.path("Value").asText(null);
|
||||
if (type != null && key != null && value != null) {
|
||||
ObjectNode condition = objectMapper.createObjectNode();
|
||||
condition.set(type, objectMapper.createObjectNode().put(key, value));
|
||||
conditionJson = condition.toString();
|
||||
}
|
||||
}
|
||||
eventBridgeService.putPermission(busName, action, principal, statementId, conditionJson, null, region);
|
||||
|
||||
r.setPhysicalId(busName + "|" + statementId);
|
||||
}
|
||||
|
||||
private void removeEventBusPolicySafe(String physicalId, String region) {
|
||||
try {
|
||||
int sep = physicalId.lastIndexOf('|');
|
||||
String busName = sep >= 0 ? physicalId.substring(0, sep) : "default";
|
||||
String statementId = sep >= 0 ? physicalId.substring(sep + 1) : physicalId;
|
||||
eventBridgeService.removePermission(busName, statementId, false, region);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not remove event bus policy {0}: {1}", physicalId, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// ── Batch ────────────────────────────────────────────────────────────────
|
||||
|
||||
private void provisionBatchComputeEnvironment(StackResource r, JsonNode props,
|
||||
|
||||
Reference in new issue
Block a user