Skip to content

Commit 3e878ff

Browse files
authored
Handle exceptions when constructing DefaultAzureCredential (#8294)
1 parent f343fb2 commit 3e878ff

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

sdk/identity/azure-identity/HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Release History
22

33
### 1.1.0b1 Unreleased
4+
- Constructing `DefaultAzureCredential` no longer raises `ImportError` on Python
5+
3.8 on Windows ([8294](https://github.com/Azure/azure-sdk-for-python/pull/8294))
46

57

68
### 2019-11-05 1.0.1

sdk/identity/azure-identity/azure/identity/_credentials/default.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
import logging
56
import os
67

78
from azure.core.exceptions import ClientAuthenticationError
@@ -12,6 +13,8 @@
1213
from .managed_identity import ManagedIdentityCredential
1314
from .user import SharedTokenCacheCredential
1415

16+
_LOGGER = logging.getLogger(__name__)
17+
1518

1619
class DefaultAzureCredential(ChainedTokenCredential):
1720
"""A default credential capable of handling most Azure SDK authentication scenarios.
@@ -37,11 +40,14 @@ def __init__(self, **kwargs):
3740

3841
# SharedTokenCacheCredential is part of the default only on supported platforms.
3942
if SharedTokenCacheCredential.supported():
40-
credentials.append(
41-
SharedTokenCacheCredential(
42-
username=os.environ.get(EnvironmentVariables.AZURE_USERNAME), authority=authority, **kwargs
43-
)
44-
)
43+
try:
44+
# username is only required to disambiguate, when the cache contains tokens for multiple identities
45+
username = os.environ.get(EnvironmentVariables.AZURE_USERNAME)
46+
shared_cache = SharedTokenCacheCredential(username=username, authority=authority, **kwargs)
47+
credentials.append(shared_cache)
48+
except Exception as ex: # pylint:disable=broad-except
49+
# transitive dependency pywin32 doesn't support 3.8 (https://github.com/mhammond/pywin32/issues/1431)
50+
_LOGGER.info("Shared token cache is unavailable: '%s'", ex)
4551

4652
super(DefaultAzureCredential, self).__init__(*credentials)
4753

sdk/identity/azure-identity/azure/identity/aio/_credentials/default.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5+
import logging
56
import os
67

78
from ..._constants import EnvironmentVariables
@@ -10,6 +11,8 @@
1011
from .managed_identity import ManagedIdentityCredential
1112
from .user import SharedTokenCacheCredential
1213

14+
_LOGGER = logging.getLogger(__name__)
15+
1316

1417
class DefaultAzureCredential(ChainedTokenCredential):
1518
"""A default credential capable of handling most Azure SDK authentication scenarios.
@@ -35,10 +38,13 @@ def __init__(self, **kwargs):
3538

3639
# SharedTokenCacheCredential is part of the default only on supported platforms.
3740
if SharedTokenCacheCredential.supported():
38-
credentials.append(
39-
SharedTokenCacheCredential(
40-
username=os.environ.get(EnvironmentVariables.AZURE_USERNAME), authority=authority, **kwargs
41-
)
42-
)
41+
try:
42+
# username is only required to disambiguate, when the cache contains tokens for multiple identities
43+
username = os.environ.get(EnvironmentVariables.AZURE_USERNAME)
44+
shared_cache = SharedTokenCacheCredential(username=username, authority=authority, **kwargs)
45+
credentials.append(shared_cache)
46+
except Exception as ex: # pylint:disable=broad-except
47+
# transitive dependency pywin32 doesn't support 3.8 (https://github.com/mhammond/pywin32/issues/1431)
48+
_LOGGER.info("Shared token cache is unavailable: '%s'", ex)
4349

4450
super().__init__(*credentials)

0 commit comments

Comments
 (0)