# brokered-shell-access / azure / t1 "Standard" # # Azure Bastion Premium with session recording, the network rules Bastion # needs and no others, copy and paste and file transfer switched off, an # operator group that can connect and do nothing else, and a NAT gateway so # machines can reach Entra ID for sign-in. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) bastion_prefix = cidrsubnet(var.address_space, 10, 0) workload_prefix = cidrsubnet(var.address_space, 8, 1) # The rules Microsoft documents for AzureBastionSubnet. Bastion does not work # in a subnet whose group is missing one. Anything else falls to # the group's default rules. bastion_rules = { in-https-internet = { priority = 120, direction = "Inbound", ports = ["443"], source = "Internet", destination = "*" } in-gateway-manager = { priority = 130, direction = "Inbound", ports = ["443"], source = "GatewayManager", destination = "*" } in-load-balancer = { priority = 140, direction = "Inbound", ports = ["443"], source = "AzureLoadBalancer", destination = "*" } in-bastion-host-traffic = { priority = 150, direction = "Inbound", ports = ["8080", "5701"], source = "VirtualNetwork", destination = "VirtualNetwork" } out-ssh-rdp = { priority = 100, direction = "Outbound", ports = ["22", "3389"], source = "*", destination = "VirtualNetwork" } out-azure-cloud = { priority = 110, direction = "Outbound", ports = ["443"], source = "*", destination = "AzureCloud" } out-bastion-host = { priority = 120, direction = "Outbound", ports = ["8080", "5701"], source = "VirtualNetwork", destination = "VirtualNetwork" } out-session-info = { priority = 130, direction = "Outbound", ports = ["80"], source = "*", destination = "Internet" } } } resource "azurerm_resource_group" "bastion" { name = "rg-bastion" location = var.location tags = local.tags } resource "azurerm_resource_group" "workloads" { name = "rg-bastion-workloads" location = var.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 = [var.address_space] tags = local.tags } resource "azurerm_subnet" "bastion" { name = "AzureBastionSubnet" resource_group_name = azurerm_resource_group.bastion.name virtual_network_name = azurerm_virtual_network.main.name address_prefixes = [local.bastion_prefix] } resource "azurerm_subnet" "workloads" { name = "workloads" resource_group_name = azurerm_resource_group.bastion.name virtual_network_name = azurerm_virtual_network.main.name address_prefixes = [local.workload_prefix] default_outbound_access_enabled = false } # Entra ID sign-in needs each machine to reach Entra ID and Microsoft's package # servers over HTTPS. A NAT gateway gives the workloads that, outbound only, # from one fixed address. resource "azurerm_public_ip" "nat" { name = "pip-workloads-nat" location = azurerm_resource_group.bastion.location resource_group_name = azurerm_resource_group.bastion.name allocation_method = "Static" sku = "Standard" tags = local.tags } resource "azurerm_nat_gateway" "workloads" { name = "nat-workloads" location = azurerm_resource_group.bastion.location resource_group_name = azurerm_resource_group.bastion.name sku_name = "Standard" tags = local.tags } resource "azurerm_nat_gateway_public_ip_association" "workloads" { nat_gateway_id = azurerm_nat_gateway.workloads.id public_ip_address_id = azurerm_public_ip.nat.id } resource "azurerm_subnet_nat_gateway_association" "workloads" { subnet_id = azurerm_subnet.workloads.id nat_gateway_id = azurerm_nat_gateway.workloads.id } # --------------------------------------------------------------------------- # Network rules # --------------------------------------------------------------------------- resource "azurerm_network_security_group" "bastion" { name = "nsg-bastion" location = azurerm_resource_group.bastion.location resource_group_name = azurerm_resource_group.bastion.name tags = local.tags } resource "azurerm_network_security_rule" "bastion" { for_each = local.bastion_rules name = each.key network_security_group_name = azurerm_network_security_group.bastion.name resource_group_name = azurerm_resource_group.bastion.name priority = each.value.priority direction = each.value.direction access = "Allow" protocol = "*" source_port_range = "*" destination_port_ranges = each.value.ports source_address_prefix = each.value.source destination_address_prefix = each.value.destination description = "Required by Azure Bastion." } resource "azurerm_subnet_network_security_group_association" "bastion" { subnet_id = azurerm_subnet.bastion.id network_security_group_id = azurerm_network_security_group.bastion.id depends_on = [azurerm_network_security_rule.bastion] } # Workloads accept SSH and RDP from the Bastion subnet only. resource "azurerm_network_security_group" "workloads" { name = "nsg-workloads" location = azurerm_resource_group.bastion.location resource_group_name = azurerm_resource_group.bastion.name tags = local.tags } resource "azurerm_network_security_rule" "from_bastion" { name = "in-ssh-rdp-from-bastion" network_security_group_name = azurerm_network_security_group.workloads.name resource_group_name = azurerm_resource_group.bastion.name priority = 100 direction = "Inbound" access = "Allow" protocol = "Tcp" source_port_range = "*" destination_port_ranges = ["22", "3389"] source_address_prefix = local.bastion_prefix destination_address_prefix = "*" } resource "azurerm_network_security_rule" "no_other_admin" { name = "in-deny-other-ssh-rdp" network_security_group_name = azurerm_network_security_group.workloads.name resource_group_name = azurerm_resource_group.bastion.name priority = 110 direction = "Inbound" access = "Deny" protocol = "Tcp" source_port_range = "*" destination_port_ranges = ["22", "3389"] source_address_prefix = "*" destination_address_prefix = "*" } resource "azurerm_subnet_network_security_group_association" "workloads" { subnet_id = azurerm_subnet.workloads.id network_security_group_id = azurerm_network_security_group.workloads.id } # --------------------------------------------------------------------------- # Bastion # --------------------------------------------------------------------------- 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 } # Copy and paste and file transfer are the easy ways data leaves through a # session, so both are off. Turn one on for a reason, in a pull request. resource "azurerm_bastion_host" "main" { name = "bastion" location = azurerm_resource_group.bastion.location resource_group_name = azurerm_resource_group.bastion.name sku = var.session_recording ? "Premium" : "Standard" tunneling_enabled = true shareable_link_enabled = false ip_connect_enabled = false copy_paste_enabled = false file_copy_enabled = false session_recording_enabled = var.session_recording tags = local.tags ip_configuration { name = "bastion" subnet_id = azurerm_subnet.bastion.id public_ip_address_id = azurerm_public_ip.bastion.id } depends_on = [azurerm_subnet_network_security_group_association.bastion] } 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 = var.log_retention_days 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" } } # --------------------------------------------------------------------------- # Who may connect # --------------------------------------------------------------------------- resource "azurerm_role_assignment" "operators_bastion" { scope = azurerm_bastion_host.main.id role_definition_name = "Reader" principal_id = var.operator_group_object_id } resource "azurerm_role_assignment" "operators_vms" { scope = azurerm_resource_group.workloads.id role_definition_name = "Virtual Machine User Login" principal_id = var.operator_group_object_id }