# breakglass-identity / oci / t1 "Standard" # # Emergency accounts in a group of their own with a policy of their own, each # allowed a console password and nothing else, an alert on anything they do, # and a second alert on any change Audit records against the accounts, their # group memberships, their group, or their policy. The second alert also # watches the parts that report use: the use connector, its alarm, the topic, # its subscription, and the connectors' policy. # # The two alerts reach the topic by different routes. A connector that sends # to Notifications may read only _Audit, one compartment's events. Use of an # account can happen in any compartment, so it is counted into a metric from # _Audit_Include_Subcompartment and an alarm emails, one per account. The # accounts, their group and policy, and the parts that report use all live in # the root compartment, so changes to them are read from the root's _Audit # and emailed in full. # # After apply, for each account: reset the password, sign in once to enrol # MFA, and seal both. See the quick start for the steps. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) namespace = "breakglass" user_ids = [for u in oci_identity_user.breakglass : u.id] # What the accounts do, in any compartment. used_condition = join(" or ", [for id in local.user_ids : "data.identity.principalId='${id}'"]) # What is done to them, and to what reports their use, in the root # compartment. A use connector moved out of the root loses its grant to # write the metric, so a move counts here too. changed_condition = join(" or ", [ for id in concat( local.user_ids, [for m in oci_identity_user_group_membership.breakglass : m.id], [oci_identity_group.breakglass.id, oci_identity_policy.breakglass.id], [ oci_sch_service_connector.used.id, oci_monitoring_alarm.used.id, oci_ons_notification_topic.breakglass.id, oci_ons_subscription.email.id, oci_identity_policy.connector.id, ], ) : "data.resourceId='${id}'" ]) } resource "oci_identity_user" "breakglass" { for_each = var.accounts compartment_id = var.tenancy_ocid name = each.key description = "Emergency administrator. Sealed credentials; every use is alerted." email = each.value freeform_tags = local.tags } resource "oci_identity_user_capabilities_management" "breakglass" { for_each = oci_identity_user.breakglass user_id = each.value.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 } # A group and policy of their own, so a change to Administrators made for # everyday reasons cannot remove emergency access by accident. resource "oci_identity_group" "breakglass" { compartment_id = var.tenancy_ocid name = "breakglass" description = "Emergency administrators. Membership is managed in Terraform." freeform_tags = local.tags } resource "oci_identity_user_group_membership" "breakglass" { for_each = oci_identity_user.breakglass group_id = oci_identity_group.breakglass.id user_id = each.value.id } resource "oci_identity_policy" "breakglass" { compartment_id = var.tenancy_ocid name = "breakglass" description = "Full control of the tenancy for the emergency accounts." freeform_tags = local.tags statements = [ "Allow group ${oci_identity_group.breakglass.name} to manage all-resources in tenancy", ] } resource "oci_ons_notification_topic" "breakglass" { compartment_id = var.tenancy_ocid name = "breakglass" description = "Use of and changes to the emergency accounts." freeform_tags = local.tags } resource "oci_ons_subscription" "email" { compartment_id = var.tenancy_ocid topic_id = oci_ons_notification_topic.breakglass.id protocol = "EMAIL" endpoint = var.alert_email freeform_tags = local.tags } # Deleting this policy or the topic stops both alerts, and neither can report # it. Hardened refuses both deletes, and its daily drift plan notices one made # by hand. resource "oci_identity_policy" "connector" { compartment_id = var.tenancy_ocid name = "breakglass-connector" description = "Lets the break-glass connectors write their metric and publish to their topic." 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='${var.tenancy_ocid}'}", "Allow any-user to use ons-topics in tenancy where all {request.principal.type='serviceconnector', target.topic.id='${oci_ons_notification_topic.breakglass.id}', request.principal.compartment.id='${var.tenancy_ocid}'}", ] } resource "oci_sch_service_connector" "used" { compartment_id = var.tenancy_ocid display_name = "breakglass-used" description = "Counts audit events made by an emergency account, in every compartment." # Set, so a connector switched off by hand shows in the daily drift plan. state = "ACTIVE" freeform_tags = local.tags source { kind = "logging" log_sources { compartment_id = var.tenancy_ocid log_group_id = "_Audit_Include_Subcompartment" } } # One log rule and no other task, here and on the change connector. # Hardened refuses a second task, which could drop events. tasks { kind = "logRule" condition = local.used_condition } target { kind = "monitoring" compartment_id = var.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] } # One notification per account, so two accounts in use are two alarms. resource "oci_monitoring_alarm" "used" { compartment_id = var.tenancy_ocid metric_compartment_id = var.tenancy_ocid display_name = "breakglass-used" namespace = local.namespace query = "used[1m].groupBy(principal).count() > 0" severity = "CRITICAL" is_enabled = true is_notifications_per_metric_dimension_enabled = true destinations = [oci_ons_notification_topic.breakglass.id] message_format = "ONS_OPTIMIZED" body = "An emergency account was used; the account is in the alarm's dimensions. 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 } resource "oci_sch_service_connector" "changed" { compartment_id = var.tenancy_ocid display_name = "breakglass-changed" description = "Changes to the emergency accounts, their memberships, group, and policy, and to what reports their use." state = "ACTIVE" freeform_tags = local.tags # Notifications accepts only _Audit. Everything this connector watches # lives in the root compartment, so the root's _Audit records every change # to it. source { kind = "logging" log_sources { compartment_id = var.tenancy_ocid log_group_id = "_Audit" } } tasks { kind = "logRule" condition = local.changed_condition } target { kind = "notifications" topic_id = oci_ons_notification_topic.breakglass.id enable_formatted_messaging = true } depends_on = [oci_identity_policy.connector] } # Earlier versions kept both connectors in one for_each map. moved { from = oci_sch_service_connector.rule["used"] to = oci_sch_service_connector.used } moved { from = oci_sch_service_connector.rule["changed"] to = oci_sch_service_connector.changed }