Light, fluffy, and always free - The AWS Local Emulator alternative
fix(eventbridge): enforce tag resource ownership (#6326)
Jimmy Liu Jia1473fc9b834d
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.javaindex ea316f1bd5..c9fdba71fc 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@@ -550,29 +550,52 @@ public class EventBridgeService implements ResourceProvider {// ──────────────────────────── Tags ────────────────────────────public Map<String, String> listTagsForResource(String resourceArn, String region) {+ validateTagResourceArn(resourceArn, region);String resource = AwsArnUtils.parse(resourceArn).resource();if (resource.startsWith("event-bus/")) {String busName = resource.substring("event-bus/".length());- String key = busKey(region, busName);- return busStore.get(key)- .map(EventBus::getTags)- .orElse(Map.of());+ if (busName.isBlank()) {+ throw new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 400);+ }+ return describeEventBus(busName, region).getTags();}if (resource.startsWith("rule/")) {RuleRef ref = parseRuleResource(resource);String key = ruleKey(region, ref.busName(), ref.ruleName());return ruleStore.get(key).map(Rule::getTags)- .orElse(Map.of());+ .orElseThrow(() -> new AwsException("ResourceNotFoundException",+ "Resource not found: " + resourceArn, 400));}if (resource.startsWith("archive/")) {String archiveName = resource.substring("archive/".length());String key = archiveKey(region, archiveName);return archiveStore.get(key).map(Archive::getTags)- .orElse(Map.of());+ .orElseThrow(() -> new AwsException("ResourceNotFoundException",+ "Archive not found: " + archiveName, 400));+ }+ throw new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 400);+ }++ /** EventBridge tag operations only address resources in the caller's account and Region. */+ private void validateTagResourceArn(String resourceArn, String region) {+ AwsArnUtils.Arn arn;+ try {+ arn = AwsArnUtils.parse(resourceArn);+ } catch (IllegalArgumentException e) {+ throw new AwsException("ResourceNotFoundException",+ "Resource not found: " + resourceArn, 400);+ }+ if (!"events".equals(arn.service())+ || arn.partition().isBlank()+ || AwsArnUtils.isForeignPartition(arn, regionResolver.partitionForRegion(region))+ || !region.equals(arn.region())+ || arn.accountId().isBlank()+ || AwsArnUtils.isForeignAccount(arn, regionResolver.getAccountId())) {+ throw new AwsException("ResourceNotFoundException",+ "Resource not found: " + resourceArn, 400);}- return Map.of();}/** Resolves the bus and rule name from a {@code rule/...} ARN resource segment. */@@ -590,6 +613,7 @@ public class EventBridgeService implements ResourceProvider {}public void tagResource(String resourceArn, Map<String, String> tags, String region) {+ validateTagResourceArn(resourceArn, region);String resource = AwsArnUtils.parse(resourceArn).resource();if (resource.startsWith("archive/")) {String archiveName = resource.substring("archive/".length());@@ -628,6 +652,7 @@ public class EventBridgeService implements ResourceProvider {}public void untagResource(String resourceArn, List<String> tagKeys, String region) {+ validateTagResourceArn(resourceArn, region);String resource = AwsArnUtils.parse(resourceArn).resource();if (resource.startsWith("archive/")) {String archiveName = resource.substring("archive/".length());diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeIntegrationTest.javaindex b33c1d7d7f..d60bcc654c 100644--- a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeIntegrationTest.java@@ -276,4 +276,21 @@ class EventBridgeIntegrationTest {.then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));}++ @Test+ @Order(9)+ void listTagsForMissingResourceReturnsNotFound() {+ for (String resource : new String[] {+ "event-bus/missing-tags-bus", "rule/missing-tags-rule",+ "archive/missing-tags-archive", "replay/unsupported-tags-resource"+ }) {+ given()+ .contentType(EVENT_BRIDGE_CONTENT_TYPE)+ .header("X-Amz-Target", "AWSEvents.ListTagsForResource")+ .body("{\"ResourceARN\":\"arn:aws:events:us-east-1:000000000000:" + resource + "\"}")+ .when().post("/")+ .then().statusCode(400)+ .body("__type", equalTo("ResourceNotFoundException"));+ }+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.javaindex 44b5129fd0..4cf935dd6c 100644--- a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeServiceTest.java@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.eventbridge.model.Rule;import io.github.hectorvent.floci.services.eventbridge.model.RuleState;import io.github.hectorvent.floci.services.eventbridge.model.Target;import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingService;+import io.github.hectorvent.floci.testing.PartitionMatrix;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.junit.jupiter.params.ParameterizedTest;@@ -115,6 +116,67 @@ class EventBridgeServiceTest {service.createEventBus("my-bus", null, null, REGION));}+ @Test+ void tagResourceRejectsArnFromAnotherAccount() {+ EventBus bus = service.createEventBus("owned-bus", null, null, REGION);+ String foreignAccountArn = bus.getArn().replace(":000000000000:", ":111111111111:");++ AwsException error = assertThrows(AwsException.class, () ->+ service.tagResource(foreignAccountArn, Map.of("env", "test"), REGION));++ assertEquals("ResourceNotFoundException", error.getErrorCode());+ assertTrue(service.listTagsForResource(bus.getArn(), REGION).isEmpty());+ }++ @Test+ void tagResourceRejectsArnFromAnotherRegion() {+ EventBus bus = service.createEventBus("owned-bus", null, null, REGION);+ String foreignRegionArn = bus.getArn().replace(":us-east-1:", ":eu-west-1:");++ AwsException error = assertThrows(AwsException.class, () ->+ service.tagResource(foreignRegionArn, Map.of("env", "test"), REGION));++ assertEquals("ResourceNotFoundException", error.getErrorCode());+ assertTrue(service.listTagsForResource(bus.getArn(), REGION).isEmpty());+ }++ @ParameterizedTest+ @MethodSource(PartitionMatrix.NON_COMMERCIAL_CASES)+ void tagResourceAcceptsResourceArnFromRequestedPartition(PartitionMatrix.PartitionCase partitionCase) {+ String region = partitionCase.region();+ EventBus bus = service.createEventBus("partition-bus", null, null, region);++ service.tagResource(bus.getArn(), Map.of("env", "test"), region);++ assertEquals(Map.of("env", "test"), service.listTagsForResource(bus.getArn(), region));+ PartitionMatrix.assertArnIn(partitionCase, bus.getArn());+ }++ @ParameterizedTest+ @ValueSource(strings = {+ "event-bus/missing", "event-bus/", "rule/missing", "archive/missing", "replay/missing"+ })+ void listTagsForResourceRejectsMissingOrUnsupportedResources(String resource) {+ String arn = "arn:aws:events:us-east-1:000000000000:" + resource;++ AwsException error = assertThrows(AwsException.class, () ->+ service.listTagsForResource(arn, REGION));++ assertEquals("ResourceNotFoundException", error.getErrorCode());+ }++ @Test+ void listTagsForResourceRejectsArnFromAnotherAccount() {+ EventBus bus = service.createEventBus("owned-bus", null, null, REGION);+ service.tagResource(bus.getArn(), Map.of("env", "test"), REGION);+ String foreignAccountArn = bus.getArn().replace(":000000000000:", ":111111111111:");++ AwsException error = assertThrows(AwsException.class, () ->+ service.listTagsForResource(foreignAccountArn, REGION));++ assertEquals("ResourceNotFoundException", error.getErrorCode());+ }+@Testvoid createEventBusBlankNameThrows() {assertThrows(AwsException.class, () ->