# evidence-capture / azure / t1 "Standard" # # The quick start's responder, with: a record of each VM and its hash saved to # an immutable container under your own key before anything changes, a # quarantine group in every region you use, an identity of its own with the # permissions it uses and nothing more, each step published to a status topic, # and events it could not handle kept for you to look at. # # Verify, on a test VM you do not mind isolating: # terraform output verify data "azurerm_subscription" "current" {} data "azurerm_client_config" "current" {} locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) home = var.locations[0] } resource "random_string" "suffix" { length = 6 special = false upper = false } resource "azurerm_resource_group" "responder" { name = "rg-${var.name}" location = local.home tags = local.tags } # Snapshots go here, apart from the responder. Deleting the responder never # deletes evidence. resource "azurerm_resource_group" "evidence" { name = "rg-${var.name}-evidence" location = local.home tags = local.tags lifecycle { prevent_destroy = true } } # Deny everything, in and out, ahead of the default rules, in every region. resource "azurerm_network_security_group" "quarantine" { for_each = toset(var.locations) name = "nsg-incident-quarantine-${each.value}" location = each.value resource_group_name = azurerm_resource_group.responder.name tags = local.tags } resource "azurerm_network_security_rule" "quarantine" { for_each = { for pair in setproduct(var.locations, ["Inbound", "Outbound"]) : "${pair[0]}/${lower(pair[1])}" => pair } name = "deny-all-${lower(each.value[1])}" network_security_group_name = azurerm_network_security_group.quarantine[each.value[0]].name resource_group_name = azurerm_resource_group.responder.name priority = 4096 direction = each.value[1] access = "Deny" protocol = "*" source_port_range = "*" destination_port_range = "*" source_address_prefix = "*" destination_address_prefix = "*" } # Evidence: each VM's record and its hash, under your own key, unchangeable # for the retention period. resource "azurerm_storage_account" "evidence" { name = "${var.name}ev${random_string.suffix.result}" resource_group_name = azurerm_resource_group.evidence.name location = azurerm_resource_group.evidence.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 shared_access_key_enabled = false default_to_oauth_authentication = true infrastructure_encryption_enabled = true tags = local.tags identity { type = "UserAssigned" identity_ids = [azurerm_user_assigned_identity.storage.id] } customer_managed_key { key_vault_key_id = azurerm_key_vault_key.evidence.versionless_id user_assigned_identity_id = azurerm_user_assigned_identity.storage.id } blob_properties { delete_retention_policy { days = 30 } container_delete_retention_policy { days = 30 } } lifecycle { prevent_destroy = true } depends_on = [azurerm_role_assignment.storage_key_user] } resource "azurerm_storage_container" "evidence" { name = "evidence" storage_account_id = azurerm_storage_account.evidence.id container_access_type = "private" } resource "azurerm_storage_container_immutability_policy" "evidence" { storage_container_resource_manager_id = azurerm_storage_container.evidence.id immutability_period_in_days = var.evidence_retention_days locked = var.lock_retention } # Status: one event per step, on a topic of its own. Subscribe a queue, a # webhook, or a Logic App that posts to your incident channel. resource "azurerm_eventgrid_topic" "status" { name = "evgt-${var.name}-status" location = azurerm_resource_group.responder.location resource_group_name = azurerm_resource_group.responder.name input_schema = "CloudEventSchemaV1_0" local_auth_enabled = false public_network_access_enabled = true tags = local.tags } # The responder's identity. Created first, so the function can use it for its # own storage from the start. resource "azurerm_user_assigned_identity" "responder" { name = "id-${var.name}" location = azurerm_resource_group.responder.location resource_group_name = azurerm_resource_group.responder.name tags = local.tags } resource "azurerm_role_definition" "responder" { name = "Incident responder function ${random_string.suffix.result}" scope = data.azurerm_subscription.current.id assignable_scopes = [data.azurerm_subscription.current.id] description = "Preserves and isolates VMs tagged incident=. Cannot delete, stop, or run commands on them." permissions { actions = [ "Microsoft.Compute/virtualMachines/read", "Microsoft.Compute/virtualMachines/instanceView/read", "Microsoft.Compute/disks/read", "Microsoft.Compute/snapshots/read", "Microsoft.Compute/snapshots/write", "Microsoft.Network/networkInterfaces/read", "Microsoft.Network/networkInterfaces/write", "Microsoft.Network/networkSecurityGroups/read", "Microsoft.Network/networkSecurityGroups/join/action", # Writing an interface re-links what it already has, and each link is # checked again. "Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/publicIPAddresses/join/action", "Microsoft.Network/loadBalancers/backendAddressPools/join/action", "Microsoft.Network/applicationSecurityGroups/joinIpConfiguration/action", "Microsoft.Authorization/locks/read", "Microsoft.Authorization/locks/write", "Microsoft.Resources/tags/read", "Microsoft.Resources/tags/write", "Microsoft.Resources/subscriptions/resourceGroups/read", ] } } # Add evidence, and never read, change, or delete it. resource "azurerm_role_definition" "evidence_writer" { name = "Incident evidence writer ${random_string.suffix.result}" scope = azurerm_storage_container.evidence.id assignable_scopes = [azurerm_storage_container.evidence.id] description = "Adds evidence blobs. Cannot read, change, or delete them." permissions { data_actions = [ "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write", "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/add/action", ] } } resource "azurerm_role_assignment" "responder" { scope = data.azurerm_subscription.current.id role_definition_id = azurerm_role_definition.responder.role_definition_resource_id principal_id = azurerm_user_assigned_identity.responder.principal_id principal_type = "ServicePrincipal" } resource "azurerm_role_assignment" "responder_writes_evidence" { scope = azurerm_storage_container.evidence.id role_definition_id = azurerm_role_definition.evidence_writer.role_definition_resource_id principal_id = azurerm_user_assigned_identity.responder.principal_id principal_type = "ServicePrincipal" } resource "azurerm_role_assignment" "responder_sends_status" { scope = azurerm_eventgrid_topic.status.id role_definition_name = "EventGrid Data Sender" principal_id = azurerm_user_assigned_identity.responder.principal_id principal_type = "ServicePrincipal" } # The function's own storage: its package, its runtime state, and the events # it could not handle. resource "azurerm_storage_account" "responder" { name = "${var.name}fn${random_string.suffix.result}" resource_group_name = azurerm_resource_group.responder.name location = azurerm_resource_group.responder.location account_tier = "Standard" account_replication_type = "LRS" min_tls_version = "TLS1_2" https_traffic_only_enabled = true allow_nested_items_to_be_public = false tags = local.tags } resource "azurerm_storage_container" "deploy" { name = "deployments" storage_account_id = azurerm_storage_account.responder.id container_access_type = "private" } resource "azurerm_storage_container" "dead_letter" { name = "undelivered" storage_account_id = azurerm_storage_account.responder.id container_access_type = "private" } data "archive_file" "responder" { type = "zip" source_dir = "${path.module}/function" output_path = "${path.module}/.build/responder.zip" } resource "azurerm_service_plan" "responder" { name = "plan-${var.name}" resource_group_name = azurerm_resource_group.responder.name location = azurerm_resource_group.responder.location os_type = "Linux" sku_name = "FC1" tags = local.tags } resource "azurerm_function_app_flex_consumption" "responder" { name = "${var.name}-${random_string.suffix.result}" resource_group_name = azurerm_resource_group.responder.name location = azurerm_resource_group.responder.location service_plan_id = azurerm_service_plan.responder.id storage_container_type = "blobContainer" storage_container_endpoint = "${azurerm_storage_account.responder.primary_blob_endpoint}${azurerm_storage_container.deploy.name}" # The Functions host reaches its own storage with the account key. The # evidence account, where it matters, has keys off and is reached as the # responder's identity. storage_authentication_type = "StorageAccountConnectionString" storage_access_key = azurerm_storage_account.responder.primary_access_key runtime_name = "python" runtime_version = "3.12" maximum_instance_count = 5 instance_memory_in_mb = 2048 https_only = true tags = local.tags identity { type = "UserAssigned" identity_ids = [azurerm_user_assigned_identity.responder.id] } site_config { minimum_tls_version = "1.2" } app_settings = { # Which identity the code signs in as. AZURE_CLIENT_ID = azurerm_user_assigned_identity.responder.client_id QUARANTINE_NSG_IDS = jsonencode({ for l, g in azurerm_network_security_group.quarantine : lower(l) => g.id }) SNAPSHOT_RESOURCE_GROUP = azurerm_resource_group.evidence.name EVIDENCE_ACCOUNT_URL = azurerm_storage_account.evidence.primary_blob_endpoint EVIDENCE_CONTAINER = azurerm_storage_container.evidence.name STATUS_TOPIC_ENDPOINT = azurerm_eventgrid_topic.status.endpoint } } # Flex Consumption takes code as a package built by the service, which the # provider cannot send, so the Azure CLI deploys it during apply. A change to # the code deploys again. resource "terraform_data" "deploy" { triggers_replace = data.archive_file.responder.output_sha256 provisioner "local-exec" { interpreter = ["bash", "-c"] environment = { GROUP = azurerm_resource_group.responder.name APP = azurerm_function_app_flex_consumption.responder.name ZIP = data.archive_file.responder.output_path } command = "az functionapp deployment source config-zip -g \"$GROUP\" -n \"$APP\" --src \"$ZIP\" --build-remote true --only-show-errors" } } # A subscription can have one system topic. If yours already has one, set # system_topic_name to it and this creates nothing. resource "azurerm_eventgrid_system_topic" "subscription" { count = var.system_topic_name == "" ? 1 : 0 name = "evgt-${var.name}" resource_group_name = azurerm_resource_group.responder.name location = "Global" source_resource_id = data.azurerm_subscription.current.id topic_type = "Microsoft.Resources.Subscriptions" tags = local.tags } resource "azurerm_eventgrid_system_topic_event_subscription" "vm_writes" { name = "vm-writes-to-${var.name}" system_topic = var.system_topic_name == "" ? azurerm_eventgrid_system_topic.subscription[0].name : var.system_topic_name resource_group_name = azurerm_resource_group.responder.name included_event_types = ["Microsoft.Resources.ResourceWriteSuccess"] advanced_filter { string_in { key = "data.operationName" values = ["Microsoft.Compute/virtualMachines/write", "Microsoft.Resources/tags/write"] } string_contains { key = "data.resourceUri" values = ["/providers/Microsoft.Compute/virtualMachines/"] } } azure_function_endpoint { function_id = "${azurerm_function_app_flex_consumption.responder.id}/functions/preserve" } retry_policy { max_delivery_attempts = 10 event_time_to_live = 60 } # An event the function could not take, after every retry, lands here # instead of vanishing. storage_blob_dead_letter_destination { storage_account_id = azurerm_storage_account.responder.id storage_blob_container_name = azurerm_storage_container.dead_letter.name } # Event Grid fetches the function's key when the subscription is created, # so the function must be deployed first. depends_on = [terraform_data.deploy, azurerm_role_assignment.responder, azurerm_role_assignment.responder_writes_evidence, azurerm_role_assignment.responder_sends_status] }