# data-perimeter / azure / t0 "Quick start"
#
# A network security perimeter around one storage account. Inside it, the
# account answers only the networks you name, whatever a token or key would
# otherwise allow. With no outbound rule it sends nothing to a name outside.
# Stolen credentials still work, but only from inside.
#
# Azure has no tenant-wide rule that says "our data stays with our
# identities", the way AWS and Google Cloud do. The perimeter is the nearest
# thing: a boundary drawn around the resources that contain the data.
#
# A perimeter also refuses you when you work from outside it, including in
# the portal. The one rule below lets the networks named here in.
#
# Change the five values below, then:
#   az login
#   terraform init && terraform apply
#
# Verify, from an address outside allowed_ip_ranges:
#   az storage blob list --account-name <account> --container-name <container> --auth-mode login
#   Expect: refused, with an authorization failure. From an allowed address it
#   lists the blobs.

locals {
  subscription_id        = "00000000-0000-0000-0000-000000000000" # change me
  resource_group_name    = "rg-data"                              # change me: contains the perimeter
  storage_account_name   = "contosodata"                          # change me: the account to protect
  storage_resource_group = "rg-data"                              # change me: its resource group
  allowed_ip_ranges      = ["203.0.113.0/24"]                     # change me: your office or VPN egress, as CIDR
}

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

provider "azurerm" {
  features {}
  subscription_id = local.subscription_id
}

data "azurerm_resource_group" "perimeter" {
  name = local.resource_group_name
}

data "azurerm_storage_account" "protected" {
  name                = local.storage_account_name
  resource_group_name = local.storage_resource_group
}

resource "azurerm_network_security_perimeter" "data" {
  name                = "nsp-data"
  location            = data.azurerm_resource_group.perimeter.location
  resource_group_name = data.azurerm_resource_group.perimeter.name
}

# A profile contains the rules. Resources join the perimeter through it.
resource "azurerm_network_security_perimeter_profile" "data" {
  name                          = "default"
  network_security_perimeter_id = azurerm_network_security_perimeter.data.id
}

# The one way in. With no outbound rule, nothing inside may send to a name
# outside.
resource "azurerm_network_security_perimeter_access_rule" "office" {
  name                                  = "office"
  network_security_perimeter_profile_id = azurerm_network_security_perimeter_profile.data.id
  direction                             = "Inbound"
  address_prefixes                      = local.allowed_ip_ranges
}

# Enforced from the start. The perimeter's rules then override the account's
# own firewall, and Azure's trusted-service exceptions are not honoured, so
# anything that reaches the account from outside stops here.
resource "azurerm_network_security_perimeter_association" "storage" {
  name                                  = local.storage_account_name
  network_security_perimeter_profile_id = azurerm_network_security_perimeter_profile.data.id
  resource_id                           = data.azurerm_storage_account.protected.id
  access_mode                           = "Enforced"
}

output "verify" {
  description = "How to prove the perimeter works."
  value       = <<-EOT
    From outside ${join(", ", local.allowed_ip_ranges)}:
      az storage blob list --account-name ${local.storage_account_name} --container-name <container> --auth-mode login
      Expect an authorization failure. The same command from inside lists the blobs.
    From anywhere, with a SAS or a key for the account, a server-side copy into an account you do not control:
      azcopy copy "https://${local.storage_account_name}.blob.core.windows.net/<container>/<blob>?<sas>" "https://<other account>.blob.core.windows.net/<container>/<blob>?<its sas>"
      Expect the read of the source refused.
  EOT
}
