# audit-log-immutable / azure / t1 "Standard" # # Subscription Activity Log to an immutable blob container, optionally locked, # with the storage account closed to the public network by default and a Log # Analytics workspace for alerting. data "azurerm_subscription" "current" {} data "azurerm_client_config" "current" {} locals { storage_name = substr(replace(lower(var.name), "/[^a-z0-9]/", ""), 0, 24) tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) } resource "azurerm_resource_group" "audit" { name = "rg-${var.name}" location = var.location tags = local.tags } resource "azurerm_storage_account" "audit" { name = local.storage_name resource_group_name = azurerm_resource_group.audit.name location = azurerm_resource_group.audit.location account_tier = "Standard" account_replication_type = "GRS" account_kind = "StorageV2" min_tls_version = "TLS1_2" https_traffic_only_enabled = true allow_nested_items_to_be_public = false # Key-based auth is the usual path to a log store an attacker can read. shared_access_key_enabled = false # Infrastructure encryption adds a second encryption pass at the platform # layer. It can only be set at creation. infrastructure_encryption_enabled = true tags = local.tags identity { type = "UserAssigned" identity_ids = [azurerm_user_assigned_identity.storage.id] } # versionless_id means the storage account follows the key as it rotates. customer_managed_key { key_vault_key_id = azurerm_key_vault_key.audit.versionless_id user_assigned_identity_id = azurerm_user_assigned_identity.storage.id } depends_on = [azurerm_role_assignment.storage_key_user] blob_properties { versioning_enabled = true change_feed_enabled = true delete_retention_policy { days = 30 } } network_rules { default_action = "Deny" bypass = ["AzureServices", "Logging", "Metrics"] ip_rules = var.allowed_ip_ranges } } # Azure creates this container on first delivery. Declaring it up front is what # lets the immutability policy attach before the first blob lands. resource "azurerm_storage_container" "activity" { name = "insights-activity-logs" storage_account_id = azurerm_storage_account.audit.id container_access_type = "private" } # Azure Monitor appends to one blob per hour. protected_append_writes_all_enabled # is what lets those appends through a policy that forbids every other change. resource "azurerm_storage_container_immutability_policy" "activity" { storage_container_resource_manager_id = azurerm_storage_container.activity.id immutability_period_in_days = var.retention_days protected_append_writes_all_enabled = true locked = var.lock_policy } resource "azurerm_log_analytics_workspace" "audit" { count = var.log_analytics ? 1 : 0 name = "law-${var.name}" resource_group_name = azurerm_resource_group.audit.name location = azurerm_resource_group.audit.location sku = "PerGB2018" retention_in_days = var.log_analytics_retention_days tags = local.tags } resource "azurerm_monitor_diagnostic_setting" "activity" { name = "activity-to-immutable-blob" target_resource_id = data.azurerm_subscription.current.id storage_account_id = azurerm_storage_account.audit.id log_analytics_workspace_id = var.log_analytics ? azurerm_log_analytics_workspace.audit[0].id : null dynamic "enabled_log" { for_each = var.log_categories content { category = enabled_log.value } } }