diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_anonymous_exchange_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_anonymous_exchange_client.py index 6ad07c0e8f9b..df07248be264 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_anonymous_exchange_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_anonymous_exchange_client.py @@ -29,13 +29,11 @@ def __init__(self, endpoint, **kwargs): # pylint: disable=missing-client-constr if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint self._endpoint = endpoint - self.credential_scope = "https://management.core.windows.net/.default" self._client = ContainerRegistry( credential=None, url=endpoint, sdk_moniker=USER_AGENT, authentication_policy=ExchangeClientAuthenticationPolicy(), - credential_scopes=kwargs.pop("credential_scopes", self.credential_scope), **kwargs ) diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_authentication_policy.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_authentication_policy.py index 590d54304f34..202a14dddc79 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_authentication_policy.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_authentication_policy.py @@ -25,7 +25,7 @@ def __init__(self, credential, endpoint, **kwargs): super(ContainerRegistryChallengePolicy, self).__init__() self._credential = credential if self._credential is None: - self._exchange_client = AnonymousACRExchangeClient(endpoint) + self._exchange_client = AnonymousACRExchangeClient(endpoint, **kwargs) else: self._exchange_client = ACRExchangeClient(endpoint, self._credential, **kwargs) diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_base_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_base_client.py index ecfd8228a9f5..2b79ade179cf 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_base_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_base_client.py @@ -28,8 +28,8 @@ class ContainerRegistryBaseClient(object): :param str endpoint: Azure Container Registry endpoint :param credential: AAD Token for authenticating requests with Azure :type credential: :class:`azure.identity.DefaultTokenCredential` - :keyword authentication_scope: URL for credential authentication if different from the default - :paramtype authentication_scope: str + :keyword credential_scopes: URL for credential authentication if different from the default + :paramtype credential_scopes: List[str] """ def __init__(self, endpoint, credential, **kwargs): @@ -40,7 +40,6 @@ def __init__(self, endpoint, credential, **kwargs): url=endpoint, sdk_moniker=USER_AGENT, authentication_policy=auth_policy, - credential_scopes=kwargs.get("credential_scopes", "https://management.core.windows.net/.default"), **kwargs ) diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py index 2e0e2e0467db..8a2ca2c0c170 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_container_registry_client.py @@ -31,8 +31,8 @@ def __init__(self, endpoint, credential=None, **kwargs): :param str endpoint: An ACR endpoint :param credential: The credential with which to authenticate :type credential: :class:`~azure.core.credentials.TokenCredential` - :keyword authentication_scope: URL for credential authentication if different from the default - :paramtype authentication_scope: str + :keyword credential_scopes: URL for credential authentication if different from the default + :paramtype credential_scopes: List[str] :returns: None :raises: None diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_exchange_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_exchange_client.py index 1662c138e066..502b5dd8719a 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_exchange_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/_exchange_client.py @@ -43,13 +43,12 @@ def __init__(self, endpoint, credential, **kwargs): if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint self._endpoint = endpoint - self.credential_scope = kwargs.get("authentication_scope", "https://management.core.windows.net/.default") + self.credential_scopes = kwargs.get("credential_scopes", ["https://management.core.windows.net/.default"]) self._client = ContainerRegistry( credential=credential, url=endpoint, sdk_moniker=USER_AGENT, authentication_policy=ExchangeClientAuthenticationPolicy(), - credential_scopes=self.credential_scope, **kwargs ) self._credential = credential @@ -74,7 +73,7 @@ def get_refresh_token(self, service, **kwargs): def exchange_aad_token_for_refresh_token(self, service=None, **kwargs): # type: (str, Dict[str, Any]) -> str refresh_token = self._client.authentication.exchange_aad_access_token_for_acr_refresh_token( - service=service, access_token=self._credential.get_token(self.credential_scope).token, **kwargs + service=service, access_token=self._credential.get_token(*self.credential_scopes).token, **kwargs ) return refresh_token.refresh_token diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_anonymous_exchange_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_anonymous_exchange_client.py index 77d7f8ed8f0b..78575b90e9ac 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_anonymous_exchange_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_anonymous_exchange_client.py @@ -28,13 +28,11 @@ def __init__( # pylint: disable=missing-client-constructor-parameter-credential if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint self._endpoint = endpoint - self._credential_scope = "https://management.core.windows.net/.default" self._client = ContainerRegistry( credential=None, url=endpoint, sdk_moniker=USER_AGENT, authentication_policy=ExchangeClientAuthenticationPolicy(), - credential_scopes=kwargs.pop("credential_scopes", self._credential_scope), **kwargs ) diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_base_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_base_client.py index 2b8d229bb2f9..4b5fcbfe18b8 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_base_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_base_client.py @@ -28,8 +28,8 @@ class ContainerRegistryBaseClient(object): :type endpoint: str :param credential: AAD Token for authenticating requests with Azure :type credential: :class:`~azure.identity.DefaultTokenCredential` - :keyword authentication_scope: URL for credential authentication if different from the default - :paramtype authentication_scope: str + :keyword credential_scopes: URL for credential authentication if different from the default + :paramtype credential_scopes: List[str] """ def __init__(self, endpoint: str, credential: Optional["AsyncTokenCredential"] = None, **kwargs) -> None: @@ -39,7 +39,6 @@ def __init__(self, endpoint: str, credential: Optional["AsyncTokenCredential"] = url=endpoint, sdk_moniker=USER_AGENT, authentication_policy=auth_policy, - credential_scopes=kwargs.get("credential_scopes", "https://management.core.windows.net/.default"), **kwargs ) diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py index 3e48bef324ce..f6c7c11b0090 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_container_registry_client.py @@ -33,8 +33,8 @@ def __init__(self, endpoint: str, credential: Optional["AsyncTokenCredential"] = :type endpoint: str :param credential: The credential with which to authenticate :type credential: :class:`~azure.core.credentials_async.AsyncTokenCredential` - :keyword authentication_scope: URL for credential authentication if different from the default - :paramtype authentication_scope: str + :keyword credential_scopes: URL for credential authentication if different from the default + :paramtype credential_scopes: List[str] :returns: None :raises: None diff --git a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_exchange_client.py b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_exchange_client.py index 343ca99667a2..5ce2c48d7354 100644 --- a/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_exchange_client.py +++ b/sdk/containerregistry/azure-containerregistry/azure/containerregistry/aio/_async_exchange_client.py @@ -40,13 +40,12 @@ def __init__(self, endpoint: str, credential: "AsyncTokencredential", **kwargs: if not endpoint.startswith("https://") and not endpoint.startswith("http://"): endpoint = "https://" + endpoint self._endpoint = endpoint - self._credential_scope = kwargs.get("authentication_scope", "https://management.core.windows.net/.default") + self.credential_scopes = kwargs.get("credential_scopes", ["https://management.core.windows.net/.default"]) self._client = ContainerRegistry( credential=credential, url=endpoint, sdk_moniker=USER_AGENT, authentication_policy=ExchangeClientAuthenticationPolicy(), - credential_scopes=self._credential_scope, **kwargs ) self._credential = credential @@ -67,7 +66,7 @@ async def get_refresh_token(self, service: str, **kwargs: Dict[str, Any]) -> str return self._refresh_token async def exchange_aad_token_for_refresh_token(self, service: str = None, **kwargs: Dict[str, Any]) -> str: - token = await self._credential.get_token(self._credential_scope) + token = await self._credential.get_token(*self.credential_scopes) refresh_token = await self._client.authentication.exchange_aad_access_token_for_acr_refresh_token( service, token.token, **kwargs ) diff --git a/sdk/containerregistry/azure-containerregistry/dev_requirements.txt b/sdk/containerregistry/azure-containerregistry/dev_requirements.txt index 29c33435b996..49ec18358303 100644 --- a/sdk/containerregistry/azure-containerregistry/dev_requirements.txt +++ b/sdk/containerregistry/azure-containerregistry/dev_requirements.txt @@ -3,4 +3,5 @@ ../../core/azure-core ../azure-mgmt-containerregistry aiohttp>=3.0; python_version >= '3.5' -azure-identity \ No newline at end of file +azure-identity +msrestazure>=0.4.11 \ No newline at end of file diff --git a/sdk/containerregistry/azure-containerregistry/tests/asynctestcase.py b/sdk/containerregistry/azure-containerregistry/tests/asynctestcase.py index 58cee32899c2..ab5455990971 100644 --- a/sdk/containerregistry/azure-containerregistry/tests/asynctestcase.py +++ b/sdk/containerregistry/azure-containerregistry/tests/asynctestcase.py @@ -3,15 +3,20 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------ +import logging +import os + from azure.containerregistry.aio import ( - # ContainerRepository, ContainerRegistryClient, ) from azure.core.credentials import AccessToken -from azure.identity.aio import DefaultAzureCredential +from azure.identity.aio import DefaultAzureCredential, ClientSecretCredential +from azure.identity import AzureAuthorityHosts + +from testcase import ContainerRegistryTestClass, get_authorization_scope, get_authority -from testcase import ContainerRegistryTestClass +logger = logging.getLogger() class AsyncFakeTokenCredential(object): @@ -30,25 +35,27 @@ class AsyncContainerRegistryTestClass(ContainerRegistryTestClass): def __init__(self, method_name): super(AsyncContainerRegistryTestClass, self).__init__(method_name) - def get_credential(self): + def get_credential(self, authority=None, **kwargs): if self.is_live: - return DefaultAzureCredential() + if authority != AzureAuthorityHosts.AZURE_PUBLIC_CLOUD: + return ClientSecretCredential( + tenant_id=os.environ["CONTAINERREGISTRY_TENANT_ID"], + client_id=os.environ["CONTAINERREGISTRY_CLIENT_ID"], + client_secret=os.environ["CONTAINERREGISTRY_CLIENT_SECRET"], + authority=authority + ) + return DefaultAzureCredential(**kwargs) return AsyncFakeTokenCredential() def create_registry_client(self, endpoint, **kwargs): - return ContainerRegistryClient( - endpoint=endpoint, - credential=self.get_credential(), - **kwargs, - ) - - def create_container_repository(self, endpoint, name, **kwargs): - return ContainerRepository( - endpoint=endpoint, - name=name, - credential=self.get_credential(), - **kwargs, - ) + authority = get_authority(endpoint) + audience = kwargs.pop("audience", None) + if not audience: + audience = get_authorization_scope(authority) + credential = self.get_credential(authority=authority) + return ContainerRegistryClient(endpoint=endpoint, credential=credential, credential_scopes=audience, **kwargs) def create_anon_client(self, endpoint, **kwargs): - return ContainerRegistryClient(endpoint=endpoint, credential=None, **kwargs) + authority = get_authority(endpoint) + audience = get_authorization_scope(authority) + return ContainerRegistryClient(endpoint=endpoint, credential=None, credential_scopes=audience, **kwargs) diff --git a/sdk/containerregistry/azure-containerregistry/tests/test_anon_access.py b/sdk/containerregistry/azure-containerregistry/tests/test_anon_access.py index 3d07b48f3366..345d14a17c52 100644 --- a/sdk/containerregistry/azure-containerregistry/tests/test_anon_access.py +++ b/sdk/containerregistry/azure-containerregistry/tests/test_anon_access.py @@ -22,6 +22,9 @@ class TestContainerRegistryClient(ContainerRegistryTestClass): @acr_preparer() def test_list_repository_names(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -40,6 +43,9 @@ def test_list_repository_names(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_list_repository_names_by_page(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -63,6 +69,9 @@ def test_list_repository_names_by_page(self, containerregistry_anonregistry_endp @acr_preparer() def test_get_repository_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -73,6 +82,9 @@ def test_get_repository_properties(self, containerregistry_anonregistry_endpoint @acr_preparer() def test_list_manifest_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -84,6 +96,9 @@ def test_list_manifest_properties(self, containerregistry_anonregistry_endpoint) @acr_preparer() def test_get_manifest_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -95,6 +110,9 @@ def test_get_manifest_properties(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_list_tag_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -106,6 +124,9 @@ def test_list_tag_properties(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_delete_repository(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -114,6 +135,9 @@ def test_delete_repository(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_delete_tag(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -122,6 +146,9 @@ def test_delete_tag(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_delete_manifest(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -130,6 +157,9 @@ def test_delete_manifest(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_update_repository_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) properties = client.get_repository_properties(HELLO_WORLD) @@ -139,6 +169,9 @@ def test_update_repository_properties(self, containerregistry_anonregistry_endpo @acr_preparer() def test_update_tag_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) properties = client.get_tag_properties(HELLO_WORLD, "latest") @@ -148,9 +181,12 @@ def test_update_tag_properties(self, containerregistry_anonregistry_endpoint): @acr_preparer() def test_update_manifest_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) properties = client.get_manifest_properties(HELLO_WORLD, "latest") with pytest.raises(ClientAuthenticationError): - client.update_manifest_properties(HELLO_WORLD, "latest", properties, can_delete=True) + client.update_manifest_properties(HELLO_WORLD, "latest", properties, can_delete=True) \ No newline at end of file diff --git a/sdk/containerregistry/azure-containerregistry/tests/test_anon_access_async.py b/sdk/containerregistry/azure-containerregistry/tests/test_anon_access_async.py index a0cda6b7b4d8..4ecb33eae239 100644 --- a/sdk/containerregistry/azure-containerregistry/tests/test_anon_access_async.py +++ b/sdk/containerregistry/azure-containerregistry/tests/test_anon_access_async.py @@ -23,6 +23,9 @@ class TestContainerRegistryClient(AsyncContainerRegistryTestClass): @acr_preparer() async def test_list_repository_names(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -41,6 +44,9 @@ async def test_list_repository_names(self, containerregistry_anonregistry_endpoi @acr_preparer() async def test_list_repository_names_by_page(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -64,6 +70,9 @@ async def test_list_repository_names_by_page(self, containerregistry_anonregistr @acr_preparer() async def test_get_repository_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -74,6 +83,9 @@ async def test_get_repository_properties(self, containerregistry_anonregistry_en @acr_preparer() async def test_list_manifest_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -85,6 +97,9 @@ async def test_list_manifest_properties(self, containerregistry_anonregistry_end @acr_preparer() async def test_get_manifest_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -96,6 +111,9 @@ async def test_get_manifest_properties(self, containerregistry_anonregistry_endp @acr_preparer() async def test_list_tag_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -107,6 +125,9 @@ async def test_list_tag_properties(self, containerregistry_anonregistry_endpoint @acr_preparer() async def test_delete_repository(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -115,6 +136,9 @@ async def test_delete_repository(self, containerregistry_anonregistry_endpoint): @acr_preparer() async def test_delete_tag(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -123,6 +147,9 @@ async def test_delete_tag(self, containerregistry_anonregistry_endpoint): @acr_preparer() async def test_delete_manifest(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) assert client._credential is None @@ -131,6 +158,9 @@ async def test_delete_manifest(self, containerregistry_anonregistry_endpoint): @acr_preparer() async def test_update_repository_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) properties = await client.get_repository_properties(HELLO_WORLD) @@ -140,6 +170,9 @@ async def test_update_repository_properties(self, containerregistry_anonregistry @acr_preparer() async def test_update_tag_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) properties = await client.get_tag_properties(HELLO_WORLD, "latest") @@ -149,9 +182,12 @@ async def test_update_tag_properties(self, containerregistry_anonregistry_endpoi @acr_preparer() async def test_update_manifest_properties(self, containerregistry_anonregistry_endpoint): + if not self.is_public_endpoint(containerregistry_anonregistry_endpoint): + pytest.skip("Not a public endpoint") + client = self.create_anon_client(containerregistry_anonregistry_endpoint) properties = await client.get_manifest_properties(HELLO_WORLD, "latest") with pytest.raises(ClientAuthenticationError): - await client.update_manifest_properties(HELLO_WORLD, "latest", properties, can_delete=True) + await client.update_manifest_properties(HELLO_WORLD, "latest", properties, can_delete=True) \ No newline at end of file diff --git a/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client.py b/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client.py index e9d2e9e38503..33b2ce982773 100644 --- a/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client.py +++ b/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client.py @@ -4,6 +4,7 @@ # Licensed under the MIT License. # ------------------------------------ from datetime import datetime +from azure.core import credentials import pytest import six import time @@ -14,11 +15,12 @@ ManifestOrder, ArtifactTagProperties, TagOrder, + ContainerRegistryClient, ) from azure.core.exceptions import ResourceNotFoundError, ClientAuthenticationError from azure.core.paging import ItemPaged -from testcase import ContainerRegistryTestClass +from testcase import ContainerRegistryTestClass, get_authority from constants import TO_BE_DELETED, HELLO_WORLD, ALPINE, BUSYBOX, DOES_NOT_EXIST from preparer import acr_preparer @@ -64,7 +66,7 @@ def test_list_repository_names_by_page(self, containerregistry_endpoint): @acr_preparer() def test_delete_repository(self, containerregistry_endpoint, containerregistry_resource_group): - self.import_image(HELLO_WORLD, [TO_BE_DELETED]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, [TO_BE_DELETED]) client = self.create_registry_client(containerregistry_endpoint) client.delete_repository(TO_BE_DELETED) @@ -91,7 +93,7 @@ def test_get_repository_properties(self, containerregistry_endpoint): def test_update_repository_properties(self, containerregistry_endpoint): repository = self.get_resource_name("repo") tag_identifier = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repository, tag_identifier)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repository, tag_identifier)]) client = self.create_registry_client(containerregistry_endpoint) properties = client.get_repository_properties(repository) @@ -123,7 +125,7 @@ def test_update_repository_properties(self, containerregistry_endpoint): def test_update_repository_properties_kwargs(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -249,7 +251,7 @@ def test_list_registry_artifacts_ascending(self, containerregistry_endpoint): def test_get_manifest_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -263,7 +265,7 @@ def test_get_manifest_properties(self, containerregistry_endpoint): def test_get_manifest_properties_does_not_exist(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -280,7 +282,7 @@ def test_get_manifest_properties_does_not_exist(self, containerregistry_endpoint def test_update_manifest_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -313,7 +315,7 @@ def test_update_manifest_properties(self, containerregistry_endpoint): def test_update_manifest_properties_kwargs(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -343,7 +345,7 @@ def test_update_manifest_properties_kwargs(self, containerregistry_endpoint): def test_get_tag_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -363,7 +365,7 @@ def test_get_tag_properties_does_not_exist(self, containerregistry_endpoint): def test_update_tag_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -395,7 +397,7 @@ def test_update_tag_properties(self, containerregistry_endpoint): def test_update_tag_properties_kwargs(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -426,7 +428,7 @@ def test_list_tag_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -441,7 +443,7 @@ def test_list_tag_properties_order_descending(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -470,7 +472,7 @@ def test_list_tag_properties_order_ascending(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -499,7 +501,7 @@ def test_delete_tag(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -521,7 +523,7 @@ def test_delete_tag_does_not_exist(self, containerregistry_endpoint): def test_delete_manifest(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) client.delete_manifest(repo, tag) @@ -535,7 +537,7 @@ def test_delete_manifest(self, containerregistry_endpoint): def test_delete_manifest_does_not_exist(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -569,8 +571,10 @@ def test_expiration_time_parsing(self, containerregistry_endpoint): # Live only, the fake credential doesn't check auth scope the same way @pytest.mark.live_test_only @acr_preparer() - def test_incorrect_authentication_scope(self, containerregistry_endpoint): - client = self.create_registry_client(containerregistry_endpoint, authentication_scope="https://microsoft.com") + def test_incorrect_credential_scopes(self, containerregistry_endpoint): + authority = get_authority(containerregistry_endpoint) + credential = self.get_credential(authority) + client = ContainerRegistryClient(endpoint=containerregistry_endpoint, credential=credential, credential_scopes="https://microsoft.com") with pytest.raises(ClientAuthenticationError): - properties = client.get_repository_properties(HELLO_WORLD) + properties = client.get_repository_properties(HELLO_WORLD) \ No newline at end of file diff --git a/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client_async.py b/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client_async.py index 5563c1837ae8..7493e8d0986b 100644 --- a/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client_async.py +++ b/sdk/containerregistry/azure-containerregistry/tests/test_container_registry_client_async.py @@ -14,12 +14,14 @@ ArtifactTagProperties, TagOrder, ) +from azure.containerregistry.aio import ContainerRegistryClient from azure.core.exceptions import ResourceNotFoundError, ClientAuthenticationError from azure.core.async_paging import AsyncItemPaged from asynctestcase import AsyncContainerRegistryTestClass from constants import TO_BE_DELETED, HELLO_WORLD, ALPINE, BUSYBOX, DOES_NOT_EXIST from preparer import acr_preparer +from testcase import get_authority class TestContainerRegistryClient(AsyncContainerRegistryTestClass): @@ -63,7 +65,7 @@ async def test_list_repository_names_by_page(self, containerregistry_endpoint): @acr_preparer() async def test_delete_repository(self, containerregistry_endpoint, containerregistry_resource_group): - self.import_image(HELLO_WORLD, [TO_BE_DELETED]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, [TO_BE_DELETED]) client = self.create_registry_client(containerregistry_endpoint) await client.delete_repository(TO_BE_DELETED) @@ -90,7 +92,7 @@ async def test_get_repository_properties(self, containerregistry_endpoint): async def test_update_properties(self, containerregistry_endpoint): repository = self.get_resource_name("repo") tag_identifier = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repository, tag_identifier)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repository, tag_identifier)]) client = self.create_registry_client(containerregistry_endpoint) properties = await client.get_repository_properties(repository) @@ -123,7 +125,7 @@ async def test_update_properties(self, containerregistry_endpoint): async def test_update_repository_properties_kwargs(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -253,7 +255,7 @@ async def test_list_registry_artifacts_ascending(self, containerregistry_endpoin async def test_get_manifest_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -274,7 +276,7 @@ async def test_get_manifest_properties_does_not_exist(self, containerregistry_en async def test_update_manifest_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -307,7 +309,7 @@ async def test_update_manifest_properties(self, containerregistry_endpoint): async def test_update_manifest_properties_kwargs(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -337,7 +339,7 @@ async def test_update_manifest_properties_kwargs(self, containerregistry_endpoin async def test_get_tag_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -357,7 +359,7 @@ async def test_get_tag_properties_does_not_exist(self, containerregistry_endpoin async def test_update_tag_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -389,7 +391,7 @@ async def test_update_tag_properties(self, containerregistry_endpoint): async def test_update_tag_properties_kwargs(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -420,7 +422,7 @@ async def test_list_tag_properties(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -435,7 +437,7 @@ async def test_list_tag_properties_order_descending(self, containerregistry_endp repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -464,7 +466,7 @@ async def test_list_tag_properties_order_ascending(self, containerregistry_endpo repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -493,7 +495,7 @@ async def test_delete_tag(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") tags = ["{}:{}".format(repo, tag + str(i)) for i in range(4)] - self.import_image(HELLO_WORLD, tags) + self.import_image(containerregistry_endpoint, HELLO_WORLD, tags) client = self.create_registry_client(containerregistry_endpoint) @@ -515,7 +517,7 @@ async def test_delete_tag_does_not_exist(self, containerregistry_endpoint): async def test_delete_manifest(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) await client.delete_manifest(repo, tag) @@ -528,7 +530,7 @@ async def test_delete_manifest(self, containerregistry_endpoint): async def test_delete_manifest_does_not_exist(self, containerregistry_endpoint): repo = self.get_resource_name("repo") tag = self.get_resource_name("tag") - self.import_image(HELLO_WORLD, ["{}:{}".format(repo, tag)]) + self.import_image(containerregistry_endpoint, HELLO_WORLD, ["{}:{}".format(repo, tag)]) client = self.create_registry_client(containerregistry_endpoint) @@ -562,8 +564,10 @@ async def test_expiration_time_parsing(self, containerregistry_endpoint): # Live only, the fake credential doesn't check auth scope the same way @pytest.mark.live_test_only @acr_preparer() - async def test_incorrect_authentication_scope(self, containerregistry_endpoint): - client = self.create_registry_client(containerregistry_endpoint, authentication_scope="https://microsoft.com") + async def test_incorrect_credential_scopes(self, containerregistry_endpoint): + authority = get_authority(containerregistry_endpoint) + credential = self.get_credential(authority) + client = ContainerRegistryClient(endpoint=containerregistry_endpoint, credential=credential, credential_scopes="https://microsoft.com") with pytest.raises(ClientAuthenticationError): - properties = await client.get_repository_properties(HELLO_WORLD) + properties = await client.get_repository_properties(HELLO_WORLD) \ No newline at end of file diff --git a/sdk/containerregistry/azure-containerregistry/tests/testcase.py b/sdk/containerregistry/azure-containerregistry/tests/testcase.py index a6dc91ce9799..fd1c89267b9c 100644 --- a/sdk/containerregistry/azure-containerregistry/tests/testcase.py +++ b/sdk/containerregistry/azure-containerregistry/tests/testcase.py @@ -5,6 +5,7 @@ # ------------------------------------ import copy import json +import logging import os import pytest import six @@ -18,7 +19,7 @@ from azure.core.credentials import AccessToken from azure.mgmt.containerregistry import ContainerRegistryManagementClient from azure.mgmt.containerregistry.models import ImportImageParameters, ImportSource, ImportMode -from azure.identity import DefaultAzureCredential +from azure.identity import DefaultAzureCredential, AzureAuthorityHosts from devtools_testutils import AzureTestCase, is_live from azure_devtools.scenario_tests import ( @@ -29,6 +30,7 @@ REDACTED = "REDACTED" +logger = logging.getLogger() class OAuthRequestResponsesFilterACR(RecordingProcessor): @@ -149,23 +151,39 @@ def sleep(self, t): if self.is_live: time.sleep(t) - def import_image(self, repository, tags): + def import_image(self, endpoint, repository, tags): # repository must be a docker hub repository # tags is a List of repository/tag combos in the format : if not self.is_live: return - import_image(repository, tags) + authority = get_authority(endpoint) + import_image(authority, repository, tags) - def get_credential(self): + def get_credential(self, authority=None, **kwargs): if self.is_live: - return DefaultAzureCredential() + if authority != AzureAuthorityHosts.AZURE_PUBLIC_CLOUD: + return ClientSecretCredential( + tenant_id=os.environ["CONTAINERREGISTRY_TENANT_ID"], + client_id=os.environ["CONTAINERREGISTRY_CLIENT_ID"], + client_secret=os.environ["CONTAINERREGISTRY_CLIENT_SECRET"], + authority=authority + ) + return DefaultAzureCredential(**kwargs) return FakeTokenCredential() def create_registry_client(self, endpoint, **kwargs): - return ContainerRegistryClient(endpoint=endpoint, credential=self.get_credential(), **kwargs) + authority = get_authority(endpoint) + audience = kwargs.pop("audience", None) + if not audience: + audience = get_authorization_scope(authority) + credential = self.get_credential(authority=authority) + logger.warning("Authority: {} \nAuthorization scope: {}".format(authority, audience)) + return ContainerRegistryClient(endpoint=endpoint, credential=credential, credential_scopes=audience, **kwargs) def create_anon_client(self, endpoint, **kwargs): - return ContainerRegistryClient(endpoint=endpoint, credential=None, **kwargs) + authority = get_authority(endpoint) + audience = get_authorization_scope(authority) + return ContainerRegistryClient(endpoint=endpoint, credential=None, credential_scopes=audience, **kwargs) def set_all_properties(self, properties, value): properties.can_delete = value @@ -188,12 +206,66 @@ def create_fully_qualified_reference(self, registry, repository, digest): digest.split(":")[-1] ) + def is_public_endpoint(self, endpoint): + return ".azurecr.io" in endpoint + + +def get_authority(endpoint): + if ".azurecr.io" in endpoint: + logger.warning("Public cloud Authority:") + return AzureAuthorityHosts.AZURE_PUBLIC_CLOUD + if ".azurecr.cn" in endpoint: + logger.warning("China Authority:") + return AzureAuthorityHosts.AZURE_CHINA + if ".azurecr.us" in endpoint: + logger.warning("US Gov Authority:") + return AzureAuthorityHosts.AZURE_GOVERNMENT + raise ValueError("Endpoint ({}) could not be understood".format(endpoint)) + + +def get_authorization_scope(authority): + if authority == AzureAuthorityHosts.AZURE_PUBLIC_CLOUD: + logger.warning("Public auth scope") + return ["https://management.core.windows.net/.default"] + if authority == AzureAuthorityHosts.AZURE_CHINA: + logger.warning("China scope") + return ["https://management.chinacloudapi.cn/.default"] + if authority == AzureAuthorityHosts.AZURE_GOVERNMENT: + logger.warning("US Gov scope") + return ["https://management.usgovcloudapi.net/.default"] + +def get_base_url(authority): + if authority == AzureAuthorityHosts.AZURE_PUBLIC_CLOUD: + logger.warning("Public auth scope") + return AZURE_PUBLIC_CLOUD + if authority == AzureAuthorityHosts.AZURE_CHINA: + logger.warning("China scope") + return AZURE_CHINA_CLOUD + if authority == AzureAuthorityHosts.AZURE_GOVERNMENT: + logger.warning("US Gov scope") + return AZURE_US_GOV_CLOUD + + + +from azure.identity import ClientSecretCredential +from msrestazure.azure_cloud import AZURE_CHINA_CLOUD, AZURE_US_GOV_CLOUD, AZURE_PUBLIC_CLOUD # Moving this out of testcase so the fixture and individual tests can use it -def import_image(repository, tags): +def import_image(authority, repository, tags): + logger.warning("Import image authority: {}".format(authority)) + credential = ClientSecretCredential( + tenant_id=os.environ["CONTAINERREGISTRY_TENANT_ID"], + client_id=os.environ["CONTAINERREGISTRY_CLIENT_ID"], + client_secret=os.environ["CONTAINERREGISTRY_CLIENT_SECRET"], + authority=authority + ) + sub_id = os.environ["CONTAINERREGISTRY_SUBSCRIPTION_ID"] + base_url = get_base_url(authority) + audience = [base_url.endpoints.resource_manager + "/.default"] mgmt_client = ContainerRegistryManagementClient( - DefaultAzureCredential(), os.environ["CONTAINERREGISTRY_SUBSCRIPTION_ID"], api_version="2019-05-01" + credential, sub_id, api_version="2019-05-01", base_url=base_url.endpoints.resource_manager, credential_scopes=audience ) + logger.warning("LOGGING: {}{}".format(os.environ["CONTAINERREGISTRY_SUBSCRIPTION_ID"], os.environ["CONTAINERREGISTRY_TENANT_ID"])) registry_uri = "registry.hub.docker.com" rg_name = os.environ["CONTAINERREGISTRY_RESOURCE_GROUP"] registry_name = os.environ["CONTAINERREGISTRY_REGISTRY_NAME"] @@ -213,7 +285,7 @@ def import_image(repository, tags): # Do the same for anonymous mgmt_client = ContainerRegistryManagementClient( - DefaultAzureCredential(), os.environ["CONTAINERREGISTRY_SUBSCRIPTION_ID"], api_version="2019-05-01" + credential, sub_id, api_version="2019-05-01", base_url=base_url.endpoints.resource_manager, credential_scopes=audience ) registry_uri = "registry.hub.docker.com" rg_name = os.environ["CONTAINERREGISTRY_RESOURCE_GROUP"] @@ -237,6 +309,7 @@ def import_image(repository, tags): def load_registry(): if not is_live(): return + authority = get_authority(os.environ.get("CONTAINERREGISTRY_ENDPOINT")) repos = [ "library/hello-world", "library/alpine", @@ -255,6 +328,6 @@ def load_registry(): ] for repo, tag in zip(repos, tags): try: - import_image(repo, tag) + import_image(authority, repo, tag) except Exception as e: - print(e) + print(e) \ No newline at end of file diff --git a/sdk/containerregistry/test-resources.json b/sdk/containerregistry/test-resources.json index 025f14e2f826..948b29d69bf8 100644 --- a/sdk/containerregistry/test-resources.json +++ b/sdk/containerregistry/test-resources.json @@ -15,13 +15,17 @@ "metadata": { "description": "The location of the resource. By default, this is the same as the resource group." } + }, + "containerRegistryEndpointSuffix": { + "defaultValue": ".azurecr.io", + "type": "string" } }, "variables": { "apiVersion": "2020-11-01-preview", - "endpointValue": "[format('https://{0}.azurecr.io', parameters('baseName'))]", + "endpointValue": "[format('https://{0}{1}', parameters('baseName'), parameters('containerRegistryEndpointSuffix'))]", "anonRegistryName": "[format('{0}anon', parameters('baseName'))]", - "anonEndpointValue": "[format('https://{0}.azurecr.io', variables('anonRegistryName'))]" + "anonEndpointValue": "[format('https://{0}{1}', variables('anonRegistryName'), parameters('containerRegistryEndpointSuffix'))]" }, "resources": [ { diff --git a/sdk/containerregistry/tests.yml b/sdk/containerregistry/tests.yml index c9d4d6428c01..e109e59cac85 100644 --- a/sdk/containerregistry/tests.yml +++ b/sdk/containerregistry/tests.yml @@ -6,9 +6,8 @@ stages: AllocateResourceGroup: false BuildTargetingString: azure-containerregistry ServiceDirectory: containerregistry - MatrixReplace: - - TestSamples=.*/true DeployArmTemplate: true + SupportedClouds: 'Public,UsGov,China' EnvVars: AZURE_CLIENT_ID: $(aad-azure-sdk-test-client-id) AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret)