# guardrail-baseline / oci / t0 "Quick start"
#
# A security zone on one compartment, using Oracle's Maximum Security Recipe.
# Inside the zone, OCI refuses changes that break the recipe's policies: a
# public bucket, a public subnet, an internet gateway, a volume or bucket
# without a customer-managed key, and more. The refusal happens before
# anything changes.
#
# Security zones need Cloud Guard. Turn it on first, such as with the
# compliance evaluation template.
#
# Change the two OCIDs below. The provider reads everything else, region
# included, from the DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Verify:
#   oci os bucket create --compartment-id <compartment> --name guardtest --public-access-type ObjectRead
#   Expect a refusal naming the security zone policy.
#
# The maximum recipe is strict on purpose. Use it on a compartment built for
# sensitive workloads, or an empty one, never on a busy compartment first.

locals {
  tenancy_ocid     = "ocid1.tenancy.oc1..change-me"     # change me
  compartment_ocid = "ocid1.compartment.oc1..change-me" # change me: the compartment to protect

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

# Every tenancy has this recipe; it is looked up, never created.
data "oci_cloud_guard_security_recipes" "maximum" {
  compartment_id = local.tenancy_ocid
  display_name   = "Maximum Security Recipe"
}

locals {
  maximum_recipes = data.oci_cloud_guard_security_recipes.maximum.security_recipe_collection[0].items
}

resource "oci_cloud_guard_security_zone" "protected" {
  compartment_id          = local.compartment_ocid
  display_name            = "guardrail-baseline"
  description             = "Refuses changes that break Oracle's Maximum Security Recipe."
  security_zone_recipe_id = local.maximum_recipes[0].id
  freeform_tags           = local.tags

  lifecycle {
    precondition {
      condition     = length(local.maximum_recipes) == 1
      error_message = "Maximum Security Recipe was not found. Is Cloud Guard enabled in this tenancy?"
    }
  }
}

output "security_zone" {
  description = "The zone. Its compartment and every compartment below it are covered."
  value       = oci_cloud_guard_security_zone.protected.id
}
