Lesson 3 of 3 Identity Management Solutions

Removing Secrets with Workload Identities

This lesson explains managed identities, service principals and workload identity federation.

Learning outcomes

After completing this lesson, you will be able to:

  • Compare system-assigned and user-assigned managed identities.
  • Explain workload identity federation.
  • Choose an identity type for a workload.

Why stored credentials are a problem

An application that uses a password, connection string or client secret must keep that secret somewhere. Secrets leak through code repositories and configuration files, and they expire and cause outages. The strongest design removes the secret altogether: the platform proves the workload's identity, and Microsoft Entra ID issues a short-lived token.

Managed identities

A managed identity is an identity in Microsoft Entra ID whose credentials are created, stored and rotated by Azure. Code running on a supported Azure resource requests a token from a local endpoint, then presents it to any service that supports Microsoft Entra authentication, such as Key Vault, Azure Storage or Azure SQL Database. Nobody sees a credential, so nothing can be leaked.

System-assigned

A system-assigned managed identity is enabled on a resource and shares that resource's lifecycle. It is created with the resource and is deleted with it. It suits a single resource that needs its own distinct identity, and it allows clean audit logs because each identity corresponds to one resource.

User-assigned

A user-assigned managed identity is a standalone resource that can be attached to many resources. Its lifecycle is independent. It suits a group of instances needing the same access, such as a virtual machine scale set, and it allows role assignments to be prepared before the compute exists, which is useful in infrastructure as code. It also reduces the churn of role assignments when instances are frequently recreated.

The following comparison summarizes the two types.

System-assigned
LifecycleTied to one resource
Shared between resourcesNo
Role assignments before compute existsNo
Clean upAutomatic with the resource
Choose when One resource with its own access
User-assigned
LifecycleIndependent
Shared between resourcesYes
Role assignments before compute existsYes
Clean upMust be deleted separately
Choose when Many resources, or frequently replaced resources

The following diagram shows a shared user-assigned managed identity for many web app instances, its roles, and a deployment workflow's federated credential.

Identities for 30 frequently replaced web app instances and a deployment workflow, with no stored secrets. One user-assigned managed identity holds Storage Blob Data Reader and Key Vault Secrets User once, and every instance attaches it, so replacing instances changes no role assignments. The GitHub Actions workflow uses workload identity federation instead of a secret.
Only some of the 30 instances are drawn.
Sort into groups

System-assigned or user-assigned?

Exam skills 1.2.2

Choose the managed identity type that fits each case.

Log in to try this activity Interactive activities are free with an account.

Service principals and workload identity federation

A managed identity works only for code running on Azure resources that support it. A workload running elsewhere uses an application registration and its service principal. Historically, such a workload authenticated with a client secret or a certificate. Certificates are better than secrets, but both need rotation and secure storage.

Workload identity federation removes the credential. An administrator configures a trust between the application registration, or a user-assigned managed identity, and an external identity provider. A GitHub Actions workflow, a Kubernetes service account or a workload in another cloud then exchanges its own short-lived token for a Microsoft Entra token. This is the recommended way for deployment pipelines to reach Azure without stored secrets.

The order of preference is therefore: a managed identity for workloads in Azure, workload identity federation for external workloads that have a supported identity provider, and a certificate only when neither is possible. A client secret is the last resort.

Put in order

Workload credential preference

Exam skills 1.2.2

Arrange the ways a workload can authenticate, from most to least preferred.

Log in to try this activity Interactive activities are free with an account.

Application objects and service principals

It helps to separate two related objects. An application registration is the global definition of an application, created in its home tenant. A service principal is the local instance of that application in a specific tenant, and it is the object that receives role assignments. A multi-tenant application has one registration, and a service principal in each tenant that uses it. A managed identity is itself a special type of service principal, which Azure manages.

Application registrations also define permissions. Delegated permissions let an application act on behalf of a signed-in user, limited by what that user can do. Application permissions let it act as itself, with no user present, which suits background services. Application permissions often need administrator consent, and should be reviewed carefully, because a compromised application with broad application permissions can read data across a tenant.

Using an identity in practice

Adopting managed identities involves more than enabling a setting. The target service must support Microsoft Entra authentication, which most Azure data services now do. The identity then needs a suitable role: a data plane role such as Storage Blob Data Reader, not a control plane role such as Contributor. Where a database is the target, a corresponding user must be created in the database. Azure SDKs simplify the code, as the DefaultAzureCredential class uses a managed identity in Azure, and the developer's own sign-in on a workstation, with no change to the code.

Some services still need a secret, such as a third-party API. The secret is then held in Key Vault, and the managed identity reads it at run time. This reduces the problem to one protected store, and only an identity without any stored secret can reach it, which is the pattern to recommend when a credential cannot be avoided entirely.

Scenario questions

Making a managed identity work

Exam skills 1.2.2

Complete the design so a managed identity actually works.

Log in to try this activity Interactive activities are free with an account.
Worked example

Authenticating a workload

A company has 30 web app instances in several regions that read the same Azure Blob Storage container and Key Vault. Instances are often replaced during deployments. A GitHub Actions workflow deploys the infrastructure. No secrets may be stored anywhere. What should you recommend?

Many resources sharing one set of permissions, combined with frequent replacement, indicates a user-assigned managed identity. The identity receives Storage Blob Data Reader and Key Vault Secrets User once, and each instance attaches the identity, so no role assignments change during deployments.

The deployment workflow runs outside Azure, so it cannot use a managed identity directly in the normal way, and instead uses workload identity federation with GitHub. A system-assigned identity on each app would produce many role assignments to manage, and a service principal with a stored secret would break the no-secrets rule.

Check yourself

Why might a user-assigned managed identity be preferred when instances are replaced often?

Show answer

Its lifecycle is independent, so role assignments remain in place. A system-assigned identity would disappear with each instance.

Exam Tips

Remember for the exam

  • Managed identity for Azure-hosted code; workload identity federation for external workloads; secrets as a last resort.
  • System-assigned shares one resource's lifecycle; user-assigned is independent and shareable.
  • GitHub Actions and Kubernetes can use federation to avoid stored credentials.