# =====================================================================
# MalwareTracker / QuickSand Cloud Storage Bucket Guard — Storage Foundation (Terraform)
# Provisions hardened `untrusted-uploads`, `clean-documents`, and `quarantine-documents`
# GCS buckets with public access prevention and zero soft-delete overhead.
# Pair with an Eventarc `google.cloud.storage.object.v1.finalized` Cloud Run handler
# calling `POST https://api.malwaretracker.com/v1/scan?store=false` (fail-closed) to route objects.
# =====================================================================

variable "project_id" {
  description = "GCP Project ID"
  type        = string
}

variable "region" {
  description = "GCP Region"
  type        = string
  default     = "us-central1"
}

variable "malwaretracker_api_key" {
  description = "MalwareTracker API Key (mt_live_...)"
  type        = string
  sensitive   = true
}

resource "google_storage_bucket" "untrusted_uploads" {
  name                        = "${var.project_id}-untrusted-uploads"
  location                    = var.region
  uniform_bucket_level_access = true
  public_access_prevention    = "enforced"

  lifecycle_rule {
    condition {
      age = 1
    }
    action {
      type = "Delete"
    }
  }
}

resource "google_storage_bucket" "clean_documents" {
  name                        = "${var.project_id}-clean-documents"
  location                    = var.region
  uniform_bucket_level_access = true
  public_access_prevention    = "enforced"

  versioning {
    enabled = true
  }
}

resource "google_storage_bucket" "quarantine_documents" {
  name                        = "${var.project_id}-quarantine-documents"
  location                    = var.region
  uniform_bucket_level_access = true
  public_access_prevention    = "enforced"

  lifecycle_rule {
    condition {
      age = 30
    }
    action {
      type = "Delete"
    }
  }
}
