Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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
vhvb1989 committed Jun 26, 2025
commit f8321edb95eab6c48de9eeec6cf0ccc7af3a32fa
40 changes: 40 additions & 0 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only changing get_raw_token is far from enough.

  1. get_raw_token is only designed for getting the raw token. It cannot be used with Python SDK. To invoke Python SDK, it is required to call get_login_credentials to get the credential.
  2. The account (returned by az account show) containing the current subscription and tenant must also be set. This makes get_subscription work, which is required by get_login_credentials:
    account = self.get_subscription(subscription_id)

try:
from azure.identity import AzureDeveloperCliCredential
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Azure CLI dropped the dependency on azure-identity in #22124 and has no plan to add it back, as azure-identity frequently causes dependency conflicts.


# 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
Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
DEPENDENCIES = [
'argcomplete~=2.0',
'azure-cli-telemetry==1.0.8.*',
'azure-identity>=1.12.0',
'azure-mgmt-core>=1.2.0,<2',
'cryptography',
# On Linux, the distribution (Ubuntu, Debian, etc) and version are logged in telemetry
Expand All @@ -53,7 +54,7 @@
'jmespath',
'knack~=0.10.1',
'msal-extensions~=1.0.0',
'msal[broker]==1.20.0',
'msal[broker]>=1.20.0,<2.0.0',
'msrestazure~=0.6.4',
'packaging>=20.9',
'paramiko>=2.0.8,<4.0.0',
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli/requirements.py3.Darwin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ azure-cli-telemetry==1.0.8
azure-cli==2.48.1
azure-common==1.1.22
azure-core==1.26.0
azure-identity==1.15.0
azure-cosmos==3.2.0
azure-data-tables==12.4.0
azure-datalake-store==0.0.49
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli/requirements.py3.Linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ azure-cli-telemetry==1.0.8
azure-cli==2.48.1
azure-common==1.1.22
azure-core==1.26.0
azure-identity==1.15.0
azure-cosmos==3.2.0
azure-data-tables==12.4.0
azure-datalake-store==0.0.49
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli/requirements.py3.windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ azure-cli-telemetry==1.0.8
azure-cli==2.48.1
azure-common==1.1.22
azure-core==1.26.0
azure-identity==1.15.0
azure-cosmos==3.2.0
azure-data-tables==12.4.0
azure-datalake-store==0.0.49
Expand Down
Loading