# brokered-shell-access / azure / t0 "Quick start"
#
# Azure Bastion in a virtual network, so machines in it are reached through
# Bastion with no public IP and no SSH or RDP port open to the internet, and
# every connection recorded in a Log Analytics workspace.
#
# Run:
#   az login
#   export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
#   terraform init && terraform apply
#
# Then put virtual machines in the workloads subnet, with no public IP, and
# connect with:
#   az network bastion ssh --name bastion --resource-group rg-bastion --target-resource-id <vm id> \
#     --auth-type ssh-key --username <user> --ssh-key <private key file>
#
# The workloads subnet has no outbound internet access, so signing in with
# Entra ID (--auth-type AAD) cannot work here. Standard adds a NAT gateway
# for it.
#
# Verify: after a session, query the workspace:
#   MicrosoftAzureBastionAuditLogs | project TimeGenerated, UserName, TargetVMIPAddress, Protocol
#   Records arrive a few minutes after the session starts.

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

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

provider "azurerm" {
  features {}
}

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

resource "azurerm_virtual_network" "main" {
  name                = "vnet-workloads"
  location            = azurerm_resource_group.bastion.location
  resource_group_name = azurerm_resource_group.bastion.name
  address_space       = ["10.60.0.0/16"]
  tags                = local.tags
}

# Bastion needs a subnet with exactly this name, /26 or larger.
resource "azurerm_subnet" "bastion" {
  name                 = "AzureBastionSubnet"
  resource_group_name  = azurerm_resource_group.bastion.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefixes     = ["10.60.0.0/26"]
}

resource "azurerm_subnet" "workloads" {
  name                            = "workloads"
  resource_group_name             = azurerm_resource_group.bastion.name
  virtual_network_name            = azurerm_virtual_network.main.name
  address_prefixes                = ["10.60.1.0/24"]
  default_outbound_access_enabled = false
}

resource "azurerm_public_ip" "bastion" {
  name                = "pip-bastion"
  location            = azurerm_resource_group.bastion.location
  resource_group_name = azurerm_resource_group.bastion.name
  allocation_method   = "Static"
  sku                 = "Standard"
  tags                = local.tags
}

# Standard adds native client support, so people use their own ssh or RDP
# client through Bastion instead of the browser.
resource "azurerm_bastion_host" "main" {
  name                   = "bastion"
  location               = azurerm_resource_group.bastion.location
  resource_group_name    = azurerm_resource_group.bastion.name
  sku                    = "Standard"
  tunneling_enabled      = true
  shareable_link_enabled = false
  ip_connect_enabled     = false
  tags                   = local.tags

  ip_configuration {
    name                 = "bastion"
    subnet_id            = azurerm_subnet.bastion.id
    public_ip_address_id = azurerm_public_ip.bastion.id
  }
}

resource "azurerm_log_analytics_workspace" "bastion" {
  name                = "law-bastion"
  location            = azurerm_resource_group.bastion.location
  resource_group_name = azurerm_resource_group.bastion.name
  sku                 = "PerGB2018"
  retention_in_days   = 365
  tags                = local.tags
}

resource "azurerm_monitor_diagnostic_setting" "bastion" {
  name                       = "sessions"
  target_resource_id         = azurerm_bastion_host.main.id
  log_analytics_workspace_id = azurerm_log_analytics_workspace.bastion.id
  enabled_log {
    category = "BastionAuditLogs"
  }
}
