# guardrail-baseline / azure / t1 "Standard" # # Six Azure Policy rules in one initiative, defined and assigned at a # management group so every subscription below it inherits them. Starts in # audit mode; enforce once the compliance results show what would be refused. locals { effect = var.enforce ? "Deny" : "Audit" rules = { deny-public-blob-access = { name = "Storage accounts must not allow public blob access" "if" = { allOf = [ { field = "type", equals = "Microsoft.Storage/storageAccounts" }, { field = "Microsoft.Storage/storageAccounts/allowBlobPublicAccess", equals = "true" }, ] } } require-storage-tls12 = { name = "Storage accounts must require TLS 1.2 or later" "if" = { allOf = [ { field = "type", equals = "Microsoft.Storage/storageAccounts" }, { field = "Microsoft.Storage/storageAccounts/minimumTlsVersion", notIn = ["TLS1_2", "TLS1_3"] }, ] } } deny-nic-public-ip = { name = "Network interfaces must not have a public IP address" "if" = { allOf = [ { field = "type", equals = "Microsoft.Network/networkInterfaces" }, { count = { field = "Microsoft.Network/networkInterfaces/ipconfigurations[*]" where = { field = "Microsoft.Network/networkInterfaces/ipconfigurations[*].publicIpAddress.id", exists = "true" } } greater = 0 }, ] } } require-keyvault-purge-protection = { name = "Key vaults must have purge protection" "if" = { allOf = [ { field = "type", equals = "Microsoft.KeyVault/vaults" }, { field = "Microsoft.KeyVault/vaults/enablePurgeProtection", notEquals = "true" }, ] } } # Access policies grant vault-wide rights that nothing else records well. # Role-based access is logged and scoped like everything else. require-keyvault-rbac = { name = "Key vaults must use role-based access" "if" = { allOf = [ { field = "type", equals = "Microsoft.KeyVault/vaults" }, { field = "Microsoft.KeyVault/vaults/enableRbacAuthorization", notEquals = "true" }, ] } } allowed-locations = { name = "Resources must be in an allowed region" "if" = { allOf = [ { field = "location", notIn = concat(var.allowed_locations, ["global"]) }, { field = "type", notEquals = "Microsoft.Resources/deployments" }, ] } } } } resource "azurerm_policy_definition" "guardrail" { for_each = local.rules name = each.key display_name = each.value.name policy_type = "Custom" mode = each.key == "allowed-locations" ? "Indexed" : "All" management_group_id = var.management_group_id metadata = jsonencode({ category = "Guardrails" }) parameters = jsonencode({ effect = { type = "String" allowedValues = ["Deny", "Audit", "Disabled"] defaultValue = "Deny" } }) policy_rule = jsonencode({ "if" = each.value.if "then" = { effect = "[parameters('effect')]" } }) } resource "azurerm_management_group_policy_set_definition" "guardrails" { name = "guardrail-baseline" display_name = "Guardrail baseline" policy_type = "Custom" management_group_id = var.management_group_id parameters = jsonencode({ effect = { type = "String" allowedValues = ["Deny", "Audit", "Disabled"] defaultValue = "Audit" } }) dynamic "policy_definition_reference" { for_each = azurerm_policy_definition.guardrail content { policy_definition_id = policy_definition_reference.value.id reference_id = policy_definition_reference.key parameter_values = jsonencode({ effect = { value = "[parameters('effect')]" } }) } } } resource "azurerm_management_group_policy_assignment" "guardrails" { name = "guardrail-baseline" display_name = "Guardrail baseline (${local.effect})" management_group_id = var.management_group_id policy_definition_id = azurerm_management_group_policy_set_definition.guardrails.id parameters = jsonencode({ effect = { value = local.effect } }) non_compliance_message { content = "Refused by the guardrail baseline. Ask the security team if this change is needed, and for an exemption with an end date if it is." } } resource "azurerm_subscription_policy_exemption" "named" { for_each = var.exemptions name = "guardrail-baseline-exemption" display_name = "Guardrail baseline exemption" subscription_id = "/subscriptions/${each.key}" policy_assignment_id = azurerm_management_group_policy_assignment.guardrails.id exemption_category = "Waiver" description = each.value.reason expires_on = each.value.expires_on }