# network-flow-logs / azure / t0 "Quick start"
#
# Flow logs for one virtual network, kept 90 days in a storage account, with
# traffic analytics summarising them in a Log Analytics workspace every ten
# minutes.
#
# Change the network below, then:
#   az login
#   export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
#   terraform init && terraform apply
#
# Verify: flow records reach the storage account within about ten minutes
# of traffic, and traffic analytics results within about an hour:
#   NTANetAnalytics | take 10
#
# Network Watcher is created by Azure the first time a network is made in a
# region, in NetworkWatcherRG. If yours was removed, create it first.

locals {
  vnet_id  = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-network/providers/Microsoft.Network/virtualNetworks/vnet-main" # change me
  location = "eastus"                                                                                                                              # change me: the network's region
  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 {}
}

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

data "azurerm_network_watcher" "region" {
  name                = "NetworkWatcher_${local.location}"
  resource_group_name = "NetworkWatcherRG"
}

resource "azurerm_resource_group" "flow" {
  name     = "rg-flow-logs"
  location = local.location
  tags     = local.tags
}

resource "azurerm_storage_account" "flow" {
  name                            = "flowlogs${random_string.suffix.result}"
  resource_group_name             = azurerm_resource_group.flow.name
  location                        = azurerm_resource_group.flow.location
  account_tier                    = "Standard"
  account_replication_type        = "LRS"
  min_tls_version                 = "TLS1_2"
  allow_nested_items_to_be_public = false
  tags                            = local.tags

  # Left on for Network Watcher; see the scan decision on CKV2_AZURE_40.
  # Nothing here hands out SAS tokens. One made to last longer than a day is
  # logged as a policy violation.
  shared_access_key_enabled = true
  sas_policy {
    expiration_period = "01.00:00:00"
  }
}

resource "azurerm_log_analytics_workspace" "flow" {
  name                = "law-flow-${random_string.suffix.result}"
  location            = azurerm_resource_group.flow.location
  resource_group_name = azurerm_resource_group.flow.name
  sku                 = "PerGB2018"
  retention_in_days   = 90
  tags                = local.tags
}

# Virtual network flow logs cover every subnet and NIC in the network,
# including traffic that no security group rule matched.
resource "azurerm_network_watcher_flow_log" "vnet" {
  name                 = "flow-${element(split("/", local.vnet_id), 8)}"
  network_watcher_name = data.azurerm_network_watcher.region.name
  resource_group_name  = data.azurerm_network_watcher.region.resource_group_name
  location             = local.location
  target_resource_id   = local.vnet_id
  storage_account_id   = azurerm_storage_account.flow.id
  enabled              = true
  version              = 2
  tags                 = local.tags

  retention_policy {
    enabled = true
    days    = 90
  }

  traffic_analytics {
    enabled               = true
    workspace_id          = azurerm_log_analytics_workspace.flow.workspace_id
    workspace_region      = azurerm_log_analytics_workspace.flow.location
    workspace_resource_id = azurerm_log_analytics_workspace.flow.id
    interval_in_minutes   = 10
  }
}
