Repository navigation
[ACR] az acr import: Add cross-tenant import support
#34210
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
Bin Du (northtyphoon)
wants to merge
3
commits into
Azure:dev
Choose a base branch
from
northtyphoon:acr-import-cross-tenant
base: dev
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.
Open
Changes from all commits
Commits
Show all changes
3 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
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
135 changes: 135 additions & 0 deletions
135
src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_client_factory.py
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,135 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from azure.cli.core.azclierror import ArgumentUsageError, ValidationError | ||
| from azure.cli.command_modules.acr import _client_factory | ||
|
|
||
|
|
||
| class AcrClientFactoryTests(unittest.TestCase): | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_mgmt_service_client') | ||
| def test_acr_service_client_uses_auxiliary_tenants(self, get_mgmt_service_client): | ||
| _client_factory.get_acr_service_client( | ||
| mock.sentinel.cli_ctx, aux_tenants=['source-tenant']) | ||
|
|
||
| _, kwargs = get_mgmt_service_client.call_args | ||
| self.assertEqual(kwargs['aux_tenants'], ['source-tenant']) | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| @mock.patch('azure.cli.core._profile.Profile') | ||
| def test_import_client_uses_registry_tenant(self, profile, get_acr_service_client): | ||
| registries = mock.sentinel.registries | ||
| get_acr_service_client.return_value.registries = registries | ||
| profile.return_value.get_subscription.return_value = {'user': {'type': 'user'}} | ||
| command_args = { | ||
| 'source_registry': ( | ||
| '/subscriptions/source-subscription/resourceGroups/source-rg/providers/' | ||
| 'Microsoft.ContainerRegistry/registries/source-registry'), | ||
| 'registry_tenant': 'source-tenant' | ||
| } | ||
|
|
||
| result = _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| self.assertIs(result, registries) | ||
| get_acr_service_client.assert_called_once_with( | ||
| mock.sentinel.cli_ctx, aux_tenants=['source-tenant']) | ||
| self.assertNotIn('registry_tenant', command_args) | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| def test_import_client_rejects_registry_name_with_registry_tenant(self, get_acr_service_client): | ||
| command_args = { | ||
| 'source_registry': 'source-registry', | ||
| 'registry_tenant': 'source-tenant' | ||
| } | ||
|
|
||
| with self.assertRaisesRegex( | ||
| ArgumentUsageError, | ||
| '--registry-tenant can only be used when --registry is a container registry resource ID'): | ||
| _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| get_acr_service_client.assert_not_called() | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| def test_import_client_rejects_missing_registry_with_registry_tenant(self, get_acr_service_client): | ||
| command_args = {'registry_tenant': 'source-tenant'} | ||
|
|
||
| with self.assertRaisesRegex( | ||
| ArgumentUsageError, | ||
| '--registry-tenant can only be used when --registry is a container registry resource ID'): | ||
| _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| get_acr_service_client.assert_not_called() | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| def test_import_client_rejects_non_registry_resource_id(self, get_acr_service_client): | ||
| command_args = { | ||
| 'source_registry': ( | ||
| '/subscriptions/source-subscription/resourceGroups/source-rg/providers/' | ||
| 'Microsoft.Storage/storageAccounts/source-storage'), | ||
| 'registry_tenant': 'source-tenant' | ||
| } | ||
|
|
||
| with self.assertRaisesRegex( | ||
| ArgumentUsageError, | ||
| '--registry-tenant can only be used when --registry is a container registry resource ID'): | ||
| _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| get_acr_service_client.assert_not_called() | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| @mock.patch('azure.cli.core._profile.Profile') | ||
| def test_import_client_rejects_managed_identity(self, profile, get_acr_service_client): | ||
| profile.return_value.get_subscription.return_value = { | ||
| 'user': {'name': 'systemAssignedIdentity'} | ||
| } | ||
| command_args = { | ||
| 'source_registry': ( | ||
| '/subscriptions/source-subscription/resourceGroups/source-rg/providers/' | ||
| 'Microsoft.ContainerRegistry/registries/source-registry'), | ||
| 'registry_tenant': 'source-tenant' | ||
| } | ||
|
|
||
| with self.assertRaisesRegex( | ||
| ValidationError, | ||
| '--registry-tenant is not supported when signed in with managed identity'): | ||
| _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| get_acr_service_client.assert_not_called() | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| @mock.patch('azure.cli.core._profile.Profile') | ||
| def test_import_client_rejects_cloud_shell(self, profile, get_acr_service_client): | ||
| profile.return_value.get_subscription.return_value = {'user': {'cloudShellID': True}} | ||
| command_args = { | ||
| 'source_registry': ( | ||
| '/subscriptions/source-subscription/resourceGroups/source-rg/providers/' | ||
| 'Microsoft.ContainerRegistry/registries/source-registry'), | ||
| 'registry_tenant': 'source-tenant' | ||
| } | ||
|
|
||
| with self.assertRaisesRegex( | ||
| ValidationError, | ||
| '--registry-tenant is not supported when signed in with managed identity or Cloud Shell'): | ||
| _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| get_acr_service_client.assert_not_called() | ||
|
|
||
| @mock.patch.object(_client_factory, 'get_acr_service_client') | ||
| def test_import_client_without_registry_tenant(self, get_acr_service_client): | ||
| registries = mock.sentinel.registries | ||
| get_acr_service_client.return_value.registries = registries | ||
| command_args = {'source_registry': 'source-registry'} | ||
|
|
||
| result = _client_factory.cf_acr_import(mock.sentinel.cli_ctx, command_args) | ||
|
|
||
| self.assertIs(result, registries) | ||
| get_acr_service_client.assert_called_once_with(mock.sentinel.cli_ctx, aux_tenants=None) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
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.
Addressed in 4136ed7.
--registry-tenantnow rejects managed identity and Cloud Shell login modes before client creation, with focused unit coverage.