Migrate region discovery to IMDS /compute JSON endpoint#929
Open
Robbie-Microsoft wants to merge 3 commits into
Open
Migrate region discovery to IMDS /compute JSON endpoint#929Robbie-Microsoft wants to merge 3 commits into
Robbie-Microsoft wants to merge 3 commits into
Conversation
Switch Azure VM region auto-detection from the legacy IMDS text endpoint (/metadata/instance/compute/location?format=text) to the JSON endpoint (/metadata/instance/compute?api-version=2021-02-01), reading the region from the 'location' field. Missing/null location and malformed JSON fall back to None as before. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR migrates Azure VM region auto-detection in MSAL Python from the legacy IMDS text endpoint (/compute/location?format=text) to the newer IMDS JSON compute endpoint (/compute), aligning with the newer metadata API behavior while preserving existing region validation and fallback behavior.
Changes:
- Updated
_detect_region_of_azure_vmto call the IMDS/compute?api-version=2021-02-01endpoint and extractlocationfrom the JSON payload. - Added unit tests covering valid/missing/null/malformed
location, invalid region values, network failure, and the updated request URL/headers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| msal/region.py | Switches IMDS endpoint to /compute JSON response and parses location for region validation. |
| tests/test_region.py | Adds focused tests to validate the new IMDS URL/headers and JSON parsing fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Treat a non-string resp.text (json.loads raising TypeError) and a non-string location field as malformed IMDS responses and fall back to None, so region auto-detection cannot raise during client setup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4gust
approved these changes
Jun 18, 2026
gladjohn
reviewed
Jun 18, 2026
gladjohn
approved these changes
Jun 18, 2026
Move the IMDS compute metadata api-version into a module-level _IMDS_API_VERSION constant so future version bumps are a single, self-documenting change, consistent with msal-go and msal-java. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines
+68
to
+71
| if location is not None and not isinstance(location, str): | ||
| logger.info("IMDS {} returned a non-string location.".format(url)) | ||
| return None | ||
| return _validate_region(location, source="IMDS endpoint") |
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.
Ports microsoft-authentication-library-for-dotnet#6057 to MSAL Python.
What
Migrates Azure VM region auto-detection from the legacy IMDS text endpoint to the newer JSON endpoint.
Details
Tests
Added
TestDetectRegionOfAzureVmto ests/test_region.py: valid location, missing/null location, malformed JSON, invalid region value, network failure, and an assertion on the new request URL/headers. All 20 tests in ests/test_region.py pass.