fix(cloudtrail): implement CreateTrail with tagging support and add integration tests (#3206)

Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-07 21:46:14 -04:00
1 parent 84a7ce67e4
commit 991470e7c4
8 files changed
+185 -3

No files matched your search

@@ -60,8 +60,10 @@ public class CloudTrailJsonHandler {
boolean enableLogFileValidation = req.path("EnableLogFileValidation").asBoolean(false);
boolean isOrganizationTrail = req.path("IsOrganizationTrail").asBoolean(false);
Map<String, String> tags = parseTagsList(req.path("TagsList"));
Trail trail = service.createTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicArn,
includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail);
includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail, tags);
ObjectNode resp = mapper.createObjectNode();
resp.put("Name", trail.name());
@@ -71,10 +71,31 @@ public class CloudTrailService {
String snsTopicArn, boolean includeGlobalServiceEvents,
boolean isMultiRegionTrail, boolean enableLogFileValidation,
boolean isOrganizationTrail) {
return createTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicArn,
includeGlobalServiceEvents, isMultiRegionTrail, enableLogFileValidation,
isOrganizationTrail, Map.of());
}
/**
* Creates a trail, optionally tagged from the outset. CreateTrail carries a {@code TagsList}
* on the wire, and the Terraform AWS provider always sends the resource's tags there rather
* than through a follow-up AddTags, then reads them back with ListTags on every refresh, so
* tags dropped here would surface as a perpetual diff on {@code aws_cloudtrail}.
*/
public Trail createTrail(String region, String name, String s3BucketName, String s3KeyPrefix,
String snsTopicArn, boolean includeGlobalServiceEvents,
boolean isMultiRegionTrail, boolean enableLogFileValidation,
boolean isOrganizationTrail, Map<String, String> tags) {
validateTrailName(name);
if (s3BucketName == null || s3BucketName.isEmpty()) {
throw new AwsException("S3BucketDoesNotExistException", "S3 bucket name is required.", 400);
}
Map<String, String> initialTags = tags == null ? Map.of() : tags;
if (initialTags.size() > MAX_TAGS_PER_RESOURCE) {
throw new AwsException("TagsLimitExceededException",
"Tag limit exceeded for trail " + name
+ ". Maximum allowed: " + MAX_TAGS_PER_RESOURCE + ".", 400);
}
String key = regionKey(region, name);
if (store.get(key).isPresent()) {
throw new AwsException("TrailAlreadyExistsException",
@@ -86,7 +107,7 @@ public class CloudTrailService {
name, arn, s3BucketName, s3KeyPrefix, snsTopicArn,
includeGlobalServiceEvents, isMultiRegionTrail, region,
enableLogFileValidation, false, false, isOrganizationTrail);
store.put(key, new CloudTrailEntry(trail, List.of(), false, null, null, Map.of()));
store.put(key, new CloudTrailEntry(trail, List.of(), false, null, null, initialTags));
return trail;
}