Let an app read a key vault secret (RBAC)
In this task you'll let an app read secrets from a key vault.
Apps shouldn't use passwords to reach the vault either. A managed identity is an identity in Entra ID that Azure gives to an app or service, and looks after for you: no password to store or rotate.
You give the identity the Key Vault Secrets User role on the vault, or on a single secret, and the app's code asks for secrets using that identity.
Example. SS Kaiser's shop app runs with a managed identity that has Key Vault Secrets User on one secret only, the database connection string. Even if the app were compromised, it couldn't read the vault's other secrets.
Before you start
You need: Owner or User Access Administrator on the key vault. Time: 15 minutes.
Where to look
- Key vaults
- kv-portal
- Access control (IAM)
- + Add
- Add role assignment
Steps
- Create an identity to stand in for an app: open Managed Identities, select + Create, choose rg-portal-practice, name it id-shop-app and create it.User-assigned managed identities are free.
- Open your key vault and select Access control (IAM), then + Add and Add role assignment.
- Choose the role Key Vault Secrets User and select Next.
- Set Assign access to to Managed identity, select + Select members, choose User-assigned managed identity and pick id-shop-app.
- Select Review + assign twice.
- To limit it to one secret instead, open the secret greeting and add the same role from the secret's own Access control (IAM) page.Narrower is safer: the app can then read only that secret.
- In a real app, you'd attach id-shop-app to the app (for example a container app or VM) and its code would read the secret with that identity.
Commands
# A user-assigned managed identity
az identity create --resource-group rg-portal-practice --name id-shop-app
# Let it read secrets in the vault
az role assignment create --role "Key Vault Secrets User" \
--assignee-object-id $(az identity show --resource-group rg-portal-practice --name id-shop-app --query principalId --output tsv) \
--assignee-principal-type ServicePrincipal \
--scope $(az keyvault show --name kv-portal-<initials> --query id --output tsv)
# After deleting the vault, free its name straight away
az keyvault purge --name kv-portal-<initials>
Watch out for
- Cost
- Managed identities and role assignments are free. The vault's small per-operation charge applies when the app reads secrets.
- Security
- Give the identity Secrets User (read only), never Officer or Administrator. Scope it to the secrets the app actually needs.
- Common mistakes
- Giving the app Contributor on the vault, which doesn't let it read secrets anyway. Forgetting to attach the identity to the app, so it signs in as nothing.
Clean up
- Resource groups
- rg-portal-practice
- Delete resource group
You've finished the security section: delete the resource group. It removes the vault (which moves to soft-deleted vaults), the managed identity and the role assignments on them.
Words to know
- Managed identity
- An identity Azure creates and manages for a resource, so it can sign in to other services without a password.
- System-assigned
- A managed identity tied to one resource, deleted along with it.
- User-assigned
- A managed identity created on its own, which can be shared by several resources.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On