diff --git a/docs/services/eventbridge.md b/docs/services/eventbridge.md index bb482520f3..26780a7952 100644 --- a/docs/services/eventbridge.md +++ b/docs/services/eventbridge.md @@ -8,7 +8,7 @@ | Action | Description | | --- | --- | -| `CreateEventBus` | Create a custom event bus | +| `CreateEventBus` | Create a custom event bus, with its description, KMS key, dead-letter config and log config | | `DeleteEventBus` | Delete an event bus | | `DescribeEventBus` | Get event bus details | | `UpdateEventBus` | Update event bus description, KMS key, dead-letter config, or log config | diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java index acd97297bc..f17f588a81 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeHandler.java @@ -118,10 +118,16 @@ public class EventBridgeHandler { private Response handleCreateEventBus(JsonNode request, String region) { String name = request.path("Name").asText(null); String description = request.path("Description").asText(null); + String kmsKey = request.path("KmsKeyIdentifier").asText(null); Map tags = parseTagsArray(request.path("Tags")); - EventBus bus = eventBridgeService.createEventBus(name, description, tags, region); + EventBus bus = eventBridgeService.createEventBus(name, description, kmsKey, + configJson(request.path("DeadLetterConfig")), configJson(request.path("LogConfig")), tags, region); + // CreateEventBus answers with the bus's ARN and the configuration it was given. + ObjectNode configuration = buildBusNode(bus); + configuration.remove(List.of("Name", "Arn", "CreationTime", "Policy")); ObjectNode response = objectMapper.createObjectNode(); response.put("EventBusArn", bus.getArn()); + response.setAll(configuration); return Response.ok(response).build(); } @@ -143,15 +149,16 @@ public class EventBridgeHandler { String kmsKey = request.path("KmsKeyIdentifier").asText(null); // Empty {} is treated as "no change", matching how omitted/null inputs flow. // Only a populated object triggers a write to the bus's nested config field. - JsonNode dlqNode = request.path("DeadLetterConfig"); - String dlq = (dlqNode.isObject() && !dlqNode.isEmpty()) ? dlqNode.toString() : null; - JsonNode logNode = request.path("LogConfig"); - String logCfg = (logNode.isObject() && !logNode.isEmpty()) ? logNode.toString() : null; - EventBus bus = eventBridgeService.updateEventBus( - name, description, kmsKey, dlq, logCfg, region); + EventBus bus = eventBridgeService.updateEventBus(name, description, kmsKey, + configJson(request.path("DeadLetterConfig")), configJson(request.path("LogConfig")), region); return Response.ok(buildBusNode(bus)).build(); } + /** A nested configuration object as its JSON; empty {} or absent means not given. */ + private static String configJson(JsonNode node) { + return node.isObject() && !node.isEmpty() ? node.toString() : null; + } + private Response handleListEventBuses(JsonNode request, String region) { String namePrefix = request.path("NamePrefix").asText(null); List buses = eventBridgeService.listEventBuses(namePrefix, region); diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java index 458dbb3f3a..0d2d86f8f0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java @@ -175,6 +175,17 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns public EventBus createEventBus(String name, String description, Map tags, String region) { + return createEventBus(name, description, null, null, null, tags, region); + } + + /** + * Creates a custom bus with the KMS key, dead-letter queue and log configuration the request + * gives, as {@code CreateEventBus} takes them; a null member leaves the bus without it. The + * dead-letter and log configurations are stored as their JSON, as {@link #updateEventBus} does. + */ + public EventBus createEventBus(String name, String description, String kmsKeyIdentifier, + String deadLetterConfig, String logConfig, + Map tags, String region) { validateCustomEventBusName(name); validateEventBusDescription(description); String key = busKey(region, name); @@ -188,6 +199,15 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns description, Instant.now() ); + if (kmsKeyIdentifier != null && !kmsKeyIdentifier.isBlank()) { + bus.setKmsKeyIdentifier(kmsKeyIdentifier); + } + if (deadLetterConfig != null && !deadLetterConfig.isBlank()) { + bus.setDeadLetterConfig(deadLetterConfig); + } + if (logConfig != null && !logConfig.isBlank()) { + bus.setLogConfig(logConfig); + } if (tags != null) { bus.getTags().putAll(tags); resourceGroupsTaggingService.tagResources(List.of(bus.getArn()), tags, region); diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeCreateEventBusIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeCreateEventBusIntegrationTest.java new file mode 100644 index 0000000000..9ca8c8e171 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeCreateEventBusIntegrationTest.java @@ -0,0 +1,101 @@ +package io.github.hectorvent.floci.services.eventbridge; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.nullValue; + +/** + * CreateEventBus keeps the KMS key, dead-letter queue and log configuration it is given, as + * {@code terraform-aws-modules/eventbridge} sets them on a bus with {@code log_config}: the create + * response echoes them, and DescribeEventBus and ListEventBuses report them afterwards. + */ +@QuarkusTest +class EventBridgeCreateEventBusIntegrationTest { + + private static final String CT = "application/x-amz-json-1.1"; + private static final String CREATE = "AWSEvents.CreateEventBus"; + private static final String DESCRIBE = "AWSEvents.DescribeEventBus"; + private static final String LIST = "AWSEvents.ListEventBuses"; + private static final String DELETE = "AWSEvents.DeleteEventBus"; + private static final String DLQ = "arn:aws:sqs:us-east-1:000000000000:bus-dlq"; + private static final String KEY = "arn:aws:kms:us-east-1:000000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab"; + + @BeforeAll + static void configure() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void theConfigurationGivenOnCreateIsKeptAndEchoed() { + String bus = "create-config-bus"; + given().contentType(CT).header("X-Amz-Target", CREATE) + .body(""" + {"Name": "%s", "Description": "orders", + "KmsKeyIdentifier": "%s", + "DeadLetterConfig": {"Arn": "%s"}, + "LogConfig": {"IncludeDetail": "NONE", "Level": "ERROR"}} + """.formatted(bus, KEY, DLQ)) + .when().post("/") + .then().statusCode(200) + .body("EventBusArn", endsWith(":event-bus/" + bus)) + .body("Description", equalTo("orders")) + .body("KmsKeyIdentifier", equalTo(KEY)) + .body("DeadLetterConfig.Arn", equalTo(DLQ)) + .body("LogConfig.IncludeDetail", equalTo("NONE")) + .body("LogConfig.Level", equalTo("ERROR")) + .body("Name", nullValue()) + .body("CreationTime", nullValue()); + + given().contentType(CT).header("X-Amz-Target", DESCRIBE) + .body("{\"Name\":\"" + bus + "\"}") + .when().post("/") + .then().statusCode(200) + .body("KmsKeyIdentifier", equalTo(KEY)) + .body("DeadLetterConfig.Arn", equalTo(DLQ)) + .body("LogConfig.IncludeDetail", equalTo("NONE")) + .body("LogConfig.Level", equalTo("ERROR")); + given().contentType(CT).header("X-Amz-Target", LIST) + .body("{\"NamePrefix\":\"" + bus + "\"}") + .when().post("/") + .then().statusCode(200) + .body("EventBuses.LogConfig.Level", hasItem("ERROR")); + + delete(bus); + } + + @Test + void aBusCreatedWithoutConfigurationOrWithEmptyObjectsHasNone() { + String bus = "create-bare-bus"; + given().contentType(CT).header("X-Amz-Target", CREATE) + .body("{\"Name\":\"" + bus + "\",\"DeadLetterConfig\":{},\"LogConfig\":{}}") + .when().post("/") + .then().statusCode(200) + .body("EventBusArn", endsWith(":event-bus/" + bus)) + .body("DeadLetterConfig", nullValue()) + .body("LogConfig", nullValue()); + + given().contentType(CT).header("X-Amz-Target", DESCRIBE) + .body("{\"Name\":\"" + bus + "\"}") + .when().post("/") + .then().statusCode(200) + .body("KmsKeyIdentifier", nullValue()) + .body("DeadLetterConfig", nullValue()) + .body("LogConfig", nullValue()); + + delete(bus); + } + + private static void delete(String bus) { + given().contentType(CT).header("X-Amz-Target", DELETE) + .body("{\"Name\":\"" + bus + "\"}") + .when().post("/") + .then().statusCode(200); + } +}