Fix #6024: probe IMDSv2 first and remove preview latch#6041
Merged
Conversation
ManagedIdentityClient.GetManagedIdentitySourceAsync now probes the IMDSv2 CSR metadata endpoint before falling back to the IMDSv1 token endpoint. Previously v1 was probed first, and because the IMDS contract returns HTTP 400 (success signal) on every host for the v1 token path, v2-capable hosts were never detected and were always cached as ImdsV1. This broke the Azure SDK mTLS PoP discovery path end-to-end. Also removes the dormant s_imdsV1UsedForPreview latch and its associated CannotSwitchBetweenImdsVersionsForPreview throw block. With v2-first discovery, that latch would have started firing on any process that performs a non-PoP MI call before a PoP MI call (the exact Azure SDK call pattern). Per-request IMDSv1 fallback for non-PoP requests is preserved without any state being latched, so subsequent PoP requests still route to the cached ImdsV2 source. The MsalError.CannotSwitchBetweenImdsVersionsForPreview public constant is kept for backward compatibility (it ships in PublicAPI.Shipped.txt across all TFMs); only the throw site is removed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes managed identity IMDS discovery so IMDSv2-capable hosts are correctly detected and cached, enabling the managed-identity mTLS PoP path end-to-end (fix for #6024).
Changes:
- Switched explicit IMDS discovery to probe IMDSv2 first and fall back to IMDSv1 only if v2 fails.
- Removed the “preview latch” behavior that blocked switching from a prior non-PoP (v1 fallback) call to a later PoP (v2) call within the same process.
- Updated unit tests and documentation to reflect the new probe order and mixed-usage behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityClient.cs | Probes IMDSv2 first during explicit discovery; removes the IMDSv1/preview latch throw while preserving per-request non-PoP fallback to v1. |
| tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs | Updates IMDS probe mock ordering to match v2-first discovery. |
| tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs | Updates probe ordering and repurposes the “cannot switch versions” test to assert mixed non-PoP then PoP usage now succeeds. |
| docs/mtlspop_managed_identity.md | Updates documentation to remove the “switch blocked” behavior and reflect per-request fallback semantics. |
gladjohn
approved these changes
May 28, 2026
Avery-Dunn
approved these changes
May 29, 2026
neha-bhargava
approved these changes
May 29, 2026
This was referenced Jun 5, 2026
Open
Open
Merged
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 #6024.
Problem
ManagedIdentityClient.GetManagedIdentitySourceAsyncprobed IMDSv1 first. By contract, IMDS returns HTTP 400 (the documented success signal for the probe) on the v1 token endpoint for every IMDS host — including v2-capable ones. As a result, v2-capable hosts were always cached asImdsV1and the Azure SDK mTLS PoP discovery path was unreachable end-to-end.Fix
s_imdsV1UsedForPreviewlatch (and itsCannotSwitchBetweenImdsVersionsForPreviewthrow). With v2-first discovery, this dormant latch would start firing on any process that performs a non-PoP MI call before a PoP MI call — the exact Azure SDK call pattern. The per-request IMDSv1 fallback for non-PoP requests is preserved, but no state is latched; subsequent PoP requests still route to the cachedImdsV2source.Backward compatibility
MsalError.CannotSwitchBetweenImdsVersionsForPreviewis kept (it ships inPublicAPI.Shipped.txtacross all TFMs); only the throw site is removed.ManagedIdentitySourceResult) is unchanged, so PR Add IsMtlsPopSupportedByHost to ManagedIdentitySourceResult #6026 (IsMtlsPopSupportedByHost) and PR Update proposal document for GetManagedIdentityCapabilitiesAsync #6040 (GetManagedIdentityCapabilitiesAsyncrename +WithMinStrength) compose cleanly.Tests
ImdsV2Tests/ManagedIdentityTestsprobe call sites.ApplicationsCannotSwitchBetweenImdsVersionsForPreview→ApplicationsCanSwitchBetweenImdsVersions: verifies non-PoP fallback followed by PoP request now succeeds (previously threw).