infrastructure / security
Azure Key Vault with Terraform: RBAC-Only Secret Management for a Third-Party API Integration
Terraform • Azure Key Vault • Azure AD • RBAC • Service Principals

The problem

A couple of internal applications needed credentials for a third-party HR/finance platform's API — a client ID, a client secret, an endpoint URL, one set per application. The existing Key Vault in the subscription already held something sensitive and unrelated: SQL Transparent Data Encryption keys. It also still used the older Access Policy model instead of RBAC, with a grab bag of identities that had accumulated permissions over time.

Dropping new application secrets into that vault would have meant two unrelated trust boundaries — database encryption and third-party API credentials — sharing one blast radius. The goal was a vault scoped to exactly one purpose, with access controlled entirely through Azure RBAC, provisioned and reproducible as code from the start.

Goal: a dedicated, RBAC-only Key Vault for third-party API credentials, isolated from unrelated secrets, with least-privilege roles assigned per identity rather than one shared set of permissions.

Architecture

Architecture diagram: Terraform provisions a Resource Group and RBAC-enabled Key Vault; an Integration Engineer writes secrets via the Secrets Officer role, while an App Registration and Service Principal read them via the Secrets User role

Provisioning the vault with Terraform

The vault and resource group were defined as code with RBAC authorization turned on from creation — no Access Policies, no legacy permission model to migrate away from later:

resource "azurerm_resource_group" "appsecrets" {
  name     = "rg-appsecrets-prod"
  location = var.location
}

resource "azurerm_key_vault" "appsecrets" {
  name                       = "kv-appsecrets-prod"
  location                   = azurerm_resource_group.appsecrets.location
  resource_group_name        = azurerm_resource_group.appsecrets.name
  tenant_id                  = data.azurerm_client_config.current.tenant_id
  sku_name                   = "standard"

  enable_rbac_authorization  = true
  soft_delete_retention_days = 90
  purge_protection_enabled   = true
}

Access was split into exactly two roles, assigned per identity rather than shared: the engineer who manages and rotates secrets gets Key Vault Secrets Officer; every application identity gets read-only Key Vault Secrets User — nothing broader, and nothing shared between applications:

resource "azurerm_role_assignment" "engineer_secrets_officer" {
  scope                = azurerm_key_vault.appsecrets.id
  role_definition_name = "Key Vault Secrets Officer"
  principal_id         = var.integration_engineer_object_id
}

resource "azurerm_role_assignment" "appA_secrets_user" {
  scope                = azurerm_key_vault.appsecrets.id
  role_definition_name = "Key Vault Secrets User"
  principal_id         = var.appA_identity_id
}

resource "azurerm_role_assignment" "appB_secrets_user" {
  scope                = azurerm_key_vault.appsecrets.id
  role_definition_name = "Key Vault Secrets User"
  principal_id         = var.appB_identity_id
}

Secrets themselves were written conditionally per application, so a given terraform apply only ever touches the app it's targeting:

resource "azurerm_key_vault_secret" "appA_client_id" {
  count        = var.deploy_appA ? 1 : 0
  name         = "appA-client-id"
  value        = var.appA_client_id
  key_vault_id = azurerm_key_vault.appsecrets.id
}

resource "azurerm_key_vault_secret" "appA_client_secret" {
  count        = var.deploy_appA ? 1 : 0
  name         = "appA-client-secret"
  value        = var.appA_client_secret
  key_vault_id = azurerm_key_vault.appsecrets.id
}

resource "azurerm_key_vault_secret" "appA_api_endpoint" {
  count        = var.deploy_appA ? 1 : 0
  name         = "appA-api-endpoint"
  value        = var.appA_api_endpoint
  key_vault_id = azurerm_key_vault.appsecrets.id
}

Phase 2: wiring up an application identity

With the vault live, each application got its own Azure AD app registration and service principal — a dedicated identity rather than a shared one — and was granted exactly the Key Vault Secrets User role, scoped to this vault only:

resource "azuread_application" "app_client" {
  display_name = "AppSecrets API Client"
  owners       = [data.azurerm_client_config.current.object_id]
}

resource "azuread_service_principal" "app_client" {
  client_id = azuread_application.app_client.client_id
}

resource "azuread_application_password" "app_client" {
  application_id     = azuread_application.app_client.id
  display_name       = "app-client-secret"
  end_date_relative   = "8760h"
}

resource "azurerm_role_assignment" "app_client_secrets_user" {
  scope                = azurerm_key_vault.appsecrets.id
  role_definition_name = "Key Vault Secrets User"
  principal_id         = azuread_service_principal.app_client.id
}

The service principal's credentials (tenant_id, client_id, client_secret) were handed to the application, which authenticates against Key Vault at startup using a client-credential flow and reads secrets by name — no credentials baked into code or config files, and the application's access is revocable independently of every other identity.

A tradeoff worth being honest about

What this design gets right

RBAC-only authorization, a dedicated vault per trust boundary, soft-delete and purge protection on by default, and least-privilege roles assigned per identity instead of one shared policy.

Where it still leaks

Marking the service principal's client secret sensitive = true in Terraform suppresses it from console output and logs — but the value is still written in plaintext into terraform.tfstate. A sensitive output is a display control, not an encryption control.

The cleaner fix: skip the Terraform output entirely and write the generated client secret straight into Key Vault as a secret (sourced from azuread_application_password.app_client.value), so the credential never has to leave the state file's protection in the first place. State still needs to be treated as sensitive — remote state with encryption and restricted access is the baseline either way — but removing the output closes one unnecessary exposure path.

What this proved out

  • RBAC authorization and Access Policies are not something you migrate incrementally with confidence — starting a new vault RBAC-only from day one avoids inheriting an older model's accumulated permissions.
  • Scoping one vault per trust boundary (rather than one vault per subscription or per team) keeps an incident in one application's credentials from touching an unrelated one.
  • Per-identity least-privilege roles, assigned explicitly in code, make it possible to answer "who can read this secret" with a terraform plan diff instead of an audit.
  • sensitive = true is a logging control, not a security boundary — reviewing where a secret actually ends up (state, outputs, provider logs) matters as much as writing the Terraform that creates it.