# compliance-evaluation / azure / t1 "Standard" # # The Microsoft cloud security benchmark assigned at a management group, an # email on every non-compliant change in the subscriptions you name, and an # optional policy that repairs one finding by itself: storage accounts that # allow public blob access. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) # Storage Account Contributor, the least role that can change the setting. storage_account_contributor = "/providers/Microsoft.Authorization/roleDefinitions/17d1049b-9a84-46fb-8f53-869881c3d3ab" } data "azurerm_policy_set_definition" "benchmark" { display_name = "Microsoft cloud security benchmark" } resource "azurerm_management_group_policy_assignment" "benchmark" { name = "security-benchmark" display_name = "Microsoft cloud security benchmark" management_group_id = var.management_group_id policy_definition_id = data.azurerm_policy_set_definition.benchmark.id location = var.location identity { type = "SystemAssigned" } } # --------------------------------------------------------------------------- # Automatic repair: public blob access switched off wherever it is on # --------------------------------------------------------------------------- resource "azurerm_policy_definition" "repair_public_blob" { count = var.auto_repair_public_blob_access ? 1 : 0 name = "repair-public-blob-access" display_name = "Switch public blob access off on storage accounts" policy_type = "Custom" mode = "Indexed" management_group_id = var.management_group_id metadata = jsonencode({ category = "Storage" }) policy_rule = jsonencode({ "if" = { allOf = [ { field = "type", equals = "Microsoft.Storage/storageAccounts" }, { field = "Microsoft.Storage/storageAccounts/allowBlobPublicAccess", notEquals = "false" }, ] } "then" = { effect = "modify" details = { roleDefinitionIds = [local.storage_account_contributor] operations = [{ operation = "addOrReplace" field = "Microsoft.Storage/storageAccounts/allowBlobPublicAccess" value = false }] } } }) } resource "azurerm_management_group_policy_assignment" "repair_public_blob" { count = var.auto_repair_public_blob_access ? 1 : 0 name = "repair-public-blob" display_name = "Switch public blob access off" management_group_id = var.management_group_id policy_definition_id = azurerm_policy_definition.repair_public_blob[0].id location = var.location identity { type = "SystemAssigned" } } # The assignment's identity makes the change, so it needs the role, at the # same scope as the assignment. resource "azurerm_role_assignment" "repair_public_blob" { count = var.auto_repair_public_blob_access ? 1 : 0 scope = var.management_group_id role_definition_id = local.storage_account_contributor principal_id = azurerm_management_group_policy_assignment.repair_public_blob[0].identity[0].principal_id } # New and changed accounts are repaired as they are written. Accounts that # already exist are repaired by a remediation task, which only finds them # after the first evaluation, so it is a step after apply. See the verify # output. # --------------------------------------------------------------------------- # Alerts # --------------------------------------------------------------------------- resource "azurerm_resource_group" "alerts" { name = "rg-compliance-alerts" location = var.location tags = local.tags } resource "azurerm_monitor_action_group" "security" { name = "compliance-alerts" resource_group_name = azurerm_resource_group.alerts.name short_name = "compliance" tags = local.tags dynamic "email_receiver" { for_each = toset(var.alert_emails) content { name = email_receiver.value email_address = email_receiver.value } } } resource "azurerm_monitor_activity_log_alert" "noncompliant" { for_each = toset(var.alert_subscription_ids) name = "noncompliant-resource-${each.value}" resource_group_name = azurerm_resource_group.alerts.name location = "global" scopes = ["/subscriptions/${each.value}"] description = "A resource was created or changed into a state the security benchmark flags." tags = local.tags criteria { category = "Policy" operation_name = "Microsoft.Authorization/policies/audit/action" } action { action_group_id = azurerm_monitor_action_group.security.id } }