Skip to content
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

[Core] Enable token cache encryption on MacOS #20636

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions src/azure-cli-core/azure/cli/core/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,8 @@ def _create_identity_instance(cli_ctx, *args, **kwargs):
"""Lazily import and create Identity instance to avoid unnecessary imports."""
from .auth.identity import Identity

# Only enable encryption for Windows (for now).
fallback = sys.platform.startswith('win32')
# Only enable encryption for Windows and MacOS (for now).
fallback = sys.platform.startswith('win32') or sys.platform.startswith('darwin')
# encrypt_token_cache affects both MSAL token cache and service principal entries.
encrypt = cli_ctx.config.getboolean('core', 'encrypt_token_cache', fallback=fallback)

Expand Down
15 changes: 14 additions & 1 deletion src/azure-cli-core/azure/cli/core/auth/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# Files extensions for encrypted and plaintext persistence
file_extensions = {True: '.bin', False: '.json'}

KEYCHAIN_SERVICE_NAME = 'azure-cli'


def load_persisted_token_cache(location, encrypt):
persistence = build_persistence(location, encrypt)
Expand All @@ -38,16 +40,27 @@ def build_persistence(location, encrypt):
logger.debug("build_persistence: location=%r, encrypt=%r", location, encrypt)
if encrypt:
if sys.platform.startswith('win'):
# For FilePersistenceWithDataProtection, location is where the credential is stored.
logger.debug("Initializing FilePersistenceWithDataProtection.")
return FilePersistenceWithDataProtection(location)
if sys.platform.startswith('darwin'):
return KeychainPersistence(location, "my_service_name", "my_account_name")
# For KeychainPersistence, location is only used as a signal for the credential's last modified time.
# The credential is stored in Keychain identified by (service_name, account_name) combination.
# msal-extensions automatically computes account_name from signal_location.
# https://github.com/AzureAD/microsoft-authentication-extensions-for-python/pull/103
logger.debug("Initializing KeychainPersistence")
return KeychainPersistence(location, service_name=KEYCHAIN_SERVICE_NAME)
Copy link
Member

Choose a reason for hiding this comment

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

You probably already know that, this line won't work until AzureAD/microsoft-authentication-extensions-for-python#103 being released. So, please help review and approve my PR there (otherwise I will not give an approval to your PR here :-D)

Copy link
Member Author

@jiasli jiasli Dec 8, 2021

Choose a reason for hiding this comment

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

I have installed the dev code of msal-extensions and it works perfectly.

if sys.platform.startswith('linux'):
# TODO: Support token cache encryption on Linux
logger.debug("Initializing LibsecretPersistence.")
return LibsecretPersistence(
location,
schema_name="my_schema_name",
attributes={"my_attr1": "foo", "my_attr2": "bar"}
)
else:
# For FilePersistence, location is where the credential is stored.
logger.debug("Initializing FilePersistence")
return FilePersistence(location)


Expand Down