Skip to content

Migrate region discovery to IMDS /compute JSON endpoint#1038

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

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

Conversation

@Robbie-Microsoft

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

Copy link
Copy Markdown
Contributor

Summary

Ports microsoft-authentication-library-for-dotnet PR #6057 to msal-java.

Migrates Azure region auto-discovery 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), reading the location field.

No public API change (the affected constants are private), no telemetry schema change, no feature flag. REGION_NAME env-var precedence, the 2s IMDS timeout, the process-wide cache, region telemetry source values, and the fallback-to-global behavior are all preserved.

What changed

  • AadInstanceDiscoveryProvider.java: DEFAULT_API_VERSION -> 2021-02-01; IMDS_ENDPOINT drops /location and &format=text. discoverRegion now parses the JSON body and reads location instead of returning the raw response body.
  • New ImdsComputeResponse.java DTO implementing JsonSerializable with a single location field (fromJson reads location and skipChildren() on all other fields, tolerating the large /compute payload), mirroring the existing InstanceDiscoveryMetadataEntry pattern.
  • New parseRegionFromImdsResponse helper: returns null for blank body, missing/null location, or malformed JSON, so all unusable responses funnel into the existing REGION_SOURCE_FAILED_AUTODETECT path (matching dotnet's explicit try/catch behavior).
  • Tests (new RegionDiscoveryTest.java): valid location, location among extra nested fields, missing location, null location, malformed JSON, and empty/null body.

Note

Unlike the dotnet implementation, msal-java's discoverRegion has no api-version negotiation / 400-fallback path, so there is only a single success branch to update.

Testing

  • mvn -pl msal4j-sdk test -Dtest=RegionDiscoveryTest -> 6/6 pass.
  • AadInstanceDiscoveryTest, ServerTelemetryTests, SovereignCloudInstanceDiscoveryTest -> 15/15 pass (no regressions).

Port of dotnet PR #6057 / msal-js PR #8660. Switch region auto-discovery from the legacy IMDS text endpoint (/metadata/instance/compute/location, api-version 2020-06-01, format=text) to the JSON endpoint (/metadata/instance/compute, api-version 2021-02-01), reading the location field.

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 Azure region auto-discovery in MSAL4J from the legacy IMDS text location endpoint to the newer IMDS JSON /metadata/instance/compute endpoint by parsing the location field, aligning behavior with the referenced MSAL .NET change while keeping public API/telemetry/precedence behavior intact.

Changes:

  • Updated IMDS endpoint and api-version to call /metadata/instance/compute?api-version=2021-02-01.
  • Added JSON parsing for location via a dedicated DTO (ImdsComputeResponse) and helper (parseRegionFromImdsResponse).
  • Added unit tests covering valid/invalid/missing location and malformed/empty bodies.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AadInstanceDiscoveryProvider.java Switches IMDS region discovery to JSON /compute and adds parsing helper.
msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ImdsComputeResponse.java Introduces DTO for IMDS /compute JSON response (reads location, skips everything else).
msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/RegionDiscoveryTest.java Adds tests validating region parsing behavior across success and failure cases.

parseRegionFromImdsResponse already treats null/blank bodies as unusable
(returning null via StringHelper.isBlank), so the extra
!httpResponse.body().isEmpty() check is redundant and could throw a
NullPointerException when body() is null. Drop it and rely on the helper.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Robbie-Microsoft

Copy link
Copy Markdown
Contributor Author

/azp run MSAL Java - Unit Tests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Robbie-Microsoft
Robbie-Microsoft merged commit c1e6b40 into dev Jun 18, 2026
5 checks passed
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.

3 participants