Lesson 1 of 7 Authentication and Access

Choosing Service Principals and Managed Identities

This lesson explains workload identities, and how to choose the correct type.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why automation needs its own identity.
  • Describe service principals and their credentials.
  • Distinguish system-assigned from user-assigned identities.
  • Choose an identity from stated requirements.

Why automation needs its own identity

A pipeline must never run as a person. A personal account may hold far more access than the pipeline needs, and it is disabled when that person leaves. It also makes audit logs misleading. A workload identity is an identity for software, and Microsoft Entra ID offers two kinds: service principals and managed identities.

Service principals

Registering an application in Microsoft Entra ID creates an application object, which is the global definition, and a service principal, which is its local identity in a tenant. Azure roles are assigned to the service principal. To sign in, it needs a credential, which may be a client secret, a certificate or a federated credential.

Secrets are the weakest option: they expire, they must be stored somewhere, and they can leak. Certificates are stronger, but they still need management. A federated credential stores no secret at all, and the lesson on secretless authentication explains it. A service principal can be used from anywhere, which suits workloads running outside Azure, including hosted pipeline agents.

Put in order

Service principal credentials from weakest to strongest

Exam skills 4.1.1

Arrange the ways a service principal can sign in, from the weakest to the strongest.

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

Managed identities

A managed identity is an identity that Azure manages, with no credentials for you to handle. A workload on an Azure resource requests a token from a local endpoint, and Azure rotates the underlying credentials automatically. Managed identities have two types.

System-assigned

A system-assigned managed identity is enabled on one Azure resource and shares its lifecycle. Deleting the resource deletes the identity, and it cannot be shared. It suits a single resource that needs its own permissions, and it leaves nothing behind.

User-assigned

A user-assigned managed identity is a standalone Azure resource, which can be attached to many resources. It survives when those resources are deleted. This suits several resources that need the same access, and resources that are replaced often, such as those in a scale set, because role assignments can be made in advance and they stay in place. A user-assigned identity can also hold federated credentials, so a pipeline can use it, even though the pipeline does not run on an Azure resource.

Sort into groups

System-assigned or user-assigned?

Exam skills 4.1.1

Decide which type of managed identity each statement describes.

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

Choosing an identity

The following comparison sets out the differences. The main question is where the workload runs.

System-assigned
Runs onAzure resource
LifecycleSame as the resource
SharedNo
CredentialManaged by Azure
Choose when One Azure resource needs its own access.
User-assigned
Runs onAzure resource, or federated
LifecycleIndependent
SharedYes
CredentialManaged by Azure, or federated
Choose when Several resources share access, or roles must exist in advance.
Service principal
Runs onAnywhere
LifecycleIndependent
SharedYes
CredentialSecret, certificate or federated
Choose when The workload runs outside Azure, or needs a Microsoft Entra app.

On a self-hosted agent running on an Azure virtual machine, a managed identity assigned to that machine can be used, and the agent needs no stored credential. Note that all pipelines using that agent then share the identity, so the agent pool must be protected. Microsoft-hosted agents cannot use your managed identities directly, which is why federation exists.

The following diagram shows one user-assigned managed identity shared by twelve container apps, with a single role assignment.

One user-assigned managed identity for twelve container apps. The identity holds Key Vault Secrets User on the vault, assigned once, and each app attaches it, so recreating an app changes no role assignment. System-assigned identities would need a new assignment after each recreation.
The scenario is illustrative.
Scenario questions

Choosing a workload identity

Exam skills 4.1.1

Choose the identity type for each workload.

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

Applying least privilege

Whichever identity is selected, role assignments should be as narrow as possible. Assign roles at the resource group or resource level, rather than the subscription, and use specific roles such as Website Contributor rather than Contributor when these are enough. Separate identities for each environment limit the damage if one is misused, and they stop a pull request pipeline from reaching production.

Select all that apply

Least privilege for workload identities

Exam skills 4.1.1

Pick the practices that keep a workload identity's access minimal.

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

Choosing the type

Twelve container apps must read secrets from the same key vault. Apps are often deleted and recreated, and the security team wants to assign the vault role once, without repeating it. Which identity should you use?

Use one user-assigned managed identity, attached to all the apps, with the Key Vault Secrets User role on the vault. The role survives when an app is recreated. System-assigned identities would each need their own role assignment after each recreation. A service principal with a secret would add a credential to store and rotate.

Check yourself

Why can a Microsoft-hosted agent not use your system-assigned managed identity?

Show answer

The agent is not one of your Azure resources, so it cannot hold that identity. Federation solves this.

Exam Tips

Remember for the exam

  • A system-assigned identity shares its resource's lifecycle.
  • A user-assigned identity is reusable and independent of any resource.
  • Use a service principal for workloads outside Azure.
  • Federated credentials can sit on app registrations or user-assigned identities.
  • Use a separate identity per environment, with narrowly scoped roles.