# audit-log-immutable / oci / t0 "Quick start"
#
# A copy of the tenancy's audit events, written by Connector Hub to an Object
# Storage bucket with a retention rule. Nothing in the bucket can be deleted or
# overwritten until it is older than the rule allows.
#
# OCI already keeps audit events for up to a year, and nobody can delete a
# single event. But the retention period is one tenancy-wide setting that an
# administrator can shorten to 90 days. This copy is outside that setting.
#
# Change the tenancy OCID below. The provider reads everything else, region
# included, from the DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Verify, once the first batch lands (a few minutes):
#   terraform output verify
#   The delete it prints must be refused because of the retention rule.
#
# Retention here is one day and the rule is unlocked, so you can try this in a
# scratch tenancy and remove it tomorrow. The Standard version defaults to 400.

locals {
  tenancy_ocid = "ocid1.tenancy.oc1..change-me" # change me

  retention_days = 1
  tags = {
    owner               = "security-engineering"
    environment         = "prod"
    data_classification = "restricted"
    managed_by          = "terraform"
  }
}

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

provider "oci" {}

# Every tenancy has one Object Storage namespace, a short string used in every
# bucket address. It is looked up, never chosen.
data "oci_objectstorage_namespace" "this" {
  compartment_id = local.tenancy_ocid
}

resource "oci_objectstorage_bucket" "audit" {
  compartment_id = local.tenancy_ocid
  namespace      = data.oci_objectstorage_namespace.this.namespace
  name           = "audit-log-immutable"
  access_type    = "NoPublicAccess"
  freeform_tags  = local.tags

  # Retention rules and versioning cannot be on at the same time. The rule
  # already stops overwrites and deletes, so versioning adds nothing here.
  versioning = "Disabled"

  # Unlocked, so it can be edited or removed while you test.
  retention_rules {
    display_name = "audit-retention"
    duration {
      time_amount = local.retention_days
      time_unit   = "DAYS"
    }
  }
}

# Connector Hub writes as a service, and needs to be allowed in. The conditions
# limit it to connectors in this tenancy writing to this one bucket. Deleting
# this policy stops new copies, and nothing reports it.
resource "oci_identity_policy" "connector_write" {
  compartment_id = local.tenancy_ocid
  name           = "audit-log-immutable-connector"
  description    = "Lets the audit connector write objects into its own bucket."
  freeform_tags  = local.tags
  statements = [
    "Allow any-user to manage objects in tenancy where all {request.principal.type='serviceconnector', target.bucket.name='${oci_objectstorage_bucket.audit.name}', request.principal.compartment.id='${local.tenancy_ocid}'}",
  ]
}

# _Audit is the built-in log group that has audit events for a compartment.
resource "oci_sch_service_connector" "audit" {
  compartment_id = local.tenancy_ocid
  display_name   = "audit-log-immutable"
  freeform_tags  = local.tags

  source {
    kind = "logging"
    log_sources {
      compartment_id = local.tenancy_ocid
      log_group_id   = "_Audit"
    }
  }

  target {
    kind               = "objectStorage"
    bucket             = oci_objectstorage_bucket.audit.name
    namespace          = data.oci_objectstorage_namespace.this.namespace
    object_name_prefix = "audit"
  }

  depends_on = [oci_identity_policy.connector_write]
}

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

output "verify" {
  description = "Two commands. The second must be refused because of the retention rule."
  value       = <<-EOT
    oci os object list --bucket-name ${oci_objectstorage_bucket.audit.name} --limit 1 --query 'data[0].name' --raw-output
    oci os object delete --bucket-name ${oci_objectstorage_bucket.audit.name} --object-name <the name printed above> --force
  EOT
}
