Skip to content

Remove StaticTokenCredential, let Azure SDK handle token refresh - #1879

Merged
lbussell merged 1 commit into
dotnet:mainfrom
lbussell:remove-static-token
Dec 8, 2025
Merged

lbussell merged 1 commit into
dotnet:mainfrom
lbussell:remove-static-token

Conversation

@lbussell

@lbussell lbussell commented Dec 8, 2025 •

Copy link
Copy Markdown
Member

Previously, AzureTokenCredentialProvider would use WorkloadIdentityCredential during builds. In order to work around Workload Identity Credential lifetimes, we implemented a StaticTokenCredential that immediately fetched the requested token and cached it forever (added with #1321). WIF token lifetimes are documented here.

Since #1712, we use AzurePipelinesCredential instead. AzurePipelinesCredential handles all of the lifetime/expiration concerns. We can use it directly and allow the Azure SDK to handle caching and automatic refresh when tokens are close to expiring. I have not found any documented restrictions on its lifetime. The System.AccessToken and the resulting AzurePipelinesCredential should be valid for the entire length of the pipeline job and no longer.

I have built a test version of ImageBuilder with this change and validated it against our longest running build job, here: https://dev.azure.com/dnceng/internal/_build/results?buildId=2854918&view=logs&j=5b10b89b-008d-5f76-ef8b-7fc595d90c00&t=a505c4d8-3f6e-56eb-f81a-e4be2494dd9a

It happily spends over 3 hours building images and then has no trouble pushing them.

Previously, AzureTokenCredentialProvider would immediately fetch a token
and wrap it in a StaticTokenCredential that returned the same token forever.
This caused issues with long-running builds (>1 hour) where the token would
expire before it was actually used.

Now the provider returns the underlying AzurePipelinesCredential (or
DefaultAzureCredential in DEBUG) directly, allowing the Azure SDK to
handle token caching and automatic refresh when tokens are close to expiring.

Also removes the commented-out 'prepopulate credential cache' code from
BuildCommand and PublishManifestCommand since it's no longer needed.
@lbussell
lbussell requested a review from a team as a code owner December 8, 2025 16:15
@lbussell
lbussell merged commit 73567a4 into dotnet:main Dec 8, 2025
14 checks passed
@lbussell
lbussell deleted the remove-static-token branch December 8, 2025 23:53
lbussell added a commit that referenced this pull request Jan 12, 2026
During release staging, authentication to the staging storage account was failing.

This is because of the changes to token caching from
#1879. Previously we needed
to pass the token scope as an argument to the `GetCredential()` method,
since that method would create both the credential and the token in
order to cache the token up front. But the scope is only needed to
create the token. Since we no longer cache the token, we need to pass in
the scope to the credential's `GetToken` method instead. The scope
argument to `GetCredential()` is not used.

This is a targeted fix to unblock release staging. A follow-up PR will
contain a proper refactor to remove the unused argument from
`TokenCredentialProvider.GetCredential`.
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.

2 participants