# siem-ingest / gcp / t0 "Quick start"
#
# Routes a project's audit logs to a Pub/Sub topic, with a subscription your
# SIEM's collector pulls from: the Splunk add-on for Google Cloud, Google
# Security Operations, or your own. Messages wait there for seven days, so a
# collector outage loses nothing shorter.
#
# Change the project below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#
# Point the collector at the subscription it prints, signed in as the
# collector service account.
#
# Verify: do one recognisable thing, such as creating a bucket named
# siem-drill-<today>, then find it in the SIEM. Before the collector is
# connected, terraform output verify shows a message waiting.

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

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

provider "google" {
  project = local.project_id
}

resource "google_project_service" "pubsub" {
  service            = "pubsub.googleapis.com"
  disable_on_destroy = false
}

resource "google_pubsub_topic" "siem" {
  name                       = "siem-audit-logs"
  message_retention_duration = "604800s"
  depends_on                 = [google_project_service.pubsub]
}

resource "google_pubsub_subscription" "siem" {
  name                       = "siem-collector"
  topic                      = google_pubsub_topic.siem.id
  ack_deadline_seconds       = 60
  message_retention_duration = "604800s"

  # By default a subscription nobody reads for 31 days is deleted, and the
  # SIEM's feed ends with no error. An empty ttl means it never expires.
  expiration_policy {
    ttl = ""
  }
}

# Every audit log in the project: admin activity, and data access where it is
# switched on.
resource "google_logging_project_sink" "siem" {
  name                   = "siem-audit-logs"
  destination            = "pubsub.googleapis.com/${google_pubsub_topic.siem.id}"
  filter                 = "logName:\"cloudaudit.googleapis.com\""
  unique_writer_identity = true
}

# The sink writes as its own identity, which needs publish rights.
resource "google_pubsub_topic_iam_member" "sink_publishes" {
  topic  = google_pubsub_topic.siem.id
  role   = "roles/pubsub.publisher"
  member = google_logging_project_sink.siem.writer_identity
}

resource "google_service_account" "collector" {
  account_id   = "siem-collector"
  display_name = "Reads audit logs for the SIEM"
}

resource "google_pubsub_subscription_iam_member" "collector" {
  subscription = google_pubsub_subscription.siem.id
  role         = "roles/pubsub.subscriber"
  member       = "serviceAccount:${google_service_account.collector.email}"
}

output "subscription" {
  description = "Give this to the collector."
  value       = google_pubsub_subscription.siem.id
}

output "collector_service_account" {
  description = "The identity the collector signs in as. Prefer workload identity federation where the collector supports it."
  value       = google_service_account.collector.email
}

output "verify" {
  description = "Shows one waiting message without removing it."
  value       = "gcloud pubsub subscriptions pull ${google_pubsub_subscription.siem.name} --limit=1"
}
