fix(cloudtrail): serialize trail mutations to prevent lost updates (#2911)

AddTags, RemoveTags, UpdateTrail, PutEventSelectors, StartLogging and StopLogging all did an unlocked get-then-put of the whole CloudTrailEntry, so two overlapping mutations of the same trail could silently drop one another's change.

Every trail mutation's read-modify-write now goes through withTrailLock, a per-trail-key mutex built on ConcurrentHashMap#compute's per-key atomicity; the remapping function runs the mutation and returns null, so no bookkeeping entry accumulates per trail. UpdateTrail reads its merge basis inside the lock and raises TrailNotFoundException if the entry is gone by the time it acquires it. CreateTrail stays unlocked, matching AwsConfigService.

Closes #2904
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-02 20:07:37 -05:00
1 parent 4ce53b51d6
commit 8c2619f965
2 files changed
+227 -35

No files matched your search

@@ -27,6 +27,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Supplier;
@ApplicationScoped
public class CloudTrailService {
@@ -45,6 +46,15 @@ public class CloudTrailService {
private final ConcurrentHashMap<TrailKey, ConcurrentLinkedQueue<ObjectNode>> pendingRecordsByTrail =
new ConcurrentHashMap<>();
/**
* Backs {@link #withTrailLock}: serializes read-modify-write mutations to the same trail. A
* plain {@code get} then {@code put} on the store is not atomic across the pair, so two
* overlapping mutations of the same trail, even from different actions, e.g. AddTags racing
* StartLogging, can interleave such that the second {@code put} replaces the whole entry the
* first read, silently discarding whatever the first call changed.
*/
private final ConcurrentHashMap<String, Object> trailLocks = new ConcurrentHashMap<>();
@Inject
public CloudTrailService(StorageFactory storageFactory, RegionResolver regionResolver,
IamService iamService, ObjectMapper mapper) {
@@ -82,7 +92,8 @@ public class CloudTrailService {
public void deleteTrail(String region, String trailNameOrArn) {
Trail trail = findTrailOrThrow(region, trailNameOrArn);
store.delete(regionKey(trail.homeRegion(), trail.name()));
String key = regionKey(trail.homeRegion(), trail.name());
withTrailLock(key, () -> store.delete(key));
pendingRecordsByTrail.keySet().removeIf(k -> k.trailName().equals(trail.name()));
}
@@ -90,23 +101,28 @@ public class CloudTrailService {
String s3BucketName, String s3KeyPrefix, String snsTopicArn,
Boolean includeGlobalServiceEvents, Boolean isMultiRegionTrail,
Boolean enableLogFileValidation, Boolean isOrganizationTrail) {
Trail existing = findTrailOrThrow(region, trailNameOrArn);
Trail updated = new Trail(
existing.name(),
existing.trailArn(),
s3BucketName != null ? s3BucketName : existing.s3BucketName(),
s3KeyPrefix != null ? s3KeyPrefix : existing.s3KeyPrefix(),
snsTopicArn != null ? snsTopicArn : existing.snsTopicArn(),
includeGlobalServiceEvents != null ? includeGlobalServiceEvents : existing.includeGlobalServiceEvents(),
isMultiRegionTrail != null ? isMultiRegionTrail : existing.isMultiRegionTrail(),
existing.homeRegion(),
enableLogFileValidation != null ? enableLogFileValidation : existing.logFileValidationEnabled(),
existing.hasCustomEventSelectors(),
existing.hasInsightSelectors(),
isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail());
String key = regionKey(existing.homeRegion(), existing.name());
store.get(key).ifPresent(entry -> store.put(key, entry.withTrail(updated)));
return updated;
Trail resolved = findTrailOrThrow(region, trailNameOrArn);
String key = regionKey(resolved.homeRegion(), resolved.name());
return withTrailLock(key, () -> {
CloudTrailEntry entry = store.get(key).orElseThrow(() -> new AwsException(
"TrailNotFoundException", "Unknown trail: " + trailNameOrArn, 400));
Trail existing = entry.trail();
Trail updated = new Trail(
existing.name(),
existing.trailArn(),
s3BucketName != null ? s3BucketName : existing.s3BucketName(),
s3KeyPrefix != null ? s3KeyPrefix : existing.s3KeyPrefix(),
snsTopicArn != null ? snsTopicArn : existing.snsTopicArn(),
includeGlobalServiceEvents != null ? includeGlobalServiceEvents : existing.includeGlobalServiceEvents(),
isMultiRegionTrail != null ? isMultiRegionTrail : existing.isMultiRegionTrail(),
existing.homeRegion(),
enableLogFileValidation != null ? enableLogFileValidation : existing.logFileValidationEnabled(),
existing.hasCustomEventSelectors(),
existing.hasInsightSelectors(),
isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail());
store.put(key, entry.withTrail(updated));
return updated;
});
}
public List<Trail> describeTrails(String region, List<String> trailNameOrArnList) {
@@ -135,7 +151,7 @@ public class CloudTrailService {
Trail trail = findTrailOrThrow(region, trailNameOrArn);
List<EventSelector> normalized = selectors == null ? List.of() : List.copyOf(selectors);
String key = regionKey(trail.homeRegion(), trail.name());
store.get(key).ifPresent(entry -> store.put(key, entry.withSelectors(normalized, true)));
withTrailLock(key, () -> store.get(key).ifPresent(entry -> store.put(key, entry.withSelectors(normalized, true))));
return normalized;
}
@@ -149,13 +165,13 @@ public class CloudTrailService {
public void startLogging(String region, String trailNameOrArn) {
Trail trail = findTrailOrThrow(region, trailNameOrArn);
String key = regionKey(trail.homeRegion(), trail.name());
store.get(key).ifPresent(entry -> store.put(key, entry.startLogging(System.currentTimeMillis())));
withTrailLock(key, () -> store.get(key).ifPresent(entry -> store.put(key, entry.startLogging(System.currentTimeMillis()))));
}
public void stopLogging(String region, String trailNameOrArn) {
Trail trail = findTrailOrThrow(region, trailNameOrArn);
String key = regionKey(trail.homeRegion(), trail.name());
store.get(key).ifPresent(entry -> store.put(key, entry.stopLogging(System.currentTimeMillis())));
withTrailLock(key, () -> store.get(key).ifPresent(entry -> store.put(key, entry.stopLogging(System.currentTimeMillis()))));
}
public TrailStatus getTrailStatus(String region, String trailNameOrArn) {
@@ -174,22 +190,30 @@ public class CloudTrailService {
private static final int MAX_TAGS_PER_RESOURCE = 50;
public void addTags(String resourceId, Map<String, String> tagsToAdd) {
CloudTrailEntry entry = findEntryByArnOrThrow(resourceId);
Map<String, String> merged = entry.mutableTags();
merged.putAll(tagsToAdd);
if (merged.size() > MAX_TAGS_PER_RESOURCE) {
throw new AwsException("TagsLimitExceededException",
"Tag limit exceeded for resource " + resourceId
+ ". Maximum allowed: " + MAX_TAGS_PER_RESOURCE + ".", 400);
}
store.put(regionKey(entry.trail().homeRegion(), entry.trail().name()), entry.withTags(merged));
String key = findKeyByArnOrThrow(resourceId);
withTrailLock(key, () -> {
CloudTrailEntry entry = store.get(key).orElseThrow(() -> new AwsException(
"ResourceNotFoundException", "Resource not found: " + resourceId, 400));
Map<String, String> merged = entry.mutableTags();
merged.putAll(tagsToAdd);
if (merged.size() > MAX_TAGS_PER_RESOURCE) {
throw new AwsException("TagsLimitExceededException",
"Tag limit exceeded for resource " + resourceId
+ ". Maximum allowed: " + MAX_TAGS_PER_RESOURCE + ".", 400);
}
store.put(key, entry.withTags(merged));
});
}
public void removeTags(String resourceId, List<String> tagKeys) {
CloudTrailEntry entry = findEntryByArnOrThrow(resourceId);
Map<String, String> remaining = entry.mutableTags();
tagKeys.forEach(remaining::remove);
store.put(regionKey(entry.trail().homeRegion(), entry.trail().name()), entry.withTags(remaining));
String key = findKeyByArnOrThrow(resourceId);
withTrailLock(key, () -> {
CloudTrailEntry entry = store.get(key).orElseThrow(() -> new AwsException(
"ResourceNotFoundException", "Resource not found: " + resourceId, 400));
Map<String, String> remaining = entry.mutableTags();
tagKeys.forEach(remaining::remove);
store.put(key, entry.withTags(remaining));
});
}
public Map<String, String> listTags(String resourceId) {
@@ -197,6 +221,15 @@ public class CloudTrailService {
}
private CloudTrailEntry findEntryByArnOrThrow(String resourceId) {
String key = findKeyByArnOrThrow(resourceId);
return store.get(key).orElseThrow(() -> new AwsException(
"ResourceNotFoundException", "Resource not found: " + resourceId, 400));
}
/** Validates {@code resourceId} as a trail ARN and resolves it to a storage key, without
* reading the entry itself. Callers that go on to mutate the entry must re-read it inside
* {@link #withTrailLock} rather than reuse a value read here. */
private String findKeyByArnOrThrow(String resourceId) {
AwsArnUtils.Arn arn;
try {
arn = AwsArnUtils.parse(resourceId);
@@ -211,13 +244,39 @@ public class CloudTrailService {
for (String k : store.keys()) {
CloudTrailEntry entry = store.get(k).orElse(null);
if (entry != null && resourceId.equals(entry.trail().trailArn())) {
return entry;
return k;
}
}
throw new AwsException("ResourceNotFoundException",
"Resource not found: " + resourceId, 400);
}
/**
* Runs {@code action} with exclusive access to trail {@code key}, so overlapping mutations of
* the same trail (even from different actions) can't interleave and clobber one another.
* Piggybacks on {@link ConcurrentHashMap#compute}'s documented per-key atomicity as the
* mutex, rather than a plain lock-object map: returning null from the remapping function
* drops the bookkeeping entry the instant the call finishes, so {@code trailLocks} never
* accumulates one entry per trail ever mutated, unlike a map of retained lock objects would.
*/
private <T> T withTrailLock(String key, Supplier<T> action) {
Object[] box = new Object[1];
trailLocks.compute(key, (k, v) -> {
box[0] = action.get();
return null;
});
@SuppressWarnings("unchecked")
T result = (T) box[0];
return result;
}
private void withTrailLock(String key, Runnable action) {
withTrailLock(key, () -> {
action.run();
return null;
});
}
// --- Data plane: called by S3 (and other services) when an op happens ---
public void emitS3DataEvent(S3EventInput in) {