# workload-identity / azure / t0 "Quick start" # # Lets one GitHub repository's main branch act as an Azure managed identity # with a short-lived token, so no client secret exists at all. # # Change the four values below, then: # az login # terraform init && terraform apply # terraform output workflow # # Paste the printed workflow into .github/workflows/ in that repository and run # it. It signs in and reads the resource group. Any other branch or repository # is refused. # # A user-assigned managed identity needs no Entra directory role to create and # cannot have a secret or certificate, so there is nothing to leak and nothing # to forbid. An app registration can have a secret added later, and in tenants # that restrict app registration it needs Application Administrator. locals { subscription_id = "00000000-0000-0000-0000-000000000000" # change me resource_group_name = "rg-deploy" # change me repository = "your-org/your-repo" # change me branch = "main" # GitHub names the repository and the branch in one claim. Repositories # created after 15 July 2026 carry IDs in it: # repo:your-org@123456/your-repo@456789:ref:refs/heads/main # Entra matches the subject exactly, so for such a repository paste the # value from a real token here. The workflow below prints it. subject = "repo:${local.repository}:ref:refs/heads/${local.branch}" } terraform { required_version = ">= 1.6" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 5.0" } } } provider "azurerm" { features {} subscription_id = local.subscription_id } data "azurerm_client_config" "current" {} data "azurerm_resource_group" "deploy" { name = local.resource_group_name } resource "azurerm_user_assigned_identity" "github" { name = "id-github-deploy" location = data.azurerm_resource_group.deploy.location resource_group_name = data.azurerm_resource_group.deploy.name } # The subject is the whole control. Entra accepts a wrong one at creation, # with no error on the credential itself, and refuses the exchange later. Check # it against a real token. resource "azurerm_federated_identity_credential" "github" { name = "github-${local.branch}" user_assigned_identity_id = azurerm_user_assigned_identity.github.id issuer = "https://token.actions.githubusercontent.com" subject = local.subject audience = ["api://AzureADTokenExchange"] } # Enough to prove the sign-in: read the resource group. resource "azurerm_role_assignment" "reader" { scope = data.azurerm_resource_group.deploy.id role_definition_name = "Reader" principal_id = azurerm_user_assigned_identity.github.principal_id principal_type = "ServicePrincipal" } output "workflow" { description = "A workflow that proves the sign-in. id-token: write is what lets it ask GitHub for a token. The three IDs are public identifiers." value = <<-EOT name: prove-azure-sign-in on: workflow_dispatch permissions: id-token: write contents: read jobs: whoami: runs-on: ubuntu-latest steps: - name: Show the subject Entra will match run: | curl -sS -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=api://AzureADTokenExchange" \ | jq -r .value \ | python3 -c 'import base64,json,sys; p=sys.stdin.read().split(".")[1]; print(json.loads(base64.urlsafe_b64decode(p + "=" * (-len(p) % 4)))["sub"])' - uses: azure/login@v2 with: client-id: ${azurerm_user_assigned_identity.github.client_id} tenant-id: ${data.azurerm_client_config.current.tenant_id} subscription-id: ${local.subscription_id} - run: az group show --name ${local.resource_group_name} --query id -o tsv EOT }