-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Container Registry] Anonymous Access Client (#18550)
* changing ContainerRepositoryClient to ContainerRepository * renaming files * re-recording, commenting out tests that are not necessary * working sync registry artifact class * async registry artifact * issue with recording infra that is removing an acr specific oauth path * pylint issues * recording and processors * removing commented out code * undoing changes to cache * more lint fixes * help with logging output * change to list_repository_names * renaming for consistency * all changes made, plus recordings * fixing up more tests again! * formatting * fixing up merge issues * undoing changes to gen code * pylint issues * consistent naming * changes * anon test * small changes to generated, eventually will be reflected in the swagger * adding basics for anon * adding test infra * adding async tests * adding more tests for anon container repo and reg artifact * added async anon client * asserting credential is false * fixing scrubber * new swagger * merge conflicts reflected in tests * lint * updating tests and resource for anonymous access * updating generated code * undoing generated code changes * shouldnt have done that oops * undoing unnecessary changes to recordings * changelog * anna and mccoys comments * lint fixes
- Loading branch information
1 parent
2420e32
commit a4545c1
Showing
34 changed files
with
2,369 additions
and
105 deletions.
There are no files selected for viewing
This file contains 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
75 changes: 75 additions & 0 deletions
75
...nerregistry/azure-containerregistry/azure/containerregistry/_anonymous_exchange_client.py
This file contains 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,75 @@ | ||
# coding=utf-8 | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
from typing import TYPE_CHECKING, Dict, Any | ||
|
||
from ._exchange_client import ExchangeClientAuthenticationPolicy | ||
from ._generated import ContainerRegistry | ||
from ._generated.models._container_registry_enums import TokenGrantType | ||
from ._helpers import _parse_challenge | ||
from ._user_agent import USER_AGENT | ||
|
||
if TYPE_CHECKING: | ||
from azure.core.credentials import TokenCredential | ||
|
||
|
||
class AnonymousACRExchangeClient(object): | ||
"""Class for handling oauth authentication requests | ||
:param endpoint: Azure Container Registry endpoint | ||
:type endpoint: str | ||
:param credential: Credential which provides tokens to authenticate requests | ||
:type credential: :class:`~azure.core.credentials.TokenCredential` | ||
""" | ||
|
||
def __init__(self, endpoint, **kwargs): # pylint: disable=missing-client-constructor-parameter-credential | ||
# type: (str, Dict[str, Any]) -> None | ||
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 | ||
) | ||
|
||
def get_acr_access_token(self, challenge, **kwargs): | ||
# type: (str, Dict[str, Any]) -> str | ||
parsed_challenge = _parse_challenge(challenge) | ||
parsed_challenge["grant_type"] = TokenGrantType.PASSWORD | ||
return self.exchange_refresh_token_for_access_token( | ||
None, | ||
service=parsed_challenge["service"], | ||
scope=parsed_challenge["scope"], | ||
grant_type=TokenGrantType.PASSWORD, | ||
**kwargs | ||
) | ||
|
||
def exchange_refresh_token_for_access_token( | ||
self, refresh_token=None, service=None, scope=None, grant_type=TokenGrantType.PASSWORD, **kwargs | ||
): | ||
# type: (str, str, str, str, Dict[str, Any]) -> str | ||
access_token = self._client.authentication.exchange_acr_refresh_token_for_acr_access_token( | ||
service=service, scope=scope, refresh_token=refresh_token, grant_type=grant_type, **kwargs | ||
) | ||
return access_token.access_token | ||
|
||
def __enter__(self): | ||
self._client.__enter__() | ||
return self | ||
|
||
def __exit__(self, *args): | ||
self._client.__exit__(*args) | ||
|
||
def close(self): | ||
# type: () -> None | ||
"""Close sockets opened by the client. | ||
Calling this method is unnecessary when using the client as a context manager. | ||
""" | ||
self._client.close() |
This file contains 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 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 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 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 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 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 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 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 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
74 changes: 74 additions & 0 deletions
74
...y/azure-containerregistry/azure/containerregistry/aio/_async_anonymous_exchange_client.py
This file contains 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,74 @@ | ||
# coding=utf-8 | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
from typing import TYPE_CHECKING, Dict, List, Any | ||
|
||
from ._async_exchange_client import ExchangeClientAuthenticationPolicy | ||
from .._generated.aio import ContainerRegistry | ||
from .._generated.models._container_registry_enums import TokenGrantType | ||
from .._helpers import _parse_challenge | ||
from .._user_agent import USER_AGENT | ||
|
||
if TYPE_CHECKING: | ||
from azure.core.credentials_async import AsyncTokenCredential | ||
|
||
|
||
class AnonymousACRExchangeClient(object): | ||
"""Class for handling oauth authentication requests | ||
:param endpoint: Azure Container Registry endpoint | ||
:type endpoint: str | ||
""" | ||
|
||
def __init__(self, endpoint: str, **kwargs: Dict[str, Any]) -> None: # 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 | ||
) | ||
|
||
async def get_acr_access_token(self, challenge: str, **kwargs: Dict[str, Any]) -> str: | ||
parsed_challenge = _parse_challenge(challenge) | ||
parsed_challenge["grant_type"] = TokenGrantType.PASSWORD | ||
return await self.exchange_refresh_token_for_access_token( | ||
None, | ||
service=parsed_challenge["service"], | ||
scope=parsed_challenge["scope"], | ||
grant_type=TokenGrantType.PASSWORD, | ||
**kwargs | ||
) | ||
|
||
async def exchange_refresh_token_for_access_token( | ||
self, | ||
refresh_token: str = None, | ||
service: str = None, | ||
scope: str = None, | ||
grant_type: str = TokenGrantType.PASSWORD, | ||
**kwargs: Any | ||
) -> str: | ||
access_token = await self._client.authentication.exchange_acr_refresh_token_for_acr_access_token( | ||
service=service, scope=scope, refresh_token=refresh_token, grant_type=grant_type, **kwargs | ||
) | ||
return access_token.access_token | ||
|
||
async def __aenter__(self): | ||
self._client.__aenter__() | ||
return self | ||
|
||
async def __aexit__(self, *args): | ||
self._client.__aexit__(*args) | ||
|
||
async def close(self) -> None: | ||
"""Close sockets opened by the client. | ||
Calling this method is unnecessary when using the client as a context manager. | ||
""" | ||
await self._client.close() |
This file contains 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 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 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.