# audit-log-immutable / gcp / t0 "Quick start"
#
# A copy of this project's audit logs, written to a Cloud Storage bucket with a
# retention policy. Nothing in the bucket can be deleted or overwritten until
# it is older than the retention period.
#
# Google Cloud already keeps Admin Activity logs for 400 days in a bucket named
# _Required that nobody can change. This copy exists for three reasons: it can
# outlast 400 days, it can store Data Access logs once you switch those on, and
# it lives in storage you control access to separately.
#
# Change the project ID below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#
# Verify, after the first batch lands (Cloud Logging writes to Cloud Storage
# in hourly batches, so allow up to an hour):
#   terraform output verify
#   The delete it prints must fail with a retention policy error.
#
# Retention here is one day and the policy is unlocked, so you can try this in
# a scratch project and remove it tomorrow. The Standard version defaults to
# 400 days.

locals {
  project_id = "your-project-id" # change me

  location          = "US"
  retention_seconds = 86400 # one day
}

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 = 4
}

resource "google_storage_bucket" "audit" {
  name     = "audit-log-immutable-${random_id.suffix.hex}"
  location = local.location

  # One permission model for the whole bucket, and no way to make it public.
  uniform_bucket_level_access = true
  public_access_prevention    = "enforced"

  # Objects cannot be deleted or replaced until they are older than this.
  # Unlocked, so the period can still be shortened or removed while you test.
  retention_policy {
    retention_period = local.retention_seconds
    is_locked        = false
  }

  # Lets terraform destroy empty the bucket once the one-day period has passed.
  force_destroy = true

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

# The sink copies every entry whose log name contains cloudaudit.googleapis.com.
# That covers Admin Activity and System Event logs, which are always on, and
# Data Access logs for any service where you have switched them on.
resource "google_logging_project_sink" "audit" {
  name        = "audit-log-immutable"
  destination = "storage.googleapis.com/${google_storage_bucket.audit.name}"
  filter      = "logName:\"cloudaudit.googleapis.com\""

  unique_writer_identity = true
}

# The sink writes as its own service account. Without this grant the sink
# exists, reports nothing wrong in Terraform, and delivers nothing.
resource "google_storage_bucket_iam_member" "sink_writer" {
  bucket = google_storage_bucket.audit.name
  role   = "roles/storage.objectCreator"
  member = google_logging_project_sink.audit.writer_identity
}

output "bucket" {
  description = "Bucket containing the audit copy."
  value       = google_storage_bucket.audit.name
}

output "sink_writer" {
  description = "The identity the sink writes as."
  value       = google_logging_project_sink.audit.writer_identity
}

output "verify" {
  description = "Two commands. The second must fail with a retention policy error."
  value       = <<-EOT
    gcloud storage ls --recursive gs://${google_storage_bucket.audit.name}/ | head -1
    gcloud storage rm <the object path printed above>
  EOT
}
