HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/__pycache__/url_helper.cpython-39.pyc
a

��`�V�@s�ddlZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZmZmZddlZddlmZdd	lmZdd
lmZe�e�ZdZdZdZdZzddd
lm Z ddl!Z!e!�"d�Z#e e#j�Zee d�kr�dZe d�ek�re d�k�rnndZWne$�y.Yn0dd�Z%dd�Z&dd�Z'Gdd�de(�Z)Gdd�de)�Z*Gdd�de(�Z+Gdd �d e,�Z-d!d"�Z.d.d$d%�Z/d/d&d'�Z0Gd(d)�d)e(�Z1d0d*d+�Z2d,d-�Z3dS)1�N)�	parsedate)�ENOENT)�partial)�	NOT_FOUND)�count)�urlparse�
urlunparse�quote)�
exceptions)�log)�versionF�REDACTED)�LooseVersion�requestsz0.8.8Tz0.7.0z1.0.0cCs<tt|dd��}|ds4|dr4|d|d<d|d<t|�S)N�http)�scheme���)�listrr)�urlZ
parsed_url�r�6/usr/lib/python3/dist-packages/cloudinit/url_helper.py�	_cleanurl1s
rcGs$dd�}|}|D]}|||�}q|S)NcSsNtt|��}|d}|r*|�d�s*|d7}|tt|�dd�7}||d<t|�S)Nr�/z/:)�safe)rr�endswithr	�strr)r�add_onZ
url_parsed�pathrrr�combine_single=sz#combine_url.<locals>.combine_singler)�baseZadd_onsr rrrrr�combine_url;s
	r"c
Ks�|��}|�d�rd|}|���d�r�|�d�r>t�d|�|td�d�}z6t|d��}|��}Wd�n1sx0YWnJt	y�}z2|j
}|j
tkr�t}t
||d|d�|�WYd}~n
d}~00t||d	�St|fi|��SdS)
a0Wrapper function around readurl to allow passing a file path as url.

    When url is not a local file path, passthrough any kwargs to readurl.

    In the case of parameter passthrough to readurl, default values for some
    parameters. See: call-signature of readurl in this module for param docs.
    rz	file://%szfile://�dataz'Unable to post data to file resource %sN�rb)�cause�code�headersr)�contents)�lstrip�
startswith�lower�get�LOG�warning�len�open�read�IOError�errnorr�UrlError�FileResponse�readurl)r�kwargs�	file_path�fpr(�er&rrr�read_file_or_urlLs"

*
(r;c@s&eZdZd	dd�Zdd�Zdd�ZdS)
�StringResponse��cCs||_i|_||_d|_dS�N)r&r'r(r)�selfr(r&rrr�__init__lszStringResponse.__init__cOs|jdkrdSdS)Nr=FT�r&�r?�argsr7rrr�okrs
zStringResponse.okcCs|j�d�S)Nzutf-8)r(�decode�r?rrr�__str__wszStringResponse.__str__N)r=)�__name__�
__module__�__qualname__r@rDrGrrrrr<ks
r<c@seZdZddd�ZdS)r5r=cCstj|||d�||_dS)NrA)r<r@r)r?rr(r&rrrr@|szFileResponse.__init__N)r=�rHrIrJr@rrrrr5{sr5c@sVeZdZdd�Zedd��Zedd��Zddd	�Zed
d��Zedd
��Z	dd�Z
dS)�UrlResponsecCs
||_dSr>)�	_response)r?�responserrrr@�szUrlResponse.__init__cCs|jjSr>)rM�contentrFrrrr(�szUrlResponse.contentscCs|jjSr>)rMrrFrrrr�szUrlResponse.urlFcCs2d}|rd}d|jkr"|kr*nndSdSdS)Ni,i�r=TFrA)r?Zredirects_ok�upperrrrrD�szUrlResponse.okcCs|jjSr>)rMr'rFrrrr'�szUrlResponse.headerscCs|jjSr>)rM�status_coderFrrrr&�szUrlResponse.codecCs|jjSr>)rM�textrFrrrrG�szUrlResponse.__str__N)F)rHrIrJr@�propertyr(rrDr'r&rGrrrrrL�s


	

rLc@seZdZddd�ZdS)r4NcCs<t�|t|��||_||_||_|jdur2i|_||_dSr>)r2r@rr%r&r'r)r?r%r&r'rrrrr@�s
zUrlError.__init__)NNNrKrrrrr4�sr4cCs�i}t|�j}|dkr�|r�ts,t�dt�ndd|vrJ|drJ|d|d<nd|d<d|vrxd|vrx|d|dg|d<nd|vr�t|d�|d<|S)	N�httpszGSSL is not supported in requests v%s, cert. verification can not occur!Zca_certs�verifyT�	cert_file�key_fileZcert)rr�SSL_ENABLEDr-r.�_REQ_VERr)r�ssl_detailsZssl_argsrrrr�
_get_ssl_args�s"
��
r[rcsXt|�}d|i}|�t||��|
|d<|s8|r4dnd}||d<|durZtt|�d�|d<|durfg}trzd	d
i}||d<d}|r�tt|�dd�}d
dt��i}�r�|���|�|sʇfdd�}|}|r�||d<|dur�d}g}|
r�t	�nt
d|�D�]H}||�|d<i}|��D]p\}�|dk�r.�q|dk�r~|�r~�fdd�|D�}|�r�t�
��||<|D]}t|||<�qhn�||<�qz�|�r�t�d||
�r�dn|||�|du�r�t��}|� }|jfi|��}Wd�n1�s�0Y|	�r|��t�d||jt|j�|d�t|�WStj�yB}z�t|tj��r�t|d��r�t|jd��r�|�t ||jj|jj!|d��n6|�t ||d��t"�r�t|tj#��r�WYd}~�qF|�r�||��|d��s�WYd}~�qF|
�r�|dk�s|d|k�r.|dk�r.|�r$t�d|�t$�%|�WYd}~q�d}~00q�|�rT|d�dS)a�Wrapper around requests.Session to read the url and retry if necessary

    :param url: Mandatory url to request.
    :param data: Optional form data to post the URL. Will set request_method
        to 'POST' if present.
    :param timeout: Timeout in seconds to wait for a response
    :param retries: Number of times to retry on exception if exception_cb is
        None or exception_cb returns True for the exception caught. Default is
        to fail with 0 retries on exception.
    :param sec_between: Default 1: amount of seconds passed to time.sleep
        between retries. None or -1 means don't sleep.
    :param headers: Optional dict of headers to send during request
    :param headers_cb: Optional callable returning a dict of values to send as
        headers during request
    :param headers_redact: Optional list of header names to redact from the log
    :param ssl_details: Optional dict providing key_file, ca_certs, and
        cert_file keys for use on in ssl connections.
    :param check_status: Optional boolean set True to raise when HTTPError
        occurs. Default: True.
    :param allow_redirects: Optional boolean passed straight to Session.request
        as 'allow_redirects'. Default: True.
    :param exception_cb: Optional callable which accepts the params
        msg and exception and returns a boolean True if retries are permitted.
    :param session: Optional exiting requests.Session instance to reuse.
    :param infinite: Bool, set True to retry indefinitely. Default: False.
    :param log_req_resp: Set False to turn off verbose debug messages.
    :param request_method: String passed as 'method' to Session.request.
        Typically GET, or POST. Default: POST if data is provided, GET
        otherwise.
    r�allow_redirects�POST�GET�methodNr�timeoutZ
store_cookiesF�configrz
User-Agentz
Cloud-Init/%scs�Sr>r�r)r'rr�_cb
szreadurl.<locals>._cbr#���r'csg|]}��|�r|�qSr)r,)�.0�k)�vrr�
<listcomp>�zreadurl.<locals>.<listcomp>z'[%s/%s] open '%s' with %s configuration�infinitez(Read from %s (%s, %sb) after %s attemptsrNrQ�r&r'rrb�1Please wait %s seconds while we wait to try again)&r�updater[�max�float�CONFIG_ENABLED�intr�version_stringr�range�items�copy�deepcopyr
r-�debugrZSession�requestZraise_for_statusrQr/rOrLr
ZRequestException�
isinstance�	HTTPError�hasattrrN�appendr4r'rXZSSLError�time�sleep)rr#r`�retries�sec_betweenr'�
headers_cb�headers_redactrZ�check_statusr\�exception_cbZsessionrjZlog_req_resp�request_methodZreq_argsZ
req_configZmanual_triesZdef_headersrcZexcps�iZfiltered_req_argsrfZmatched_headers�keyZsess�rr:r)r'rgrr6�s�#���

