Infrastructure
Azure App Service: validate a managed identity cutover before removing the system identity
A production runbook for moving an App Service from a system-assigned to a user-assigned managed identity, proving the effective principal and retaining a tested rollback.
An App Service uses its system-assigned managed identity to read Key Vault secrets and write to a Storage account. The team wants authorization to survive app recreation and slot changes, so it creates a user-assigned identity. The tempting migration is short: attach the new identity, copy roles, set AZURE_CLIENT_ID, then disable the old identity.
That sequence changes several contracts at once. The identity may be attached without being selected by the application. A role may exist at the wrong scope. A warm instance may still hold a token for the former principal. Disabling the system identity too early removes the simplest rollback and can leave production returning 403 while the control plane looks correct.
The running case is app-orders-prod, which reads one Key Vault secret and writes order exports to one Storage container. The objective is to prove the effective principal on those two data-plane operations, preserve least privilege, and end with an explicit promotion, hold, or rollback decision.
Freeze the current identity contract
Before attaching anything, record the source workload, target operations, current principal, scopes and last known-good probes. Do not infer access from an app name or a role-assignment display name.
change: CHG-2026-1102-17
source:
app: app-orders-prod
slot: production
current_identity: system-assigned
targets:
- resource: kv-orders-prod
operation: secrets/get
expected_role: Key Vault Secrets User
scope: exact-vault
- resource: stordersprod/exports
operation: blobs/write
expected_role: Storage Blob Data Contributor
scope: exact-container
canaries:
positive: read named canary secret and write/delete canary blob
negative: secret set and write to another container remain denied
rollback:
identity: system-assigned
app_revision: previous-qualified-release Also freeze the current application configuration and deployment revision. If the application already receives an explicit managed identity client ID, prove what it points to before changing it. A stale setting may explain why the runtime and the resource configuration disagree.
Inventory attachment, selection and authorization separately
Three independent facts must align:
- Attachment: the user-assigned identity is associated with the App Service.
- Selection: the application asks the local token service for that identity.
- Authorization: the selected principal has the required role at the required target scope.
Inspect the current state before the cutover.
set -eu
RG="rg-orders-prod"
APP="app-orders-prod"
az webapp identity show --resource-group "$RG" --name "$APP" --query '{type:type,systemPrincipal:principalId,userAssigned:userAssignedIdentities}' --output json
az webapp config appsettings list --resource-group "$RG" --name "$APP" --query "[?name=='AZURE_CLIENT_ID'].{name:name,value:value}" --output json Do not download or rotate unrelated publishing credentials to repair a managed identity migration. They do not prove which principal the application presents to its dependencies.
Prepare the user-assigned identity before selecting it
Create or identify the user-assigned identity under a named owner. Record its resource ID, client ID and principal ID because they serve different purposes: attachment uses the resource ID, runtime selection commonly uses the client ID, and RBAC binds to the principal ID.
set -eu
IDENTITY_RG="rg-identities-prod"
IDENTITY_NAME="id-orders-app-prod"
az identity show --resource-group "$IDENTITY_RG" --name "$IDENTITY_NAME" --query '{resourceId:id,clientId:clientId,principalId:principalId,tenantId:tenantId}' --output json
UAMI_RESOURCE_ID=$(az identity show -g "$IDENTITY_RG" -n "$IDENTITY_NAME" --query id -o tsv)
UAMI_PRINCIPAL_ID=$(az identity show -g "$IDENTITY_RG" -n "$IDENTITY_NAME" --query principalId -o tsv)
az role assignment list --assignee-object-id "$UAMI_PRINCIPAL_ID" --all --fill-principal-name false --query '[].{role:roleDefinitionName,scope:scope,assignmentId:id}' --output table Recreate only the two approved role tuples. Do not clone every assignment from the system principal: historical or inherited permissions are not part of the application contract merely because they exist.
Attach both identities during the canary window
Keep the system-assigned identity enabled while attaching the user-assigned identity. This creates a reversible observation window; it does not prove that the application uses the new principal.
set -eu
RG="rg-orders-prod"
APP="app-orders-prod"
UAMI_RESOURCE_ID="/subscriptions/<sub>/resourceGroups/rg-identities-prod/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id-orders-app-prod"
az webapp identity assign --resource-group "$RG" --name "$APP" --identities "$UAMI_RESOURCE_ID"
az webapp identity show --resource-group "$RG" --name "$APP" --query '{type:type,systemPrincipal:principalId,userAssigned:userAssignedIdentities}' --output json The expected type now includes both system- and user-assigned identities. If attachment fails, stop there. Broadening roles or changing application code cannot repair an identity that the host does not expose.
Make runtime selection explicit
When several identities are attached, an implicit credential chain can select the system identity or fail ambiguously. Configure the application with the user-assigned identity client ID through the mechanism supported by its Azure Identity SDK and pin that configuration to the release being tested.
set -eu
RG="rg-orders-prod"
APP="app-orders-prod"
UAMI_CLIENT_ID="<user-assigned-identity-client-id>"
az webapp config appsettings set --resource-group "$RG" --name "$APP" --settings AZURE_CLIENT_ID="$UAMI_CLIENT_ID" --output none
az webapp config appsettings list --resource-group "$RG" --name "$APP" --query "[?name=='AZURE_CLIENT_ID'].name" --output tsv Treat the application setting as routing metadata, not proof of use. The SDK configuration must consume it, and the deployed revision must be the revision under test. Restart only the bounded canary instance or slot when the SDK requires a fresh process; do not restart the whole estate merely to hide token-cache uncertainty.
Prove the caller at the target
Run one positive operation per target and correlate it with target-side logs. The decisive field is the caller object ID matching the user-assigned identity principal ID.
let StartTime = datetime(2026-11-02T08:30:00Z);
let EndTime = datetime(2026-11-02T09:00:00Z);
let ExpectedPrincipalId = "<user-assigned-identity-principal-id>";
AzureDiagnostics
| where TimeGenerated between (StartTime .. EndTime)
| where ResourceProvider in ("MICROSOFT.KEYVAULT", "MICROSOFT.STORAGE")
| extend CallerObjectId = tostring(column_ifexists("identity_claim_oid_g", ""))
| extend Status = tostring(column_ifexists("ResultSignature", column_ifexists("statusCode_s", "")))
| project TimeGenerated, ResourceProvider, Resource, OperationName,
CallerObjectId, Status, CorrelationId
| order by TimeGenerated asc Table and column names depend on diagnostic settings and collection mode. Inspect a raw event first. If the target sees the old system principal, the problem is selection or deployment. If it sees the new principal and returns 403, inspect role, scope, authorization mode and propagation. If no request reaches the target, return to token acquisition, DNS, firewall and routing.
Run positive and negative canaries
A successful secret read alone can conceal excess privilege. Validate the intended operations and expected denials with a unique correlation value.
CANARY EXPECTED
Read the named Key Vault canary secret success
Set or delete a Key Vault secret denied
Write and delete a blob in exports/canary success
Write a blob in an unapproved container denied
Target logs show the user-assigned principalId yes
Target logs show the system principal after switch no
Application health and dependency latency within baseline
Fresh instance after scale-out same principal and decisions The fresh-instance check matters. A single warm worker may succeed with a cached token while a scale-out instance follows the new configuration and fails, or the reverse. The canary must cover at least one newly started process before removing the old identity.
Decide promotion, hold or rollback
PROMOTE
Both identities are attached during validation
Runtime configuration explicitly selects the user-assigned client ID
Target logs prove the user-assigned principal on every required operation
Positive canaries succeed and negative canaries remain denied
A fresh instance reproduces the same result
IaC owns identity attachment, role assignments and runtime selection
HOLD
The effective principal cannot be observed
Roles are broader than the frozen contract
Only a warm instance has been tested
Diagnostic settings cannot distinguish old and new callers
Another slot or deployment path still relies on the system identity
ROLL BACK
Restore the previous application revision and identity selection
Keep or reselect the still-attached system identity
Obtain a fresh token and replay the full canary matrix
Remove only the unqualified user-assigned role assignments
Preserve logs and correlation IDs for the failed cutover Disable the system-assigned identity only after the promotion criteria remain true through the observation window. Its removal prevents new tokens from being issued, but previously issued tokens can remain valid until expiry. Keep monitoring both principal IDs until the old token lifetime has elapsed, then remove orphaned role assignments by exact assignment ID.
Conclusion
A managed identity cutover is not complete when Azure shows two identities on the App Service. It is complete when the application deliberately selects the new identity, target logs prove the new principal, least-privilege canaries pass on warm and fresh instances, and the previous identity remains a tested rollback until promotion.
The closing decision is binary: remove the system identity only with end-to-end evidence, or keep the dual attachment and repair the missing contract. A control-plane configuration is intent; a target-side authorization decision is proof.