auth-db backups: provision workload-identity UAMI (tofu only) #26

Merged
nelsong6 merged 2 commits from claude/auth-db-backup-wi-tofu into main 2026-05-16 00:14:27 -07:00
nelsong6 commented 2026-05-16 00:10:18 -07:00 (Migrated from github.com)

First half of the connection-string -> workload-identity migration for auth-db's CNPG backup writes.

This PR is tofu only — adds Azure-side infrastructure. The chart still uses the connection string until the follow-up PR flips azureCredentials.

Resources

  • UAMI auth-db-backup-writer
  • Federated credential trusting the AKS OIDC issuer for subject system:serviceaccount:auth:auth-db
  • Role assignment: Storage Blob Data Contributor at container scope (not account) so the UAMI only sees the auth-db backup container's blobs

After apply

The output auth_db_backup_writer_client_id will be available. The follow-up chart PR will:

  1. Bump k8s/values.yaml backups.workloadIdentityClientId to that client ID
  2. Add serviceAccountTemplate.metadata.annotations.azure.workload.identity/client-id to the Cluster spec
  3. Add inheritedMetadata.labels.azure.workload.identity/use: "true" on the pods
  4. Swap azureCredentials.connectionString → azureCredentials.inheritFromAzureAD: true
  5. Delete the ExternalSecret + KV connection-string entry (per migration-policy.md — no parallel paths)

Test plan

  • tofu fmt clean
  • tofu plan shows 3 adds: UAMI, federated credential, role assignment. 0 changes, 0 destroys
  • After apply: az identity show --name auth-db-backup-writer -g auth-rg returns; clientId matches output
  • kubectl exec -n auth auth-db-1 -c postgres -- /bin/sh -c 'cat /var/run/secrets/azure/tokens/azure-identity-token | head -c 20' after chart cutover returns a JWT prefix (validates the projected token mount)

🤖 Generated with Claude Code

First half of the connection-string -> workload-identity migration for auth-db's CNPG backup writes. This PR is **tofu only** — adds Azure-side infrastructure. The chart still uses the connection string until the follow-up PR flips `azureCredentials`. ## Resources - UAMI `auth-db-backup-writer` - Federated credential trusting the AKS OIDC issuer for subject `system:serviceaccount:auth:auth-db` - Role assignment: `Storage Blob Data Contributor` at **container** scope (not account) so the UAMI only sees the auth-db backup container's blobs ## After apply The output `auth_db_backup_writer_client_id` will be available. The follow-up chart PR will: 1. Bump `k8s/values.yaml` `backups.workloadIdentityClientId` to that client ID 2. Add `serviceAccountTemplate.metadata.annotations.azure.workload.identity/client-id` to the Cluster spec 3. Add `inheritedMetadata.labels.azure.workload.identity/use: "true"` on the pods 4. Swap `azureCredentials.connectionString` → `azureCredentials.inheritFromAzureAD: true` 5. Delete the ExternalSecret + KV connection-string entry (per migration-policy.md — no parallel paths) ## Test plan - [x] `tofu fmt` clean - [ ] `tofu plan` shows 3 adds: UAMI, federated credential, role assignment. 0 changes, 0 destroys - [ ] After apply: `az identity show --name auth-db-backup-writer -g auth-rg` returns; clientId matches output - [ ] `kubectl exec -n auth auth-db-1 -c postgres -- /bin/sh -c 'cat /var/run/secrets/azure/tokens/azure-identity-token | head -c 20'` after chart cutover returns a JWT prefix (validates the projected token mount) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-05-16 00:13:47 -07:00 (Migrated from github.com)

OpenTofu Plan

