mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(cloudtrail): keep a trail's KMS key and CloudWatch Logs delivery (#6989)
This commit is contained in:
1 parent
02852f0e67
commit
5ad2f64840
7 files changed
+269
-6
No files matched your search
@@ -95,7 +95,8 @@ public record CloudTrailEntry(
|
||||
trail.name(), trail.trailArn(), trail.s3BucketName(), trail.s3KeyPrefix(),
|
||||
trail.snsTopicArn(), trail.includeGlobalServiceEvents(), trail.isMultiRegionTrail(),
|
||||
trail.homeRegion(), trail.logFileValidationEnabled(), hasCustomSelectors,
|
||||
trail.hasInsightSelectors(), trail.isOrganizationTrail());
|
||||
trail.hasInsightSelectors(), trail.isOrganizationTrail(), trail.kmsKeyId(),
|
||||
trail.cloudWatchLogsLogGroupArn(), trail.cloudWatchLogsRoleArn());
|
||||
}
|
||||
|
||||
/** Returns a mutable copy suitable for merging in new tags. */
|
||||
|
||||
+23
-2
@@ -65,7 +65,9 @@ public class CloudTrailJsonHandler {
|
||||
Map<String, String> tags = parseTagsList(req.path("TagsList"));
|
||||
|
||||
Trail trail = service.createTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicArn,
|
||||
includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail, tags);
|
||||
includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail, tags,
|
||||
textOrNull(req, "KmsKeyId"), textOrNull(req, "CloudWatchLogsLogGroupArn"),
|
||||
textOrNull(req, "CloudWatchLogsRoleArn"));
|
||||
|
||||
ObjectNode resp = mapper.createObjectNode();
|
||||
resp.put("Name", trail.name());
|
||||
@@ -80,6 +82,9 @@ public class CloudTrailJsonHandler {
|
||||
resp.put("TrailARN", trail.trailArn());
|
||||
resp.put("LogFileValidationEnabled", trail.logFileValidationEnabled());
|
||||
resp.put("IsOrganizationTrail", trail.isOrganizationTrail());
|
||||
putIfSet(resp, "KmsKeyId", trail.kmsKeyId());
|
||||
putIfSet(resp, "CloudWatchLogsLogGroupArn", trail.cloudWatchLogsLogGroupArn());
|
||||
putIfSet(resp, "CloudWatchLogsRoleArn", trail.cloudWatchLogsRoleArn());
|
||||
return Response.ok(resp).build();
|
||||
}
|
||||
|
||||
@@ -89,6 +94,17 @@ public class CloudTrailJsonHandler {
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
/** The member's text when present (an empty string included: UpdateTrail uses it to clear a setting). */
|
||||
private static String textOrNull(JsonNode req, String field) {
|
||||
return req.has(field) && !req.get(field).isNull() ? req.get(field).asText() : null;
|
||||
}
|
||||
|
||||
private static void putIfSet(ObjectNode node, String field, String value) {
|
||||
if (value != null) {
|
||||
node.put(field, value);
|
||||
}
|
||||
}
|
||||
|
||||
private Response updateTrail(JsonNode req, String region) {
|
||||
String name = req.path("Name").asText(null);
|
||||
String s3BucketName = req.has("S3BucketName") ? req.path("S3BucketName").asText(null) : null;
|
||||
@@ -105,7 +121,9 @@ public class CloudTrailJsonHandler {
|
||||
? req.path("IsOrganizationTrail").asBoolean() : null;
|
||||
|
||||
Trail trail = service.updateTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicName,
|
||||
includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail);
|
||||
includeGlobal, isMultiRegion, enableLogFileValidation, isOrganizationTrail,
|
||||
textOrNull(req, "KmsKeyId"), textOrNull(req, "CloudWatchLogsLogGroupArn"),
|
||||
textOrNull(req, "CloudWatchLogsRoleArn"));
|
||||
|
||||
ObjectNode resp = mapper.createObjectNode();
|
||||
resp.put("Name", trail.name());
|
||||
@@ -120,6 +138,9 @@ public class CloudTrailJsonHandler {
|
||||
resp.put("TrailARN", trail.trailArn());
|
||||
resp.put("LogFileValidationEnabled", trail.logFileValidationEnabled());
|
||||
resp.put("IsOrganizationTrail", trail.isOrganizationTrail());
|
||||
putIfSet(resp, "KmsKeyId", trail.kmsKeyId());
|
||||
putIfSet(resp, "CloudWatchLogsLogGroupArn", trail.cloudWatchLogsLogGroupArn());
|
||||
putIfSet(resp, "CloudWatchLogsRoleArn", trail.cloudWatchLogsRoleArn());
|
||||
return Response.ok(resp).build();
|
||||
}
|
||||
|
||||
|
||||
+56
-2
@@ -98,7 +98,23 @@ public class CloudTrailService {
|
||||
String snsTopicArn, boolean includeGlobalServiceEvents,
|
||||
boolean isMultiRegionTrail, boolean enableLogFileValidation,
|
||||
boolean isOrganizationTrail, Map<String, String> tags) {
|
||||
return createTrail(region, name, s3BucketName, s3KeyPrefix, snsTopicArn, includeGlobalServiceEvents,
|
||||
isMultiRegionTrail, enableLogFileValidation, isOrganizationTrail, tags, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param kmsKeyId the key the trail's log files are encrypted with, recorded and
|
||||
* reported back; Floci writes the files unencrypted
|
||||
* @param cloudWatchLogsLogGroupArn the log group events are also delivered to, recorded only
|
||||
* @param cloudWatchLogsRoleArn the role CloudTrail assumes for that delivery
|
||||
*/
|
||||
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, String kmsKeyId,
|
||||
String cloudWatchLogsLogGroupArn, String cloudWatchLogsRoleArn) {
|
||||
validateTrailName(name);
|
||||
validateCloudWatchLogs(cloudWatchLogsLogGroupArn, cloudWatchLogsRoleArn);
|
||||
if (s3BucketName == null || s3BucketName.isEmpty()) {
|
||||
throw new AwsException("S3BucketDoesNotExistException", "S3 bucket name is required.", 400);
|
||||
}
|
||||
@@ -118,12 +134,31 @@ public class CloudTrailService {
|
||||
Trail trail = new Trail(
|
||||
name, arn, s3BucketName, s3KeyPrefix, snsTopicArn,
|
||||
includeGlobalServiceEvents, isMultiRegionTrail, region,
|
||||
enableLogFileValidation, false, false, isOrganizationTrail);
|
||||
enableLogFileValidation, false, false, isOrganizationTrail,
|
||||
blankToNull(kmsKeyId), blankToNull(cloudWatchLogsLogGroupArn), blankToNull(cloudWatchLogsRoleArn));
|
||||
store.put(key, new CloudTrailEntry(trail, List.of(), List.of(), false, null, null,
|
||||
initialTags, null, null));
|
||||
return trail;
|
||||
}
|
||||
|
||||
/** CloudTrail needs both halves of CloudWatch Logs delivery, or neither. */
|
||||
private static void validateCloudWatchLogs(String logGroupArn, String roleArn) {
|
||||
boolean group = logGroupArn != null && !logGroupArn.isBlank();
|
||||
boolean role = roleArn != null && !roleArn.isBlank();
|
||||
if (group && !role) {
|
||||
throw new AwsException("InvalidCloudWatchLogsRoleArnException",
|
||||
"You must specify a role ARN when you specify a CloudWatch Logs log group.", 400);
|
||||
}
|
||||
if (role && !group) {
|
||||
throw new AwsException("InvalidCloudWatchLogsLogGroupArnException",
|
||||
"You must specify a log group ARN when you specify a CloudWatch Logs role.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String blankToNull(String value) {
|
||||
return value == null || value.isBlank() ? null : value;
|
||||
}
|
||||
|
||||
public void deleteTrail(String region, String trailNameOrArn) {
|
||||
Trail trail = findTrailOrThrow(region, trailNameOrArn);
|
||||
String key = regionKey(trail.homeRegion(), trail.name());
|
||||
@@ -136,6 +171,20 @@ public class CloudTrailService {
|
||||
String s3BucketName, String s3KeyPrefix, String snsTopicArn,
|
||||
Boolean includeGlobalServiceEvents, Boolean isMultiRegionTrail,
|
||||
Boolean enableLogFileValidation, Boolean isOrganizationTrail) {
|
||||
return updateTrail(region, trailNameOrArn, s3BucketName, s3KeyPrefix, snsTopicArn,
|
||||
includeGlobalServiceEvents, isMultiRegionTrail, enableLogFileValidation, isOrganizationTrail,
|
||||
null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A null KMS or CloudWatch Logs argument leaves the setting as it is; an empty one removes it,
|
||||
* as UpdateTrail does on AWS.
|
||||
*/
|
||||
public Trail updateTrail(String region, String trailNameOrArn,
|
||||
String s3BucketName, String s3KeyPrefix, String snsTopicArn,
|
||||
Boolean includeGlobalServiceEvents, Boolean isMultiRegionTrail,
|
||||
Boolean enableLogFileValidation, Boolean isOrganizationTrail,
|
||||
String kmsKeyId, String cloudWatchLogsLogGroupArn, String cloudWatchLogsRoleArn) {
|
||||
Trail resolved = findTrailOrThrow(region, trailNameOrArn);
|
||||
String key = regionKey(resolved.homeRegion(), resolved.name());
|
||||
return withTrailLock(key, () -> {
|
||||
@@ -154,7 +203,12 @@ public class CloudTrailService {
|
||||
enableLogFileValidation != null ? enableLogFileValidation : existing.logFileValidationEnabled(),
|
||||
existing.hasCustomEventSelectors(),
|
||||
existing.hasInsightSelectors(),
|
||||
isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail());
|
||||
isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail(),
|
||||
kmsKeyId != null ? blankToNull(kmsKeyId) : existing.kmsKeyId(),
|
||||
cloudWatchLogsLogGroupArn != null
|
||||
? blankToNull(cloudWatchLogsLogGroupArn) : existing.cloudWatchLogsLogGroupArn(),
|
||||
cloudWatchLogsRoleArn != null ? blankToNull(cloudWatchLogsRoleArn) : existing.cloudWatchLogsRoleArn());
|
||||
validateCloudWatchLogs(updated.cloudWatchLogsLogGroupArn(), updated.cloudWatchLogsRoleArn());
|
||||
store.put(key, entry.withTrail(updated));
|
||||
return updated;
|
||||
});
|
||||
|
||||
@@ -20,5 +20,18 @@ public record Trail(
|
||||
@JsonProperty("LogFileValidationEnabled") boolean logFileValidationEnabled,
|
||||
@JsonProperty("HasCustomEventSelectors") boolean hasCustomEventSelectors,
|
||||
@JsonProperty("HasInsightSelectors") boolean hasInsightSelectors,
|
||||
@JsonProperty("IsOrganizationTrail") boolean isOrganizationTrail) {
|
||||
@JsonProperty("IsOrganizationTrail") boolean isOrganizationTrail,
|
||||
@JsonProperty("KmsKeyId") String kmsKeyId,
|
||||
@JsonProperty("CloudWatchLogsLogGroupArn") String cloudWatchLogsLogGroupArn,
|
||||
@JsonProperty("CloudWatchLogsRoleArn") String cloudWatchLogsRoleArn) {
|
||||
|
||||
/** A trail without encryption or CloudWatch Logs delivery. */
|
||||
public Trail(String name, String trailArn, String s3BucketName, String s3KeyPrefix, String snsTopicArn,
|
||||
boolean includeGlobalServiceEvents, boolean isMultiRegionTrail, String homeRegion,
|
||||
boolean logFileValidationEnabled, boolean hasCustomEventSelectors, boolean hasInsightSelectors,
|
||||
boolean isOrganizationTrail) {
|
||||
this(name, trailArn, s3BucketName, s3KeyPrefix, snsTopicArn, includeGlobalServiceEvents, isMultiRegionTrail,
|
||||
homeRegion, logFileValidationEnabled, hasCustomEventSelectors, hasInsightSelectors,
|
||||
isOrganizationTrail, null, null, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user