Skip to content

Migrate region discovery to IMDS /compute JSON endpoint#8660

Merged
Robbie-Microsoft merged 6 commits into
devfrom
rginsburg/imds-compute-json
Jun 23, 2026
Merged

Migrate region discovery to IMDS /compute JSON endpoint#8660
Robbie-Microsoft merged 6 commits into
devfrom
rginsburg/imds-compute-json

Conversation

@Robbie-Microsoft

Copy link
Copy Markdown
Collaborator

Summary

Ports microsoft-authentication-library-for-dotnet PR #6057 to MSAL.js.

Migrates region auto-discovery in lib/msal-common from the legacy IMDS text endpoint /metadata/instance/compute/location (api-version=2020-06-01, format=text) to the newer JSON endpoint /metadata/instance/compute (api-version=2021-02-01), parsing the location field.

There is no public API surface change (only the values of the existing IMDS_ENDPOINT / IMDS_VERSION constants change), no telemetry schema change, and no feature flag. REGION_NAME precedence, the api-version 400 fallback, process-wide caching, and timeouts are all preserved.

What changed

  • Constants.ts: IMDS_ENDPOINT -> .../metadata/instance/compute (drop /location); IMDS_VERSION -> 2021-02-01.
  • New ImdsComputeResponse type ({ location: string }) mirroring dotnet's LocalImdsComputeResponse DTO.
  • RegionDiscovery.ts: getRegionFromIMDS drops format=text and is typed as NetworkResponse<ImdsComputeResponse>; detectRegion reads body?.location in both success branches and only sets region_source = IMDS when a usable location exists. Missing/null location funnels into the existing FAILED_AUTO_DETECTION path; malformed JSON is caught by the existing try/catch (msal-js auto-parses via response.json()).
  • Tests (Authority.spec.ts): new RegionDiscovery IMDS compute endpoint block covering the success path, missing location, null location, malformed JSON, and the 400 api-version negotiation retry.
  • apiReview/msal-common.api.md refreshed; @azure/msal-common changefile added.

Testing

  • npm run build, npm run lint (0 errors), npm test (1026 passed, incl. 5 new), npm run format:check, npm run apiExtractor, and root npm run beachball:check all pass.

Out of scope

Unified /compute snapshot cache for VM-only consumers; MI source detection.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 17, 2026 22:52
@Robbie-Microsoft Robbie-Microsoft requested a review from a team as a code owner June 17, 2026 22:52
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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

Migrates msal-common region auto-discovery from the legacy IMDS text endpoint (/compute/location?format=text) to the IMDS /compute JSON endpoint (api-version 2021-02-01), reading the location field while preserving the existing precedence/fallback behavior.

Changes:

  • Updated IMDS endpoint + api-version constants to point to /metadata/instance/compute and 2021-02-01.
  • Updated region discovery to parse body.location from JSON responses (including 400-version-negotiation retry path).
  • Added unit tests for success/missing/null/malformed JSON + 400 api-version negotiation; refreshed apiReview and added changefile.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/msal-common/src/utils/Constants.ts Updates IMDS endpoint path and api-version used for region discovery.
lib/msal-common/src/response/ImdsComputeResponse.ts Adds a typed shape for the IMDS /compute JSON response.
lib/msal-common/src/authority/RegionDiscovery.ts Switches region parsing from text response to JSON location field and adjusts region_source assignment.
lib/msal-common/test/authority/Authority.spec.ts Adds test coverage for new IMDS /compute behavior and edge cases.
lib/msal-common/apiReview/msal-common.api.md Reflects updated constant values in the reviewed API snapshot.
change/@azure-msal-common-82a28208-9ab4-418a-9bf7-07f559d81efd.json Beachball changefile for the msal-common patch.

Comment thread lib/msal-common/src/response/ImdsComputeResponse.ts
Comment thread lib/msal-common/src/authority/RegionDiscovery.ts
The IMDS /compute payload can legitimately omit location or return null,
and the region-discovery code already handles that via optional chaining.
Declare location as optional so the type reflects reality and future
callers can't assume it is always a non-empty string.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread lib/msal-common/src/authority/RegionDiscovery.ts Outdated
gladjohn
gladjohn previously approved these changes Jun 18, 2026
The @param referenced a removed imdsEndpointUrl parameter; update it to
document the actual version and options parameters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Robbie-Microsoft Robbie-Microsoft enabled auto-merge (squash) June 22, 2026 18:25
@Robbie-Microsoft Robbie-Microsoft merged commit 906f4d8 into dev Jun 23, 2026
75 of 76 checks passed
@Robbie-Microsoft Robbie-Microsoft deleted the rginsburg/imds-compute-json branch June 23, 2026 15:01
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