# The workstation, its own disk key, and the analysts' access. data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${local.partition}:iam::${local.account}:root"] } } } resource "aws_kms_key" "disk" { description = "Forensic workstation disks for ${var.name}" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.key.json } resource "aws_security_group" "workstation" { name_prefix = "${var.name}-workstation-" description = "No inbound. Out only to the endpoints and S3." vpc_id = aws_vpc.forensics.id egress { description = "HTTPS to the private endpoints" from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = [var.cidr] } egress { description = "HTTPS to S3 through the gateway endpoint" from_port = 443 to_port = 443 protocol = "tcp" prefix_list_ids = [aws_vpc_endpoint.s3.prefix_list_id] } } data "aws_iam_policy_document" "ec2_trust" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["ec2.amazonaws.com"] } } } resource "aws_iam_role" "workstation" { name = "${var.name}-workstation" assume_role_policy = data.aws_iam_policy_document.ec2_trust.json } resource "aws_iam_role_policy_attachment" "ssm" { role = aws_iam_role.workstation.name policy_arn = "arn:${local.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore" } resource "aws_iam_role_policy" "read_evidence" { name = "read-evidence" role = aws_iam_role.workstation.id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = ["s3:GetObject", "s3:ListBucket"] Resource = ["arn:${local.partition}:s3:::${var.evidence_bucket}", "arn:${local.partition}:s3:::${var.evidence_bucket}/*"] }] }) } resource "aws_iam_instance_profile" "workstation" { name = "${var.name}-workstation" role = aws_iam_role.workstation.name } data "aws_ssm_parameter" "al2023" { name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64" } resource "aws_instance" "workstation" { ami = data.aws_ssm_parameter.al2023.value instance_type = var.instance_type subnet_id = aws_subnet.forensics.id vpc_security_group_ids = [aws_security_group.workstation.id] iam_instance_profile = aws_iam_instance_profile.workstation.name ebs_optimized = true monitoring = true metadata_options { http_tokens = "required" http_put_response_hop_limit = 1 } root_block_device { volume_size = 500 volume_type = "gp3" encrypted = true kms_key_id = aws_kms_key.disk.arn } tags = { Name = "${var.name}-workstation", "forensics:access" = "analysts" } lifecycle { ignore_changes = [ami] } } # Analysts open sessions on this workstation only, and attach evidence # volumes to it only. data "aws_iam_policy_document" "analyst" { statement { sid = "SessionsOnTheWorkstation" actions = ["ssm:StartSession"] resources = [aws_instance.workstation.arn, "arn:${local.partition}:ssm:${local.region}::document/SSM-SessionManagerRunShell"] } statement { sid = "EndOwnSessions" actions = ["ssm:TerminateSession", "ssm:ResumeSession"] resources = ["arn:${local.partition}:ssm:*:*:session/$${aws:userid}-*"] } statement { sid = "VolumesFromEvidenceSnapshots" actions = ["ec2:CreateVolume", "ec2:CreateTags", "ec2:DescribeSnapshots", "ec2:DescribeVolumes"] resources = ["*"] } statement { sid = "AttachToTheWorkstationOnly" actions = ["ec2:AttachVolume", "ec2:DetachVolume"] resources = [aws_instance.workstation.arn, "arn:${local.partition}:ec2:${local.region}:${local.account}:volume/*"] } } resource "aws_iam_policy" "analyst" { name = "${var.name}-analyst" description = "Sessions on the forensic workstation, and evidence volumes attached to it." policy = data.aws_iam_policy_document.analyst.json } resource "aws_iam_role_policy_attachment" "analyst" { for_each = toset(var.analyst_role_arns) role = element(split("/", each.value), length(split("/", each.value)) - 1) policy_arn = aws_iam_policy.analyst.arn }