fix(events): resolve stored bus and rule tags for IAM (#8412)

IAM enforcement now reads an event bus's or rule's stored tags for
aws:ResourceTag conditions, from the exact ARN's account, Region and
partition, without creating the default bus. ListTagsForResource,
TagResource and UntagResource share the same lookup.

Closes #8397
This commit is contained in:
luiseterc authored and GitHub committed 2026-11-16 07:26:59 -04:00
1 parent 8625f09f4c
commit bd3431283b
5 files changed
+294 -94

No files matched your search

@@ -56,6 +56,7 @@ import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -748,30 +749,41 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
public Map<String, String> listTagsForResource(String resourceArn, String region) {
validateTagResourceArn(resourceArn, region);
String resource = AwsArnUtils.parse(resourceArn).resource();
if ("event-bus/default".equals(resource)) {
getOrCreateDefaultBus(region);
}
return requireTagResource(resourceArn, region, resource).tags();
}
private record TagResource(String arn, Map<String, String> tags, Runnable save) {}
private Optional<TagResource> findTagResource(String accountId, String region, String resource) {
if (resource.startsWith("event-bus/")) {
String busName = resource.substring("event-bus/".length());
if (busName.isBlank()) {
throw new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 400);
}
return describeEventBus(busName, region).getTags();
String key = busKey(region, resource.substring("event-bus/".length()));
return getInAccount(busStore, accountId, key)
.map(bus -> new TagResource(bus.getArn(), bus.getTags(),
() -> putInAccount(busStore, accountId, key, bus)));
}
if (resource.startsWith("rule/")) {
RuleRef ref = parseRuleResource(resource);
String key = ruleKey(region, ref.busName(), ref.ruleName());
return ruleStore.get(key)
.map(Rule::getTags)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceArn, 400));
return getInAccount(ruleStore, accountId, key)
.map(rule -> new TagResource(rule.getArn(), rule.getTags(),
() -> putInAccount(ruleStore, accountId, key, rule)));
}
if (resource.startsWith("archive/")) {
String archiveName = resource.substring("archive/".length());
String key = archiveKey(region, archiveName);
return archiveStore.get(key)
.map(Archive::getTags)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Archive not found: " + archiveName, 400));
String key = archiveKey(region, resource.substring("archive/".length()));
return getInAccount(archiveStore, accountId, key)
.map(archive -> new TagResource(archive.getArchiveArn(), archive.getTags(),
() -> putInAccount(archiveStore, accountId, key, archive)));
}
throw new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 400);
return Optional.empty();
}
private TagResource requireTagResource(String resourceArn, String region, String resource) {
return findTagResource(regionResolver.getAccountId(), region, resource)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceArn, 400));
}
/** EventBridge tag operations only address resources in the caller's account and Region. */
@@ -811,79 +823,19 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
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());
String key = archiveKey(region, archiveName);
Archive archive = archiveStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Archive not found: " + archiveName, 400));
archive.getTags().putAll(tags);
archiveStore.put(key, archive);
resourceGroupsTaggingService.tagResources(List.of(resourceArn), tags, region);
return;
}
if (resource.startsWith("event-bus/")) {
String busName = resource.substring("event-bus/".length());
String key = busKey(region, busName);
EventBus bus = busStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceArn, 400));
bus.getTags().putAll(tags);
busStore.put(key, bus);
resourceGroupsTaggingService.tagResources(List.of(resourceArn), tags, region);
return;
}
if (resource.startsWith("rule/")) {
RuleRef ref = parseRuleResource(resource);
String key = ruleKey(region, ref.busName(), ref.ruleName());
Rule rule = ruleStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceArn, 400));
rule.getTags().putAll(tags);
ruleStore.put(key, rule);
resourceGroupsTaggingService.tagResources(List.of(resourceArn), tags, region);
return;
}
throw new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 400);
TagResource target = requireTagResource(resourceArn, region, resource);
target.tags().putAll(tags);
target.save().run();
resourceGroupsTaggingService.tagResources(List.of(resourceArn), tags, region);
}
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());
String key = archiveKey(region, archiveName);
Archive archive = archiveStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Archive not found: " + archiveName, 400));
tagKeys.forEach(archive.getTags()::remove);
archiveStore.put(key, archive);
resourceGroupsTaggingService.untagResources(List.of(resourceArn), tagKeys, region);
return;
}
if (resource.startsWith("event-bus/")) {
String busName = resource.substring("event-bus/".length());
String key = busKey(region, busName);
EventBus bus = busStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceArn, 400));
tagKeys.forEach(bus.getTags()::remove);
busStore.put(key, bus);
resourceGroupsTaggingService.untagResources(List.of(resourceArn), tagKeys, region);
return;
}
if (resource.startsWith("rule/")) {
RuleRef ref = parseRuleResource(resource);
String key = ruleKey(region, ref.busName(), ref.ruleName());
Rule rule = ruleStore.get(key)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceArn, 400));
tagKeys.forEach(rule.getTags()::remove);
ruleStore.put(key, rule);
resourceGroupsTaggingService.untagResources(List.of(resourceArn), tagKeys, region);
return;
}
throw new AwsException("ResourceNotFoundException", "Resource not found: " + resourceArn, 400);
TagResource target = requireTagResource(resourceArn, region, resource);
tagKeys.forEach(target.tags()::remove);
target.save().run();
resourceGroupsTaggingService.untagResources(List.of(resourceArn), tagKeys, region);
}
// ──────────────────────────── Permissions ────────────────────────────
@@ -1920,6 +1872,24 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
return "events";
}
/** The tags of the exact bus or rule ARN, read without changing the stored resource. */
@Override
public Map<String, String> storedTags(String resourceArn) {
if (!AwsArnUtils.isArnFor(resourceArn, "events")) {
return Map.of();
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
if (!regionResolver.partitionForRegion(arn.region()).equals(arn.partition())) {
return Map.of();
}
if (!arn.resource().startsWith("event-bus/") && !arn.resource().startsWith("rule/")) {
return Map.of();
}
return findTagResource(arn.accountId(), arn.region(), arn.resource())
.filter(target -> resourceArn.equals(target.arn()))
.map(TagResource::tags).map(LinkedHashMap::new).orElseGet(LinkedHashMap::new);
}
/** The Region's default event bus, which a request naming no bus acts on. */
@Override
public Optional<String> findDefault(String type, String region) {