�
0��
��

���"r6c
Cs,t��}
ddd�}|dur|}dd�}d}
d}|durB|||
�}nt|
d�d}|D�]�}t��}|
dkr�|||
�r|�q�|dur�|r�|||
|kr�t|
||�}d	}d}z�|dur�||�}ni}t||||d
|	d�}|j�sd|j}tt|�|j|j|d
�}n>|���s6d|j}tt|�|j|j|d
�}n||jfWSWnbt�yz}zd|}|}WYd}~n:d}~0t	�y�}zd|}|}WYd}~n
d}~00tt��|
�}|�r�d|nd}d||||f}||�|rV|||d�qV|||
��r�q(|
d}
t
�d|�t�|�q.dS)aI
    urls:      a list of urls to try
    max_wait:  roughly the maximum time to wait before giving up
               The max time is *actually* len(urls)*timeout as each url will
               be tried once and given the timeout provided.
               a number <= 0 will always result in only one try
    timeout:   the timeout provided to urlopen
    status_cb: call method with string message when a url is not available
    headers_cb: call method with single argument of url to get headers
                for request.
    headers_redact: a list of header names to redact from the log
    exception_cb: call method with 2 arguments 'msg' (per status_cb) and
                  'exception', the exception that occurred.
    sleep_time_cb: call method with 2 arguments (response, loop_n) that
                   generates the next sleep time.
    request_method: indicate the type of HTTP request, GET, PUT, or POST
    returns: tuple of (url, response contents), on failure, (False, None)

    the idea of this routine is to wait for the EC2 metadata service to
    come up.  On both Eucalyptus and EC2 we have seen the case where
    the instance hit the MD before the MD service was up.  EC2 seems
    to have permanently fixed this, though.

    In openstack, the metadata service might be painfully slow, and
    unable to avoid hitting a timeout of even up to 10 seconds or more
    (LP: #894279) for a simple GET.

    Offset those needs with the need to not hang forever (and block boot)
    on a system where cloud-init is configured to look for EC2 Metadata
    service but is not going to find one.  It is possible that the instance
    data host (169.254.169.254) may be firewalled off Entirely for a system,
    meaning that the connection will block forever unless a timeout is set.

    A value of None for max_wait will retry indefinitely.
    NcSst�|�dSr>)r-rw��msg�excrrr�
