# breakglass-identity / oci / t0 "Quick start"
#
# A local emergency account in the tenancy's Default identity domain, outside
# any federation, in the Administrators group, allowed a console password and
# nothing else, and an email when Audit records it doing anything, in any
# compartment.
#
# Change the four values below. The provider reads credentials from the
# DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Identity events are recorded in the home region, so everything here is
# created there.
#
# After apply, in the console, as an administrator:
#   1. Reset the account's password and seal it where your runbook says.
#   2. Sign in as the account once and enrol MFA, with a security key if
#      your domain allows it. Seal the key with the password.
#   3. Check the domain's sign-on policies do not route this account through
#      your identity provider.
# Terraform never sees the password.
#
# Verify: confirm the subscription email, then sign in as the account and
# open any page in the console. The console's calls are recorded under the
# account, and the alarm email, naming the account, follows within a few
# minutes. Logging Search on _Audit_Include_Subcompartment for the account
# lists what it did.
#
# Why an alarm and not a direct email: a connector that sends to
# Notifications may read only _Audit, which is one compartment. Counting into
# a metric reads _Audit_Include_Subcompartment, so use in any compartment is
# seen.

locals {
  tenancy_ocid  = "ocid1.tenancy.oc1..change-me" # change me
  home_region   = "us-ashburn-1"                 # change me: the tenancy's home region
  account_email = "breakglass-owner@example.com" # change me: a mailbox the account's owners read
  alert_email   = "security-oncall@example.com"  # change me

  namespace = "breakglass"

  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" {
  region = local.home_region
}

resource "oci_identity_user" "breakglass" {
  compartment_id = local.tenancy_ocid
  name           = "breakglass"
  description    = "Emergency administrator. Sealed credentials; every use is alerted."
  email          = local.account_email
  freeform_tags  = local.tags
}

# A console password only: no API keys, auth tokens, or other credentials
# that could be made and kept quietly.
resource "oci_identity_user_capabilities_management" "breakglass" {
  user_id                      = oci_identity_user.breakglass.id
  can_use_console_password     = true
  can_use_api_keys             = false
  can_use_auth_tokens          = false
  can_use_customer_secret_keys = false
  can_use_smtp_credentials     = false
}

data "oci_identity_groups" "administrators" {
  compartment_id = local.tenancy_ocid
  name           = "Administrators"
}

resource "oci_identity_user_group_membership" "administrators" {
  group_id = data.oci_identity_groups.administrators.groups[0].id
  user_id  = oci_identity_user.breakglass.id
}

resource "oci_ons_notification_topic" "breakglass" {
  compartment_id = local.tenancy_ocid
  name           = "breakglass-used"
  description    = "Every action by the emergency account."
  freeform_tags  = local.tags
}

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

# Deleting this policy or the topic stops the alarm, and nothing reports it.
resource "oci_identity_policy" "connector" {
  compartment_id = local.tenancy_ocid
  name           = "breakglass-connector"
  description    = "Lets the break-glass connector write its metric."
  freeform_tags  = local.tags
  statements = [
    "Allow any-user to use metrics in tenancy where all {request.principal.type='serviceconnector', target.metrics.namespace='${local.namespace}', request.principal.compartment.id='${local.tenancy_ocid}'}",
  ]
}

resource "oci_sch_service_connector" "used" {
  compartment_id = local.tenancy_ocid
  display_name   = "breakglass-used"
  description    = "Counts audit events made by the emergency account, in every compartment."
  # Set, so a connector switched off by hand shows in the next plan.
  state         = "ACTIVE"
  freeform_tags = local.tags

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

  tasks {
    kind      = "logRule"
    condition = "data.identity.principalId='${oci_identity_user.breakglass.id}'"
  }

  target {
    kind             = "monitoring"
    compartment_id   = local.tenancy_ocid
    metric_namespace = local.namespace
    metric           = "used"
    dimensions {
      name = "principal"
      dimension_value {
        kind = "jmesPath"
        path = "logContent.data.identity.principalName"
      }
    }
  }

  depends_on = [oci_identity_policy.connector]
}

resource "oci_monitoring_alarm" "used" {
  compartment_id        = local.tenancy_ocid
  metric_compartment_id = local.tenancy_ocid
  display_name          = "breakglass-used"
  namespace             = local.namespace
  query                 = "used[1m].count() > 0"
  severity              = "CRITICAL"
  is_enabled            = true
  destinations          = [oci_ons_notification_topic.breakglass.id]
  message_format        = "ONS_OPTIMIZED"
  body                  = "The emergency account was used. Confirm an incident needs it. In Logging Search, search the tenancy's _Audit_Include_Subcompartment for the account to see what it did."
  freeform_tags         = local.tags
}

output "user_id" {
  description = "The emergency account."
  value       = oci_identity_user.breakglass.id
}
