-
Notifications
You must be signed in to change notification settings - Fork 268
Support cross-tenant authentication for remote environment state in Azure Blob Storage #6441
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
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-remote-authentication-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−27
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4481e9
Initial plan
Copilot e521db5
Add subscriptionId support to remote state configuration for cross-te…
Copilot ecf66bb
Add unit tests for cross-tenant blob storage authentication
Copilot b0091cf
Fix environment manager tests to register SubscriptionTenantResolver …
Copilot bcf3456
Fix test assertion order in blob client unit test
Copilot f961b76
Use account.SubscriptionTenantResolver instead of duplicate interface…
Copilot 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
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,155 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/account" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/cloud" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // mockMultiTenantCredentialProvider is a mock implementation for testing | ||
| type mockMultiTenantCredentialProvider struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *mockMultiTenantCredentialProvider) GetTokenCredential( | ||
| ctx context.Context, tenantId string) (azcore.TokenCredential, error) { | ||
| args := m.Called(ctx, tenantId) | ||
| if args.Get(0) == nil { | ||
| return nil, args.Error(1) | ||
| } | ||
| return args.Get(0).(azcore.TokenCredential), args.Error(1) | ||
| } | ||
|
|
||
| // mockSubscriptionTenantResolver is a mock implementation for testing | ||
| type mockSubscriptionTenantResolver struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| var _ account.SubscriptionTenantResolver = (*mockSubscriptionTenantResolver)(nil) | ||
|
|
||
| func (m *mockSubscriptionTenantResolver) LookupTenant( | ||
| ctx context.Context, subscriptionId string) (string, error) { | ||
| args := m.Called(ctx, subscriptionId) | ||
| return args.String(0), args.Error(1) | ||
| } | ||
|
|
||
| // mockTokenCredential is a minimal mock implementation for testing | ||
| type mockTokenCredential struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *mockTokenCredential) GetToken( | ||
| ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error) { | ||
| args := m.Called(ctx, opts) | ||
| return args.Get(0).(azcore.AccessToken), args.Error(1) | ||
| } | ||
|
|
||
| func Test_NewBlobSdkClient_UsesHomeTenantWhenNoSubscriptionId(t *testing.T) { | ||
| mockCredProvider := &mockMultiTenantCredentialProvider{} | ||
| mockTenantResolver := &mockSubscriptionTenantResolver{} | ||
| mockCred := &mockTokenCredential{} | ||
|
|
||
| accountConfig := &AccountConfig{ | ||
| AccountName: "testaccount", | ||
| ContainerName: "testcontainer", | ||
| // No SubscriptionId set - should use home tenant | ||
| } | ||
|
|
||
| coreClientOptions := &azcore.ClientOptions{} | ||
|
|
||
| // Expect credential provider to be called with empty tenant ID (home tenant) | ||
| mockCredProvider.On("GetTokenCredential", mock.Anything, "").Return(mockCred, nil) | ||
|
|
||
| client, err := NewBlobSdkClient( | ||
| mockCredProvider, | ||
| accountConfig, | ||
| coreClientOptions, | ||
| cloud.AzurePublic(), | ||
| mockTenantResolver, | ||
| ) | ||
|
|
||
| require.NoError(t, err) | ||
| require.NotNil(t, client) | ||
| mockCredProvider.AssertExpectations(t) | ||
|
|
||
| // TenantResolver should NOT be called when no subscription ID is provided | ||
| mockTenantResolver.AssertNotCalled(t, "LookupTenant", mock.Anything, mock.Anything) | ||
| } | ||
|
|
||
| func Test_NewBlobSdkClient_ResolvesTenantWhenSubscriptionIdProvided(t *testing.T) { | ||
| mockCredProvider := &mockMultiTenantCredentialProvider{} | ||
| mockTenantResolver := &mockSubscriptionTenantResolver{} | ||
| mockCred := &mockTokenCredential{} | ||
|
|
||
| testSubscriptionId := "test-subscription-id" | ||
| testTenantId := "test-tenant-id" | ||
|
|
||
| accountConfig := &AccountConfig{ | ||
| AccountName: "testaccount", | ||
| ContainerName: "testcontainer", | ||
| SubscriptionId: testSubscriptionId, | ||
| } | ||
|
|
||
| coreClientOptions := &azcore.ClientOptions{} | ||
|
|
||
| // Expect tenant resolver to be called with the subscription ID | ||
| mockTenantResolver.On("LookupTenant", mock.Anything, testSubscriptionId).Return(testTenantId, nil) | ||
|
|
||
| // Expect credential provider to be called with resolved tenant ID | ||
| mockCredProvider.On("GetTokenCredential", mock.Anything, testTenantId).Return(mockCred, nil) | ||
|
|
||
| client, err := NewBlobSdkClient( | ||
| mockCredProvider, | ||
| accountConfig, | ||
| coreClientOptions, | ||
| cloud.AzurePublic(), | ||
| mockTenantResolver, | ||
| ) | ||
|
|
||
| require.NoError(t, err) | ||
| require.NotNil(t, client) | ||
| mockCredProvider.AssertExpectations(t) | ||
| mockTenantResolver.AssertExpectations(t) | ||
| } | ||
|
|
||
| func Test_NewBlobSdkClient_ReturnsErrorWhenTenantResolutionFails(t *testing.T) { | ||
| mockCredProvider := &mockMultiTenantCredentialProvider{} | ||
| mockTenantResolver := &mockSubscriptionTenantResolver{} | ||
|
|
||
| testSubscriptionId := "test-subscription-id" | ||
|
|
||
| accountConfig := &AccountConfig{ | ||
| AccountName: "testaccount", | ||
| ContainerName: "testcontainer", | ||
| SubscriptionId: testSubscriptionId, | ||
| } | ||
|
|
||
| coreClientOptions := &azcore.ClientOptions{} | ||
|
|
||
| // Simulate tenant resolution failure | ||
| mockTenantResolver.On("LookupTenant", mock.Anything, testSubscriptionId). | ||
| Return("", errors.New("subscription not found")) | ||
|
|
||
| client, err := NewBlobSdkClient( | ||
| mockCredProvider, | ||
| accountConfig, | ||
| coreClientOptions, | ||
| cloud.AzurePublic(), | ||
| mockTenantResolver, | ||
| ) | ||
|
|
||
| require.Error(t, err) | ||
| require.Nil(t, client) | ||
| require.Contains(t, err.Error(), "failed to resolve tenant for subscription") | ||
| mockTenantResolver.AssertExpectations(t) | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the third-party
github.com/pkg/errorspackage for new code goes against modern Go best practices. Since Go 1.13, the standard library'serrorspackage andfmt.Errorfwith%wprovide equivalent functionality. For consistency and to avoid unnecessary dependencies, useerrors.New("subscription not found")instead, and import the standarderrorspackage.