fix(cloudtrail): give resource-tag conditions the tags of the trail a request names (#8814)

The generated request resources give CloudTrail's trail type the
aws:ResourceTag/ key, but no service answered the cloudtrail namespace's
stored tags, so a condition on a trail's tags saw none.
CloudTrailService now answers it: storedTags reads the trail stored
under its home Region, which its ARN names, and its name, in the
request's account, where the tag handlers read, in place, and matched on
the whole ARN. A tag with no value, which AddTags allows, reads as the
empty string. A trail is stored whole and its tags never change in
place, so no lock change.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 23:02:52 -04:00
1 parent 98c0a50ab5
commit 6829c0b592
5 files changed
+132 -17

No files matched your search

@@ -48,9 +48,10 @@ import java.util.Optional;
* stream; Cognito, for the tags of a user pool or an identity pool; OpenSearch, for the tags of a domain; Signer, for
* the tags of a signing profile; Access Analyzer, for the tags of an analyzer; Managed Service for Apache Flink, for
* the tags of an application; S3 Vectors, for the tags of a vector bucket or an index; Timestream for InfluxDB, for the
* tags of a DB instance, a DB cluster, a parameter group or a backup); looked up lazily by {@code ResourceArnBuilder}
* and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or
* an ARN is of, so IAM never depends on that service directly.</p>
* tags of a DB instance, a DB cluster, a parameter group or a backup; CloudTrail, for the tags of a trail); looked up
* lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
* service directly.</p>
*
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
@@ -11,6 +11,8 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.AwsRegions;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.ServicePrincipals;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.cloudtrail.model.AdvancedEventSelector;
@@ -32,6 +34,7 @@ import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -43,12 +46,14 @@ import java.util.function.Supplier;
import java.util.regex.Pattern;
@ApplicationScoped
public class CloudTrailService {
public class CloudTrailService implements StoredResourceArns {
private static final Logger LOG = Logger.getLogger(CloudTrailService.class);
private static final String EVENT_VERSION = "1.11";
private static final String S3_EVENT_SOURCE = ServicePrincipals.of("s3");
/** What a trail's ARN resource starts with, its name following. */
private static final String TRAIL_RESOURCE = "trail/";
static final int MAX_PENDING_RECORDS_PER_TRAIL = 1024;
static final long MAX_PENDING_BYTES_PER_TRAIL = 4L * 1024L * 1024L;
@@ -133,7 +138,7 @@ public class CloudTrailService {
"Trail " + name + " already exists.", 400);
}
String arn = AwsArnUtils.Arn.of("cloudtrail", region, regionResolver.getAccountId(),
"trail/" + name).toString();
TRAIL_RESOURCE + name).toString();
Trail trail = new Trail(
name, arn, s3BucketName, s3KeyPrefix, snsTopicArn,
includeGlobalServiceEvents, isMultiRegionTrail, region,
@@ -382,6 +387,42 @@ public class CloudTrailService {
return findEntryByArnOrThrow(resourceId).tags();
}
@Override
public String namespace() {
return "cloudtrail";
}
/** A request names a trail by its name or its ARN, the name being the end of its ARN: nothing to find. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
/**
* The tags of the trail whose ARN is {@code arn}, in the request's account, where the tag handlers read: an ARN of
* another account names none. Read in place under the trail's home Region, which its ARN names, and its name, a
* trail stored before storage was account-aware included, and matched on the whole ARN. A tag CloudTrail holds
* with no value, which AddTags allows, reads as the empty string, so a condition compares a string. A trail is
* stored whole and its tags never change in place, so the read is of one whole set.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "cloudtrail")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
if (!parsed.accountId().equals(regionResolver.getAccountId())
|| !parsed.resource().startsWith(TRAIL_RESOURCE)) {
return Map.of();
}
Map<String, String> tags = new LinkedHashMap<>();
AccountAwareStorageBackend.readInAccount(store, parsed.accountId(),
regionKey(parsed.region(), parsed.resource().substring(TRAIL_RESOURCE.length())))
.filter(entry -> arn.equals(entry.trail().trailArn()))
.ifPresent(entry -> entry.tags().forEach((key, value) -> tags.put(key, value == null ? "" : value)));
return tags;
}
private CloudTrailEntry findEntryByArnOrThrow(String resourceId) {
String key = findKeyByArnOrThrow(resourceId);
return store.get(key).orElseThrow(() -> new AwsException(
@@ -399,7 +440,7 @@ public class CloudTrailService {
throw new AwsException("CloudTrailARNInvalidException",
resourceId + " is not a valid ARN.", 400);
}
if (!"cloudtrail".equals(arn.service()) || !arn.resource().startsWith("trail/")) {
if (!"cloudtrail".equals(arn.service()) || !arn.resource().startsWith(TRAIL_RESOURCE)) {
throw new AwsException("CloudTrailARNInvalidException",
resourceId + " is not a valid trail ARN.", 400);
}