Skip to content

Migrate region discovery to IMDS /compute JSON endpoint#929

Open
Robbie-Microsoft wants to merge 3 commits into
devfrom
rginsburg/imds_compute_json
Open

Migrate region discovery to IMDS /compute JSON endpoint#929
Robbie-Microsoft wants to merge 3 commits into
devfrom
rginsburg/imds_compute_json

Conversation

@Robbie-Microsoft

@Robbie-Microsoft Robbie-Microsoft commented Jun 18, 2026

Copy link
Copy Markdown

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

  • msal/region.py _detect_region_of_azure_vm: updated URL (dropped /location route param and format=text, bumped api-version to 2021-02-01) and now parses json.loads(resp.text).get("location").
  • Missing/null location and malformed JSON funnel into the existing fallback (return None); the network-failure path and _validate_region are unchanged.
  • No public API, telemetry, or config changes. REGION_NAME env-var precedence preserved.

Tests

Added TestDetectRegionOfAzureVm to 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.

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>
Copilot AI review requested due to automatic review settings June 18, 2026 01:07
@Robbie-Microsoft Robbie-Microsoft requested a review from a team as a code owner June 18, 2026 01:07

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 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_vm to call the IMDS /compute?api-version=2021-02-01 endpoint and extract location from 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.

Comment thread msal/region.py
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>
Comment thread msal/region.py Outdated
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>
Copilot AI review requested due to automatic review settings June 18, 2026 19:49

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 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread msal/region.py
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")
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.

4 participants