Skip to content

Conversation

@jiasli
Copy link
Member

@jiasli jiasli commented Mar 18, 2025

Related command
az account get-access-token

Description
get_raw_token started to use Python SDK's get_token protocol since #19853. Because importing azure.core.credentials.AccessToken is expensive, #19898 defined our own AccessToken.

After #31577 dropped MSIAuthenticationWrapper which only supports get_token protocol, there is no need for get_raw_token to use get_token protocol.

In this PR, get_raw_token directly calls acquire_token on MSAL credentials, instead of get_token protocol.

Actually, the function name acquire_token was used during the ADAL age.

For user credential, CLI calls ADAL's acquire_token:

token_entry = context.acquire_token(resource, username, _CLIENT_ID)

For service credential, Azure CLI calls

token_entry = sp_auth.acquire_token(context, resource, sp_id)

which internally calls ADAL's acquire_token_with_client_credentials and acquire_token_with_client_certificate:

return authentication_context.acquire_token_with_client_credentials(resource, client_id, self.secret)

return authentication_context.acquire_token_with_client_certificate(resource, client_id, self.cert_file_string,

MSAL provides:

result = self._msal_app.acquire_token_silent_with_error(

result = self._msal_app.acquire_token_for_client(scopes, **kwargs)

result = self._msal_client.acquire_token_for_client(resource=scopes_to_resource(scopes))

Bringing acquire_token back is a Renaissance and a memory of the past.

Testing Guide

az account get-access-token

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Mar 18, 2025

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.9
️✔️acs
️✔️latest
️✔️3.12
️✔️3.9
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.9
️✔️ams
️✔️latest
️✔️3.12
️✔️3.9
️✔️apim
️✔️latest
️✔️3.12
️✔️3.9
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.9
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️aro
️✔️latest
️✔️3.12
️✔️3.9
️✔️backup
️✔️latest
️✔️3.12
️✔️3.9
️✔️batch
️✔️latest
️✔️3.12
️✔️3.9
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.9
️✔️billing
️✔️latest
️✔️3.12
️✔️3.9
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.9
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.9
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.9
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.9
️✔️config
️✔️latest
️✔️3.12
️✔️3.9
️✔️configure
️✔️latest
️✔️3.12
️✔️3.9
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.9
️✔️container
️✔️latest
️✔️3.12
️✔️3.9
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.9
️✔️core
️✔️latest
️✔️3.12
️✔️3.9
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.9
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.9
️✔️dls
️✔️latest
️✔️3.12
️✔️3.9
️✔️dms
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.9
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.9
️✔️find
️✔️latest
️✔️3.12
️✔️3.9
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.9
️✔️identity
️✔️latest
️✔️3.12
️✔️3.9
️✔️iot
️✔️latest
️✔️3.12
️✔️3.9
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.9
️✔️lab
️✔️latest
️✔️3.12
️✔️3.9
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️maps
️✔️latest
️✔️3.12
️✔️3.9
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.9
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.9
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.9
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.9
️✔️network
️✔️latest
️✔️3.12
️✔️3.9
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.9
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.9
️✔️profile
️✔️latest
️✔️3.12
️✔️3.9
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.9
️✔️redis
️✔️latest
️✔️3.12
️✔️3.9
️✔️relay
️✔️latest
️✔️3.12
️✔️3.9
️✔️resource
️✔️latest
️✔️3.12
️✔️3.9
️✔️role
️✔️latest
️✔️3.12
️✔️3.9
️✔️search
️✔️latest
️✔️3.12
️✔️3.9
️✔️security
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.9
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.9
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.9
️✔️sql
️✔️latest
️✔️3.12
️✔️3.9
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.9
️✔️storage
️✔️latest
️✔️3.12
️✔️3.9
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.9
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.9
️✔️util
️✔️latest
️✔️3.12
️✔️3.9
️✔️vm
️✔️latest
️✔️3.12
️✔️3.9

@azure-client-tools-bot-prd
Copy link

Hi @jiasli,
Since the current milestone time is less than 7 days, this pr will be reviewed in the next milestone.

@azure-client-tools-bot-prd
Copy link

azure-client-tools-bot-prd bot commented Mar 18, 2025

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@yonzhan
Copy link
Collaborator

yonzhan commented Mar 18, 2025

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@microsoft-github-policy-service microsoft-github-policy-service bot added the Auto-Assign Auto assign by bot label Mar 18, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Core CLI core infrastructure labels Mar 18, 2025
@jiasli jiasli marked this pull request as ready for review June 10, 2025 07:01
Copilot AI review requested due to automatic review settings June 10, 2025 07:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Decouples get_raw_token from the Python SDK’s token protocol by calling MSAL’s acquire_token directly and replacing the private timestamp helper with a public now_timestamp.

  • Exposed and renamed _now_timestamp to now_timestamp, updating all references (including tests).
  • Removed CredentialAdaptor in get_raw_token and updated token‐building logic to use MSAL’s expires_in.
  • Updated tests to mock the new now_timestamp function.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/azure-cli-core/azure/cli/core/auth/util.py Renamed _now_timestamp to now_timestamp and updated its call sites.
src/azure-cli-core/azure/cli/core/auth/credential_adaptor.py Updated import and usage of now_timestamp in AccessTokenInfo builder.
src/azure-cli-core/azure/cli/core/_profile.py Switched get_raw_token to use cred.acquire_token and now_timestamp, removed CredentialAdaptor.
src/azure-cli-core/azure/cli/core/tests/test_profile.py Renamed test helper _now_timestamp_mock to now_timestamp_mock and patched accordingly.



def _now_timestamp():
def now_timestamp():
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

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

Insert an additional blank line above this function so there are two blank lines between top-level definitions, per the style guide.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

There are already 2 blank lines.

expires_on = now_timestamp() + msal_token[EXPIRES_IN]
expiresOn = datetime.datetime.fromtimestamp(expires_on).strftime("%Y-%m-%d %H:%M:%S.%f")

token_entry = {
Copy link
Member Author

Choose a reason for hiding this comment

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

token_entry is actually a remnant of ADAL, but replacing it with msal_token will be a breaking change.

msal_token contains:

{
    "access_token": "...",
    "token_type": "Bearer",
    "expires_in": 4678,
    "token_source": "cache"
}

@jiasli jiasli merged commit bc6c4d9 into Azure:dev Jun 23, 2025
48 checks passed
@jiasli jiasli deleted the get_raw_token branch June 23, 2025 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Auto-Assign Auto assign by bot Core CLI core infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants