Add CloudTrail delivery to CloudWatch Logs
This commit is contained in:
@@ -63,6 +63,25 @@ run "apply_group1_osaka" {
|
||||
error_message = "The Osaka test CloudTrail trail must be single-Region."
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
length(aws_cloudwatch_log_group.cloudtrail_protected) == 0 &&
|
||||
length(aws_cloudwatch_log_group.cloudtrail_unprotected) == 1 &&
|
||||
aws_cloudwatch_log_group.cloudtrail_unprotected[0].retention_in_days == 30
|
||||
)
|
||||
error_message = "The Osaka test must create one unprotected CloudTrail log group with 30-day retention."
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
length(aws_iam_role.cloudtrail_cloudwatch_protected) == 0 &&
|
||||
length(aws_iam_role.cloudtrail_cloudwatch_unprotected) == 1 &&
|
||||
length(aws_iam_role_policy.cloudtrail_cloudwatch_protected) == 0 &&
|
||||
length(aws_iam_role_policy.cloudtrail_cloudwatch_unprotected) == 1
|
||||
)
|
||||
error_message = "The Osaka test must use unprotected CloudTrail delivery IAM resources."
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = aws_kms_key.vault_unprotected[0].deletion_window_in_days == 7
|
||||
error_message = "The Osaka test KMS key must use the minimum seven-day deletion window."
|
||||
|
||||
@@ -6,6 +6,8 @@ resource "aws_cloudtrail" "management_events" {
|
||||
is_organization_trail = false
|
||||
enable_log_file_validation = true
|
||||
enable_logging = true
|
||||
cloud_watch_logs_group_arn = "${local.cloudtrail_log_group_arn}:*"
|
||||
cloud_watch_logs_role_arn = local.cloudtrail_cloudwatch_role_arn
|
||||
|
||||
advanced_event_selector {
|
||||
name = "Management events selector"
|
||||
@@ -16,5 +18,9 @@ resource "aws_cloudtrail" "management_events" {
|
||||
}
|
||||
}
|
||||
|
||||
depends_on = [aws_s3_bucket_policy.this["cloudtrail"]]
|
||||
depends_on = [
|
||||
aws_s3_bucket_policy.this["cloudtrail"],
|
||||
aws_iam_role_policy.cloudtrail_cloudwatch_protected,
|
||||
aws_iam_role_policy.cloudtrail_cloudwatch_unprotected,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
resource "aws_cloudwatch_log_group" "cloudtrail_protected" {
|
||||
count = var.protect_from_destroy ? 1 : 0
|
||||
|
||||
name = local.cloudtrail_log_group_name
|
||||
retention_in_days = var.cloudtrail_cloudwatch_logs.retention_in_days
|
||||
log_group_class = "STANDARD"
|
||||
|
||||
lifecycle {
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_cloudwatch_log_group" "cloudtrail_unprotected" {
|
||||
count = var.protect_from_destroy ? 0 : 1
|
||||
|
||||
name = local.cloudtrail_log_group_name
|
||||
retention_in_days = var.cloudtrail_cloudwatch_logs.retention_in_days
|
||||
log_group_class = "STANDARD"
|
||||
}
|
||||
|
||||
resource "aws_iam_role" "cloudtrail_cloudwatch_protected" {
|
||||
count = var.protect_from_destroy ? 1 : 0
|
||||
|
||||
name = local.cloudtrail_cloudwatch_role_config_name
|
||||
path = "/service-role/"
|
||||
max_session_duration = 3600
|
||||
assume_role_policy = local.cloudtrail_cloudwatch_assume_role_policy
|
||||
|
||||
lifecycle {
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_iam_role" "cloudtrail_cloudwatch_unprotected" {
|
||||
count = var.protect_from_destroy ? 0 : 1
|
||||
|
||||
name = local.cloudtrail_cloudwatch_role_config_name
|
||||
path = "/service-role/"
|
||||
max_session_duration = 3600
|
||||
assume_role_policy = local.cloudtrail_cloudwatch_assume_role_policy
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy" "cloudtrail_cloudwatch_protected" {
|
||||
count = var.protect_from_destroy ? 1 : 0
|
||||
|
||||
name = local.cloudtrail_cloudwatch_policy_name
|
||||
role = local.cloudtrail_cloudwatch_role_name
|
||||
policy = local.cloudtrail_cloudwatch_role_policy
|
||||
|
||||
lifecycle {
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy" "cloudtrail_cloudwatch_unprotected" {
|
||||
count = var.protect_from_destroy ? 0 : 1
|
||||
|
||||
name = local.cloudtrail_cloudwatch_policy_name
|
||||
role = local.cloudtrail_cloudwatch_role_name
|
||||
policy = local.cloudtrail_cloudwatch_role_policy
|
||||
}
|
||||
@@ -21,6 +21,52 @@ locals {
|
||||
|
||||
trail_arn = "arn:aws:cloudtrail:${var.region}:${var.account_id}:trail/${var.trail_name}"
|
||||
|
||||
cloudtrail_log_group_name = coalesce(
|
||||
var.cloudtrail_cloudwatch_logs.log_group_name,
|
||||
"${var.name_prefix}-cloudtrail",
|
||||
)
|
||||
cloudtrail_cloudwatch_role_config_name = coalesce(
|
||||
var.cloudtrail_cloudwatch_logs.role_name,
|
||||
"${var.name_prefix}-cloudtrail-cloudwatch",
|
||||
)
|
||||
cloudtrail_cloudwatch_policy_name = coalesce(
|
||||
var.cloudtrail_cloudwatch_logs.policy_name,
|
||||
"${var.name_prefix}-cloudtrail-cloudwatch",
|
||||
)
|
||||
|
||||
cloudtrail_log_group_arn = var.protect_from_destroy ? aws_cloudwatch_log_group.cloudtrail_protected[0].arn : aws_cloudwatch_log_group.cloudtrail_unprotected[0].arn
|
||||
cloudtrail_cloudwatch_role_arn = var.protect_from_destroy ? aws_iam_role.cloudtrail_cloudwatch_protected[0].arn : aws_iam_role.cloudtrail_cloudwatch_unprotected[0].arn
|
||||
cloudtrail_cloudwatch_role_name = var.protect_from_destroy ? aws_iam_role.cloudtrail_cloudwatch_protected[0].name : aws_iam_role.cloudtrail_cloudwatch_unprotected[0].name
|
||||
|
||||
cloudtrail_cloudwatch_assume_role_policy = jsonencode({
|
||||
Version = "2012-10-17"
|
||||
Statement = [{
|
||||
Effect = "Allow"
|
||||
Principal = {
|
||||
Service = "cloudtrail.amazonaws.com"
|
||||
}
|
||||
Action = "sts:AssumeRole"
|
||||
}]
|
||||
})
|
||||
|
||||
cloudtrail_cloudwatch_role_policy = jsonencode({
|
||||
Version = "2012-10-17"
|
||||
Statement = [
|
||||
{
|
||||
Sid = "AWSCloudTrailCreateLogStream20141101"
|
||||
Effect = "Allow"
|
||||
Action = "logs:CreateLogStream"
|
||||
Resource = "${local.cloudtrail_log_group_arn}:log-stream:${var.account_id}_CloudTrail_*"
|
||||
},
|
||||
{
|
||||
Sid = "AWSCloudTrailPutLogEvents20141101"
|
||||
Effect = "Allow"
|
||||
Action = "logs:PutLogEvents"
|
||||
Resource = "${local.cloudtrail_log_group_arn}:log-stream:${var.account_id}_CloudTrail_*"
|
||||
},
|
||||
]
|
||||
})
|
||||
|
||||
kms_policy = jsonencode({
|
||||
Version = "2012-10-17"
|
||||
Id = "key-consolepolicy-3"
|
||||
|
||||
@@ -60,6 +60,25 @@ run "plan_group1_osaka" {
|
||||
error_message = "The Osaka test CloudTrail trail must be single-Region."
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
length(aws_cloudwatch_log_group.cloudtrail_protected) == 0 &&
|
||||
length(aws_cloudwatch_log_group.cloudtrail_unprotected) == 1 &&
|
||||
aws_cloudwatch_log_group.cloudtrail_unprotected[0].retention_in_days == 30
|
||||
)
|
||||
error_message = "The Osaka test must create one unprotected CloudTrail log group with 30-day retention."
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = (
|
||||
length(aws_iam_role.cloudtrail_cloudwatch_protected) == 0 &&
|
||||
length(aws_iam_role.cloudtrail_cloudwatch_unprotected) == 1 &&
|
||||
length(aws_iam_role_policy.cloudtrail_cloudwatch_protected) == 0 &&
|
||||
length(aws_iam_role_policy.cloudtrail_cloudwatch_unprotected) == 1
|
||||
)
|
||||
error_message = "The Osaka test must use unprotected CloudTrail delivery IAM resources."
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = aws_kms_key.vault_unprotected[0].deletion_window_in_days == 7
|
||||
error_message = "The Osaka test KMS key must use the minimum seven-day deletion window."
|
||||
|
||||
@@ -62,6 +62,17 @@ variable "cloudtrail_is_multi_region" {
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "cloudtrail_cloudwatch_logs" {
|
||||
description = "CloudWatch Logs delivery settings for the CloudTrail trail. Omitted names are derived from name_prefix for isolated tests."
|
||||
type = object({
|
||||
log_group_name = optional(string)
|
||||
retention_in_days = optional(number, 30)
|
||||
role_name = optional(string)
|
||||
policy_name = optional(string)
|
||||
})
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "kms_alias_name" {
|
||||
description = "Alias of the customer managed Vault key."
|
||||
type = string
|
||||
|
||||
Reference in New Issue
Block a user