-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add support to use AZD as token credential provider #31728
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
vhvb1989
wants to merge
1
commit into
Azure:dev
Choose a base branch
from
vhvb1989:azd
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
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
Add support to use AZD as token credential provider
- Loading branch information
commit f8321edb95eab6c48de9eeec6cf0ccc7af3a32fa
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,6 +347,46 @@ def get_login_credentials(self, resource=None, client_id=None, subscription_id=N | |
| str(account[_TENANT_ID])) | ||
|
|
||
| def get_raw_token(self, resource=None, scopes=None, subscription=None, tenant=None): | ||
| # Check if AZ_USE_AZD_AUTH environment variable is set | ||
| if os.environ.get('AZ_USE_AZD_AUTH'): | ||
| try: | ||
| from azure.identity import AzureDeveloperCliCredential | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Azure CLI dropped the dependency on |
||
|
|
||
| # Convert resource to scopes if needed | ||
| if resource and not scopes: | ||
| from .auth.util import resource_to_scopes | ||
| scopes = resource_to_scopes(resource) | ||
|
|
||
| # Use ARM as the default scopes | ||
| if not scopes: | ||
| scopes = self._arm_scope | ||
|
|
||
| # Create Azure Developer CLI credential | ||
| azd_credential = AzureDeveloperCliCredential() | ||
| sdk_token = azd_credential.get_token(*scopes) | ||
|
|
||
| # Convert epoch int 'expires_on' to datetime string 'expiresOn' for backward compatibility | ||
| # WARNING: expiresOn is deprecated and will be removed in future release. | ||
| import datetime | ||
| expiresOn = datetime.datetime.fromtimestamp(sdk_token.expires_on).strftime("%Y-%m-%d %H:%M:%S.%f") | ||
|
|
||
| token_entry = { | ||
| 'accessToken': sdk_token.token, | ||
| 'expires_on': sdk_token.expires_on, # epoch int, like 1605238724 | ||
| 'expiresOn': expiresOn # datetime string, like "2020-11-12 13:50:47.114324" | ||
| } | ||
|
|
||
| # (tokenType, accessToken, tokenEntry) | ||
| creds = 'Bearer', sdk_token.token, token_entry | ||
|
|
||
| # For AZD authentication, we don't have subscription/tenant info from the current account | ||
| # So we return None for these values to let the caller handle them appropriately | ||
| return (creds, None, None) | ||
|
|
||
| except Exception as ex: | ||
| logger.warning("Failed to use Azure Developer CLI credential: %s. Falling back to standard authentication.", str(ex)) | ||
| # Fall through to standard authentication flow | ||
|
|
||
| # Convert resource to scopes | ||
| if resource and not scopes: | ||
| from .auth.util import resource_to_scopes | ||
|
|
||
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
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.
Only changing
get_raw_tokenis far from enough.get_raw_tokenis only designed for getting the raw token. It cannot be used with Python SDK. To invoke Python SDK, it is required to callget_login_credentialsto get the credential.az account show) containing the current subscription and tenant must also be set. This makesget_subscriptionwork, which is required byget_login_credentials:azure-cli/src/azure-cli-core/azure/cli/core/_profile.py
Line 308 in 9091fee