Workload identities and credential rollover
Choose certificates, managed identities or federated credentials for workloads and plan a credential rollover.
By Invicly Tech, , in Microsoft Entra & Microsoft 365 Administration. About 13 minutes to read.
In short
A workload identity is the identity of software rather than a person: an app, a script, a pipeline or a container. It can prove itself with a client secret, a certificate, a managed identity or a federated credential. Prefer managed identities and federation because they remove stored secrets, grant only the narrow permissions a job needs, and plan how to roll any remaining credential before it expires.
Think of it like this: A secret is a spare key under the doormat; a certificate is a key kept in a safe; a managed identity or federation is a guard who already knows the worker by sight.
Explained simply
People sign in with passwords and passkeys. Software needs to sign in too: a nightly script that reads users, a web app that writes to storage, a pipeline that deploys to Azure. The identity used by software is called a workload identity.
The old way was a client secret, a long password for the app stored in a config file. Secrets leak: into code repositories, screenshots, chat messages and tutorials. A certificate is better, because the private key can stay in a key vault. But both still expire and need replacing.
Better still is no stored credential at all. A managed identity is an identity Azure gives to a resource such as a VM or a function app; Azure handles its credentials. Workload identity federation lets a trusted outside system, such as GitHub Actions, swap its own short-lived token for an Entra token.
Whatever the credential, give the workload only the permissions it needs. A script that reads one SharePoint site does not need access to every site. And when a credential must be replaced, add the new one first, switch over, check it works, then remove the old one.
Press Play on a diagram to walk through it one step at a time.
- A password for the app
- Easy to leak in code or logs
- Expires; must be rotated
- Use only when nothing else works
- Private key signs an assertion
- Key can stay in Key Vault or an HSM
- Still expires and needs rollover
- Better than a secret
- No secret stored by you
- Azure or a trusted issuer vouches for the workload
- Tokens are short-lived
- First choice where available
- Workflow to GitHub OIDC issuer: Request an OIDC token for this run1Request an OIDC token for this run
- GitHub OIDC issuer to Workflow: Signed token: repo, branch or environment2Signed token: repo, branch or environment
- Workflow to Entra ID: Exchange token for an Entra access token3Exchange token for an Entra access token
- Entra ID to Workflow: Issuer, subject and audience match: access token4Issuer, subject and audience match: access token
- Workflow to Azure or Graph: Call API with short-lived token5Call API with short-lived token
- Step 1Runs on Azure?
Yes: use a managed identity
- Step 2Runs in a trusted OIDC issuer?
GitHub, Kubernetes, another cloud: federate
- Step 3Neither?
Use a certificate kept in a key vault
- Step 4Last resort
Short-lived secret, rotated and monitored
In depth
Secrets, certificates and federation compared
A client secret is a string the app sends with its client ID. Anyone who copies it can act as the app until it expires or is deleted. The portal lets you pick a lifetime of up to 24 months; shorter is safer. A certificate credential works differently: the app signs a short assertion with its private key, and Entra checks it with the public key you uploaded. The private key never travels, and it can live in Azure Key Vault or a hardware module.
Federated identity credentials remove stored secrets entirely. You tell Entra to trust tokens from an issuer, such as https://token.actions.githubusercontent.com, with a specific subject, such as repo:contoso/payroll:environment:prod, and the audience api://AzureADTokenExchange. An app or user-assigned managed identity can hold up to 20 federated credentials. Admins can also enforce app management policies that block secrets or cap their lifetime.
- Secret: copyable, max 24 months in the portal
- Certificate: private key stays put; still expires
- Federation: issuer, subject and audience must match
- App management policies can block new secrets
Managed identities first
If code runs on Azure, a managed identity is usually the answer. A system-assigned identity is created with one resource and deleted with it. A user-assigned identity is a separate Azure resource you can attach to several resources and keep across rebuilds. Either way, the code asks the local Azure endpoint for a token and gets one without any secret, and libraries like DefaultAzureCredential do this for you.
Azure RBAC roles for Azure resources are assigned in the Azure portal or CLI. Microsoft Graph permissions for a managed identity cannot be granted in the portal's API permissions page; you assign the Graph app role to the identity's service principal with Microsoft Graph PowerShell or the Graph API. Managed identity sign-ins appear in their own tab of the sign-in logs.
- Created with one resource
- Deleted with that resource
- One identity per resource
- Simplest for a single app
- Standalone Azure resource
- Shared by several resources
- Survives redeploys
- Pre-grant permissions before deploy
Granting narrow Graph permissions
Start from the task, not the error message. List the exact API calls the workload makes, then look up the least-privileged permission for each in the Graph API reference, which lists permissions from least to most privileged per call. Reading groups needs GroupMember.Read.All or Group.Read.All, not Directory.ReadWrite.All.
Some services offer resource-scoped permissions. Sites.Selected grants nothing until an admin allows the app on specific SharePoint sites. For mail, calendars and contacts, Exchange Online RBAC for applications can limit an app permission to a set of mailboxes. Record each granted permission, its reason and its owner, and review service principals with application permissions regularly. Conditional Access for workload identities, which can limit service principals by location, needs Workload Identities Premium.
- Read the permissions table for each Graph call
- Prefer Read over ReadWrite, and specific over Directory
- Sites.Selected for named SharePoint sites
- Exchange RBAC for applications for named mailboxes
Rolling over a credential
Apps can hold several secrets or certificates at once, which is what makes a zero-downtime rollover possible. Step 1: add the new credential with a clear display name and end date. Step 2: store it where the workload reads it, such as Key Vault, and deploy. Step 3: check the service principal sign-in logs; each entry shows a credential key ID, so you can confirm the workload now uses the new one. Step 4: after a safe overlap, remove the old credential by its key ID.
Rollback is easy while both exist: point the workload back at the old one. Set a reminder well before each expiry, or better, automate rotation. Every rotation is an audit event on the application, so you have evidence of who changed it and when.
Avoiding long-lived tokens and secrets in tutorials
Many leaks start as a demo. When you write or follow a tutorial, use placeholders such as the client ID in angle brackets, never real values, and read secrets from environment variables or a key vault. Prefer interactive sign-in or a managed identity in examples so readers never create a secret at all.
Access tokens from Entra are short-lived, typically about an hour, and refresh tokens are bound to the client. Do not try to stretch them; let the library fetch new tokens. Do not paste tokens into chat, tickets or public decoders. If a secret is exposed, treat it as compromised: delete it from the app at once, create a replacement, and check sign-in logs for use from unexpected IP addresses.
Learn it in 5 short steps
About 30m each. Track them with a timer in your plan.
- 1Compare secrets, certificates and federation~30m
- 2Use managed identities where possible~30m
- 3Grant narrow Graph permissions~30m
- 4Roll over a credential~30m
- 5Avoid long-lived tokens~30m
Key points
- Use a managed identity for code running on Azure resources; Azure handles the credential for you.
- Use workload identity federation for GitHub Actions, Kubernetes or other clouds instead of stored secrets.
- When you must use a credential, prefer a certificate to a client secret and keep it in a key vault.
- Grant the narrowest Microsoft Graph permission, such as Sites.Selected instead of Sites.ReadWrite.All.
- Roll credentials by adding the new one, switching the workload, checking sign-ins, then removing the old one.
Key terms
Tap a card to flip it. 0/5 known
Examples
# Runs on an Azure VM, App Service or Function with a managed identity
from azure.identity import DefaultAzureCredential
import requests
cred = DefaultAzureCredential() # finds the managed identity; no secret in code
token = cred.get_token("https://graph.microsoft.com/.default")
r = requests.get(
"https://graph.microsoft.com/v1.0/groups?$select=displayName&$top=5",
headers={"Authorization": f"Bearer {token.token}"},
)
print(r.status_code, [g["displayName"] for g in r.json().get("value", [])])A managed identity calls Microsoft Graph with no stored credential; it needs Group.Read.All granted as an app role.
Giving a managed identity one Graph permission
Contoso's report function app has a system-assigned managed identity and needs to read group names. Step 1: find the identity's service principal and the Graph service principal. Step 2: find the Group.Read.All app role on Graph. Step 3: assign it. Evidence: the identity's Permissions tab in Enterprise applications (filter by Managed identities) lists Group.Read.All, and the function's next call returns 200. Rollback: Remove-MgServicePrincipalAppRoleAssignment with the assignment ID.
Connect-MgGraph -Scopes "Application.Read.All","AppRoleAssignment.ReadWrite.All"
$mi = Get-MgServicePrincipal -Filter "displayName eq 'func-contoso-reports'"
$graph = Get-MgServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'"
$role = $graph.AppRoles | Where-Object { $_.Value -eq "Group.Read.All" -and $_.AllowedMemberTypes -contains "Application" }
New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $mi.Id `
-PrincipalId $mi.Id -ResourceId $graph.Id -AppRoleId $role.IdRolling a client secret with overlap
A legacy script at Fabrikam still uses a secret that expires next month. Step 1: add a new secret valid for six months with a dated name. Step 2: store it in Key Vault and update the script's configuration. Step 3: run the script, then check service principal sign-ins for the new credential key ID. Step 4: after a week, remove the old secret by its key ID. Longer term, move the script to a certificate or a managed identity so the cycle ends.
Connect-MgGraph -Scopes "Application.ReadWrite.All"
$app = Get-MgApplication -Filter "appId eq '<client-id>'"
$new = Add-MgApplicationPassword -ApplicationId $app.Id -PasswordCredential @{
displayName = "rotated-2026-12"; endDateTime = (Get-Date).AddMonths(6) }
# store $new.SecretText in Key Vault now; it is shown only once
$app.PasswordCredentials | Select-Object DisplayName, KeyId, EndDateTime
# after verifying sign-ins with the new key:
Remove-MgApplicationPassword -ApplicationId $app.Id -KeyId "<old-key-id>"Quick reference
- Order of preference: managed identity, federation, certificate, short-lived secret
- Federation trust = issuer + subject + audience api://AzureADTokenExchange
- Up to 20 federated credentials per app or user-assigned managed identity
- Grant Graph app roles to managed identities with New-MgServicePrincipalAppRoleAssignment
- Sites.Selected limits SharePoint access to sites you name
- Rollover: add new, deploy, verify key ID in sign-in logs, remove old
- A leaked secret is compromised: delete it, replace it, check sign-ins
Common mistakes
- Pasting a client secret into a tutorial, script or repository, where it lives on long after the demo.
- Granting Directory.ReadWrite.All to make an error go away instead of finding the narrow permission.
- Removing the old credential before confirming the workload signs in with the new one.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List every app registration whose secrets or certificates expire in the next 60 days.
- Exercise 2medium
Add a federated credential so GitHub Actions in repo contoso/payroll, environment prod, can sign in as a lab app. Show the workflow login step.
- Exercise 3hard
Write a rollover plan for an app with one certificate used by three servers, including verification and rollback.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can a managed identity be used outside Azure?
- Not directly; it only works from Azure resources. For workloads elsewhere, use workload identity federation with a trusted issuer, or a certificate as a fallback.
- Why can I not add Graph permissions to a managed identity in the portal?
- Managed identities have no app registration page with API permissions. You assign Graph app roles to their service principal with PowerShell or the Graph API instead.
- How long should a client secret last?
- As short as you can manage operationally, often six months or less, and never longer than the 24 months the portal allows. Better still, replace it with a certificate, managed identity or federation.
- Do workload identities need a licence?
- App registrations, managed identities and federation do not need a premium licence. Extra controls such as Conditional Access for workload identities need Workload Identities Premium.