Migrate region discovery to IMDS /compute JSON endpoint#1038
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
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
locationvia a dedicated DTO (ImdsComputeResponse) and helper (parseRegionFromImdsResponse). - Added unit tests covering valid/invalid/missing
locationand 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>
Contributor
Author
|
/azp run MSAL Java - Unit Tests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
gladjohn
approved these changes
Jun 18, 2026
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.
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 thelocationfield.No public API change (the affected constants are
private), no telemetry schema change, no feature flag.REGION_NAMEenv-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_ENDPOINTdrops/locationand&format=text.discoverRegionnow parses the JSON body and readslocationinstead of returning the raw response body.ImdsComputeResponse.javaDTO implementingJsonSerializablewith a singlelocationfield (fromJsonreadslocationandskipChildren()on all other fields, tolerating the large/computepayload), mirroring the existingInstanceDiscoveryMetadataEntrypattern.parseRegionFromImdsResponsehelper: returnsnullfor blank body, missing/nulllocation, or malformed JSON, so all unusable responses funnel into the existingREGION_SOURCE_FAILED_AUTODETECTpath (matching dotnet's explicit try/catch behavior).RegionDiscoveryTest.java): validlocation,locationamong extra nested fields, missinglocation, nulllocation, malformed JSON, and empty/null body.Note
Unlike the dotnet implementation, msal-java's
discoverRegionhas 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).