# k8s-audit-logging / aws / t1 "Standard" # # The EKS audit log kept encrypted under its own key, five detections with # alarms, and an optional feed into your SIEM. data "aws_caller_identity" "current" {} data "aws_region" "current" {} data "aws_partition" "current" {} locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "confidential" managed_by = "terraform" }, var.extra_tags) account = data.aws_caller_identity.current.account_id region = data.aws_region.current.region partition = data.aws_partition.current.partition group = "/aws/eks/${var.cluster_name}/cluster" topic_arn = var.alert_topic_arn != "" ? var.alert_topic_arn : aws_sns_topic.alerts[0].arn rc = "($.stage = \"ResponseComplete\")" detections = { pod-exec = "{ ${local.rc} && (($.objectRef.subresource = \"exec\") || ($.objectRef.subresource = \"attach\")) }" cluster-role-binding = "{ ${local.rc} && ($.objectRef.resource = \"clusterrolebindings\") && ($.verb = \"create\") }" anonymous-request = "{ ${local.rc} && ($.user.username = \"system:anonymous\") }" # People reading secrets. Controllers read them all day under system: # names, so those are left out. secret-read-by-person = "{ ${local.rc} && ($.objectRef.resource = \"secrets\") && (($.verb = \"get\") || ($.verb = \"list\")) && ($.user.username != \"system:*\") }" # Requests refused by authorisation. A burst is someone probing what a # stolen credential can do. forbidden-request = "{ ${local.rc} && ($.responseStatus.code = 403) }" } } # Audit events include request bodies, so the log gets its own key. data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${local.partition}:iam::${local.account}:root"] } } statement { sid = "CloudWatchLogsEncryptsThisGroup" actions = ["kms:Encrypt*", "kms:Decrypt*", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:Describe*"] resources = ["*"] principals { type = "Service" identifiers = ["logs.${local.region}.amazonaws.com"] } condition { test = "ArnLike" variable = "kms:EncryptionContext:aws:logs:arn" values = ["arn:${local.partition}:logs:${local.region}:${local.account}:log-group:${local.group}"] } } # The alert topic is encrypted with this key too. CloudWatch alarms can only # publish to an encrypted topic when the key policy names them, and the AWS # managed SNS key cannot be changed to do that, so alarms on a topic using # it fail without an error anyone sees. statement { sid = "CloudWatchAlarmsPublishToTheTopic" actions = ["kms:Decrypt", "kms:GenerateDataKey*"] resources = ["*"] principals { type = "Service" identifiers = ["cloudwatch.amazonaws.com"] } } } resource "aws_kms_key" "audit" { description = "EKS audit log for ${var.cluster_name}" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.key.json } resource "aws_cloudwatch_log_group" "audit" { name = local.group retention_in_days = var.retention_days kms_key_id = aws_kms_key.audit.arn } resource "aws_sns_topic" "alerts" { count = var.alert_topic_arn == "" ? 1 : 0 name = "eks-audit-${var.cluster_name}" kms_master_key_id = aws_kms_key.audit.arn } resource "aws_sns_topic_subscription" "email" { for_each = var.alert_topic_arn == "" ? toset(var.alert_emails) : toset([]) topic_arn = aws_sns_topic.alerts[0].arn protocol = "email" endpoint = each.value } resource "aws_cloudwatch_log_metric_filter" "detection" { for_each = local.detections name = each.key log_group_name = aws_cloudwatch_log_group.audit.name pattern = each.value metric_transformation { name = each.key namespace = "EKS/Audit/${var.cluster_name}" value = "1" default_value = "0" } } resource "aws_cloudwatch_metric_alarm" "detection" { for_each = local.detections alarm_name = "eks-${var.cluster_name}-${each.key}" alarm_description = "Seen in the ${var.cluster_name} audit log. Search ${local.group} for the event to see who and which object." namespace = "EKS/Audit/${var.cluster_name}" metric_name = each.key statistic = "Sum" period = 300 evaluation_periods = 1 # A single forbidden request is ordinary. Ten in five minutes is not. threshold = each.key == "forbidden-request" ? 10 : 1 comparison_operator = "GreaterThanOrEqualToThreshold" treat_missing_data = "notBreaching" alarm_actions = [local.topic_arn] depends_on = [aws_cloudwatch_log_metric_filter.detection] } # Optional: every audit event also goes to the SIEM. data "aws_iam_policy_document" "logs_trust" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["logs.${local.region}.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } } } resource "aws_iam_role" "to_siem" { count = var.siem_firehose_arn == "" ? 0 : 1 name = "eks-audit-${var.cluster_name}-to-siem" assume_role_policy = data.aws_iam_policy_document.logs_trust.json } resource "aws_iam_role_policy" "to_siem" { count = var.siem_firehose_arn == "" ? 0 : 1 name = "put-to-firehose" role = aws_iam_role.to_siem[0].id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow", Action = ["firehose:PutRecord", "firehose:PutRecordBatch"], Resource = var.siem_firehose_arn }] }) } resource "aws_cloudwatch_log_subscription_filter" "to_siem" { count = var.siem_firehose_arn == "" ? 0 : 1 name = "to-siem" log_group_name = aws_cloudwatch_log_group.audit.name filter_pattern = "" destination_arn = var.siem_firehose_arn role_arn = aws_iam_role.to_siem[0].arn }