-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Added WorkloadIdentityCredential
#28536
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 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
72f6247
Added `WorkloadIdentityCredential`
xiangyan99 1a9e719
update
xiangyan99 0d6a248
updates
xiangyan99 148dda1
updates
xiangyan99 4d4b42a
update
xiangyan99 f897434
update
xiangyan99 b3f3747
updates
xiangyan99 cd21b88
Merge branch 'main' into identity_workload_identity
xiangyan99 f883e7d
update
xiangyan99 8db4c9b
Merge branch 'identity_workload_identity' of https://github.com/Azure…
xiangyan99 13227c6
Update sdk/identity/azure-identity/azure/identity/_credentials/worklo…
xiangyan99 fadf860
Update sdk/identity/azure-identity/CHANGELOG.md
xiangyan99 c57c090
Update sdk/identity/azure-identity/azure/identity/_credentials/worklo…
xiangyan99 0fe40b1
updates
xiangyan99 4b2224d
updates
xiangyan99 7efd559
update
xiangyan99 e2f55da
Updates
xiangyan99 974b341
update
xiangyan99 d65c523
Update sdk/identity/azure-identity/azure/identity/_credentials/defaul…
xiangyan99 4e5fe07
Update sdk/identity/azure-identity/azure/identity/aio/_credentials/de…
xiangyan99 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
66 changes: 66 additions & 0 deletions
66
sdk/identity/azure-identity/azure/identity/_credentials/workload_identity.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,66 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| from typing import Any | ||
| import logging | ||
| from azure.core.credentials import AccessToken | ||
| from .token_exchange import TokenExchangeCredential | ||
| from .._internal.decorators import log_get_token | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class WorkloadIdentityCredential: | ||
| """WorkloadIdentityCredential supports Azure workload identity on Kubernetes. | ||
| See https://learn.microsoft.com/azure/aks/workload-identity-overview for more information | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
|
|
||
| :param str tenant_id: ID of the application's Azure Active Directory tenant. Also called its "directory" ID. | ||
| :param str client_id: the client ID of an Azure AD app registration. | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| :param str file: The path to a file containing a Kubernetes service account token that authenticates the identity. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| tenant_id: str, | ||
| client_id: str, | ||
| file: str, | ||
| **kwargs: Any | ||
| ) -> None: | ||
| kwargs.pop("token_file_path", None) | ||
| self._credential = TokenExchangeCredential( | ||
|
xiangyan99 marked this conversation as resolved.
Outdated
|
||
| tenant_id=tenant_id, | ||
| client_id=client_id, | ||
| token_file_path=file, | ||
| **kwargs | ||
| ) | ||
|
|
||
| def __enter__(self): | ||
| if self._credential: | ||
| self._credential.__enter__() | ||
| return self | ||
|
|
||
| def __exit__(self, *args): | ||
| if self._credential: | ||
| self._credential.__exit__(*args) | ||
|
|
||
| def close(self) -> None: | ||
| """Close the credential's transport session.""" | ||
| self.__exit__() | ||
|
|
||
| @log_get_token("WorkloadIdentityCredential") | ||
| def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken: | ||
| """Request an access token for `scopes`. | ||
|
|
||
| This method is called automatically by Azure SDK clients. | ||
|
|
||
| :param str scopes: desired scopes for the access token. This method requires at least one scope. | ||
| For more information about scopes, see | ||
| https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc. | ||
| :keyword str tenant_id: optional tenant to include in the token request. | ||
|
|
||
| :rtype: :class:`azure.core.credentials.AccessToken` | ||
|
|
||
| :raises ~azure.identity.CredentialUnavailableError: environment variable configuration is incomplete | ||
| """ | ||
| return self._credential.get_token(*scopes, **kwargs) | ||
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
56 changes: 56 additions & 0 deletions
56
sdk/identity/azure-identity/azure/identity/aio/_credentials/workload_identity.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,56 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| from typing import Any | ||
| import logging | ||
| from azure.core.credentials import AccessToken | ||
| from .token_exchange import TokenExchangeCredential | ||
| from .._internal.decorators import log_get_token_async | ||
| from .._internal import AsyncContextManager | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class WorkloadIdentityCredential(AsyncContextManager): | ||
| """WorkloadIdentityCredential supports Azure workload identity on Kubernetes. | ||
| See https://learn.microsoft.com/azure/aks/workload-identity-overview for more information | ||
|
|
||
| :param str tenant_id: ID of the application's Azure Active Directory tenant. Also called its "directory" ID. | ||
| :param str client_id: the client ID of an Azure AD app registration. | ||
| :param str file: The path to a file containing a Kubernetes service account token that authenticates the identity. | ||
| """ | ||
| def __init__(self, tenant_id: str, client_id: str, file: str, **kwargs: Any) -> None: | ||
| kwargs.pop("token_file_path", None) | ||
| self._credential = TokenExchangeCredential( | ||
| tenant_id=tenant_id, | ||
| client_id=client_id, | ||
| token_file_path=file, | ||
| **kwargs | ||
| ) | ||
|
|
||
| async def __aenter__(self): | ||
| if self._credential: | ||
| await self._credential.__aenter__() | ||
| return self | ||
|
|
||
| async def close(self) -> None: | ||
| """Close the credential's transport session.""" | ||
|
|
||
| if self._credential: | ||
| await self._credential.__aexit__() | ||
|
|
||
| @log_get_token_async | ||
| async def get_token(self, *scopes: str, **kwargs: Any) -> AccessToken: | ||
| """Asynchronously request an access token for `scopes`. | ||
|
|
||
| This method is called automatically by Azure SDK clients. | ||
|
|
||
| :param str scopes: desired scopes for the access token. This method requires at least one scope. | ||
| For more information about scopes, see | ||
| https://learn.microsoft.com/azure/active-directory/develop/scopes-oidc. | ||
| :keyword str tenant_id: optional tenant to include in the token request. | ||
| :rtype: :class:`azure.core.credentials.AccessToken` | ||
| :raises ~azure.identity.CredentialUnavailableError: environment variable configuration is incomplete | ||
| """ | ||
| return await self._credential.get_token(*scopes, **kwargs) |
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.
Uh oh!
There was an error while loading. Please reload this page.