[profile] Enable token cache encryption on MacOS and Linux - #33849
Draft
Yeming Liu (isra-fel) wants to merge 15 commits into
Draft
[profile] Enable token cache encryption on MacOS and Linux#33849Yeming Liu (isra-fel) wants to merge 15 commits into
Yeming Liu (isra-fel) wants to merge 15 commits into
Conversation
Collaborator
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Collaborator
|
/azp run |
|
Azure Pipelines: Successfully started running 3 pipeline(s). |
Contributor
Live test skipped⏭️ Skipping the live test for this revision because no new test file was added ( The live-test pipeline runs only the test files a PR changes, so there is nothing to execute for this commit. This is informational — a regression test is encouraged where it makes sense, but not required. If a test file is added in a later commit, the live test will run automatically. |
Contributor
There was a problem hiding this comment.
Automated Review Summary
This PR currently has failing checks and needs attention from the author before it can be merged.
- Live test (Tester): passed
- CI checks: 89/95 passed, 4 failed, 0 pending
Failed checks:
azdev-style— https://github.com/Azure/azure-cli/actions/runs/32217609750/job/95961947786Azure.azure-cli(Build #20260805.21 failed) — https://github.com/Azure/azure-cli/runs/92494811403Azure.azure-cli (Check CLI Style)(Check CLI Style failed) — https://github.com/Azure/azure-cli/runs/92478117667azdev-style— https://github.com/Azure/azure-cli/actions/runs/31056245894/job/92474260204
Please review the linked job logs and update the PR to resolve the style/build failures.
This was referenced Aug 27, 2026
xuming-ms
force-pushed
the
shared/token-cache-encryption
branch
from
August 28, 2026 06:14
c403333 to
65ed354
Compare
Cloud Shell, GitHub Actions and Azure Pipelines agents have no OS credential store and the user cannot install one, so the warning asks for something they cannot do. Gate it behind in_managed_environment() and keep a debug line so the fallback is still traceable.
…s secret store since it may have user interactive for unlock keyring
xuming-ms
force-pushed
the
shared/token-cache-encryption
branch
from
August 30, 2026 23:56
65ed354 to
ac81221
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 PR Validation — ️✔️ All clear
Related command
az login,az logout,az account clearDescription
Token cache encryption has been Windows-only so far. On macOS and Linux the MSAL token cache and the service principal secret store are written to disk in plaintext, so anything that can read
~/.azurecan read a usable credential.This PR turns encryption on by default on all platforms, and fixes what that exposes in the surrounding code.
Encryption on by default.
should_encrypt_token_cachenow falls back toTrueinstead ofsys.platform.startswith('win32').core.encrypt_token_cache=falsestill opts out, and is now the documented escape hatch for machines where the keyring prompt is unwanted.Real names in the OS credential store. Keychain and libsecret entries were registered under placeholders (
my_service_name,my_account_name,my_schema_name,my_attr1=foo). They now useMicrosoft Azure CLIplus the persistence type, so a user browsing Keychain Access or Seahorse seesMicrosoft Azure CLI - Token cache/- Secret storerather than an unlabeled row.Separate file extension for the signal file. With encryption on, the payload lives in the OS credential store and the file on disk is only a last-modified signal — it is not
.bincontent. Signal files now use.sigso the three states (.binencrypted-on-Windows,.jsonplaintext,.sigsignal) are distinguishable, which is what makes correct cleanup possible.Logout actually clears the credential.
az logout/az account clearused to just delete the cache files. With encryption on, that orphans the payload in libsecret/Keychain: the credential stays readable.erase_persistencenow overwrites the payload with an empty one under the sameCrossPlatLockused by writers, then removes the files. It also cleans up files from every extension, so switchingencrypt_token_cachedoesn't leave stale credentials behind.Plaintext fallback is graceful, and reported.
LibsecretPersistencefails to initialize in some Linux environments (no D-Bus session, headless containers). Instead of failing the command, we fall back toFilePersistenceand warn once at sign-in that credentials are in plaintext. The warning is suppressed in Cloud Shell, where nothing can be installed to fix it.Honest warnings when a clear can't reach the credential store. With encryption off we deliberately do not touch the keyring — emptying it would raise the unlock prompt the user opted out of. The
.sigfile is kept as evidence that a payload may still be there, and the user is told how to remove it. If the clear itself fails (typically anotherazprocess holding the lock), nothing is removed and the user is asked to retry, rather than the files being deleted while the credential survives.Testing Guide
macOS / Linux, with a keyring available:
Opting out:
Linux without a keyring (e.g.
docker runwith no D-Bus):az loginsucceeds and warns that credentials are stored in plaintext.Unit tests:
azdev test --src-file-changed python -m pytest src/azure-cli-core/azure/cli/core/auth/tests/test_persistence.pyHistory Notes
[Core]
az login: Enable token cache and service principal secret encryption by default on macOS and Linux. Setcore.encrypt_token_cache=falseto opt out.[Core]
az account clear: Also remove leftover plaintext cache files whencore.encrypt_token_cacheis on.[Core]
az logout: Clear the persisted credential according tocore.encrypt_token_cache— the OS credential store payload when encryption is on, the plaintext files when it is off.This checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.