# Object-level reads from the buckets you name, and an alarm when one # principal reads more of them in five minutes than bulk_read_threshold. # # Management trails do not record GetObject. Recording it for every bucket # costs more than most teams will pay, so this trail records reads from the # named buckets only, and records nothing else, so management events are not # paid for twice. locals { read_trail_arn = "arn:${local.partition}:cloudtrail:${local.region}:${local.account}:trail/${var.name}-reads" read_group = "/aws/cloudtrail/${var.name}-reads" } resource "aws_s3_bucket" "reads" { count = local.reads_on ? 1 : 0 bucket = "${var.name}-reads-${local.account}" force_destroy = false } resource "aws_s3_bucket_versioning" "reads" { count = local.reads_on ? 1 : 0 bucket = aws_s3_bucket.reads[0].id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_public_access_block" "reads" { count = local.reads_on ? 1 : 0 bucket = aws_s3_bucket.reads[0].id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_s3_bucket_server_side_encryption_configuration" "reads" { count = local.reads_on ? 1 : 0 bucket = aws_s3_bucket.reads[0].id rule { apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" kms_master_key_id = aws_kms_key.insider.arn } bucket_key_enabled = true } } resource "aws_s3_bucket_lifecycle_configuration" "reads" { count = local.reads_on ? 1 : 0 bucket = aws_s3_bucket.reads[0].id depends_on = [aws_s3_bucket_versioning.reads] rule { id = "expire-read-records" status = "Enabled" filter {} expiration { days = var.read_log_retention_days } noncurrent_version_expiration { noncurrent_days = 7 } abort_incomplete_multipart_upload { days_after_initiation = 7 } } } data "aws_iam_policy_document" "reads_bucket" { count = local.reads_on ? 1 : 0 statement { sid = "AWSCloudTrailAclCheck" actions = ["s3:GetBucketAcl"] resources = [aws_s3_bucket.reads[0].arn] principals { type = "Service" identifiers = ["cloudtrail.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceArn" values = [local.read_trail_arn] } } statement { sid = "AWSCloudTrailWrite" actions = ["s3:PutObject"] resources = ["${aws_s3_bucket.reads[0].arn}/AWSLogs/${local.account}/*"] principals { type = "Service" identifiers = ["cloudtrail.amazonaws.com"] } condition { test = "StringEquals" variable = "s3:x-amz-acl" values = ["bucket-owner-full-control"] } condition { test = "StringEquals" variable = "aws:SourceArn" values = [local.read_trail_arn] } } statement { sid = "DenyUnencryptedTransport" effect = "Deny" actions = ["s3:*"] principals { type = "AWS" identifiers = ["*"] } resources = [aws_s3_bucket.reads[0].arn, "${aws_s3_bucket.reads[0].arn}/*"] condition { test = "Bool" variable = "aws:SecureTransport" values = ["false"] } } } resource "aws_s3_bucket_policy" "reads" { count = local.reads_on ? 1 : 0 bucket = aws_s3_bucket.reads[0].id policy = data.aws_iam_policy_document.reads_bucket[0].json depends_on = [aws_s3_bucket_public_access_block.reads] } resource "aws_cloudwatch_log_group" "reads" { count = local.reads_on ? 1 : 0 name = local.read_group retention_in_days = var.read_log_retention_days kms_key_id = aws_kms_key.insider.arn } data "aws_iam_policy_document" "trail_assume" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["cloudtrail.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceArn" values = [local.read_trail_arn] } } } resource "aws_iam_role" "reads_delivery" { count = local.reads_on ? 1 : 0 name = "${var.name}-reads-delivery" assume_role_policy = data.aws_iam_policy_document.trail_assume.json } resource "aws_iam_role_policy" "reads_delivery" { count = local.reads_on ? 1 : 0 name = "delivery" role = aws_iam_role.reads_delivery[0].id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = ["logs:CreateLogStream", "logs:PutLogEvents"] Resource = "${aws_cloudwatch_log_group.reads[0].arn}:*" }] }) } # A new IAM role can take a few seconds to be usable. If the first apply stops # here with InvalidCloudWatchLogsRoleArnException, apply again. resource "aws_cloudtrail" "reads" { count = local.reads_on ? 1 : 0 name = "${var.name}-reads" s3_bucket_name = aws_s3_bucket.reads[0].id kms_key_id = aws_kms_key.insider.arn is_multi_region_trail = true include_global_service_events = false enable_log_file_validation = true cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.reads[0].arn}:*" cloud_watch_logs_role_arn = aws_iam_role.reads_delivery[0].arn # Reads of objects in the named buckets, and nothing else. advanced_event_selector { name = "Object reads from the sensitive buckets" field_selector { field = "eventCategory" equals = ["Data"] } field_selector { field = "resources.type" equals = ["AWS::S3::Object"] } field_selector { field = "eventName" equals = ["GetObject"] } field_selector { field = "resources.ARN" starts_with = [for b in var.sensitive_bucket_arns : "${b}/"] } } depends_on = [aws_s3_bucket_policy.reads, aws_iam_role_policy.reads_delivery] } # Contributor Insights ranks principals by how many objects each read. The # alarm reads the top contributor's count, so it fires on one person reading # a lot, and ten people each reading a little does not add up to an alarm. resource "aws_cloudwatch_contributor_insight_rule" "bulk_readers" { count = local.reads_on ? 1 : 0 rule_name = "${var.name}-bulk-readers" rule_state = "ENABLED" rule_definition = jsonencode({ Schema = { Name = "CloudWatchLogRule", Version = 1 } LogGroupNames = [aws_cloudwatch_log_group.reads[0].name] LogFormat = "JSON" Contribution = { Keys = ["$.userIdentity.arn"] Filters = [{ Match = "$.eventName", In = ["GetObject"] }] } AggregateOn = "Count" }) } resource "aws_cloudwatch_metric_alarm" "bulk_read" { count = local.reads_on ? 1 : 0 alarm_name = "${var.name}-bulk-read" alarm_description = "One principal read more than ${var.bulk_read_threshold} objects from the sensitive buckets in five minutes. Open the ${var.name}-bulk-readers rule in Contributor Insights to see who." evaluation_periods = 1 threshold = var.bulk_read_threshold comparison_operator = "GreaterThanThreshold" treat_missing_data = "notBreaching" alarm_actions = [local.topic_arn] metric_query { id = "top" expression = "INSIGHT_RULE_METRIC(\"${aws_cloudwatch_contributor_insight_rule.bulk_readers[0].rule_name}\", \"MaxContributorValue\")" label = "Objects read by the busiest principal" period = 300 return_data = true } }