# compliance-evaluation / oci / t0 "Quick start"
#
# Cloud Guard switched on for the whole tenancy, with Oracle's configuration
# and activity detector recipes, and an email for every problem it finds.
#
# Change the three values below. The provider reads credentials from the
# DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# The reporting region is where Cloud Guard keeps its findings and sends its
# events. It cannot be changed once set, so pick it with care: usually the
# home region.
#
# Verify: confirm the subscription email first. Then, in any compartment,
# create a bucket with public access:
#   oci os bucket create --compartment-id <compartment> --name cgtest --public-access-type ObjectRead
# A problem appears in Cloud Guard when the configuration detector next scans,
# and the email follows. Delete the bucket after.

locals {
  tenancy_ocid     = "ocid1.tenancy.oc1..change-me" # change me
  reporting_region = "us-ashburn-1"                 # change me
  alert_email      = "security-alerts@example.com"  # change me

  tags = {
    owner               = "security-engineering"
    environment         = "prod"
    data_classification = "internal"
    managed_by          = "terraform"
  }
}

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

provider "oci" {
  region = local.reporting_region
}

# The permissions Oracle documents for Cloud Guard to read what it checks.
# The console adds the same statements when Cloud Guard is switched on there.
resource "oci_identity_policy" "cloud_guard" {
  compartment_id = local.tenancy_ocid
  name           = "cloud-guard"
  description    = "Lets Cloud Guard read the resources it evaluates."
  freeform_tags  = local.tags
  statements = [
    "allow service cloudguard to manage cloudevents-rules in tenancy where target.rule.type='managed'",
    "allow service cloudguard to read audit-events in tenancy",
    "allow service cloudguard to read authentication-policies in tenancy",
    "allow service cloudguard to read autonomous-database-family in tenancy",
    "allow service cloudguard to read compartments in tenancy",
    "allow service cloudguard to read compute-management-family in tenancy",
    "allow service cloudguard to read database-family in tenancy",
    "allow service cloudguard to read data-safe-family in tenancy",
    "allow service cloudguard to read dynamic-groups in tenancy",
    "allow service cloudguard to read groups in tenancy",
    "allow service cloudguard to read instance-family in tenancy",
    "allow service cloudguard to read keys in tenancy",
    "allow service cloudguard to read load-balancers in tenancy",
    "allow service cloudguard to read log-groups in tenancy",
    "allow service cloudguard to read object-family in tenancy",
    "allow service cloudguard to read policies in tenancy",
    "allow service cloudguard to read tenancies in tenancy",
    "allow service cloudguard to read users in tenancy",
    "allow service cloudguard to read vaults in tenancy",
    "allow service cloudguard to read virtual-network-family in tenancy",
    "allow service cloudguard to read volume-family in tenancy",
    "allow service cloudguard to use network-security-groups in tenancy",
  ]
}

resource "oci_cloud_guard_cloud_guard_configuration" "this" {
  compartment_id   = local.tenancy_ocid
  reporting_region = local.reporting_region
  status           = "ENABLED"

  depends_on = [oci_identity_policy.cloud_guard]
}

# Oracle's own recipes, looked up by the detector they belong to.
data "oci_cloud_guard_detector_recipes" "oracle" {
  compartment_id = local.tenancy_ocid

  depends_on = [oci_cloud_guard_cloud_guard_configuration.this]
}

locals {
  oracle_recipes = {
    for r in data.oci_cloud_guard_detector_recipes.oracle.detector_recipe_collection[0].items :
    r.detector => r.id if r.owner == "ORACLE"
  }
  detectors = ["IAAS_CONFIGURATION_DETECTOR", "IAAS_ACTIVITY_DETECTOR"]
}

# One target on the root compartment covers every compartment below it.
resource "oci_cloud_guard_target" "tenancy" {
  compartment_id       = local.tenancy_ocid
  display_name         = "tenancy"
  target_resource_id   = local.tenancy_ocid
  target_resource_type = "COMPARTMENT"
  freeform_tags        = local.tags

  dynamic "target_detector_recipes" {
    for_each = local.detectors
    content {
      detector_recipe_id = local.oracle_recipes[target_detector_recipes.value]
    }
  }
}

resource "oci_ons_notification_topic" "problems" {
  compartment_id = local.tenancy_ocid
  name           = "cloud-guard-problems"
  description    = "Every problem Cloud Guard finds."
  freeform_tags  = local.tags
}

resource "oci_ons_subscription" "email" {
  compartment_id = local.tenancy_ocid
  topic_id       = oci_ons_notification_topic.problems.id
  protocol       = "EMAIL"
  endpoint       = local.alert_email
  freeform_tags  = local.tags
}

# Cloud Guard sends its events in the reporting region, which is where this
# rule is created.
resource "oci_events_rule" "problems" {
  compartment_id = local.tenancy_ocid
  display_name   = "cloud-guard-problem-detected"
  description    = "Emails every new Cloud Guard problem."
  is_enabled     = true
  condition      = jsonencode({ eventType = ["com.oraclecloud.cloudguard.problemdetected"] })
  freeform_tags  = local.tags

  actions {
    actions {
      action_type = "ONS"
      is_enabled  = true
      topic_id    = oci_ons_notification_topic.problems.id
    }
  }
}
