# container-evidence / oci / t0 "Quick start"
#
# What a responder needs to preserve a compromised container on OKE: a bucket
# for the evidence that nobody can change or delete from for a day, the right
# to read a node and back up its volumes and nothing more on the OCI 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. On OKE, the binding's subject is the responder group's OCID:
#   subjects:
#     - apiGroup: rbac.authorization.k8s.io
#       kind: Group
#       name: <the same group OCID as below>
#
# This quick start assumes the cluster, its worker nodes, and the bucket share
# one compartment. Standard separates them.
#
# Change the three values below, then:
#   terraform init && terraform apply     (credentials from ~/.oci/config)
#   terraform output capture
#
# Verify: run the printed commands against a test pod. The pod's record,
# logs, and hashes land in the bucket, and the node's volumes have backups
# tagged with the incident. As someone who can delete objects in the bucket,
# try to delete one: the retention rule refuses it.

locals {
  region               = "us-ashburn-1"                  # change me
  cluster_id           = "ocid1.cluster.oc1..replace-me" # change me
  responder_group_ocid = "ocid1.group.oc1..replace-me"   # change me: an IAM group
  tags = {
    owner               = "security-operations"
    environment         = "prod"
    data_classification = "restricted"
    managed_by          = "terraform"
  }
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 9.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.6"
    }
  }
}

provider "oci" {
  region = local.region
}

data "oci_containerengine_cluster" "this" {
  cluster_id = local.cluster_id
}

data "oci_objectstorage_namespace" "this" {
  compartment_id = data.oci_containerengine_cluster.this.compartment_id
}

locals {
  compartment = data.oci_containerengine_cluster.this.compartment_id
  group       = "group id ${local.responder_group_ocid}"
}

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

# One day, unlocked, so you can try this and clean it up tomorrow. The
# Standard version defaults to a year and can schedule a lock. Retention rules
# cannot be added to a bucket with versioning on.
resource "oci_objectstorage_bucket" "evidence" {
  compartment_id = local.compartment
  namespace      = data.oci_objectstorage_namespace.this.namespace
  name           = "container-evidence-${random_id.suffix.hex}"
  access_type    = "NoPublicAccess"
  versioning     = "Disabled"
  freeform_tags  = local.tags

  retention_rules {
    display_name = "evidence-retention"
    duration {
      time_amount = 1
      time_unit   = "DAYS"
    }
  }
}

resource "oci_identity_policy" "responders" {
  compartment_id = local.compartment
  name           = "container-evidence-${random_id.suffix.hex}"
  description    = "Lets incident responders reach the cluster, back up node volumes, and add evidence. No deletes."
  freeform_tags  = local.tags
  statements = [
    # A kubeconfig. What they may do inside the cluster is set by the
    # incident-responder role.
    "Allow ${local.group} to use clusters in compartment id ${local.compartment}",

    # Find a node's volumes, and back them up. The same grants Oracle's own
    # backup example uses, less deleting backups.
    "Allow ${local.group} to read instances in compartment id ${local.compartment}",
    "Allow ${local.group} to inspect volume-attachments in compartment id ${local.compartment}",
    "Allow ${local.group} to use volumes in compartment id ${local.compartment}",
    "Allow ${local.group} to read boot-volume-backups in compartment id ${local.compartment}",
    "Allow ${local.group} to manage boot-volume-backups in compartment id ${local.compartment} where request.permission = 'BOOT_VOLUME_BACKUP_CREATE'",
    "Allow ${local.group} to read volume-backups in compartment id ${local.compartment}",
    "Allow ${local.group} to manage volume-backups in compartment id ${local.compartment} where request.permission = 'VOLUME_BACKUP_CREATE'",

    # Add objects and list their names. Not read, overwrite, or delete them.
    "Allow ${local.group} to read buckets in compartment id ${local.compartment} where target.bucket.name = '${oci_objectstorage_bucket.evidence.name}'",
    "Allow ${local.group} to manage objects in compartment id ${local.compartment} where all {target.bucket.name = '${oci_objectstorage_bucket.evidence.name}', any {request.permission = 'OBJECT_CREATE', request.permission = 'OBJECT_INSPECT'}}",
  ]
}

output "capture" {
  description = "The commands, in order. Set ID, NS, and POD first."
  value       = <<-EOT
    ID=inc-001 NS=<namespace> POD=<pod>
    # PUBLIC_ENDPOINT, or PRIVATE_ENDPOINT from inside the cluster's network.
    oci ce cluster create-kubeconfig --cluster-id ${local.cluster_id} --file "$PWD/$ID.kubeconfig" --region ${local.region} --token-version 2.0.0 --kube-endpoint PUBLIC_ENDPOINT && export KUBECONFIG="$PWD/$ID.kubeconfig"
    NODE=$(kubectl get pod -n "$NS" "$POD" -o jsonpath='{.spec.nodeName}')
    mkdir -p "$ID" && cd "$ID"

    # 1. Keep the node. The cluster autoscaler skips it, and nothing new lands
    #    on it. OCI has no termination protection, so ask the platform team to
    #    pause node pool changes: node cycling replaces nodes.
    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. Back up the node's boot volume and every attached block volume,
    #    persistent volumes included. Each backup is fixed at the moment you
    #    ask for it. FULL makes each one stand alone.
    INSTANCE=$(kubectl get node "$NODE" -o jsonpath='{.spec.providerID}'); INSTANCE=$${INSTANCE#oci://}
    AD=$(oci compute instance get --instance-id "$INSTANCE" --query 'data."availability-domain"' --raw-output)
    BOOT=$(oci compute boot-volume-attachment list --availability-domain "$AD" --compartment-id ${local.compartment} --instance-id "$INSTANCE" --query 'data[0]."boot-volume-id"' --raw-output)
    oci bv boot-volume-backup create --boot-volume-id "$BOOT" --type FULL --display-name "ir-$ID-boot-$NODE" --freeform-tags "{\"incident\":\"$ID\"}"
    for v in $(oci compute volume-attachment list --compartment-id ${local.compartment} --instance-id "$INSTANCE" --query 'join(`" "`, data[?"lifecycle-state"==`ATTACHED`]."volume-id")' --raw-output); do
      oci bv backup create --volume-id "$v" --type FULL --display-name "ir-$ID-$${v: -12}" --freeform-tags "{\"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. --no-multipart,
    #    because multipart uploads need read and overwrite, which responders lack.
    for f in *; do sha256sum "$f" > "$f.sha256"; done
    DEST="$ID/$NS/$POD/$(date -u +%Y%m%dT%H%M%SZ)"
    for f in *; do oci os object put -ns ${data.oci_objectstorage_namespace.this.namespace} -bn ${oci_objectstorage_bucket.evidence.name} --name "$DEST/$f" --file "$f" --no-multipart --no-overwrite; done
  EOT
}
