Secrets¶
Each website gets its own Key Vault. The Web App’s managed identity is granted Key Vault Secrets User. Optional vault_administrators receive admin role assignments.
Pulumi config → Key Vault¶
Store structured secrets in Pulumi config:
pulumi config set --secret --path 'mywebsite_social_auth_azure.key' '...'
pulumi config set --secret --path 'mywebsite_social_auth_azure.secret' '...'
pulumi config set --secret --path 'mywebsite_social_auth_azure.tenant_id' '...'
pulumi config set --secret --path 'mywebsite_social_auth_azure.client_id' '...'
Pass a mapping into add_django_website:
django.add_django_website(
# ...
secrets={
"mywebsite_social_auth_azure": "AZURE_OAUTH",
},
environment_variables={},
)
- Key: Pulumi config object name
- Value: logical name used for the Key Vault secret and the App Setting prefix
The app receives AZURE_OAUTH_SECRET_NAME (the vault secret’s name). Secret names are normalized (underscores → hyphens, lowercased) in the vault.
Reading secrets in Django¶
With Azure settings imported, AZURE_KEY_VAULT_CLIENT is available when AZURE_KEY_VAULT is set:
import json
import environ
from pulumi_django_azure.settings import AZURE_KEY_VAULT_CLIENT
env = environ.Env()
oauth_secret = AZURE_KEY_VAULT_CLIENT.get_secret(env("AZURE_OAUTH_SECRET_NAME"))
oauth_secret = json.loads(oauth_secret.value)
SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = oauth_secret["client_id"]
SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = oauth_secret["secret"]
SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = oauth_secret["tenant_id"]
Django secret key¶
The Django SECRET_KEY is generated once per website (a Pulumi RandomString, kept in Pulumi state) and stored in the site Key Vault as django-secret-key. The Web App only receives DJANGO_SECRET_KEY_SECRET_NAME; pulumi_django_azure.settings reads the value through AZURE_KEY_VAULT_CLIENT at startup, so the key never appears in the App Settings, the Portal or an az webapp config appsettings list dump.
Resolution order in pulumi_django_azure.settings:
- Key Vault, when
AZURE_KEY_VAULTis set (secret name fromDJANGO_SECRET_KEY_SECRET_NAME, defaultdjango-secret-key) DJANGO_SECRET_KEYenvironment variable (local development and tests)
Locally, keep setting DJANGO_SECRET_KEY in .env as before.
Do not rename or recreate the RandomString resource: a replacement generates a new key and invalidates every session and password reset link of that site.