a 'Dg.@sddlZddlZddlmZddlmZmZmZmZddl Z ddl m Z ddl m Z ddlmZmZeeZdZdd ZGd d d Zd d deeeeedddZeeedddZdeeeedddZedddZdS)N) monotonic)DictOptionalTypeUnion)util)report_diagnostic_event)UrlErrorreadurlzhttp://169.254.169.254/metadatacCsdttdS)Ntrue)ZMetadatazx-ms-client-request-id)struuidZuuid4)Z_urlr@/usr/lib/python3.9/site-packages/cloudinit/sources/azure/imds.py headers_cbs rc@sFeZdZdZdddddeeeeeddddZed 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)ilogging_backoffmax_connection_errors retry_codesretry_deadline)rrrreturncCs.||_||_||_||_d|_d|_d|_dS)Nrr)rrrr_logging_threshold_request_count _last_error)selfrrrrrrr__init__%s zReadUrlRetryHandler.__init__rcCsJ|jd7_t|ts.td|tjddSd}|jdurNt|jkrNd}nd}|jdurt|j t j r|jd8_|jdkrd}n|j dur|j |j vrd}|j|jkr|j|j9_nd}|j dur|j|j krd}|j |_n:|jdust|jtrt|j |js"d}t|j |_|s.|sFtd|j|ftjd|S)Nz1Polling IMDS failed with unexpected exception: %rZ logger_funcFTrz1Polling IMDS failed attempt %d with exception: %r)r isinstancer rLOGwarningrrrcauserequestsConnectionErrorcoderrrrtype)rZreq_args exceptionlogZretryrrrexception_callback:sd             z&ReadUrlRetryHandler.exception_callback) __name__ __module__ __qualname____doc__floatrintr boolr.rrrrrs  rT) log_responsetimeout)url retry_handlerr7r8rc CsZzt||jtd||d}Wn8tyR}z td|tjdWYd}~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. TZ exception_cbrZinfiniteZ log_req_respr8z&Failed to fetch metadata from IMDS: %sr#N)r r.rr rr%r&contents)r9r:r7r8responseerrorrrr _fetch_url{s  r?)r9r:rc Cs\t||d}zt|dWStyV}z td|tjdWYd}~n d}~00dS)zFetch 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_jsondecode ValueErrorrr%r&)r9r:metadatar>rrr_fetch_metadatas rD)rrrc Cst||d}ztd}t||dWSty}zR|jdkrvtdtjdt||d}td}t||dWYd}~SWYd}~n d}~00dS) zFetch 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@iz,Falling back to IMDS api-version: 2019-06-01r#z /instance?api-version=2019-06-01N)rIMDS_URLrDr r*rr%r&)rrr:r9r>rrr fetch_metadata_with_api_fallbacks(  rFr!cCsPtd}tddddd}t||jtddd d }td |jdd 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=rrrfetch_reprovision_datas(  rH)N) Zloggingr timertypingrrrrr(Z cloudinitrZcloudinit.sources.helpers.azurerZcloudinit.url_helperr r Z getLoggerr/r%rErrr r5r4bytesr?rDr3rFrHrrrrs<    c $