Skip to content

Cache service principal access tokens across operations - #29858

Open
keystroke wants to merge 2 commits into
Azure:mainfrom
keystroke:keystroke-fix-service-principal-token-cache
Open

Cache service principal access tokens across operations#29858
keystroke wants to merge 2 commits into
Azure:mainfrom
keystroke:keystroke-fix-service-principal-token-cache

Conversation

@keystroke

Copy link
Copy Markdown

Description

Service principal authentication currently creates a new Azure.Identity credential for each Azure PowerShell operation. Because service principal cache persistence was disabled, every credential starts with an empty MSAL cache and requests a new access token.

This change introduces a separate process-local cache for app-only tokens and shares it across recreated service principal and client assertion credentials. The existing user-token cache remains isolated, avoiding the user/app cache collision that caused service principal caching to be disabled previously. App tokens are cleared when app-only accounts disconnect and when contexts are cleared.

Regression coverage verifies that repeated service principal authentication receives the same app-only cache while remaining separate from the user cache.

Fixes #29857

Mandatory Checklist

  • SHOULD update ChangeLog.md file(s) appropriately
    • Update src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.
      • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header in the past tense.
    • Should not change ChangeLog.md if no new release is required, such as fixing test case only.
  • SHOULD regenerate markdown help files if there is cmdlet API change. Instruction
  • SHOULD have proper test coverage for changes in pull request.
  • SHOULD NOT adjust version of module manually in pull request

Copilot AI review requested due to automatic review settings July 17, 2026 20:36
@azure-client-tools-bot-prd

Copy link
Copy Markdown
Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status.

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request re-enables effective service principal (app-only) access token reuse in Az.Accounts by introducing a separate, process-local MSAL token cache for app-only credentials, while keeping the existing user-token cache isolated to avoid prior collisions.

Changes:

  • Added a shared in-memory app-only token cache (GetAppTokenCachePersistenceOptions) and wired it into service principal and client assertion credentials.
  • Ensured app-only tokens are cleared when app-only accounts disconnect and when token caches/contexts are cleared.
  • Added regression tests validating the shared app-only cache is reused across authentications and remains distinct from the user-token cache.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Accounts/Authenticators/ServicePrincipalAuthenticator.cs Uses the new app-only cache persistence options for client secret/certificate credentials.
src/Accounts/Authenticators/ClientAssertionAuthenticator.cs Uses the new app-only cache persistence options for client assertion credentials.
src/Accounts/Authentication/Factories/AuthenticationFactory.cs Clears app-only token cache when removing service principal / client assertion accounts.
src/Accounts/Authentication/Authentication/TokenCache/SharedTokenCacheProvider.cs Clears app-only cache when clearing the shared cache.
src/Accounts/Authentication/Authentication/TokenCache/PowerShellTokenCacheProvider.cs Introduces shared in-memory app-only cache options and clearing API.
src/Accounts/Authentication/Authentication/TokenCache/InMemoryTokenCacheProvider.cs Clears app-only cache when clearing the in-memory provider cache.
src/Accounts/Authentication/Authentication/TokenCache/InMemoryTokenCacheOptions.cs Adds a thread-safe Clear() operation for the in-memory cache payload.
src/Accounts/Authentication.Test/AuthenticatorsTest/ServicePrincipalAuthenticatorTests.cs Adds regression tests for shared app-only cache reuse and clearing behavior.
src/Accounts/Accounts/ChangeLog.md Documents the behavior change and references the fixed issue.
src/Accounts/Accounts/Account/DisconnectAzureRmAccount.cs Clears app-only cache on disconnect for app-only accounts in non-current-user scope.

Comment thread src/Accounts/Accounts/ChangeLog.md Outdated
Copilot AI review requested due to automatic review settings July 17, 2026 23:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

@keystroke

Copy link
Copy Markdown
Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Commenter does not have sufficient privileges for PR 29858 in repo Azure/azure-powershell

@keystroke
keystroke marked this pull request as ready for review July 18, 2026 00:30
@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@VeryEarly VeryEarly self-assigned this Jul 19, 2026
@VeryEarly

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
Successfully started running 3 pipeline(s).

@VeryEarly VeryEarly assigned xuming-ms and VeryEarly and unassigned VeryEarly Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Every call acquires a new token!

4 participants