# container-evidence / aws / t1 "Standard" # # An evidence bucket under your own key, locked for the period you choose, a # responder role with short sessions that can protect and snapshot nodes and # add evidence but not read, change, or delete it, access to the cluster for # that role, and capture.sh, which runs every step in order and records each # one. # # Kubernetes rights come from the incident-responder cluster role in the # Kubernetes evidence template. The access entry puts this role in its group. data "aws_caller_identity" "current" {} data "aws_region" "current" {} data "aws_partition" "current" {} data "aws_eks_cluster" "this" { name = var.cluster } # Keeps names unique, so several clusters, or the quick start, can share an # account. resource "random_id" "suffix" { byte_length = 3 } locals { name = "container-evidence" bucket = "${local.name}-${random_id.suffix.hex}-${local.account_id}" account_id = data.aws_caller_identity.current.account_id region = data.aws_region.current.region partition = data.aws_partition.current.partition tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) } # --------------------------------------------------------------------------- # The key and the bucket # --------------------------------------------------------------------------- data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${local.partition}:iam::${local.account_id}:root"] } } # Responders can encrypt new evidence through S3, and only for this bucket. # Decrypt is here because S3 needs it to finish a multipart upload; without # s3:GetObject it cannot be used to read evidence. statement { sid = "RespondersWriteThroughS3" actions = ["kms:GenerateDataKey", "kms:Encrypt", "kms:Decrypt"] resources = ["*"] principals { type = "AWS" identifiers = [aws_iam_role.responder.arn] } condition { test = "StringEquals" variable = "kms:ViaService" values = ["s3.${local.region}.amazonaws.com"] } condition { test = "StringLike" variable = "kms:EncryptionContext:aws:s3:arn" # With an S3 Bucket Key the context is the bucket's ARN; without one it # is the object's. values = ["arn:${local.partition}:s3:::${local.bucket}", "arn:${local.partition}:s3:::${local.bucket}/*"] } } } resource "aws_kms_key" "evidence" { description = "Container evidence for ${var.cluster}" deletion_window_in_days = 30 enable_key_rotation = true policy = data.aws_iam_policy_document.key.json } resource "aws_kms_alias" "evidence" { name = "alias/${local.name}-${random_id.suffix.hex}" target_key_id = aws_kms_key.evidence.key_id } resource "aws_s3_bucket" "evidence" { bucket = local.bucket object_lock_enabled = true # Losing the bucket loses the case. lifecycle { prevent_destroy = true } } resource "aws_s3_bucket_versioning" "evidence" { bucket = aws_s3_bucket.evidence.id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_object_lock_configuration" "evidence" { bucket = aws_s3_bucket.evidence.id rule { default_retention { mode = var.lock_mode days = var.retention_days } } depends_on = [aws_s3_bucket_versioning.evidence] } resource "aws_s3_bucket_server_side_encryption_configuration" "evidence" { bucket = aws_s3_bucket.evidence.id rule { apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" kms_master_key_id = aws_kms_key.evidence.arn } bucket_key_enabled = true } } resource "aws_s3_bucket_public_access_block" "evidence" { bucket = aws_s3_bucket.evidence.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_s3_bucket_ownership_controls" "evidence" { bucket = aws_s3_bucket.evidence.id rule { object_ownership = "BucketOwnerEnforced" } } data "aws_iam_policy_document" "bucket" { statement { sid = "DenyUnencryptedTransport" effect = "Deny" actions = ["s3:*"] resources = [aws_s3_bucket.evidence.arn, "${aws_s3_bucket.evidence.arn}/*"] principals { type = "*" identifiers = ["*"] } condition { test = "Bool" variable = "aws:SecureTransport" values = ["false"] } } } resource "aws_s3_bucket_policy" "evidence" { bucket = aws_s3_bucket.evidence.id policy = data.aws_iam_policy_document.bucket.json depends_on = [aws_s3_bucket_public_access_block.evidence] } # --------------------------------------------------------------------------- # The responder role # --------------------------------------------------------------------------- data "aws_iam_policy_document" "trust" { statement { actions = ["sts:AssumeRole"] principals { type = "AWS" identifiers = var.responders } } } resource "aws_iam_role" "responder" { name = "${local.name}-responder-${random_id.suffix.hex}" assume_role_policy = data.aws_iam_policy_document.trust.json max_session_duration = var.session_hours * 3600 } # Protect, snapshot, and record. Nothing here can stop or terminate an # instance, read evidence back, or delete it. data "aws_iam_policy_document" "responder" { statement { sid = "FindTheCluster" actions = ["eks:DescribeCluster"] resources = [data.aws_eks_cluster.this.arn] } statement { sid = "FindTheNode" actions = [ "ec2:DescribeInstances", "ec2:DescribeVolumes", "ec2:DescribeSnapshots", "autoscaling:DescribeAutoScalingInstances", ] resources = ["*"] } statement { sid = "KeepTheNode" actions = ["ec2:ModifyInstanceAttribute"] resources = ["arn:${local.partition}:ec2:${local.region}:${local.account_id}:instance/*"] } statement { sid = "KeepTheNodeInItsGroup" actions = ["autoscaling:SetInstanceProtection"] resources = ["arn:${local.partition}:autoscaling:${local.region}:${local.account_id}:autoScalingGroup:*:autoScalingGroupName/*"] } statement { sid = "SnapshotTheNode" actions = ["ec2:CreateSnapshots", "ec2:CreateSnapshot"] resources = [ "arn:${local.partition}:ec2:${local.region}:${local.account_id}:instance/*", "arn:${local.partition}:ec2:${local.region}:${local.account_id}:volume/*", "arn:${local.partition}:ec2:${local.region}::snapshot/*", ] } statement { sid = "TagOnlyNewSnapshots" actions = ["ec2:CreateTags"] resources = ["arn:${local.partition}:ec2:${local.region}::snapshot/*"] condition { test = "StringEquals" variable = "ec2:CreateAction" values = ["CreateSnapshots", "CreateSnapshot"] } } statement { sid = "StoreEvidence" actions = ["s3:PutObject"] resources = ["${aws_s3_bucket.evidence.arn}/*"] } statement { sid = "EncryptEvidence" actions = ["kms:GenerateDataKey", "kms:Encrypt", "kms:Decrypt"] resources = [aws_kms_key.evidence.arn] condition { test = "StringEquals" variable = "kms:ViaService" values = ["s3.${local.region}.amazonaws.com"] } } } resource "aws_iam_role_policy" "responder" { name = "container-evidence" role = aws_iam_role.responder.id policy = data.aws_iam_policy_document.responder.json } # The role joins the incident-responders group inside the cluster, which the # incident-responder cluster role is bound to. resource "aws_eks_access_entry" "responder" { cluster_name = var.cluster principal_arn = aws_iam_role.responder.arn kubernetes_groups = ["incident-responders"] type = "STANDARD" }