From 0e4855644e0b07a58d0774f9e19a5e76b71b6af5 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Tue, 24 Aug 2021 10:24:29 -0700 Subject: [PATCH] Chain exceptions from LibsecretPersistence (#20380) --- .../azure/identity/_persistent_cache.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/sdk/identity/azure-identity/azure/identity/_persistent_cache.py b/sdk/identity/azure-identity/azure/identity/_persistent_cache.py index 0630de2d9f62..6c6765c5ad95 100644 --- a/sdk/identity/azure-identity/azure/identity/_persistent_cache.py +++ b/sdk/identity/azure-identity/azure/identity/_persistent_cache.py @@ -2,14 +2,19 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------ +import logging import os import sys from typing import TYPE_CHECKING +import six + if TYPE_CHECKING: from typing import Any import msal_extensions +_LOGGER = logging.getLogger(__name__) + class TokenCachePersistenceOptions(object): """Options for persistent token caching. @@ -86,12 +91,16 @@ def _get_persistence(allow_unencrypted, account_name, cache_name): return msal_extensions.LibsecretPersistence( file_path, cache_name, {"MsalClientID": "Microsoft.Developer.IdentityService"}, label=account_name ) - except ImportError: + except Exception as ex: # pylint:disable=broad-except + _LOGGER.debug('msal-extensions is unable to encrypt a persistent cache: "%s"', ex, exc_info=True) if not allow_unencrypted: - raise ValueError( - "PyGObject is required to encrypt the persistent cache. Please install that library or " - + 'specify "allow_unencrypted_storage=True" to store the cache without encryption.' + error = ValueError( + "Cache encryption is impossible because libsecret dependencies are not installed or are unusable," + + " for example because no display is available (as in an SSH session). The chained exception has" + + ' more information. Specify "allow_unencrypted_storage=True" to store the cache unencrypted' + + " instead of raising this exception." ) - return msal_extensions.FilePersistence(file_path) + six.raise_from(error, ex) + return msal_extensions.FilePersistence(file_path) raise NotImplementedError("A persistent cache is not available in this environment.")