Skip to content

Commit 52109c4

Browse files
authored
Make IMDS unavailability easier to debug (Azure#19423)
1 parent 3a53a1c commit 52109c4

File tree

2 files changed

+26
-26
lines changed
  • sdk/identity/azure-identity/azure/identity

2 files changed

+26
-26
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
import logging
65
import os
76
from typing import TYPE_CHECKING
87

@@ -21,8 +20,6 @@
2120
from typing import Any, Optional
2221
from azure.core.credentials import AccessToken
2322

24-
_LOGGER = logging.getLogger(__name__)
25-
2623
IMDS_URL = "http://169.254.169.254/metadata/identity/oauth2/token"
2724

2825
PIPELINE_SETTINGS = {
@@ -51,6 +48,7 @@ def __init__(self, **kwargs):
5148
self._endpoint_available = True # type: Optional[bool]
5249
else:
5350
self._endpoint_available = None
51+
self._error_message = None # type: Optional[str]
5452
self._user_assigned_identity = "client_id" in kwargs or "identity_config" in kwargs
5553

5654
def _acquire_token_silently(self, *scopes):
@@ -67,16 +65,18 @@ def _request_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
6765
self._client.request_token(*scopes, connection_timeout=0.3, retry_total=0)
6866
self._endpoint_available = True
6967
except HttpResponseError:
70-
# received a response, choked on it
68+
# IMDS responded
7169
self._endpoint_available = True
72-
except Exception: # pylint:disable=broad-except
70+
except Exception as ex: # pylint:disable=broad-except
7371
# if anything else was raised, assume the endpoint is unavailable
7472
self._endpoint_available = False
75-
_LOGGER.info("No response from the IMDS endpoint.")
73+
self._error_message = (
74+
"ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint."
75+
)
76+
six.raise_from(CredentialUnavailableError(self._error_message), ex)
7677

7778
if not self._endpoint_available:
78-
message = "ManagedIdentityCredential authentication unavailable, no managed identity endpoint found."
79-
raise CredentialUnavailableError(message=message)
79+
raise CredentialUnavailableError(self._error_message)
8080

8181
try:
8282
token = self._client.request_token(*scopes, headers={"Metadata": "true"})
@@ -85,13 +85,13 @@ def _request_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
8585
# or the identity with the specified client_id is not available
8686
if ex.status_code == 400:
8787
self._endpoint_available = False
88-
message = "ManagedIdentityCredential authentication unavailable. "
88+
self._error_message = "ManagedIdentityCredential authentication unavailable. "
8989
if self._user_assigned_identity:
90-
message += "The requested identity has not been assigned to this resource."
90+
self._error_message += "The requested identity has not been assigned to this resource."
9191
else:
92-
message += "No identity has been assigned to this resource."
93-
six.raise_from(CredentialUnavailableError(message=message), ex)
92+
self._error_message += "No identity has been assigned to this resource."
93+
six.raise_from(CredentialUnavailableError(message=self._error_message), ex)
9494

9595
# any other error is unexpected
96-
six.raise_from(ClientAuthenticationError(message=ex.message, response=ex.response), None)
96+
six.raise_from(ClientAuthenticationError(message=ex.message, response=ex.response), ex)
9797
return token

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright (c) Microsoft Corporation.
33
# Licensed under the MIT License.
44
# ------------------------------------
5-
import logging
65
import os
76
from typing import TYPE_CHECKING
87

@@ -19,8 +18,6 @@
1918
from typing import Any, Optional
2019
from azure.core.credentials import AccessToken
2120

22-
_LOGGER = logging.getLogger(__name__)
23-
2421

2522
class ImdsCredential(AsyncContextManager, GetTokenMixin):
2623
def __init__(self, **kwargs: "Any") -> None:
@@ -31,6 +28,7 @@ def __init__(self, **kwargs: "Any") -> None:
3128
self._endpoint_available = True # type: Optional[bool]
3229
else:
3330
self._endpoint_available = None
31+
self._error_message = None # type: Optional[str]
3432
self._user_assigned_identity = "client_id" in kwargs or "identity_config" in kwargs
3533

3634
async def close(self) -> None:
@@ -48,16 +46,18 @@ async def _request_token(self, *scopes, **kwargs: "Any") -> "AccessToken": # py
4846
await self._client.request_token(*scopes, connection_timeout=0.3, retry_total=0)
4947
self._endpoint_available = True
5048
except HttpResponseError:
51-
# received a response, choked on it
49+
# IMDS responded
5250
self._endpoint_available = True
53-
except Exception: # pylint:disable=broad-except
51+
except Exception as ex: # pylint:disable=broad-except
5452
# if anything else was raised, assume the endpoint is unavailable
5553
self._endpoint_available = False
56-
_LOGGER.info("No response from the IMDS endpoint.")
54+
self._error_message = (
55+
"ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint."
56+
)
57+
raise CredentialUnavailableError(message=self._error_message) from ex
5758

5859
if not self._endpoint_available:
59-
message = "ManagedIdentityCredential authentication unavailable, no managed identity endpoint found."
60-
raise CredentialUnavailableError(message=message)
60+
raise CredentialUnavailableError(message=self._error_message)
6161

6262
try:
6363
token = await self._client.request_token(*scopes, headers={"Metadata": "true"})
@@ -66,13 +66,13 @@ async def _request_token(self, *scopes, **kwargs: "Any") -> "AccessToken": # py
6666
# or the identity with the specified client_id is not available
6767
if ex.status_code == 400:
6868
self._endpoint_available = False
69-
message = "ManagedIdentityCredential authentication unavailable. "
69+
self._error_message = "ManagedIdentityCredential authentication unavailable. "
7070
if self._user_assigned_identity:
71-
message += "The requested identity has not been assigned to this resource."
71+
self._error_message += "The requested identity has not been assigned to this resource."
7272
else:
73-
message += "No identity has been assigned to this resource."
74-
raise CredentialUnavailableError(message=message) from ex
73+
self._error_message += "No identity has been assigned to this resource."
74+
raise CredentialUnavailableError(message=self._error_message) from ex
7575

7676
# any other error is unexpected
77-
raise ClientAuthenticationError(message=ex.message, response=ex.response) from None
77+
raise ClientAuthenticationError(message=ex.message, response=ex.response) from ex
7878
return token

0 commit comments

Comments
 (0)