# siem-ingest / azure / t0 "Quick start"
#
# The subscription's Activity Log streamed to an Event Hub, with a consumer
# group and a listen-only key for your SIEM's Azure input to read from, such
# as the Splunk Add-on for Microsoft Cloud Services.
#
# Run:
#   az login
#   export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
#   terraform init && terraform apply
#   terraform output -raw siem_connection_string   # give this to the SIEM input
#
# Verify: change a tag on any resource. The event reaches the hub within a
# few minutes, and the namespace's incoming messages chart rises:
#   az monitor metrics list --resource <namespace id> --metric IncomingMessages --interval PT5M

locals {
  location = "eastus"
  tags = {
    owner               = "security-engineering"
    environment         = "prod"
    data_classification = "confidential"
    managed_by          = "terraform"
  }
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 5.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.6"
    }
  }
}

provider "azurerm" {
  features {}
}

data "azurerm_subscription" "current" {}

resource "random_string" "suffix" {
  length  = 6
  special = false
  upper   = false
}

resource "azurerm_resource_group" "siem" {
  name     = "rg-siem-ingest"
  location = local.location
  tags     = local.tags
}

resource "azurerm_eventhub_namespace" "siem" {
  name                = "siem-ingest-${random_string.suffix.result}"
  location            = azurerm_resource_group.siem.location
  resource_group_name = azurerm_resource_group.siem.name
  sku                 = "Standard"
  capacity            = 1
  minimum_tls_version = "1.2"
  tags                = local.tags
}

# Events wait here for up to seven days, so a SIEM outage of less than that
# loses nothing.
resource "azurerm_eventhub" "security" {
  name              = "security-logs"
  namespace_id      = azurerm_eventhub_namespace.siem.id
  partition_count   = 4
  message_retention = 7
}

resource "azurerm_eventhub_consumer_group" "siem" {
  name                = "siem"
  namespace_name      = azurerm_eventhub_namespace.siem.name
  eventhub_name       = azurerm_eventhub.security.name
  resource_group_name = azurerm_resource_group.siem.name
}

# Azure Monitor streams with a namespace key that can manage, send, and
# listen; it will not accept less.
resource "azurerm_eventhub_namespace_authorization_rule" "diagnostics" {
  name                = "azure-monitor"
  namespace_name      = azurerm_eventhub_namespace.siem.name
  resource_group_name = azurerm_resource_group.siem.name
  listen              = true
  send                = true
  manage              = true
}

# The SIEM only reads, from this one hub.
resource "azurerm_eventhub_authorization_rule" "siem" {
  name                = "siem-reader"
  namespace_name      = azurerm_eventhub_namespace.siem.name
  eventhub_name       = azurerm_eventhub.security.name
  resource_group_name = azurerm_resource_group.siem.name
  listen              = true
  send                = false
  manage              = false
}

resource "azurerm_monitor_diagnostic_setting" "activity_log" {
  name                           = "to-siem"
  target_resource_id             = data.azurerm_subscription.current.id
  eventhub_name                  = azurerm_eventhub.security.name
  eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.diagnostics.id

  dynamic "enabled_log" {
    for_each = ["Administrative", "Security", "Policy", "Alert", "ServiceHealth"]
    content {
      category = enabled_log.value
    }
  }
}

output "siem_connection_string" {
  description = "Listen-only connection string for the SIEM input, with consumer group siem."
  value       = azurerm_eventhub_authorization_rule.siem.primary_connection_string
  sensitive   = true
}
