Skip to content

fix: use the connected environment's Graph endpoint in Test-MtConditionalAccessWhatIf - #2077

Open
SebastianClaesson wants to merge 3 commits into
maester365:mainfrom
SebastianClaesson:fix/cawhatif-sovereign-cloud-graph-endpoint
Open

fix: use the connected environment's Graph endpoint in Test-MtConditionalAccessWhatIf#2077
SebastianClaesson wants to merge 3 commits into
maester365:mainfrom
SebastianClaesson:fix/cawhatif-sovereign-cloud-graph-endpoint

Conversation

@SebastianClaesson

@SebastianClaesson SebastianClaesson commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

📑 Description

Closes #2076

Test-MtConditionalAccessWhatIf posted to https://graph.microsoft.com/beta/identity/conditionalAccess/evaluate regardless of which cloud the session was connected to. In a sovereign tenant the evaluation request goes to the Global host, so the CA What If tests (MT.1033, MT.1034) cannot pass there even when Connect-MgGraph was given the correct environment.

This was found running Maester against a China tenant, where the session connects to https://microsoftgraph.chinacloudapi.cn but this one call still went to graph.microsoft.com.

The fix derives the base URI from the connected environment, matching how Invoke-MtGraphRequest already resolves it:

$graphBaseUri = ([string](Get-MgEnvironment -Name (Get-MgContext).Environment).GraphEndpoint).TrimEnd('/')

Get-MgEnvironment resolves this correctly for every environment it knows about, so the call now follows the session:

Environment Resolved endpoint
Global https://graph.microsoft.com
China https://microsoftgraph.chinacloudapi.cn
USGov https://graph.microsoft.us
USGovDoD https://dod-graph.microsoft.us
DelosCloud / BleuCloud / GovSGCloud https://graph.svc.sovcloud.{de,fr,sg}

Behaviour in the Global cloud is unchanged.

✅ Checks

  • My pull request adheres to the code style of this project.
  • My code requires changes to the documentation.
  • I have updated the documentation as required.
  • The build and unit tests pass after running /powershell/tests/pester.ps1 locally.

ℹ️ Additional Information

Local test status: the full pester.ps1 suite runs with 10334 tests and 0 failures, and PSScriptAnalyzer reports no findings on the changed file.

Kept deliberately to one function, but as noted in #2076 the same hardcoded-host pattern appears elsewhere and may be worth a follow-up. Happy to cover the remaining call sites in this PR or open separate ones, whichever you prefer:

Location Impact
Test-MtHighRiskAppPermissions (2 calls) Same class of bug — a test that cannot pass outside the Global cloud
Get-MtTotalEntraIdUserCount Internal helper, affects user counts in sovereign tenants
Send-MtMail (2 calls) Mail notifications would target the wrong host
Send-MtTeamsMessage Teams notifications would target the wrong host
GetOrganizationLogos Report branding logo fetch

Invoke-MtAzureRequest also references the Global host, but as an explicit if (-not $baseUri) fallback default, so that one looks intentional and is left alone.

For the record, Test-EIDSCA.Generated.Tests.ps1 contains 44 occurrences of https://graph.microsoft.com, but all of them are literal text inside test descriptions (Check if "https://graph.microsoft.com/beta/...") rather than API calls, so nothing to change there.

Summary by CodeRabbit

  • Bug Fixes
    • Conditional Access What If now uses the connected Microsoft Graph environment, improving compatibility across supported cloud environments.
    • Missing environments default to the Global cloud, and trailing slashes are handled automatically.
    • Clear errors are now shown when no Graph endpoint can be resolved.
    • Existing filtering, results, and error-handling behavior remain unchanged.

…onalAccessWhatIf

Test-MtConditionalAccessWhatIf posted to https://graph.microsoft.com/beta
regardless of which cloud the session was connected to, so the Conditional
Access What If evaluation could only ever work in the Global cloud. In a
sovereign tenant the request goes to the wrong host and the CAWhatIf tests
(MT.1033, MT.1034) cannot pass.

Derive the base URI from the connected environment instead, matching how
Invoke-MtGraphRequest already resolves it:

    (Get-MgEnvironment -Name (Get-MgContext).Environment).GraphEndpoint

This resolves correctly for every environment Get-MgEnvironment knows about,
including China (https://microsoftgraph.chinacloudapi.cn), USGov, USGovDoD and
the newer sovereign clouds.
@SebastianClaesson
SebastianClaesson requested a review from a team as a code owner August 3, 2026 13:03
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ce7fa939-a652-4f9b-a867-cd4979c6f2b3

📥 Commits

Reviewing files that changed from the base of the PR and between 93dd301 and 4841406.

📒 Files selected for processing (1)
  • powershell/public/maester/entra/Test-MtConditionalAccessWhatIf.ps1
🚧 Files skipped from review as they are similar to previous changes (1)
  • powershell/public/maester/entra/Test-MtConditionalAccessWhatIf.ps1

📝 Walkthrough

Walkthrough

The Conditional Access What If request now derives its Microsoft Graph beta endpoint from the connected environment. A missing environment defaults to Global, and unresolved endpoints produce an explicit error. Existing request and result handling remain unchanged.

Changes

Conditional Access What If

Layer / File(s) Summary
Environment-specific Graph endpoint
powershell/public/maester/entra/Test-MtConditionalAccessWhatIf.ps1
The request URI uses the connected environment’s trimmed Graph endpoint. A missing environment defaults to Global, and an empty endpoint raises an explicit error.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: using the connected environment's Graph endpoint.
Description check ✅ Passed The description explains the bug, implementation, affected environments, testing, and related follow-up scope.
Linked Issues check ✅ Passed The change satisfies issue #2076 by deriving the Graph endpoint from the active environment while preserving Global behavior.
Out of Scope Changes check ✅ Passed The changes are limited to Test-MtConditionalAccessWhatIf and do not include unrelated call-site updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@powershell/public/maester/entra/Test-MtConditionalAccessWhatIf.ps1`:
- Around line 214-219: Move the Get-MgContext/Get-MgEnvironment
endpoint-resolution logic into the existing try block so lookup failures reach
its catch handler. Add -ErrorAction Stop, validate that the Graph context and
environment are available, then construct $graphBaseUri from the environment’s
GraphEndpoint using Get-MgEnvironment -Name before invoking the request.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d96ebf02-da4a-4c2f-8b2a-2be316e8bb4c

📥 Commits

Reviewing files that changed from the base of the PR and between 5e15463 and 93dd301.

📒 Files selected for processing (1)
  • powershell/public/maester/entra/Test-MtConditionalAccessWhatIf.ps1

Comment thread powershell/public/maester/entra/Test-MtConditionalAccessWhatIf.ps1 Outdated
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@SebastianClaesson SebastianClaesson changed the title fix: use the connected environment's Graph endpoint in Test-MtConditi… fix: use the connected environment's Graph endpoint in Test-MtConditionalAccessWhatIf Aug 3, 2026
SebastianClaesson and others added 2 commits August 4, 2026 21:04
Move the endpoint resolution into the try so a failed lookup reaches the
existing catch instead of escaping the function.

When no Graph context is available, (Get-MgContext).Environment returns
null and Get-MgEnvironment -Name $null throws a terminating parameter
binding error. Default to Global in that case, consistent with
Initialize-MtSession, so an unconnected session reports the Graph
authentication error rather than a binding failure.

Also guard against an environment name that Get-MgEnvironment cannot
resolve. It returns null silently, which would otherwise collapse the
request URI to a relative path.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

🪲 Test-MtConditionalAccessWhatIf always calls the Global Graph endpoint, breaking CA What If in sovereign clouds

1 participant