# workload-identity / azure / t1 "Standard" # # Short-lived Azure access for GitHub Actions, limited to named branches or # deployment environments of one repository, optionally for one AKS service # account, with every token exchange recorded. # # The identity is a user-assigned managed identity: no Entra directory role is # needed to create it, and it cannot have a secret or certificate. locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) github_issuer = "https://token.actions.githubusercontent.com" audience = ["api://AzureADTokenExchange"] # One credential per subject. Entra matches the subject exactly, and offers # no wildcard, so each branch or environment is its own credential. # The name is the subject made safe, with a hash so two subjects that differ # only in a punctuation mark keep separate names. credentials = { for s in var.allowed_subjects : "${substr(replace(s, "/[^A-Za-z0-9_-]/", "-"), 0, 110)}-${substr(sha1(s), 0, 8)}" => s } repository_slug = replace(var.repository, "/[^A-Za-z0-9_-]/", "-") aks = var.aks_cluster_name != "" } data "azurerm_resource_group" "deploy" { name = var.resource_group_name } resource "azurerm_user_assigned_identity" "deploy" { name = "id-github-${local.repository_slug}" location = data.azurerm_resource_group.deploy.location resource_group_name = data.azurerm_resource_group.deploy.name tags = local.tags } resource "azurerm_federated_identity_credential" "github" { for_each = local.credentials name = each.key user_assigned_identity_id = azurerm_user_assigned_identity.deploy.id issuer = local.github_issuer subject = each.value audience = local.audience } resource "azurerm_role_assignment" "deploy" { for_each = { for a in var.role_assignments : "${a.role}@${a.scope}" => a } scope = each.value.scope role_definition_name = each.value.role principal_id = azurerm_user_assigned_identity.deploy.principal_id principal_type = "ServicePrincipal" } # AKS workload identity: one Kubernetes service account may act as this # identity. Anyone able to run a pod with that service account in that # namespace can therefore act as it too. # # The cluster must already have oidc_issuer_enabled and # workload_identity_enabled. This reads the issuer; it does not change the # cluster. data "azurerm_kubernetes_cluster" "aks" { count = local.aks ? 1 : 0 name = var.aks_cluster_name resource_group_name = var.aks_resource_group_name != "" ? var.aks_resource_group_name : var.resource_group_name lifecycle { postcondition { condition = self.oidc_issuer_enabled error_message = "The cluster has no OIDC issuer. Enable it first: az aks update --enable-oidc-issuer --enable-workload-identity." } } } resource "azurerm_federated_identity_credential" "aks" { count = local.aks ? 1 : 0 name = "aks-${var.aks_namespace}-${var.aks_service_account}" user_assigned_identity_id = azurerm_user_assigned_identity.deploy.id issuer = data.azurerm_kubernetes_cluster.aks[0].oidc_issuer_url subject = "system:serviceaccount:${var.aks_namespace}:${var.aks_service_account}" audience = local.audience } # Every token exchange is a sign-in by the identity, kept in the tenant's # sign-in logs for 7 days on Entra ID Free and 30 on P1 and P2. Exporting them # is what lets anyone say, months later, which workflow or pod acted as which # identity. Both tables carry the credential that was used # (FederatedCredentialId). resource "azurerm_monitor_aad_diagnostic_setting" "sign_ins" { count = var.sign_in_log_workspace_id != "" ? 1 : 0 name = "workload-identity" log_analytics_workspace_id = var.sign_in_log_workspace_id enabled_log { category = "ServicePrincipalSignInLogs" } enabled_log { category = "ManagedIdentitySignInLogs" } }