o
    i/                     @   s   d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZ ddlmZ dd	lmZ d
dlmZ d
dlmZ d
dlmZ ddlmZ ddlmZmZmZ G dd deZG dd deeZdS )    N)OptionalAnyDict)ClientAuthenticationErrorHttpResponseError)AccessTokenInfo)AsyncRetryPolicy)PipelineResponse   )CredentialUnavailableError)EnvironmentVariables   )AsyncContextManager)GetTokenMixin)AsyncManagedIdentityClient)within_credential_chain)_get_request_check_forbidden_responsePIPELINE_SETTINGSc                       sT   e Zd ZdZdeddf fddZdeeef deeef de	f fd	d
Z
  ZS )AsyncImdsRetryPolicyaX  Async custom retry policy for IMDS credential with extended retry duration for 410 responses.

    This policy ensures that specifically for 410 status codes, the total exponential backoff duration
    is at least 70 seconds to handle temporary IMDS endpoint unavailability.
    For other status codes, it uses the standard retry behavior.
    kwargsreturnNc                    s   d| _ t jdi | d S )Ng      @ )backoff_factor_for_410super__init__selfr   	__class__r   [/home/app/Keep/.python/lib/python3.10/site-packages/azure/identity/aio/_credentials/imds.pyr      s   zAsyncImdsRetryPolicy.__init__settingsresponsec                    s0   |j jdkr| j|d< n| j|d< t ||S )Ni  backoff)http_responsestatus_coder   backoff_factorr   is_retry)r   r!   r"   r   r   r    r'   $   s   
zAsyncImdsRetryPolicy.is_retry)__name__
__module____qualname____doc__r   r   r   strr	   boolr'   __classcell__r   r   r   r    r      s    2r   c                       sj   e Zd Zdeddf fddZdddZddd	Zd
ededee	 fddZ
d
edede	fddZ  ZS )ImdsCredentialr   r   Nc                    sn   t    |dd | _t|d< ttfi ttfi || _	t
jtjv r)d| _nd | _d|v p3d|v | _d S )N_enable_imds_probeZretry_policy_classTZ	client_idZidentity_config)r   r   popr0   r   r   r   dictr   _clientr   Z!AZURE_POD_IDENTITY_AUTHORITY_HOSTosenviron_endpoint_available_user_assigned_identityr   r   r   r    r   -   s   
zImdsCredential.__init__c                    s   | j  I d H  | S N)r3   
__aenter__r   r   r   r    r9   <   s   zImdsCredential.__aenter__c                    s   | j  I d H  d S r8   )r3   closer:   r   r   r    r;   @   s   zImdsCredential.closescopesc                    s   | j j|i |S r8   )r3   Zget_cached_token)r   r<   r   r   r   r    _acquire_token_silentlyC   s   z&ImdsCredential._acquire_token_silentlyc              
      sr  | j d ur	| j nt }|rQ| jsQz| jj|dddI d H  d| _W n, ty= } zt| d| _W Y d }~nd }~w tyP } zd}t	|d|d }~ww z| jj|ddd	iiI d H }W |S  t	yj     ty } z1|j
d
krd}| jr|d7 }n|d7 }|jr|d|j 7 }t	|d|t| t|j|jd|d }~w ty } zd}t	||d }~ww )N   r   )Zconnection_timeoutZretry_totalTzYManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint.)messageheadersZMetadatatruei  z6ManagedIdentityCredential authentication unavailable. z>The requested identity has not been assigned to this resource.z/No identity has been assigned to this resource.z Error: )r?   r"   )r0   r   getr6   r3   Zrequest_tokenr   r   	Exceptionr   r%   r7   r?   r   r"   )r   r<   r   Zdo_probeexerror_messageZ
token_infor   r   r    _request_tokenF   sL   




zImdsCredential._request_token)r   r/   )r   N)r(   r)   r*   r   r   r9   r;   r,   r   r   r=   rF   r.   r   r   r   r    r/   ,   s    

r/   )r4   typingr   r   r   Zazure.core.exceptionsr   r   Zazure.core.credentialsr   Zazure.core.pipeline.policiesr   Zazure.core.pipeliner	    r   
_constantsr   	_internalr   Z_internal.get_token_mixinr   Z!_internal.managed_identity_clientr   r   Z_credentials.imdsr   r   r   r   r/   r   r   r   r    <module>   s   