# data-perimeter / azure / t1 "Standard" # # A network security perimeter around the resources you name, with the # networks and subscriptions allowed in. Every refusal is logged and # alerted on. Azure Policy refuses the storage settings that would carry # data out another way. With alerts on, native alerts read the activity log # for the changes that would quiet the refusal alert, and locks keep the # alerts from being deleted. # # Azure has no tenant-wide rule that says "our data stays with our # identities". This is the nearest composite. The perimeter stops requests # from outside and server-side copies out. The policies stop accounts being # configured around it. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) log = var.log_analytics_workspace_id != "" alert = local.log && length(var.alert_emails) > 0 # The workspace's subscription. Its role assignments are recorded in that # subscription's activity log, which the native alerts read only when it is # this template's own. workspace_subscription = try(lower(split("/", var.log_analytics_workspace_id)[2]), "") same_subscription = local.workspace_subscription == lower(var.subscription_id) # Changes the workspace might never hear of. A deleted or rewritten export # stops the record of its own change, a rule without its read access reads # nothing, and a disabled rule does not run. native_alerts = { diagnostic-setting-deleted = { operation = "Microsoft.Insights/diagnosticSettings/delete" doc = "A diagnostic setting was deleted in the subscription, possibly the one that sends the perimeter's access log." } # 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 a lock is the step before deleting an alert. lock-deleted = { operation = "Microsoft.Authorization/locks/delete" doc = "A management lock was deleted in the subscription, possibly one that keeps the perimeter's alerts from being deleted." } # Only the rule's own assignment, named in the alert below. rule-access-removed = { operation = "Microsoft.Authorization/roleAssignments/delete" doc = "The role assignment that lets the refusal alert read the workspace was deleted. The rule now reads nothing, and Azure disables it after a week." } } native = { for k, v in local.native_alerts : k => v if k != "rule-access-removed" || local.same_subscription } # The access log's categories, as Microsoft names them. Microsoft documents # no category group for perimeters, so each is named. access_categories = [ "NspPublicInboundPerimeterRulesAllowed", "NspPublicInboundPerimeterRulesDenied", "NspPublicOutboundPerimeterRulesAllowed", "NspPublicOutboundPerimeterRulesDenied", "NspOutboundAttempt", "NspIntraPerimeterInboundAllowed", "NspPublicInboundResourceRulesAllowed", "NspPublicInboundResourceRulesDenied", "NspPublicOutboundResourceRulesAllowed", "NspPublicOutboundResourceRulesDenied", "NspPrivateInboundAllowed", ] # The allowed subscriptions in the form the perimeter takes. allowed_subscriptions = [for s in var.allowed_subscription_ids : "/subscriptions/${trimprefix(s, "/subscriptions/")}"] # Where the policies go: a management group covers every subscription # beneath it; otherwise this template's own subscription. policy_mg = var.policy_management_group_id != "" policy_sub = !local.policy_mg && var.assign_policies_to_subscription # Built-in policies, by the IDs Azure gives them, each set to Deny. builtin_policies = { cross-tenant-replication = "92a89a79-6c52-4a7e-a03f-61306fc49312" # Storage accounts should prevent cross tenant object replication shared-key-access = "8c6a50c6-9ffd-4ae7-986f-5fa6111f9a54" # Storage accounts should prevent shared key access public-network-access = "b2982f36-99f2-4db5-8eff-283140c09693" # Storage accounts should disable public network access public-blob-access = "4fa4b6c0-31ca-4c0d-b10d-24b96f62a751" # Storage account public access should be disallowed key-vault-firewall = "55615ac9-af46-4a59-874e-391cc3dfb490" # Azure Key Vault should have firewall enabled or public network access disabled } # An account must refuse copies whose source is outside the tenant: copy # scope AAD, or the stricter PrivateLink. Azure has no built-in for this. copy_scope_rule = jsonencode({ "if" = { allOf = [ { field = "type", equals = "Microsoft.Storage/storageAccounts" }, { anyOf = [ { field = "Microsoft.Storage/storageAccounts/allowedCopyScope", exists = "false" }, { field = "Microsoft.Storage/storageAccounts/allowedCopyScope", notIn = ["AAD", "PrivateLink"] }, ] }, ] } "then" = { effect = "Deny" } }) } data "azurerm_resource_group" "perimeter" { name = var.resource_group_name } resource "azurerm_network_security_perimeter" "data" { name = "nsp-data" location = data.azurerm_resource_group.perimeter.location resource_group_name = data.azurerm_resource_group.perimeter.name tags = local.tags } resource "azurerm_network_security_perimeter_profile" "data" { name = "default" network_security_perimeter_id = azurerm_network_security_perimeter.data.id } resource "azurerm_network_security_perimeter_access_rule" "networks" { count = length(var.allowed_ip_ranges) > 0 ? 1 : 0 name = "allowed-networks" network_security_perimeter_profile_id = azurerm_network_security_perimeter_profile.data.id direction = "Inbound" address_prefixes = var.allowed_ip_ranges } resource "azurerm_network_security_perimeter_access_rule" "subscriptions" { count = length(local.allowed_subscriptions) > 0 ? 1 : 0 name = "allowed-subscriptions" network_security_perimeter_profile_id = azurerm_network_security_perimeter_profile.data.id direction = "Inbound" subscription_ids = local.allowed_subscriptions } # No outbound rule: nothing inside may send to a name outside. resource "azurerm_network_security_perimeter_association" "protected" { for_each = toset(var.protected_resource_ids) name = "${substr(replace(lower(element(split("/", each.value), 8)), "/[^a-z0-9-]/", "-"), 0, 50)}-${substr(sha1(lower(each.value)), 0, 8)}" network_security_perimeter_profile_id = azurerm_network_security_perimeter_profile.data.id resource_id = each.value access_mode = var.access_mode } # The access log: what the perimeter allowed and refused, and in Learning # mode what each resource's own firewall decided in its place. resource "azurerm_monitor_diagnostic_setting" "access" { count = local.log ? 1 : 0 name = "perimeter-access" target_resource_id = azurerm_network_security_perimeter.data.id log_analytics_workspace_id = var.log_analytics_workspace_id dynamic "enabled_log" { for_each = local.access_categories content { category = enabled_log.value } } } resource "azurerm_monitor_action_group" "perimeter" { count = local.alert ? 1 : 0 name = "data-perimeter-alerts" resource_group_name = data.azurerm_resource_group.perimeter.name short_name = "perimeter" tags = local.tags # Receiver names are numbered, because names must differ regardless of # case and two addresses can reduce to the same name. 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. resource "azurerm_monitor_activity_log_alert" "native" { for_each = local.alert ? local.native : {} name = "data-perimeter-${each.key}" resource_group_name = data.azurerm_resource_group.perimeter.name location = "global" scopes = ["/subscriptions/${var.subscription_id}"] tags = local.tags description = each.value.doc # Microsoft records a role assignment in the activity log under its full # ID, scope included, which is what resource_ids matches. The verify output # shows how to test it. criteria { category = "Administrative" operation_name = each.value.operation resource_ids = each.key == "rule-access-removed" ? azurerm_role_assignment.rule_reads_workspace[*].id : null } action { action_group_id = azurerm_monitor_action_group.perimeter[0].id } } # Every refusal within about ten minutes, once the access log is flowing. In # Learning mode, also every request a resource's own firewall let through # after the perimeter's rules did not: those are what Enforced will refuse. # One alert per protected resource, so a noisy account does not hide a # second one. The rule is made after the export, though an export can still # take up to 90 minutes to start delivering. resource "azurerm_monitor_scheduled_query_rules_alert_v2" "refused" { count = local.alert ? 1 : 0 name = "data-perimeter-refused" resource_group_name = data.azurerm_resource_group.perimeter.name location = data.azurerm_resource_group.perimeter.location scopes = [var.log_analytics_workspace_id] description = "A request crossed the data perimeter and was refused, or in Learning mode passed only on a resource's own firewall. The NSPAccessLogs table has the source, the destination, and the rule." severity = 1 evaluation_frequency = "PT5M" window_duration = "PT1H" skip_query_validation = true tags = local.tags identity { type = "SystemAssigned" } criteria { query = "NSPAccessLogs | where ingestion_time() > ago(5m) | where Category endswith \"Denied\" or Category in (\"NspPublicInboundResourceRulesAllowed\", \"NspPublicOutboundResourceRulesAllowed\") | extend Resource = coalesce(ServiceResourceId, DestinationResourceId, SourceResourceId)" time_aggregation_method = "Count" operator = "GreaterThan" threshold = 0 dimension { name = "Resource" operator = "Include" values = ["*"] } } action { action_groups = [azurerm_monitor_action_group.perimeter[0].id] } depends_on = [azurerm_monitor_diagnostic_setting.access] } resource "azurerm_role_assignment" "rule_reads_workspace" { count = local.alert ? 1 : 0 scope = var.log_analytics_workspace_id role_definition_name = "Log Analytics Reader" principal_id = azurerm_monitor_scheduled_query_rules_alert_v2.refused[0].identity[0].principal_id principal_type = "ServicePrincipal" } # The action group and every alert cannot be deleted while these locks are # on. Each lock sits on one resource, so the rest of the resource group is # untouched. Deleting a lock first fires data-perimeter-lock-deleted. A # cannot-delete lock does not stop an edit, such as disabling the action # group or an alert. # # The locks are made last and removed first, so terraform destroy can run. # Any change that replaces or removes a locked resource fails while its lock # is on. resource "azurerm_management_lock" "alerts" { for_each = local.alert ? merge({ action-group = azurerm_monitor_action_group.perimeter[0].id, refused = azurerm_monitor_scheduled_query_rules_alert_v2.refused[0].id }, { for k, a in azurerm_monitor_activity_log_alert.native : k => a.id }) : {} name = "data-perimeter-${each.key}" scope = each.value lock_level = "CanNotDelete" notes = "Keeps the perimeter's alerts from being deleted. Managed by Terraform." } # Azure Policy: the storage settings that would carry data around the # perimeter, refused at creation and at every update. A definition can only # be assigned at or below the scope it is made in, so both live together: # on a management group, or on this template's own subscription. # On a management group. resource "azurerm_policy_definition" "copy_scope_mg" { count = local.policy_mg ? 1 : 0 name = "deny-storage-copy-from-outside-tenant" display_name = "Storage accounts should refuse copies from outside the tenant" description = "Requires allowedCopyScope to be AAD or PrivateLink, so the account accepts server-side copies only from accounts in the same tenant." policy_type = "Custom" mode = "Indexed" management_group_id = var.policy_management_group_id policy_rule = local.copy_scope_rule } resource "azurerm_management_group_policy_set_definition" "perimeter" { count = local.policy_mg ? 1 : 0 name = "data-perimeter" display_name = "Data perimeter: storage settings that keep data inside" policy_type = "Custom" management_group_id = var.policy_management_group_id dynamic "policy_definition_reference" { for_each = local.builtin_policies content { reference_id = policy_definition_reference.key policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/${policy_definition_reference.value}" parameter_values = jsonencode({ effect = { value = "Deny" } }) } } policy_definition_reference { reference_id = "copy-scope" policy_definition_id = azurerm_policy_definition.copy_scope_mg[0].id } } resource "azurerm_management_group_policy_assignment" "perimeter" { count = local.policy_mg ? 1 : 0 name = "data-perimeter" display_name = "Data perimeter" management_group_id = var.policy_management_group_id policy_definition_id = azurerm_management_group_policy_set_definition.perimeter[0].id enforce = true } # On this template's subscription. resource "azurerm_policy_definition" "copy_scope" { count = local.policy_sub ? 1 : 0 name = "deny-storage-copy-from-outside-tenant" display_name = "Storage accounts should refuse copies from outside the tenant" description = "Requires allowedCopyScope to be AAD or PrivateLink, so the account accepts server-side copies only from accounts in the same tenant." policy_type = "Custom" mode = "Indexed" policy_rule = local.copy_scope_rule } resource "azurerm_policy_set_definition" "perimeter" { count = local.policy_sub ? 1 : 0 name = "data-perimeter" display_name = "Data perimeter: storage settings that keep data inside" policy_type = "Custom" dynamic "policy_definition_reference" { for_each = local.builtin_policies content { reference_id = policy_definition_reference.key policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/${policy_definition_reference.value}" parameter_values = jsonencode({ effect = { value = "Deny" } }) } } policy_definition_reference { reference_id = "copy-scope" policy_definition_id = azurerm_policy_definition.copy_scope[0].id } } resource "azurerm_subscription_policy_assignment" "perimeter" { count = local.policy_sub ? 1 : 0 name = "data-perimeter" display_name = "Data perimeter" subscription_id = "/subscriptions/${var.subscription_id}" policy_definition_id = azurerm_policy_set_definition.perimeter[0].id enforce = true }