# breakglass-identity / azure / t1 "Standard" # # Break-glass accounts with Owner at the scope you choose, an alert on any # sign-in or action by them, and a second alert on any change to them: a # password reset, a new or removed sign-in method, a group or role change, or # their Owner assignment removed. Four native alerts read the activity log # without the workspace, and a lock stops anything in the monitoring group # being deleted. # # Create the accounts in Entra first: cloud-only, a FIDO2 key each, excluded # from Conditional Access. 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) upns = jsonencode(keys(var.accounts)) ids = jsonencode(values(var.accounts)) queries = { used = { severity = 0 description = "A break-glass account signed in or acted." kql = <<-KQL let upns = dynamic(${local.upns}); union isfuzzy=true (SigninLogs | where ingestion_time() > ago(5m) | where UserPrincipalName in~ (upns)), (AzureActivity | where ingestion_time() > ago(5m) | where Caller in~ (upns)) KQL } changed = { severity = 1 description = "Someone changed a break-glass account: password, sign-in methods, groups, roles, or its Owner assignment." kql = <<-KQL let upns = dynamic(${local.upns}); let ids = dynamic(${local.ids}); union isfuzzy=true (AuditLogs | where ingestion_time() > ago(5m) | mv-expand t = TargetResources | where tostring(t.userPrincipalName) in~ (upns) or tostring(t.id) in (ids)), (AzureActivity | where ingestion_time() > ago(5m) | where OperationNameValue =~ "MICROSOFT.AUTHORIZATION/ROLEASSIGNMENTS/DELETE" | where tostring(Properties) has_any (ids)) KQL } } # Changes the workspace might never hear of. A deleted or rewritten export # stops the record of its own change, and a disabled rule does not run. native = { diagnostic-setting-deleted = { operation = "Microsoft.Insights/diagnosticSettings/delete" doc = "A diagnostic setting was deleted in the subscription, possibly the one that feeds the break-glass alerts." } # Policy remediation and other teams' monitoring write diagnostic # settings too, so expect some of these. diagnostic-setting-written = { operation = "Microsoft.Insights/diagnosticSettings/write" doc = "A diagnostic setting was created or changed in the subscription. A rewrite can drop a category or send the logs somewhere else." } # Azure writes this when a log search rule's query has failed for a week. rule-disabled = { operation = "Microsoft.Insights/ScheduledQueryRules/disable/action" doc = "Azure disabled a log search alert rule in the subscription because its query kept failing." } # Deleting the lock is the step before deleting anything in the # monitoring group. lock-deleted = { operation = "Microsoft.Authorization/locks/delete" doc = "A management lock was deleted in the subscription, possibly the one that keeps the break-glass alerts from being deleted." } } } data "azurerm_subscription" "current" {} resource "azurerm_role_assignment" "owner" { for_each = var.accounts scope = var.role_scope role_definition_name = "Owner" principal_id = each.value description = "Break-glass account ${each.key}. Every use alerts." } resource "azurerm_resource_group" "break_glass" { name = "rg-break-glass-monitoring" location = var.location tags = local.tags } resource "azurerm_log_analytics_workspace" "break_glass" { name = "law-break-glass" location = azurerm_resource_group.break_glass.location resource_group_name = azurerm_resource_group.break_glass.name sku = "PerGB2018" retention_in_days = var.retention_days tags = local.tags } resource "azurerm_monitor_aad_diagnostic_setting" "entra" { name = "break-glass-watch" log_analytics_workspace_id = azurerm_log_analytics_workspace.break_glass.id dynamic "enabled_log" { for_each = ["SignInLogs", "NonInteractiveUserSignInLogs", "AuditLogs"] content { category = enabled_log.value } } } # Actions show in each subscription's Activity Log. With Owner at a management # group, list every subscription below it in watched_subscription_ids, or # actions in the others go unseen. resource "azurerm_monitor_diagnostic_setting" "activity" { for_each = toset(length(var.watched_subscription_ids) > 0 ? var.watched_subscription_ids : [data.azurerm_subscription.current.subscription_id]) name = "break-glass-watch" target_resource_id = "/subscriptions/${each.value}" log_analytics_workspace_id = azurerm_log_analytics_workspace.break_glass.id enabled_log { category = "Administrative" } } resource "azurerm_monitor_action_group" "oncall" { name = "break-glass-alerts" resource_group_name = azurerm_resource_group.break_glass.name short_name = "breakglass" tags = local.tags # Receiver names are numbered, because names must differ regardless of # case. dynamic "email_receiver" { for_each = var.alert_emails content { name = "email-${email_receiver.key}" email_address = email_receiver.value } } } # Azure evaluates these alerts on the activity log itself, with no workspace # in between, and they cost nothing. They still notify through the one action # group, so someone who can edit it can disable it or change its addresses. # They read var.subscription_id's activity log only. The Entra log stream is # set at the tenant, and the other watched subscriptions keep their own # activity logs, so none of them sees those change. resource "azurerm_monitor_activity_log_alert" "native" { for_each = local.native name = "break-glass-${each.key}" resource_group_name = azurerm_resource_group.break_glass.name location = "global" scopes = ["/subscriptions/${var.subscription_id}"] tags = local.tags description = each.value.doc criteria { category = "Administrative" operation_name = each.value.operation } action { action_group_id = azurerm_monitor_action_group.oncall.id } } # The rules are made after the exports, though an export can still take up # to 90 minutes to start delivering. resource "azurerm_monitor_scheduled_query_rules_alert_v2" "detection" { for_each = local.queries name = "break-glass-${each.key}" resource_group_name = azurerm_resource_group.break_glass.name location = azurerm_resource_group.break_glass.location scopes = [azurerm_log_analytics_workspace.break_glass.id] description = each.value.description severity = each.value.severity # Look back an hour, keep rows that arrived in the last five minutes: a # late row is neither missed nor alerted twice. evaluation_frequency = "PT5M" window_duration = "PT1H" skip_query_validation = true tags = local.tags criteria { query = each.value.kql time_aggregation_method = "Count" operator = "GreaterThan" threshold = 0 } action { action_groups = [azurerm_monitor_action_group.oncall.id] } depends_on = [ azurerm_monitor_aad_diagnostic_setting.entra, azurerm_monitor_diagnostic_setting.activity, ] } # Nothing in the monitoring group can be deleted while this lock is on: the # workspace, the action group, and the alerts. Deleting the lock first fires # break-glass-lock-deleted. A cannot-delete lock does not stop an edit, such # as disabling the action group or an alert. # # The lock is made last and removed first, so terraform destroy can run. # Any change that replaces or removes a resource in the group fails while # it is on. resource "azurerm_management_lock" "monitoring" { name = "break-glass-monitoring" scope = azurerm_resource_group.break_glass.id lock_level = "CanNotDelete" notes = "Keeps the break-glass alerts and their record from being deleted. Managed by Terraform." depends_on = [ azurerm_log_analytics_workspace.break_glass, azurerm_monitor_action_group.oncall, azurerm_monitor_activity_log_alert.native, azurerm_monitor_scheduled_query_rules_alert_v2.detection, ] }