-
Notifications
You must be signed in to change notification settings - Fork 341
fix: resolve principal ID in resource tenant for preflight role check #7174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
| "github.com/AzureAD/microsoft-authentication-library-for-go/apps/public" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/cloud" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // spyPublicClient records the options passed to AcquireTokenSilent so tests can | ||
| // verify that GetToken forwards TenantID correctly. | ||
| type spyPublicClient struct { | ||
| mockPublicClient | ||
| silentOptions []public.AcquireSilentOption | ||
| } | ||
|
|
||
| func (s *spyPublicClient) AcquireTokenSilent( | ||
| ctx context.Context, scopes []string, options ...public.AcquireSilentOption, | ||
| ) (public.AuthResult, error) { | ||
| s.silentOptions = options | ||
| return public.AuthResult{ | ||
| AccessToken: "test-token", | ||
| ExpiresOn: time.Now().Add(time.Hour), | ||
| Account: public.Account{ | ||
| HomeAccountID: "test.id", | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| func TestAzdCredential_GetToken_ForwardsTenantID(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| credTID string // tenant stored in credential | ||
| optsTID string // tenant from TokenRequestOptions | ||
| // expectTenantOption is true when WithTenantID should be in the options | ||
| expectTenantOption bool | ||
| // expectedTenantID is the tenant ID value that should be forwarded. | ||
| // When optsTID is set it overrides credTID. | ||
| expectedTenantID string | ||
| }{ | ||
| { | ||
| name: "CredentialTenantUsed", | ||
| credTID: "resource-tenant-id", | ||
| optsTID: "", | ||
| expectTenantOption: true, | ||
| expectedTenantID: "resource-tenant-id", | ||
| }, | ||
| { | ||
| name: "OptionsTenantOverrides", | ||
| credTID: "resource-tenant-id", | ||
| optsTID: "override-tenant-id", | ||
| expectTenantOption: true, | ||
| expectedTenantID: "override-tenant-id", | ||
| }, | ||
| { | ||
| name: "NoTenant", | ||
| credTID: "", | ||
| optsTID: "", | ||
| expectTenantOption: false, | ||
| expectedTenantID: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| spy := &spyPublicClient{} | ||
| account := &public.Account{HomeAccountID: "test.id"} | ||
| cred := newAzdCredential(spy, account, cloud.AzurePublic(), tt.credTID) | ||
|
|
||
| _, err := cred.GetToken(t.Context(), policy.TokenRequestOptions{ | ||
| Scopes: []string{"https://graph.microsoft.com/.default"}, | ||
| TenantID: tt.optsTID, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| if tt.expectTenantOption { | ||
| require.Len(t, spy.silentOptions, 3, | ||
| "expected WithSilentAccount + WithClaims + WithTenantID") | ||
| } else { | ||
| require.Len(t, spy.silentOptions, 2, | ||
| "expected WithSilentAccount + WithClaims only") | ||
| } | ||
|
|
||
| // Verify the resolved tenant ID matches expectations. | ||
| // The credential resolves: optsTID if non-empty, else credTID. | ||
| // MSAL's WithTenantID option is opaque, so we verify the credential's | ||
| // tenant resolution logic directly (same package gives access to internals). | ||
| resolvedTenant := cred.tenantID | ||
| if tt.optsTID != "" { | ||
| resolvedTenant = tt.optsTID | ||
| } | ||
| require.Equal(t, tt.expectedTenantID, resolvedTenant, | ||
| "resolved tenant ID should match expected value") | ||
| }) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.