Click to expand plan output
random_string.backups_suffix: Refreshing state... [id=pjbrsd]
random_password.better_auth_secret: Refreshing state... [id=none]
data.azurerm_key_vault.main: Reading...
azurerm_resource_group.auth: Refreshing state... [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg]
azurerm_storage_account.auth_db_backups: Refreshing state... [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg/providers/Microsoft.Storage/storageAccounts/authdbbackupspjbrsd]
data.azurerm_key_vault.main: Read complete after 0s [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/infra/providers/Microsoft.KeyVault/vaults/romaine-kv]
azurerm_key_vault_secret.better_auth_secret: Refreshing state... [id=https://romaine-kv.vault.azure.net/secrets/auth-better-auth-secret/c48ed1105b6e4428bda3f27c58f0a472]
azurerm_storage_container.auth_db_backups: Refreshing state... [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg/providers/Microsoft.Storage/storageAccounts/authdbbackupspjbrsd/blobServices/default/containers/auth-db]
azurerm_key_vault_secret.auth_db_backup_connection_string: Refreshing state... [id=https://romaine-kv.vault.azure.net/secrets/auth-db-backup-storage-connection/8514be902cc04157afdef5069ac48f9f]

OpenTofu used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

OpenTofu will perform the following actions:

  # azurerm_federated_identity_credential.auth_db_backup_writer will be created
  + resource "azurerm_federated_identity_credential" "auth_db_backup_writer" {
      + audience                  = [
          + "api://AzureADTokenExchange",
        ]
      + id                        = (known after apply)
      + issuer                    = "https://westus2.oic.prod-aks.azure.com/2236b5e4-81d2-4d82-bde5-17b1037999ea/5aced6d5-4299-421b-84a9-6638aebbf4f0/"
      + name                      = "auth-db"
      + parent_id                 = (known after apply)
      + resource_group_name       = "auth-rg"
      + subject                   = "system:serviceaccount:auth:auth-db"
      + user_assigned_identity_id = (known after apply)
    }

  # azurerm_role_assignment.auth_db_backup_writer_blob_contributor will be created
  + resource "azurerm_role_assignment" "auth_db_backup_writer_blob_contributor" {
      + condition_version                = (known after apply)
      + id                               = (known after apply)
      + name                             = (known after apply)
      + principal_id                     = (known after apply)
      + principal_type                   = (known after apply)
      + role_definition_id               = (known after apply)
      + role_definition_name             = "Storage Blob Data Contributor"
      + scope                            = "/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg/providers/Microsoft.Storage/storageAccounts/authdbbackupspjbrsd/blobServices/default/containers/auth-db"
      + skip_service_principal_aad_check = (known after apply)
    }

  # azurerm_user_assigned_identity.auth_db_backup_writer will be created
  + resource "azurerm_user_assigned_identity" "auth_db_backup_writer" {
      + client_id           = (known after apply)
      + id                  = (known after apply)
      + location            = "westus2"
      + name                = "auth-db-backup-writer"
      + principal_id        = (known after apply)
      + resource_group_name = "auth-rg"
      + tenant_id           = (known after apply)
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + auth_db_backup_writer_client_id = (known after apply)
╷
│ Warning: Argument is deprecated
│ 
│   with azurerm_federated_identity_credential.auth_db_backup_writer,
│   on workload-identity.tf line 26, in resource "azurerm_federated_identity_credential" "auth_db_backup_writer":
│   26:   resource_group_name = azurerm_resource_group.auth.name
│ 
│ This field is no longer used and will be removed in the next major version
│ of the Azure Provider
╵
╷
│ Warning: Deprecated attribute
│ 
│   on workload-identity.tf line 37, in resource "azurerm_role_assignment" "auth_db_backup_writer_blob_contributor":
│   37:   scope                = azurerm_storage_container.auth_db_backups.resource_manager_id
│ 
│ The attribute "resource_manager_id" is deprecated. Refer to the provider
│ documentation for details.
│ 
│ (and one more similar warning elsewhere)
╵

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tfplan

To perform exactly these actions, run the following command to apply:
    tofu apply "tfplan"

### OpenTofu Plan <details> <summary>Click to expand plan output</summary> ``` random_string.backups_suffix: Refreshing state... [id=pjbrsd] random_password.better_auth_secret: Refreshing state... [id=none] data.azurerm_key_vault.main: Reading... azurerm_resource_group.auth: Refreshing state... [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg] azurerm_storage_account.auth_db_backups: Refreshing state... [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg/providers/Microsoft.Storage/storageAccounts/authdbbackupspjbrsd] data.azurerm_key_vault.main: Read complete after 0s [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/infra/providers/Microsoft.KeyVault/vaults/romaine-kv] azurerm_key_vault_secret.better_auth_secret: Refreshing state... [id=https://romaine-kv.vault.azure.net/secrets/auth-better-auth-secret/c48ed1105b6e4428bda3f27c58f0a472] azurerm_storage_container.auth_db_backups: Refreshing state... [id=/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg/providers/Microsoft.Storage/storageAccounts/authdbbackupspjbrsd/blobServices/default/containers/auth-db] azurerm_key_vault_secret.auth_db_backup_connection_string: Refreshing state... [id=https://romaine-kv.vault.azure.net/secrets/auth-db-backup-storage-connection/8514be902cc04157afdef5069ac48f9f] OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create OpenTofu will perform the following actions:  # azurerm_federated_identity_credential.auth_db_backup_writer will be created  + resource "azurerm_federated_identity_credential" "auth_db_backup_writer" { + audience = [ + "api://AzureADTokenExchange", ] + id = (known after apply) + issuer = "https://westus2.oic.prod-aks.azure.com/2236b5e4-81d2-4d82-bde5-17b1037999ea/5aced6d5-4299-421b-84a9-6638aebbf4f0/" + name = "auth-db" + parent_id = (known after apply) + resource_group_name = "auth-rg" + subject = "system:serviceaccount:auth:auth-db" + user_assigned_identity_id = (known after apply) }  # azurerm_role_assignment.auth_db_backup_writer_blob_contributor will be created  + resource "azurerm_role_assignment" "auth_db_backup_writer_blob_contributor" { + condition_version = (known after apply) + id = (known after apply) + name = (known after apply) + principal_id = (known after apply) + principal_type = (known after apply) + role_definition_id = (known after apply) + role_definition_name = "Storage Blob Data Contributor" + scope = "/subscriptions/aee0cbd2-8074-4001-b610-0f8edb4eaa3c/resourceGroups/auth-rg/providers/Microsoft.Storage/storageAccounts/authdbbackupspjbrsd/blobServices/default/containers/auth-db" + skip_service_principal_aad_check = (known after apply) }  # azurerm_user_assigned_identity.auth_db_backup_writer will be created  + resource "azurerm_user_assigned_identity" "auth_db_backup_writer" { + client_id = (known after apply) + id = (known after apply) + location = "westus2" + name = "auth-db-backup-writer" + principal_id = (known after apply) + resource_group_name = "auth-rg" + tenant_id = (known after apply) } Plan: 3 to add, 0 to change, 0 to destroy.  Changes to Outputs: + auth_db_backup_writer_client_id = (known after apply) ╷ │ Warning: Argument is deprecated │  │  with azurerm_federated_identity_credential.auth_db_backup_writer, │  on workload-identity.tf line 26, in resource "azurerm_federated_identity_credential" "auth_db_backup_writer": │  26: resource_group_name = azurerm_resource_group.auth.name │  │ This field is no longer used and will be removed in the next major version │ of the Azure Provider ╵ ╷ │ Warning: Deprecated attribute │  │  on workload-identity.tf line 37, in resource "azurerm_role_assignment" "auth_db_backup_writer_blob_contributor": │  37: scope = azurerm_storage_container.auth_db_backups.resource_manager_id │  │ The attribute "resource_manager_id" is deprecated. Refer to the provider │ documentation for details. │  │ (and one more similar warning elsewhere) ╵  ───────────────────────────────────────────────────────────────────────────── Saved the plan to: tfplan To perform exactly these actions, run the following command to apply: tofu apply "tfplan" ``` </details>
Sign in to join this conversation.
No description provided.