# container-evidence / gcp / t0 "Quick start"
#
# What a responder needs to preserve a compromised container on GKE: a bucket
# for the evidence that nobody can delete from for a day, the right to
# snapshot node disks and nothing more on the Compute Engine side, and the
# commands to run, in order, printed with your names filled in.
#
# Kubernetes rights come from the incident-responder role in the Kubernetes
# evidence template (outcomes/evidence-capture/kubernetes). Apply it to the
# cluster, and bind it to the same Google group through Google Groups for
# RBAC: the binding's subject is the group's email address, and the group must
# be a member of gke-security-groups@<your domain>.
#
# Change the four values below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#   terraform output capture
#
# Verify: run the printed commands against a test pod. The pod's record, logs,
# and hashes land in the bucket, the node's disks have snapshots labelled with
# the incident, and deleting an object from the bucket is refused.

locals {
  project_id       = "your-project-id"                       # change me
  cluster          = "your-cluster"                          # change me
  cluster_location = "us-central1"                           # change me: region or zone of the cluster
  responders       = "group:incident-responders@example.com" # change me

  labels = {
    owner               = "security-operations"
    environment         = "prod"
    data_classification = "restricted"
    managed_by          = "terraform"
  }
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 8.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.6"
    }
  }
}

provider "google" {
  project = local.project_id
}

resource "random_id" "suffix" {
  byte_length = 3
}

# Retention is one day and unlocked, so you can try this and clean it up
# tomorrow. The Standard version defaults to a year and can lock it.
resource "google_storage_bucket" "evidence" {
  name                        = "container-evidence-${random_id.suffix.hex}"
  location                    = "US"
  uniform_bucket_level_access = true
  public_access_prevention    = "enforced"
  labels                      = local.labels

  retention_policy {
    retention_period = 86400
  }
}

# Responders add evidence and list object names, which gcloud storage cp needs.
# They cannot read evidence back, change it, or delete it.
resource "google_project_iam_custom_role" "writer" {
  role_id     = "containerEvidenceWriter_${random_id.suffix.hex}"
  title       = "Container evidence: add evidence"
  description = "Adds objects and lists their names. Cannot read, replace, or delete them."
  permissions = ["storage.objects.create", "storage.objects.list"]
}

resource "google_storage_bucket_iam_member" "responders_write" {
  bucket = google_storage_bucket.evidence.name
  role   = google_project_iam_custom_role.writer.name
  member = local.responders
}

# Snapshot a node's disks, and look up which disks those are. Nothing here
# can stop, delete, or change a VM.
resource "google_project_iam_custom_role" "snapshot" {
  role_id     = "containerEvidenceSnapshot_${random_id.suffix.hex}"
  title       = "Container evidence: snapshot node disks"
  description = "Reads a node VM and snapshots its disks. Cannot stop, delete, or change anything."
  permissions = [
    "compute.instances.get",
    "compute.disks.get",
    "compute.disks.createSnapshot",
    "compute.snapshots.create",
    "compute.snapshots.get",
    "compute.snapshots.setLabels",
    "compute.zoneOperations.get",
    "compute.globalOperations.get",
  ]
}

resource "google_project_iam_member" "responders" {
  for_each = toset([
    google_project_iam_custom_role.snapshot.name,
    # Enough to fetch cluster credentials. What they may do inside the cluster
    # is set by the incident-responder role.
    "roles/container.clusterViewer",
  ])
  project = local.project_id
  role    = each.value
  member  = local.responders
}

output "bucket" {
  description = "Where evidence goes."
  value       = google_storage_bucket.evidence.name
}

output "capture" {
  description = "The commands, in order. Set ID, NS, and POD first."
  value       = <<-EOT
    ID=inc-001 NS=<namespace> POD=<pod>
    gcloud container clusters get-credentials ${local.cluster} --location ${local.cluster_location} --project ${local.project_id}
    NODE=$(kubectl get pod -n "$NS" "$POD" -o jsonpath='{.spec.nodeName}')
    mkdir -p "$ID" && cd "$ID"

    # 1. Keep the node. The autoscaler skips it and nothing new lands on it.
    #    On Autopilot, skip the cordon: a node cordoned for about 10 minutes is repaired, which replaces it.
    kubectl annotate node "$NODE" cluster-autoscaler.kubernetes.io/scale-down-disabled=true
    kubectl cordon "$NODE"

    # 2. Record the pod, its images by digest, its logs, and its node.
    kubectl get pod -n "$NS" "$POD" -o json > pod.json
    kubectl logs -n "$NS" "$POD" --all-containers --timestamps > logs.txt
    kubectl logs -n "$NS" "$POD" --all-containers --timestamps --previous > logs-previous.txt || true
    kubectl get events -n "$NS" --field-selector involvedObject.name="$POD" -o json > events.json
    kubectl get node "$NODE" -o json > node.json

    # 3. Snapshot the node's disks. A snapshot is fixed when you ask for it, so
    #    losing the node after this line does not lose its disks.
    ZONE=$(kubectl get node "$NODE" -o jsonpath='{.metadata.labels.topology\.kubernetes\.io/zone}')
    for src in $(gcloud compute instances describe "$NODE" --zone "$ZONE" --project ${local.project_id} --flatten='disks[]' --format='value(disks.source)'); do
      scope=$(echo "$src" | awk -F/ '{print $(NF-3)}'); where=$(echo "$src" | awk -F/ '{print $(NF-2)}')   # zones or regions, and which
      gcloud compute disks snapshot "$${src##*/}" --"$${scope%s}" "$where" --project ${local.project_id} --labels incident="$ID"
    done

    # 4. Cut it off. The quarantine network policy from the Kubernetes evidence template matches this label.
    kubectl label pod -n "$NS" "$POD" quarantine=true incident="$ID" --overwrite

    # 5. Hash every file, then store them with their hashes.
    for f in *; do sha256sum "$f" > "$f.sha256"; done
    gcloud storage cp ./* gs://${google_storage_bucket.evidence.name}/"$ID"/"$NS"/"$POD"/"$(date -u +%Y%m%dT%H%M%SZ)"/
  EOT
}
