22# Copyright (c) Microsoft Corporation.
33# Licensed under the MIT License.
44# ------------------------------------
5- import logging
65import os
76from typing import TYPE_CHECKING
87
2120 from typing import Any , Optional
2221 from azure .core .credentials import AccessToken
2322
24- _LOGGER = logging .getLogger (__name__ )
25-
2623IMDS_URL = "http://169.254.169.254/metadata/identity/oauth2/token"
2724
2825PIPELINE_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
0 commit comments