Skip to content

Fix Get-AzSubscription silently ignoring mismatched -TenantId under MSI auth - #29994

Open
HarnageaGabriel wants to merge 1 commit into
Azure:mainfrom
HarnageaGabriel:fix-msi-tenant-mismatch-25710
Open

Fix Get-AzSubscription silently ignoring mismatched -TenantId under MSI auth#29994
HarnageaGabriel wants to merge 1 commit into
Azure:mainfrom
HarnageaGabriel:fix-msi-tenant-mismatch-25710

Conversation

@HarnageaGabriel

Copy link
Copy Markdown

Description

Fixes #25710.

Under Managed Service Identity (MSI) authentication, Get-AzSubscription only supports the tenant of the Default Context. When a caller passed -TenantId that did not match DefaultContext.Tenant.Id, the cmdlet silently did nothing — no output, no warning, no error — leaving the caller with no indication their request was ignored.

This change throws a clear PSInvalidOperationException (ErrorKind.UserError) in that mismatch case, with an actionable message pointing to removing -TenantId or using Connect-AzAccount to target a different tenant. Behavior for the matching-tenant case and for non-MSI auth is unchanged.

Mandatory Checklist

  • SHOULD update ChangeLog.md file(s) appropriately
    • Updated src/Accounts/Accounts/ChangeLog.md under ## Upcoming Release.
  • SHOULD regenerate markdown help files if there is cmdlet API change.
    • No parameter/API change, so no markdown help regeneration needed.
  • SHOULD have proper test coverage for changes in pull request.
    • Added GetAzureRMSubscriptionTest.cs covering the MSI tenant-mismatch exception (type, message, ErrorKind).
  • SHOULD NOT adjust version of module manually in pull request
    • Not changed.

Note: this is my first PR to this repo — I understand the Microsoft CLA needs to be signed before merge.

…SI auth

Under Managed Service Identity authentication, Get-AzSubscription only
supports the tenant of the Default Context. Passing a different -TenantId
was silently swallowed (no output, no error), leaving scripts to fail
confusingly downstream. Throw a clear PSInvalidOperationException instead
so the mismatch is obvious and actionable.

Fixes Azure#25710
Copilot AI lite review requested due to automatic review settings August 11, 2026 17:00
@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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

Fixes Get-AzSubscription behavior under Managed Service Identity (MSI) authentication so that providing a -TenantId that does not match the current context’s tenant results in an explicit, actionable error instead of silently returning no output.

Changes:

  • Added an MSI tenant mismatch check in GetAzureRMSubscriptionCommand that throws a PSInvalidOperationException marked as ErrorKind.UserError.
  • Introduced a new localized resource string for the mismatch message and updated the generated resource designer.
  • Added/updated release notes and a unit test covering the new exception behavior.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Accounts/Accounts/Subscription/GetAzureRMSubscription.cs Adds MSI tenant mismatch validation and throws a user-facing error.
src/Accounts/Accounts/Properties/Resources.resx Adds the localized MSI tenant mismatch message text.
src/Accounts/Accounts/Properties/Resources.Designer.cs Adds the strongly-typed accessor for the new resource string.
src/Accounts/Accounts/ChangeLog.md Documents the behavior change for the upcoming release.
src/Accounts/Accounts.Test/UnitTest/GetAzureRMSubscriptionTest.cs Adds a unit test asserting the mismatch exception type/message/ErrorKind.
Files not reviewed (1)
  • src/Accounts/Accounts/Properties/Resources.Designer.cs: Generated file

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +22 to 23
* Fixed `Get-AzSubscription` to throw a clear error instead of silently returning nothing when `-TenantId` does not match the current context under Managed Service Identity (MSI) authentication.

Comment on lines +15 to +20
using Microsoft.Azure.Commands.Common;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using System;
using System.Management.Automation;
using Xunit;
Comment on lines +46 to +52
Assert.Equal(
string.Format(
"The current context is using Managed Service Identity (MSI) authentication which only supports the tenant of the Managed Identity ({0}). The requested TenantId '{1}' does not match and cannot be used. Remove the -TenantId parameter or use Connect-AzAccount with a different authentication method to target a different tenant.",
defaultTenantId,
requestedTenantId),
exception.Message);
Assert.Equal(ErrorKind.UserError, exception.Data[AzurePSErrorDataKeys.ErrorKindKey]);
@a0x1ab Aditya Pujara (a0x1ab) added the azure-client-tools-agent Pull request commented on or reviewed by Azure Client Tools Agent label Aug 12, 2026
@thomas-temby

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines:
Successfully started running 3 pipeline(s).

@HarnageaGabriel

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

azure-client-tools-agent Pull request commented on or reviewed by Azure Client Tools Agent customer-reported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get-AzSubscription command should display warning or fail if non-default tenant id is given when MSi auth is being used

4 participants