# siem-ingest / oci / t0 "Quick start"
#
# Every audit event in the tenancy, copied by Connector Hub into a stream that
# keeps them for seven days, and read by your SIEM's OCI input through the
# Streaming or Kafka API, such as the Splunk Add-on for OCI.
#
# Change the two values below. The provider reads everything else, region
# included, from the DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# The SIEM signs in as a member of the group named below. Create the group and
# its user first, and give the user an API key or an auth token as the SIEM
# input asks.
#
# Verify: change a tag on any resource. Within a few minutes the SIEM shows
# the event. To read the stream directly:
#   oci streaming stream cursor create-cursor --stream-id <stream> --partition 0 --type TRIM_HORIZON --endpoint <messages endpoint>
#   oci streaming stream message get --stream-id <stream> --cursor <value from above> --limit 5 --endpoint <messages endpoint>

locals {
  tenancy_ocid = "ocid1.tenancy.oc1..change-me" # change me
  siem_group   = "siem-readers"                 # change me: the SIEM user's group

  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" {}

# Seven days is the most a stream keeps. A SIEM outage longer than that loses
# events.
resource "oci_streaming_stream" "audit" {
  compartment_id     = local.tenancy_ocid
  name               = "siem-audit"
  partitions         = 1
  retention_in_hours = 168
  freeform_tags      = local.tags
}

# The connector writes as a service. The conditions limit it to connectors in
# this tenancy's root compartment, writing to this stream.
resource "oci_identity_policy" "siem" {
  compartment_id = local.tenancy_ocid
  name           = "siem-ingest"
  description    = "Lets the audit connector write to the SIEM stream, and the SIEM read it."
  freeform_tags  = local.tags
  statements = [
    "Allow any-user to use stream-push in tenancy where all {request.principal.type='serviceconnector', target.stream.id='${oci_streaming_stream.audit.id}', request.principal.compartment.id='${local.tenancy_ocid}'}",
    "Allow group ${local.siem_group} to use stream-pull in tenancy where target.stream.id='${oci_streaming_stream.audit.id}'",
    "Allow group ${local.siem_group} to read streams in tenancy",
  ]
}

# _Audit_Include_Subcompartment is every compartment's audit events, read from
# the root.
resource "oci_sch_service_connector" "audit" {
  compartment_id = local.tenancy_ocid
  display_name   = "siem-audit"
  description    = "Copies every audit event in the tenancy to the SIEM stream."
  freeform_tags  = local.tags

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

  target {
    kind      = "streaming"
    stream_id = oci_streaming_stream.audit.id
  }

  depends_on = [oci_identity_policy.siem]
}

output "stream_id" {
  description = "Give this to the SIEM input."
  value       = oci_streaming_stream.audit.id
}

output "messages_endpoint" {
  description = "The Streaming API endpoint for the stream."
  value       = oci_streaming_stream.audit.messages_endpoint
}
