Skip to content

Commit 58613a1

Browse files
authored
enable pii logging (Azure#23658)
* enable pii logging * add changelog * update version
1 parent 3ef8ca8 commit 58613a1

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Release History
22

3-
## 1.9.0 (Unreleased)
3+
## 1.9.0 (2022-04-05)
44

55
### Features Added
66

7+
- Added PII logging if logging.DEBUG is enabled. ([#23203](https://github.com/Azure/azure-sdk-for-python/issues/23203))
8+
79
### Breaking Changes
810

911
- `validate_authority` support is not available in 1.9.0.
1012

1113
### Bugs Fixed
1214

13-
- Added check on `content` from msal response ([#23483](https://github.com/Azure/azure-sdk-for-python/issues/23483))
15+
- Added check on `content` from msal response. ([#23483](https://github.com/Azure/azure-sdk-for-python/issues/23483))
1416
- Fixed the issue that async OBO credential does not refresh correctly. ([#21981](https://github.com/Azure/azure-sdk-for-python/issues/21981))
1517

1618
### Other Changes

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .._internal import normalize_authority, validate_tenant_id
1313
from .._internal.aad_client import AadClient
1414
from .._internal.get_token_mixin import GetTokenMixin
15+
from .._internal.decorators import log_get_token
1516

1617
if sys.platform.startswith("win"):
1718
from .._internal.win_vscode_adapter import get_refresh_token, get_user_settings
@@ -136,6 +137,7 @@ def close(self):
136137
"""Close the credential's transport session."""
137138
self.__exit__()
138139

140+
@log_get_token("VSCodeCredential")
139141
def get_token(self, *scopes, **kwargs):
140142
# type: (*str, **Any) -> AccessToken
141143
"""Request an access token for `scopes` as the user currently signed in to Visual Studio Code.

sdk/identity/azure-identity/azure/identity/_internal/decorators.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# ------------------------------------
55
import functools
66
import logging
7+
import json
8+
import base64
79

810
from six import raise_from
911
from azure.core.exceptions import ClientAuthenticationError
@@ -31,8 +33,24 @@ def wrapper(*args, **kwargs):
3133
_LOGGER.log(
3234
logging.DEBUG if within_credential_chain.get() else logging.INFO, "%s succeeded", qualified_name
3335
)
36+
if _LOGGER.isEnabledFor(logging.DEBUG):
37+
try:
38+
base64_meta_data = token.token.split(".")[1].encode("utf-8") + b'=='
39+
json_bytes = base64.decodebytes(base64_meta_data)
40+
json_string = json_bytes.decode('utf-8')
41+
json_dict = json.loads(json_string)
42+
upn = json_dict.get('upn', 'unavailableUpn')
43+
log_string = '[Authenticated account] Client ID: {}. Tenant ID: {}. User Principal Name: {}. ' \
44+
'Object ID (user): {}'.format(json_dict['appid'],
45+
json_dict['tid'],
46+
upn,
47+
json_dict['oid']
48+
)
49+
_LOGGER.debug(log_string)
50+
except Exception: # pylint: disable=broad-except
51+
_LOGGER.debug("Fail to log the account information")
3452
return token
35-
except Exception as ex:
53+
except Exception as ex: # pylint: disable=broad-except
3654
_LOGGER.log(
3755
logging.DEBUG if within_credential_chain.get() else logging.WARNING,
3856
"%s failed: %s",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .._internal import AsyncContextManager
99
from .._internal.aad_client import AadClient
1010
from .._internal.get_token_mixin import GetTokenMixin
11+
from .._internal.decorators import log_get_token_async
1112
from ..._credentials.vscode import _VSCodeCredentialBase
1213

1314
if TYPE_CHECKING:
@@ -39,6 +40,7 @@ async def close(self) -> None:
3940
if self._client:
4041
await self._client.__aexit__()
4142

43+
@log_get_token_async
4244
async def get_token(self, *scopes: str, **kwargs: "Any") -> "AccessToken":
4345
"""Request an access token for `scopes` as the user currently signed in to Visual Studio Code.
4446

sdk/identity/azure-identity/azure/identity/aio/_internal/decorators.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# ------------------------------------
55
import functools
66
import logging
7+
import json
8+
import base64
79

810
from azure.core.exceptions import ClientAuthenticationError
911

@@ -20,8 +22,24 @@ async def wrapper(*args, **kwargs):
2022
_LOGGER.log(
2123
logging.DEBUG if within_credential_chain.get() else logging.INFO, "%s succeeded", fn.__qualname__
2224
)
25+
if _LOGGER.isEnabledFor(logging.DEBUG):
26+
try:
27+
base64_meta_data = token.token.split(".")[1].encode("utf-8") + b'=='
28+
json_bytes = base64.decodebytes(base64_meta_data)
29+
json_string = json_bytes.decode('utf-8')
30+
json_dict = json.loads(json_string)
31+
upn = json_dict.get('upn', 'unavailableUpn')
32+
log_string = '[Authenticated account] Client ID: {}. Tenant ID: {}. User Principal Name: {}. ' \
33+
'Object ID (user): {}'.format(json_dict['appid'],
34+
json_dict['tid'],
35+
upn,
36+
json_dict['oid']
37+
)
38+
_LOGGER.debug(log_string)
39+
except Exception: # pylint: disable=broad-except
40+
_LOGGER.debug("Fail to log the account information")
2341
return token
24-
except Exception as ex:
42+
except Exception as ex: # pylint: disable=broad-except
2543
_LOGGER.log(
2644
logging.DEBUG if within_credential_chain.get() else logging.WARNING,
2745
"%s failed: %s",

0 commit comments

Comments
 (0)