Conversation
bgavrilMS
reviewed
Mar 6, 2026
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
bgavrilMS
reviewed
Mar 6, 2026
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
bgavrilMS
reviewed
Mar 6, 2026
src/client/Microsoft.Identity.Client/ManagedIdentityApplication.cs
Outdated
Show resolved
Hide resolved
bgavrilMS
requested changes
Mar 6, 2026
Member
bgavrilMS
left a comment
There was a problem hiding this comment.
Single responsability principle
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors managed identity (MI) source selection to restore “classic” fallback behavior by defaulting to IMDS without probing, while making IMDS probing an explicit opt-in via GetManagedIdentitySourceAsync() and caching the explicit discovery result (including “NoneFound”).
Changes:
- Split implicit MI source selection (env-var detection + default-to-IMDS) from explicit discovery (IMDS probing + caching).
- Updated IMDSv2 CSR error handling to translate CSR-endpoint 404s into a clearer client-side failure mode.
- Adjusted unit/E2E tests to align with the new probe order (IMDSv1 first) and caching semantics.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs | Introduces explicit discovery result caching and removes IMDS probing from the default token path. |
| src/client/Microsoft.Identity.Client/ManagedIdentityApplication.cs | Routes public GetManagedIdentitySourceAsync through the new explicit discovery API/caching behavior. |
| src/client/Microsoft.Identity.Client/ManagedIdentity/V2/ImdsV2ManagedIdentitySource.cs | Adds special-case handling for CSR endpoint 404 to produce a client exception. |
| tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs | Updates unit tests to match new discovery/probe behavior and cached “NoneFound” semantics. |
| tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs | Updates probe order expectations and adds coverage for mTLS PoP without prior discovery. |
| tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsTests.cs | Removes now-unneeded probe mocks due to default path no longer probing. |
| tests/Microsoft.Identity.Test.E2e/ManagedIdentityImdsV2Tests.cs | Adjusts E2E behavior around attestation prerequisites. |
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/ManagedIdentity/V2/ImdsV2ManagedIdentitySource.cs
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs
Outdated
Show resolved
Hide resolved
Contributor
Author
Implemented the single responsibility feedback in db1dd6f (“single responsibility”):
This keeps discovery/caching contained to ManagedIdentityClient and avoids leaking cache mechanics into the application layer. |
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
bgavrilMS
approved these changes
Mar 6, 2026
bgavrilMS
reviewed
Mar 9, 2026
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Outdated
Show resolved
Hide resolved
bgavrilMS
reviewed
Mar 9, 2026
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs
Show resolved
Hide resolved
RyAuld
approved these changes
Mar 9, 2026
This was referenced Mar 9, 2026
chore(deps): Bump Azure.Identity and Microsoft.Identity.Client
LaurentAerens/rsd-clamav-api-azure#53
Merged
Closed
This was referenced Mar 16, 2026
This was referenced Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5812 #5651
Changes proposed in this request
This pull request refactors and improves the managed identity source discovery and caching logic in the MSAL library. The changes clarify the distinction between explicit source discovery and implicit selection, ensure correct caching of discovery results (including "NoneFound"), and improve error handling for IMDSv2 scenarios. Several tests are updated to reflect the new discovery order and caching behavior. The most important changes are grouped below by theme.
Managed Identity Source Discovery & Caching
s_isSourceDiscoveryCachedands_cachedSourceResultfields and updated logic inManagedIdentityClientto use them. (src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs) [1] [2]GetOrSelectManagedIdentitySourceAsyncto use cached discovery results when available, otherwise select source based on environment variables and default to IMDS without probing. Explicit discovery now probes IMDSv1 first, then IMDSv2, and caches the result. (src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs) [1] [2] [3]ManagedIdentityApplication.GetManagedIdentitySourceAsyncto use the new caching mechanism and removed unnecessary parameters. (src/client/Microsoft.Identity.Client/ManagedIdentityApplication.cs)IMDSv2 Error Handling
src/client/Microsoft.Identity.Client/ManagedIdentity/V2/ImdsV2ManagedIdentitySource.cs)Test Updates
tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs,tests/Microsoft.Identity.Test.E2e/ManagedIdentityImdsV2Tests.cs) [1] [2]tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsTests.cs) [1] [2] [3] [4] [5] [6] [7]These changes make the managed identity source selection more robust, predictable, and easier to reason about, and improve developer experience when using mTLS PoP and IMDSv2.
Testing
unit, integration, and e2e
Performance impact
none
Documentation