Fix Get-AzSubscription silently ignoring mismatched -TenantId under MSI auth - #29994
Open
HarnageaGabriel wants to merge 1 commit into
Open
Fix Get-AzSubscription silently ignoring mismatched -TenantId under MSI auth#29994HarnageaGabriel wants to merge 1 commit into
HarnageaGabriel wants to merge 1 commit into
Conversation
…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
Contributor
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
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
GetAzureRMSubscriptionCommandthat throws aPSInvalidOperationExceptionmarked asErrorKind.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]); |
Contributor
|
/azp run |
Contributor
|
Azure Pipelines: Successfully started running 3 pipeline(s). |
Author
|
@microsoft-github-policy-service agree |
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.
Description
Fixes #25710.
Under Managed Service Identity (MSI) authentication,
Get-AzSubscriptiononly supports the tenant of the Default Context. When a caller passed-TenantIdthat did not matchDefaultContext.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-TenantIdor usingConnect-AzAccountto target a different tenant. Behavior for the matching-tenant case and for non-MSI auth is unchanged.Mandatory Checklist
Please choose the target release of Azure PowerShell. (⚠️ Target release is a different concept from API readiness. Please click below links for details.)
Check this box to confirm: I have read the Submitting Changes section of
CONTRIBUTING.mdand reviewed the following information:ChangeLog.mdfile(s) appropriatelysrc/Accounts/Accounts/ChangeLog.mdunder## Upcoming Release.GetAzureRMSubscriptionTest.cscovering the MSI tenant-mismatch exception (type, message,ErrorKind).Note: this is my first PR to this repo — I understand the Microsoft CLA needs to be signed before merge.