Skip to content

Commit

Permalink
Chain exceptions from LibsecretPersistence (#20380)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlowell authored and iscai-msft committed Sep 29, 2021
1 parent d8dba45 commit 53a5e32
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions sdk/identity/azure-identity/azure/identity/_persistent_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.")

0 comments on commit 53a5e32

Please sign in to comment.