log_status_cb�sz#wait_for_url.<locals>.log_status_cbcSs$|durdS|dkp"t��||kS)NFr)r})�max_wait�
start_timerrr�timeup�szwait_for_url.<locals>.timeupr�rrF)r'r�r`r�r�zempty response [%s]rkzbad status code [%s]zrequest error [%s]zunexpected error [%s]z%ssZ	unlimitedzCalling '%s' failed [%s/%s]: %s)r��	exceptionrl)FN)N)
r}rqr6r(r&r4�
ValueErrorr'rD�	Exceptionr-rwr~)Zurlsr�r`�	status_cbr�r�Z
sleep_timer�Z
sleep_time_cbr�r�r�r�Zloop_nrNr�now�reasonZurl_excr'r:Z
time_takenZmax_wait_strZ
status_msgrrr�wait_for_urlXs�&


��
�
�

���r�c@s^eZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�ZdS)�OauthUrlHelperN�/run/oauth_skew.jsoncCst||_|pd|_||_||_||_d|_d|_|j|j|jf}t|�sNd|_nt|�s^t	d��|�
�}|pli|_dS)NrTr�FzBall or none of token_key, token_secret, or consumer_key can be set)�consumer_key�consumer_secret�	token_key�token_secret�skew_data_file�	_do_oauth�skew_change_limit�any�allr��read_skew_file�	skew_data)r?r�r�r�r�r��required�oldrrrr@�s
zOauthUrlHelper.__init__cCsP|jrLtj�|j�rLt|jdd��}t�|�Wd�S1sB0YdS)Nr���mode)r��osr�isfiler0�json�load)r?r9rrrr��s(zOauthUrlHelper.read_skew_filecCsh|js
dS|��}|duri}|||<t|jdd�� }|�t�|��Wd�n1sZ0YdS)N�wr�)r�r�r0�writer��dumps)r?�host�value�curr9rrr�update_skew_file�szOauthUrlHelper.update_skew_filec	
Cs�t|t�r|jdks"|jdks"dSd|jvr>t�d|j�dS|jd}zt�t|��}Wn4t	y�}zt�d||�WYd}~dSd}~00t
|t���}t|j�j
}|j�|d�}t||�|jkr�|�||�t�d||�||j|<dS)Ni�i��datez$Missing header 'date' in %s responsez#Failed to convert datetime '%s': %srz$Setting oauth clockskew for %s to %d)ryr4r&r'r-r.r}�mktimerr�rqrr�netlocr�r,�absr�r�)	r?r�r�r�Zremote_timer:Zskewr�Zold_skewrrrr��s2
��
�

