name : imds.cpython-39.opt-1.pyc
a

'�Dg.�@s�ddlZddlZddlmZddlmZmZmZmZddl	Z	ddl
mZddlm
Z
ddlmZmZe�e�ZdZdd	�ZGd
d�d�Zdd
d�eeeeed�dd�Zeeed�dd�Zdeeeed�dd�Zed�dd�ZdS)�N)�	monotonic)�Dict�Optional�Type�Union)�util)�report_diagnostic_event)�UrlError�readurlzhttp://169.254.169.254/metadatacCsdtt���d�S)N�true)ZMetadatazx-ms-client-request-id)�str�uuidZuuid4)Z_url�r�@/usr/lib/python3.9/site-packages/cloudinit/sources/azure/imds.py�
headers_cbs
�rc@sFeZdZdZddddd�eeeeedd�dd�Zed	�d
d�Z	dS)�ReadUrlRetryHandleraKManager for readurl retry behavior using exception_callback().

    :param logging_backoff: Backoff to limit logging.
    :param max_connection_errors: Number of connection errors to retry on.
    :param retry_codes: Set of http codes to retry on.
    :param retry_deadline: Optional monotonic()-based deadline to retry until.
    ��?N)���i���logging_backoff�max_connection_errors�retry_codes�retry_deadline)rrr�returncCs.||_||_||_||_d|_d|_d|_dS)Nrr)rrrr�_logging_threshold�_request_count�_last_error)�selfrrrrrrr�__init__%s
zReadUrlRetryHandler.__init__�rcCsJ|jd7_t|t�s.td|tjd�dSd}|jdurNt�|jkrNd}nd}|jdur�t|j	t
j�r�|jd8_|jdkr�d}n|jdur�|j|j
vr�d}|j|jkr�|j|j9_nd}|jdur�|j|jkr�d}|j|_n:|jdu�st|jt��rt|j	|j��s"d}t|j	�|_|�s.|�sFtd|j|ftjd�|S)N�z1Polling IMDS failed with unexpected exception: %r�Zlogger_funcFTrz1Polling IMDS failed attempt %d with exception: %r)r�
isinstancer	r�LOG�warningrrr�cause�requests�ConnectionError�coderrrr�type)rZreq_args�	exception�logZretryrrr�exception_callback:sd
���
��
�
�

�
����z&ReadUrlRetryHandler.exception_callback)
�__name__�
__module__�__qualname__�__doc__�floatr�intr �boolr.rrrrrs��rT�)�log_response�timeout)�url�
retry_handlerr7r8rc
CsZzt||jtd||d�}Wn8tyR}z td|tjd��WYd}~n
d}~00|jS)a'Fetch URL from IMDS.

    :param url: url to fetch.
    :param log_response: log responses in readurl().
    :param retry_deadline: time()-based deadline to retry until.
    :param timeout: Read/connection timeout in seconds for readurl().

    :raises UrlError: on error fetching metadata.
    T�Zexception_cbrZinfiniteZlog_req_respr8z&Failed to fetch metadata from IMDS: %sr#N)r
r.rr	rr%r&�contents)r9r:r7r8�response�errorrrr�
_fetch_url{s �
�r?)r9r:rc
Cs\t||d�}zt�|�d��WStyV}z td|tjd��WYd}~n
d}~00dS)z�Fetch IMDS metadata.

    :param url: url to fetch.
    :param retry_deadline: time()-based deadline to retry until.

    :raises UrlError: on error fetching metadata.
    :raises ValueError: on error parsing metadata.
    �r:zutf-8z&Failed to parse metadata from IMDS: %sr#N)r?rZ	load_json�decode�
ValueErrorrr%r&)r9r:�metadatar>rrr�_fetch_metadata�s
�rD)rrrc
Cs�t||d�}ztd}t||d�WSty�}zR|jdkrvtdtjd�t||d�}td}t||d�WYd}~S�WYd}~n
d}~00dS)	z�Fetch extended metadata, falling back to non-extended as required.

    :param retry_deadline: time()-based deadline to retry until.

    :raises UrlError: on error fetching metadata.
    :raises ValueError: on error parsing metadata.
    )rrz./instance?api-version=2021-08-01&extended=truer@i�z,Falling back to IMDS api-version: 2019-06-01r#z /instance?api-version=2019-06-01N)r�IMDS_URLrDr	r*rr%r&)rrr:r9r>rrr� fetch_metadata_with_api_fallback�s(
�
��rFr!cCsPtd}tddddd�}t||jtddd	d
�}td|jd�d�tjd
�|j	S)zhFetch extended metadata, falling back to non-extended as required.

    :raises UrlError: on error.
    z'/reprovisiondata?api-version=2019-06-01g@r")rrrNrTFr6r;zPolled IMDS z time(s)r#)
rErr
r.rrrr%�debugr<)r9Zhandlerr=rrr�fetch_reprovision_data�s(�
�	�rH)N) Zloggingr
�timer�typingrrrrr(Z	cloudinitrZcloudinit.sources.helpers.azurerZcloudinit.url_helperr	r
Z	getLoggerr/r%rErrrr5r4�bytesr?rDr3rFrHrrrr�<module>s<
c��$��� 

© 2025 Cubjrnet7