Implementing Managed Identities
This lesson explains how managed identities remove stored credentials, and how to choose between the two types.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the problem that managed identities solve.
- Compare system-assigned and user-assigned identities.
- Assign an identity least-privilege access to a resource.
- Identify the risks that remain.
The secret-zero problem
An application that reads a secret from Key Vault must first authenticate to Key Vault. If it uses a client secret, that secret must be stored somewhere, which recreates the original problem. This is sometimes called the secret-zero problem. A managed identity solves it: the Azure platform creates a service principal for the resource, keeps the credential out of reach, rotates it automatically, and gives the code a token through a local endpoint.
The code asks the local identity endpoint on the host for a token for a specific resource, such as https://vault.azure.net. No credential appears in code, configuration or environment. The Azure Identity client libraries wrap this in DefaultAzureCredential, so the same code can run with a developer's sign-in locally and a managed identity in Azure.
System-assigned and user-assigned
There are two types, and the choice concerns lifecycle and sharing. The following comparison shows how they differ.
Consider a scale set whose instances are replaced frequently, or a group of ten function apps that read the same storage account. With system-assigned identities, every new resource needs a new role assignment, and each assignment takes time to propagate. With one user-assigned identity, permissions are already in place. Microsoft's guidance for its own services favours user-assigned identities for this reason. On the other hand, a shared identity means that its permissions are the union of what all those resources need, so a compromise of any one of them exposes all of that access.
System-assigned or user-assigned?
Granting access
A managed identity has no rights until it is given them. Grant Azure roles at the narrowest scope, such as Key Vault Secrets User on one vault or Storage Blob Data Reader on one container. For a target that uses Microsoft Entra authentication but not Azure RBAC, such as Azure SQL, create a contained database user for the identity. For Microsoft Graph, application permissions are assigned by script, because the portal has no consent page for managed identities.
The following commands, run in Azure Cloud Shell, enable a system-assigned identity on a virtual machine and grant it read access to the secrets in a vault. The values in capitals are placeholders.
PRINCIPAL=$(az vm identity assign --resource-group RG --name VM --query systemAssignedIdentity -o tsv)
VAULT_ID=$(az keyvault show --name VAULT --query id -o tsv)
az role assignment create --assignee-object-id "$PRINCIPAL" --assignee-principal-type ServicePrincipal \
--role "Key Vault Secrets User" --scope "$VAULT_ID"Granting an identity access
Limits and risks
A managed identity is safer than a stored secret, but it does not remove all risk. Anyone who can run code on the resource can obtain its tokens. A person who can edit a Logic Apps workflow, for example, can make the workflow request a token for any audience, so workflow edit rights are effectively identity rights. A compromised virtual machine exposes its identity through the local endpoint in the same way. Least privilege therefore still matters.
Managed identities are not suitable for workloads that run outside Azure, unless those workloads are Azure Arc-enabled servers, which receive a managed identity of their own, or use a federated identity credential on a user-assigned identity. They have no extra cost, and Conditional Access for workload identities does not apply to them.
Managed identity risks
Choosing the identity type
A company has 12 App Service apps that all read from one Key Vault. Apps are created and deleted weekly by a deployment process that cannot be granted permission to create role assignments. Which identity design should you recommend?
The decisive constraint is that the deployment process cannot create role assignments. A system-assigned identity needs a new role assignment for each new app, which that process cannot do. A user-assigned managed identity, given Key Vault Secrets User once on the vault, can be attached to each app at deployment, so that no role assignment is needed.
A service principal with a client secret would also work, but it brings back the credential that a managed identity removes. Giving the deployment process Owner would remove the constraint by granting far more than it needs.
Twenty VMs in a scale set need the same access to one vault. Which identity type causes the fewest role assignments?
Show answer
A user-assigned identity. One assignment covers all of them, whereas system-assigned identities need one each.
Exam Tips
Remember for the exam
- System-assigned ends with its resource; user-assigned must be deleted explicitly.
- A managed identity has no access until a role is assigned.
- Anyone who can run code on the resource can use its identity.
- User-assigned identities let you assign roles before resources exist.