zOauthUrlHelper.exception_cbcCsZ|js
iSd}t|�j}|jr>||jvr>tt���|j|}t||j|j|j	|j
|d�S)N)rr�r�r�r��	timestamp)r�rr�r�rqr}�
oauth_headersr�r�r�r�)r?rr�r�rrrr�s
�zOauthUrlHelper.headers_cbcCs:t|j|�d��|d<t|j|�d��|d<||i|��S)Nr�r�)r�_headers_cbr,�
_exception_cb)r?Zwrapped_funcrCr7rrr�_wrapped s��zOauthUrlHelper._wrappedcOs|�t||�Sr>)r�r�rBrrrr�'szOauthUrlHelper.wait_for_urlcOs|�t||�Sr>)r�r6rBrrrr6*szOauthUrlHelper.readurlc
Cs6d}z|r|||�}W|�||�n|�||�0|Sr>)r�)r?Zextra_exception_cbr�r��retrrrr�-szOauthUrlHelper._exception_cbcCs$i}|r||�}|�|�|��|Sr>)rmr�)r?Zextra_headers_cbrr'rrrr�6s
zOauthUrlHelper._headers_cb)NNNNr�)
rHrIrJr@r�r�r�r�r�r�r6r�r�rrrrr��s�
	r�c
Cs~zddlm}Wn.ty>}ztd�|�WYd}~n
d}~00|rNt|�}nd}|j|||||j|d�}|�|�\}	}
}|
S)Nrzoauth support is not available)Z
client_secretZresource_owner_keyZresource_owner_secretZsignature_methodr�)Zoauthlib.oauth1�oauth1�ImportError�NotImplementedErrorrZClientZSIGNATURE_PLAINTEXT�sign)rr�r�r�r�r�r�r:�clientZ_uriZsigned_headersZ_bodyrrrr�>s" 
�r�cCs8t|t�sdS|jtkrdS|jr4t|jtj�r4dSdS)z�readurl exception_cb that will retry on NOT_FOUND and Timeout.

    Returns False to raise the exception from readurl, True to retry.
    FT)ryr4r&rr%rZTimeoutr�rrr�retry_on_url_excUs

r�)NNrrNNNNTTNNFTN)	NNNNNrNNN)N)4rur�r�r}�email.utilsrr3r�	functoolsr�http.clientr�	itertoolsr�urllib.parserrr	rr
�	cloudinitr�loggingr�	getLoggerrHr-rXrprYr
Zdistutils.versionr�
pkg_resourcesZget_distributionZ_REQr�rr"r;�objectr<r5rLr2r4r[r6r�r�r�r�rrrr�<module>sh


$
!�
�
yn