U
    M8c                 J   @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZmZ d dlZd dlmZ d dlmZ d dlZd dlZd dl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(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZK eLeMZNdZOdZPdZQdZRdZSeTdZUeDe9e7e8fZVdgZWdddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d2d5d6d7d8d9d9d%d&d*d:d;d<d=d>d?d@d7dAdBdBdCd=dDdEdFd:dGdHdId>dJdKdLdMdNdOdOdPd/dGdQIZXejTdRejYdSZZdTdU Z[dVdW Z\dXdY Z]dZd[ Z^d\d] Z_d^d_ Z`d`da Zadbdc Zbddde ZcddgdhZddidj ZeG dkdl dlefZgG dmdn dnefZhG dodp dpZiG dqdr dreiZjG dsdt dtZkG dudv dveiZlddxdyZmdzd{ Zneofd|d}Zpd~d ZqeSfddZreSfddZsdd Ztdd Zudd ZvdddZwdddZxdd Zydd ZzG dd dZ{G dd dZ|dd Z}dd Z~dd Zdd Zdd Zdd ZdddZdddZdd Zdd Zdd Zdd ZdddZdddZdd Zdd ZG dd dZG dd dZG dd deZG dd dZG dd dZG dd dZG ddÄ dÃZG ddń dŃZG ddǄ deZG ddɄ dɃZdd˄ Zdd̈́ Zddτ Zddd҄ZddԄ Zddք Zdd؄ Zddڄ ZG dd܄ d܃ZG ddބ dރZG dd dZdd ZG dd dZdS )    N)Path)
getproxiesproxy_bypass)tzutc)LocationParseError)HEX_PAT)IPV4_PAT)IPV6_ADDRZ_PAT)IPV6_PAT)LS32_PAT)UNRESERVED_PAT)ZONE_ID_PAT)HAS_CRTIPV4_REIPV6_ADDRZ_REMD5_AVAILABLEUNSAFE_URL_CHARSOrderedDictget_md5get_tzinfo_optionsjsonquoteurlparseurlsplit
urlunsplitzip_longest)ClientErrorConfigNotFoundConnectionClosedErrorConnectTimeoutErrorEndpointConnectionErrorHTTPClientErrorInvalidDNSNameError!InvalidEndpointConfigurationErrorInvalidExpressionErrorInvalidHostLabelErrorInvalidIMDSEndpointErrorInvalidIMDSEndpointModeErrorInvalidRegionErrorMetadataRetrievalErrorMissingDependencyExceptionReadTimeoutErrorSSOTokenLoadErrorUnsupportedOutpostResourceError*UnsupportedS3AccesspointConfigurationErrorUnsupportedS3ArnErrorUnsupportedS3ConfigurationErrorUnsupportedS3ControlArnError&UnsupportedS3ControlConfigurationError   zhttp://169.254.169.254/zhttp://[fd00:ec2::254]/)ipv4ipv6z-._~z[a-z0-9][a-z0-9\-]*[a-z0-9]	dualstackzalexa-for-businessZmediatailorZpricingZ	sagemakerzapi-gatewayzapplication-auto-scalingZ	appstreamzauto-scalingzauto-scaling-planszcost-explorerzcloudhsm-v2zcloudsearch-domainzcognito-identity-providerzconfig-servicezcost-and-usage-report-serviceziot-data-planeziot-jobs-data-planezmediastore-datazdata-pipelinezdevice-farmziot-1click-devices-servicezdirect-connectzapplication-discovery-servicezdatabase-migration-servicezdirectory-servicezdynamodb-streamszelastic-beanstalkZefszelastic-load-balancingZemrzelastic-transcoderzelastic-load-balancing-v2Zseszmarketplace-entitlement-servicezelasticsearch-serviceZeventbridgeziot-1click-projectszkinesis-analyticszkinesis-videozlex-model-building-servicezlex-runtime-servicezcloudwatch-logszmachine-learningzmarketplace-commerce-analyticszmarketplace-meteringzmigration-hubZ
cloudwatchZmturkZ
opsworkscmzresource-groups-tagging-apizroute-53zroute-53-domainszsagemaker-runtimeZsimpledbzsecrets-managerZserverlessapplicationrepositoryzservice-catalogsfnzstorage-gateway)IZa4bZalexaforbusinesszapi.mediatailorzapi.pricingzapi.sagemakerZ
apigatewayzapplication-autoscalingZ
appstream2Zautoscalingzautoscaling-plansZceZ
cloudhsmv2Zcloudsearchdomainzcognito-idpconfigcurzdata.iotzdata.jobs.iotzdata.mediastoreZdatapipelineZ
devicefarmzdevices.iot1clickZdirectconnectZ	discoveryZdmsZdsZdynamodbstreamsZelasticbeanstalkZelasticfilesystemZelasticloadbalancingZelasticmapreduceZelastictranscoderZelbZelbv2emailzentitlement.marketplaceeseventszcloudwatch-eventsziot-dataziot-jobs-dataziot1click-devicesziot1click-projectsZkinesisanalyticsZkinesisvideoz
lex-modelszlex-runtimeZlogsZmachinelearningzmarketplace-entitlementZmarketplacecommerceanalyticszmetering.marketplaceZmeteringmarketplaceZmghz
models.lexZ
monitoringzmturk-requesterzopsworks-cmzprojects.iot1clickZresourcegroupstaggingapiZroute53Zroute53domainszruntime.lexzruntime.sagemakerZsdbZsecretsmanagerZserverlessrepoZservicecatalogZstatesZstepfunctionsZstoragegatewayzstreams.dynamodbZtaggingz^X-Amz-Checksum-([a-z0-9]*)$)flagsc                 C   s,   t | tr| S t | tr$|  dkS dS dS )z~Ensures a boolean value if a string or boolean is provided

    For strings, the value for True/False is case insensitive
    trueFN)
isinstanceboolstrlowerval rE   2/tmp/pip-unpacked-wheel-ozje0y8b/botocore/utils.pyensure_boolean   s
    

rG   c                 C   sL   |  d}|dk	r:| }|tkr6|td}tf ||S |  drHdS dS )zResolving IMDS endpoint mode to either IPv6 or IPv4.

    ec2_metadata_service_endpoint_mode takes precedence over imds_use_ipv6.
    "ec2_metadata_service_endpoint_modeN)modeZvalid_modesZimds_use_ipv6r5   r4   )get_config_variablerB   METADATA_ENDPOINT_MODESr'   )sessionZendpoint_modeZlendpoint_modeZerror_msg_kwargsrE   rE   rF   resolve_imds_endpoint_mode   s    

rM   c                 C   s2   t | do0| jddo0| jddko0| jdkS )zDetermines if the provided shape is the special header type jsonvalue.

    :type shape: botocore.shape
    :param shape: Shape to be inspected for the jsonvalue trait.

    :return: True if this type is a jsonvalue, False otherwise
    :rtype: Bool
    serializationZ	jsonvalueFlocationheaderstring)hasattrrN   get	type_name)shaperE   rE   rF   is_json_value_header   s    

rV   c                 C   s@   | dkrdS t |tjjr"| |kS |  dd | D kS dS )z&Case-insensitive check for header key.NFc                 S   s   g | ]}|  qS rE   rB   ).0keyrE   rE   rF   
<listcomp>   s     zhas_header.<locals>.<listcomp>)r?   botocore
awsrequestZHeadersDictrB   keys)header_nameheadersrE   rE   rF   
has_header   s
    r`   c                 C   sD   | j d| j d| j}|dd}|dd}tdd|}|S )zvReturns the module name for a service

    This is the value used in both the documentation and client class name
    ZserviceAbbreviationZserviceFullNameZAmazon ZAWSz\W+)metadatarS   Zservice_namereplaceresub)Zservice_modelnamerE   rE   rF   get_service_module_name   s     rg   c                 C   s   | sdS t | S )N/)remove_dot_segmentspathrE   rE   rF   normalize_url_path  s    rl   c                 C   s   | dkr| S t | S dS )zLReturns None if val is None, otherwise ensure value
    converted to booleanN)rG   rC   rE   rE   rF   normalize_boolean  s    rm   c                 C   s   | sdS |  d}g }|D ]0}|r|dkr|dkr@|rJ|  q|| q| d dkr^d}nd}| d dkrx|rxd}nd}|d| | S )Nra   rh   .z..r   )splitpopappendjoin)urlZ	input_urlZoutput_listxfirstlastrE   rE   rF   ri     s"    

ri   c                 C   s6   | r| dkrt | ddD ]}|| krt | dqd S )Nrn   
expression)[]*)r$   )ry   invalidrE   rE   rF   validate_jmespath_for_set:  s
    
r~   Tc                 C   s|   |rt | |dd}|d t|dkr2|d nd }}|sHt|d|rp|| kr\i | |< t| | ||ddS || |< d S )Nrn   r3   r   ra   rx   F)is_first)r~   rp   lenr$   set_value_from_jmespath)sourcery   valuer   bitscurrent_key	remainderrE   rE   rF   r   E  s     "
   r   c                 C   s   |  di }| ddk}|S )z9Determine if request is intended for an MRAP accesspoint.s3_accesspointregionra   rS   )contextr   	is_globalrE   rE   rF   is_global_accesspointc  s    r   c                   @   s   e Zd ZdZdS )_RetriesExceededErrorz@Internal exception used when the number of retries are exceeded.N)__name__
__module____qualname____doc__rE   rE   rE   rF   r   j  s   r   c                   @   s   e Zd Zdd ZdS )BadIMDSRequestErrorc                 C   s
   || _ d S Nrequestselfr   rE   rE   rF   __init__q  s    zBadIMDSRequestError.__init__N)r   r   r   r   rE   rE   rE   rF   r   p  s   r   c                   @   s   e Zd ZeZdZdZededddfddZ	dd Z
d	d
 Zdd Zdd ZdddZdd Zdd Zdd Zdd Zdd ZdddZdS ) IMDSFetcherzlatest/api/tokenZ21600r3   Nc                 C   s   || _ || _|d kri }| ||| _|| _|d kr>tj }|dd	 | _
| j
dk| _
|| _tjj| j t| jd| _d S )NZAWS_EC2_METADATA_DISABLEDfalser>   )timeoutproxies)_timeout_num_attempts_select_base_url	_base_url_configosenvironcopyrS   rB   	_disabled_user_agentr[   httpsessionURLLib3Sessionget_environ_proxies_session)r   r   num_attemptsbase_urlenv
user_agentr8   rE   rE   rF   r   {  s    	
zIMDSFetcher.__init__c                 C   s   | j S r   )r   r   rE   rE   rF   get_base_url  s    zIMDSFetcher.get_base_urlc                 C   s   |d kri }| ddk}| d}|r6|r6td d }|tkrH|}n|rR|}n|r\t}nt}td|  t|st|d|S )NrH   r5   ec2_metadata_service_endpointzFCustom endpoint and IMDS_USE_IPV6 are both set. Using custom endpoint.zIMDS ENDPOINT: %s)endpoint)rS   loggerwarningMETADATA_BASE_URLMETADATA_BASE_URL_IPv6debugis_valid_urir&   )r   r   r8   Zrequires_ipv6Zcustom_metadata_endpointZchosen_base_urlrE   rE   rF   r     s*    

zIMDSFetcher._select_base_urlc                 C   s,   d}| j r| j dsd}| j  | | S )Nra   rh   )r   endswith)r   rk   seprE   rE   rF   _construct_url  s    zIMDSFetcher._construct_urlc                 C   s.  |    | | j}d| ji}| | tjjd||d}t| j	D ]}zN| j
| }|jdkrp|jW   S |jdkrW  d S |jdkrt|W qD tk
r   Y  d S  tk
r } ztjd||dd	 W 5 d }~X Y qD tk
r& } z(t|jd
trt||dn W 5 d }~X Y qDX qDd S )Nz$x-aws-ec2-metadata-token-ttl-secondsPUTmethodrt   r_      )i  i  i  )i  OCaught retryable HTTP exception while making metadata service request to %s: %sTexc_infoerror)r   r   )_assert_enabledr   _TOKEN_PATH
_TOKEN_TTL_add_user_agentr[   r\   
AWSRequestranger   r   sendpreparestatus_codetextr   r+   RETRYABLE_HTTP_ERRORSr   r   r!   r?   kwargsrS   r   r&   )r   rt   r_   r   iresponseerE   rE   rF   _fetch_metadata_token  sD     
  


z!IMDSFetcher._fetch_metadata_tokenc           
      C   s   |    |dkr| j}| |}i }|dk	r4||d< | | t| jD ]r}z8tjjd||d}| j	
| }||s|W   S W qH tk
r }	 ztjd||	dd W 5 d}	~	X Y qHX qH|  dS )aZ  Make a get request to the Instance Metadata Service.

        :type url_path: str
        :param url_path: The path component of the URL to make a get request.
            This arg is appended to the base_url that was provided in the
            initializer.

        :type retry_func: callable
        :param retry_func: A function that takes the response as an argument
             and determines if it needs to retry. By default empty and non
             200 OK responses are retried.

        :type token: str
        :param token: Metadata token to send along with GET requests to IMDS.
        Nzx-aws-ec2-metadata-tokenGETr   r   Tr   )r   _default_retryr   r   r   r   r[   r\   r   r   r   r   r   r   r   _RETRIES_EXCEEDED_ERROR_CLS)
r   url_path
retry_functokenrt   r_   r   r   r   r   rE   rE   rF   _get_request  s4    

  zIMDSFetcher._get_requestc                 C   s   | j d k	r| j |d< d S )Nz
User-Agent)r   )r   r_   rE   rE   rF   r     s    
zIMDSFetcher._add_user_agentc                 C   s   | j rtd |  d S )Nz)Access to EC2 metadata has been disabled.)r   r   r   r   r   rE   rE   rF   r     s    
zIMDSFetcher._assert_enabledc                 C   s   |  |p| |S r   _is_non_ok_response	_is_emptyr   r   rE   rE   rF   r     s    zIMDSFetcher._default_retryc                 C   s"   |j dkr| j|ddd dS dS )Nr   znon-200Tlog_bodyF)r   _log_imds_responser   rE   rE   rF   r     s    
zIMDSFetcher._is_non_ok_responsec                 C   s   |j s| j|ddd dS dS )Nzno bodyTr   F)contentr   r   rE   rE   rF   r     s    zIMDSFetcher._is_emptyFc                 C   s>   d}||j |jg}|r*|d7 }||j tj|f|  d S )NzHMetadata service returned %s response with status code of %s for url: %sz, content body: %s)r   rt   rr   r   r   r   )r   r   Zreason_to_logr   Z	statementZlogger_argsrE   rE   rF   r   #  s    zIMDSFetcher._log_imds_response)N)F)r   r   r   r   r   r   r    DEFAULT_METADATA_SERVICE_TIMEOUTr   r   r   r   r   r   r   r   r   r   r   r   r   rE   rE   rE   rF   r   u  s*   
$
*r   c                   @   s`   e Zd ZdZddddgZdd Zdd	d
ZdddZdd Zdd Z	dd Z
dd Zdd ZdS )InstanceMetadataFetcherz*latest/meta-data/iam/security-credentials/AccessKeyIdSecretAccessKeyToken
Expirationc              
   C   s   z~|   }| |}| ||}| |rZ||d |d |d |d d}| | |W S d|krvd|krvtd| i W S W nR | jk
r   td	| j Y n0 t	k
r } ztd
|j
 W 5 d }~X Y nX i S )Nr   r   r   r   )	role_nameZ
access_keyZ
secret_keyr   expiry_timeCodeMessagez7Error response received when retrievingcredentials: %s.\Max number of attempts exceeded (%s) when attempting to retrieve data from metadata service.zBad IMDS request: %s)r   _get_iam_role_get_credentials_contains_all_credential_fields_evaluate_expirationr   r   r   r   r   r   )r   r   r   credentialsr   rE   rE   rF   retrieve_iam_role_credentials8  s6    




 z5InstanceMetadataFetcher.retrieve_iam_role_credentialsNc                 C   s   | j | j| j|djS Nr   r   r   )r   	_URL_PATH_needs_retry_for_role_namer   )r   r   rE   rE   rF   r   a  s
    z%InstanceMetadataFetcher._get_iam_rolec                 C   s$   | j | j| | j|d}t|jS r   )r   r   _needs_retry_for_credentialsr   loadsr   )r   r   r   rrE   rE   rF   r   h  s    z(InstanceMetadataFetcher._get_credentialsc                 C   s:   zt |j W dS  tk
r4   | |d Y dS X d S )NFzinvalid jsonT)r   r   r   
ValueErrorr   r   rE   rE   rF   _is_invalid_jsonp  s    z(InstanceMetadataFetcher._is_invalid_jsonc                 C   s   |  |p| |S r   r   r   rE   rE   rF   r   x  s    z2InstanceMetadataFetcher._needs_retry_for_role_namec                 C   s   |  |p| |p| |S r   )r   r   r   r   rE   rE   rF   r   {  s
    
z4InstanceMetadataFetcher._needs_retry_for_credentialsc                 C   s*   | j D ]}||krtd|  dS qdS )Nz3Retrieved credentials is missing required field: %sFT)_REQUIRED_CREDENTIAL_FIELDSr   r   )r   r   fieldrE   rE   rF   r     s    
z7InstanceMetadataFetcher._contains_all_credential_fieldsc           
      C   s   | d}|d krd S ztj|d}| j dd}tdd}|| }tj }tj|d}|| }||kr|| }	|	d|d< t	
d|d d	d
 W n( tk
r   t	d|d   Y nX d S )Nr   z%Y-%m-%dT%H:%M:%SZZec2_credential_refresh_windowiX  x   )secondszAttempting credential expiration extension due to a credential service availability issue. A refresh of these credentials will be attempted again within the next <   z.0fz	 minutes.zUnable to parse expiry_time in )rS   datetimestrptimer   randomrandintutcnow	timedeltastrftimer   infor   r   )
r   r   Z
expirationZrefresh_intervaljitterZrefresh_interval_with_jittercurrent_timeZrefresh_offsetZextension_timeZnew_timerE   rE   rF   r     s>    
  
z,InstanceMetadataFetcher._evaluate_expiration)N)N)r   r   r   r   r   r   r   r   r   r   r   r   r   rE   rE   rE   rF   r   /  s   )


r   c                   @   s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )IMDSRegionProviderNc                 C   s$   || _ |dkrtj}|| _|| _dS )aU  Initialize IMDSRegionProvider.
        :type session: :class:`botocore.session.Session`
        :param session: The session is needed to look up configuration for
            how to contact the instance metadata service. Specifically the
            whether or not it should use the IMDS region at all, and if so how
            to configure the timeout and number of attempts to reach the
            service.
        :type environ: None or dict
        :param environ: A dictionary of environment variables to use. If
            ``None`` is the argument then ``os.environ`` will be used by
            default.
        :type fecther: :class:`botocore.utils.InstanceMetadataRegionFetcher`
        :param fetcher: The class to actually handle the fetching of the region
            from the IMDS. If not provided a default one will be created.
        N)r   r   r   _environ_fetcher)r   rL   r   fetcherrE   rE   rF   r     s
    zIMDSRegionProvider.__init__c                 C   s   |   }|S )z#Provide the region value from IMDS.)_get_instance_metadata_region)r   Zinstance_regionrE   rE   rF   provide  s    zIMDSRegionProvider.providec                 C   s   |   }| }|S r   )_get_fetcherretrieve_region)r   r  r   rE   rE   rF   r    s    z0IMDSRegionProvider._get_instance_metadata_regionc                 C   s   | j d kr|  | _ | j S r   )r  _create_fetcherr   rE   rE   rF   r    s    

zIMDSRegionProvider._get_fetcherc                 C   sN   | j d}| j d}| j dt| j d}t||| j| j  |d}|S )NZmetadata_service_timeoutZmetadata_service_num_attemptsr   )r   rH   )r   r   r   r   r8   )r   rJ   rM   InstanceMetadataRegionFetcherr  r   )r   Zmetadata_timeoutZmetadata_num_attemptsZimds_configr  rE   rE   rF   r    s*    z"IMDSRegionProvider._create_fetcher)NN)r   r   r   r   r  r  r  r  rE   rE   rE   rF   r    s
   
r  c                   @   s    e Zd ZdZdd Zdd ZdS )r  z-latest/meta-data/placement/availability-zone/c                 C   s8   z|   }|W S  | jk
r2   td| j Y nX dS )aR  Get the current region from the instance metadata service.
        :rvalue: str
        :returns: The region the current instance is running in or None
            if the instance metadata service cannot be contacted or does not
            give a valid response.
        :rtype: None or str
        :returns: Returns the region as a string if it is configured to use
            IMDS as a region source. Otherwise returns ``None``. It will also
            return ``None`` if it fails to get the region from IMDS due to
            exhausting its retries or not being able to connect.
        r   N)_get_regionr   r   r   r   )r   r   rE   rE   rF   r    s    
z-InstanceMetadataRegionFetcher.retrieve_regionc                 C   s2   |   }| j| j| j|d}|j}|d d }|S )Nr   ro   )r   r   r   r   r   )r   r   r   Zavailability_zoner   rE   rE   rF   r    s    z)InstanceMetadataRegionFetcher._get_regionN)r   r   r   r   r  r  rE   rE   rE   rF   r    s   r  Fc                 C   s   |D ]}t || trH|| kr:||kr:t| | ||  q|| | |< qt || tr|r|| krt | | tr| | ||  q|| | |< q|| | |< qdS )zGiven two dict, merge the second dict into the first.

    The dicts can have arbitrary nesting.

    :param append_lists: If true, instead of clobbering a list with the new
        value, append all of the new values onto the original list.
    N)r?   dictmerge_dictslistextend)Zdict1Zdict2Zappend_listsrY   rE   rE   rF   r    s    r  c                 C   s"   i }| D ]}| | ||  < q|S )zDCopies the given dictionary ensuring all keys are lowercase strings.rW   )originalr   rY   rE   rE   rF   lowercase_dict1  s    r  c              
   C   sV   z2||  }|  }t|W  5 Q R  W S Q R X W n tk
rP   t| dY nX d S )Nrj   )readparse_key_val_file_contentsOSErrorr   )filename_openfcontentsrE   rE   rF   parse_key_val_file9  s    
 r$  c                 C   sH   i }|   D ]6}d|krq|dd\}}| }| }|||< q|S )N=r3   )
splitlinesrp   strip)r#  finallinerY   rD   rE   rE   rF   r  B  s    
r  c                 C   s   g }t | dr|  }n| }|D ]V\}}t|trZ|D ] }|t| dt|  q6q |t| dt|  q d|S )af  Urlencode a dict or list into a string.

    This is similar to urllib.urlencode except that:

    * It uses quote, and not quote_plus
    * It has a default list of safe chars that don't need
      to be encoded, which matches what AWS services expect.

    If any value in the input ``mapping`` is a list type,
    then each list element wil be serialized.  This is the equivalent
    to ``urlencode``'s ``doseq=True`` argument.

    This function should be preferred over the stdlib
    ``urlencode()`` function.

    :param mapping: Either a dict to urlencode or a list of
        ``(key, value)`` pairs.

    itemsr%  &)rR   r*  r?   r  rr   percent_encoders   )mappingsafeZencoded_pairspairsrY   r   elementrE   rE   rF   percent_encode_sequenceQ  s    


r1  c                 C   s6   t | ttfst| } t | ts*| d} t| |dS )a  Urlencodes a string.

    Whereas percent_encode_sequence handles taking a dict/sequence and
    producing a percent encoded string, this function deals only with
    taking a string (not a dict/sequence) and percent encoding it.

    If given the binary type, will simply URL encode it. If given the
    text type, will produce the binary type by UTF-8 encoding the
    text. If given something else, will convert it to the text type
    first.
    utf-8)r.  )r?   bytesrA   encoder   )	input_strr.  rE   rE   rF   r,  w  s
    

r,  c              
   C   s   t | ttfrtj| | S ztjt| | W S  ttfk
rL   Y nX ztjj	| dt
 idW S  ttfk
r } ztd|  d| W 5 d}~X Y nX dS )z.Parse timestamp with pluggable tzinfo options.GMT)ZtzinfoszInvalid timestamp "z": N)r?   intfloatr  fromtimestamp	TypeErrorr   dateutilparserparser   r   tzinfor   rE   rE   rF   _parse_timestamp_with_tzinfo  s    r@  c                 C   sb   t  D ]J}zt| |W   S  tk
rN } ztjd|j|d W 5 d}~X Y qX qtd|  dS )zParse a timestamp into a datetime object.

    Supported formats:

        * iso8601
        * rfc822
        * epoch (value is an integer)

    This will return a ``datetime.datetime`` object.

    z2Unable to parse timestamp with "%s" timezone info.r   Nz4Unable to calculate correct timezone offset for "%s")r   r@  r  r   r   r   RuntimeErrorr>  rE   rE   rF   parse_timestamp  s    
rB  c                 C   sD   t | tjr| }nt| }|jdkr4|jt d}n|t }|S )a  Converted the passed in value to a datetime object with tzinfo.

    This function can be used to normalize all timestamp inputs.  This
    function accepts a number of different types of inputs, but
    will always return a datetime.datetime object with time zone
    information.

    The input param ``value`` can be one of several types:

        * A datetime object (both naive and aware)
        * An integer representing the epoch time (can also be a string
          of the integer, i.e '0', instead of 0).  The epoch time is
          considered to be UTC.
        * An iso8601 formatted timestamp.  This does not need to be
          a complete timestamp, it can contain just the date portion
          without the time component.

    The returned value will be a datetime object that will have tzinfo.
    If no timezone info was provided in the input value, then UTC is
    assumed, not local time.

    Nr?  )r?   r  rB  r?  rc   r   
astimezone)r   Zdatetime_objrE   rE   rF   parse_to_aware_datetime  s    
rE  c                 C   s~   t  ddd}| jdkr2|dkr&t }| j|d} | jdd|   | }t|dr\| S |j|j|j	d d  d  d S )	aw  Calculate the timestamp based on the given datetime instance.

    :type dt: datetime
    :param dt: A datetime object to be converted into timestamp
    :type default_timezone: tzinfo
    :param default_timezone: If it is provided as None, we treat it as tzutc().
                             But it is only used when dt is a naive datetime.
    :returns: The timestamp
      r3   NrC  total_seconds   i  i@B )
r  r?  r   rc   	utcoffsetrR   rG  microsecondsr   days)dtZdefault_timezoneepochdrE   rE   rF   datetime2timestamp  s    


rO  c                    sB   t  }t fdddD ]}|| q|r6| S | S dS )a  Calculate a sha256 checksum.

    This method will calculate the sha256 checksum of a file like
    object.  Note that this method will iterate through the entire
    file contents.  The caller is responsible for ensuring the proper
    starting position of the file and ``seek()``'ing the file back
    to its starting location if other consumers need to read from
    the file like object.

    :param body: Any file like object.  The file must be opened
        in binary mode such that a ``.read()`` call returns bytes.
    :param as_hex: If True, then the hex digest is returned.
        If False, then the digest (as binary bytes) is returned.

    :returns: The sha256 checksum

    c                      s
     dS N   r  rE   bodyrE   rF   <lambda>      z"calculate_sha256.<locals>.<lambda>rV  N)hashlibsha256iterupdate	hexdigestdigest)rT  Zas_hexchecksumchunkrE   rS  rF   calculate_sha256  s    r_  c                    s   g }dt j}t fdddD ]}|||  q"|sJ|d S t|dkrg }t|D ]2\}}|dk	r||||   qb|| qb|}qJt	|d 
dS )	a\  Calculate a tree hash checksum.

    For more information see:

    http://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html

    :param body: Any file like object.  This has the same constraints as
        the ``body`` param in calculate_sha256

    :rtype: str
    :returns: The hex version of the calculated tree hash

    rQ  c                      s
     S r   rR  rE   rT  Zrequired_chunk_sizerE   rF   rU  )  rV  z%calculate_tree_hash.<locals>.<lambda>rV  r3   Nr   ascii)rW  rX  rY  rr   r\  r[  r   	_in_pairsbinasciihexlifydecode)rT  chunksrX  r^  Z
new_chunksrv   secondrE   r`  rF   calculate_tree_hash  s    rh  c                 C   s   t | }t||S r   )rY  r   )iterableZshared_iterrE   rE   rF   rb  9  s    	rb  c                   @   s    e Zd ZdZdd Zdd ZdS )CachedPropertyzA read only property that caches the initially computed value.

    This descriptor will only call the provided ``fget`` function once.
    Subsequent access to this property will return the cached value.

    c                 C   s
   || _ d S r   )_fget)r   fgetrE   rE   rF   r   R  s    zCachedProperty.__init__c                 C   s,   |d kr| S |  |}||j| j j< |S d S r   )rk  __dict__r   )r   objclsZcomputed_valuerE   rE   rF   __get__U  s
    
zCachedProperty.__get__N)r   r   r   r   r   rp  rE   rE   rE   rF   rj  J  s   rj  c                   @   sD   e Zd ZdZdddZdd Zddd	Zd
d Zdd Zdd Z	dS )ArgumentGeneratora  Generate sample input based on a shape model.

    This class contains a ``generate_skeleton`` method that will take
    an input/output shape (created from ``botocore.model``) and generate
    a sample dictionary corresponding to the input/output shape.

    The specific values used are place holder values. For strings either an
    empty string or the member name can be used, for numbers 0 or 0.0 is used.
    The intended usage of this class is to generate the *shape* of the input
    structure.

    This can be useful for operations that have complex input shapes.
    This allows a user to just fill in the necessary data instead of
    worrying about the specific structure of the input arguments.

    Example usage::

        s = botocore.session.get_session()
        ddb = s.get_service_model('dynamodb')
        arg_gen = ArgumentGenerator()
        sample_input = arg_gen.generate_skeleton(
            ddb.operation_model('CreateTable').input_shape)
        print("Sample input for dynamodb.CreateTable: %s" % sample_input)

    Fc                 C   s
   || _ d S r   )_use_member_names)r   Zuse_member_namesrE   rE   rF   r   y  s    zArgumentGenerator.__init__c                 C   s   g }|  ||S )zGenerate a sample input.

        :type shape: ``botocore.model.Shape``
        :param shape: The input shape.

        :return: The generated skeleton input corresponding to the
            provided input shape.

        )_generate_skeleton)r   rU   stackrE   rE   rF   generate_skeleton|  s    
z#ArgumentGenerator.generate_skeletonra   c                 C   s   | |j z|jdkr(| ||W S |jdkrB| ||W S |jdkr\| ||W S |jdkr| jrt|W |S |jrt	
|jW fS W `dS |jdkrW NdS |jdkrW <d	S |jd
krW *dS |jdkrtddddddW S W 5 |  X d S )NZ	structurer  maprQ   ra   )integerlongr   )r8  doubleg        booleanT	timestamprF  r3   )rr   rf   rq   rT   _generate_type_structure_generate_type_list_generate_type_maprr  enumr  choicer  r   rU   rt  rf   rE   rE   rF   rs    s.    







z$ArgumentGenerator._generate_skeletonc                 C   sF   | |jdkri S t }|j D ]\}}| j|||d||< q$|S )Nr3   )rf   )countrf   r   membersr*  rs  )r   rU   rt  Zskeletonmember_nameZmember_shaperE   rE   rF   r|    s      z*ArgumentGenerator._generate_type_structurec                 C   s$   d}| j r|jj}| |j||gS )Nra   )rr  memberrf   rs  r  rE   rE   rF   r}    s
    z%ArgumentGenerator._generate_type_listc                 C   s0   |j }|j}|jdksttd| ||fgS )NrQ   ZKeyName)rY   r   rT   AssertionErrorr   rs  )r   rU   rt  Z	key_shapeZvalue_shaperE   rE   rF   r~    s    z$ArgumentGenerator._generate_type_mapN)F)ra   )
r   r   r   r   r   ru  rs  r|  r}  r~  rE   rE   rE   rF   rq  ^  s   



rq  c                 C   s.   t | rdS dt| j d}t|d k	S )NFrz   r{   )r   intersectionr   hostnamer   matchendpoint_urlr  rE   rE   rF   is_valid_ipv6_endpoint_url  s    
r  c                 C   s   t | j}t|d k	S r   )r   r  r   r  r  rE   rE   rF   is_valid_ipv4_endpoint_url  s    
r  c                 C   sh   t | rdS t| }|j}|dkr(dS t|dkr8dS |d dkrP|dd }tdtj}||S )zVerify the endpoint_url is valid.

    :type endpoint_url: string
    :param endpoint_url: An endpoint_url.  Must have at least a scheme
        and a hostname.

    :return: True if the endpoint url is valid. False otherwise.

    FN   ro   rn   z;^((?!-)[A-Z\d-]{1,63}(?<!-)\.)*((?!-)[A-Z\d-]{1,63}(?<!-))$)	r   r  r   r  r   rd   compile
IGNORECASEr  )r  partsr  allowedrE   rE   rF   is_valid_endpoint_url  s    
r  c                 C   s   t | pt| S r   )r  r  )r  rE   rE   rF   r     s    
r   c                 C   s2   | dkrdS t d}|| }|s.t| ddS )z0Provided region_name must be a valid host label.Nz)^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?<!-)$)region_name)rd   r  r  r(   )r  Zvalid_host_labelZvalidrE   rE   rF   validate_region_name  s    

r  c                 C   sR   d| krdS t | }|dk s$|dkr(dS t| }|dksJ| t | krNdS dS )a  
    Check to see if the ``bucket_name`` complies with the
    restricted DNS naming conventions necessary to allow
    access via virtual-hosting style.

    Even though "." characters are perfectly valid in this DNS
    naming scheme, we are going to punt on any name containing a
    "." character because these will cause SSL cert validation
    problems if we try to use virtual-hosting style addressing.
    rn   F   ?   NT)r   LABEL_REr  end)bucket_namenr  rE   rE   rF   check_dns_name  s    
r  c              
   K   s`   | j ddrd}zt| || W n8 tk
rZ } z|jd }td| W 5 d}~X Y nX dS )ar  
    This handler looks at S3 requests just before they are signed.
    If there is a bucket name on the path (true for everything except
    ListAllBuckets) it checks to see if that bucket name conforms to
    the DNS naming conventions.  If it does, it alters the request to
    use ``virtual hosting`` style addressing rather than ``path-style``
    addressing.

    Zuse_global_endpointFzs3.amazonaws.comr  z2Not changing URI, bucket is not DNS compatible: %sN)r   rS   switch_to_virtual_host_styler"   r   r   r   )r   signature_versionr  default_endpoint_urlr   r   r  rE   rE   rF   fix_s3_host  s      
 r  c                 K   s  | j dk	rdS t| r$td dS t| j}|j| _ |jd}|dkrP|j}t	|dkr|d }|sndS td| j t
|rt	|dkr| j d dkr|  j d7  _ || d|pd}|}|d | }	|j|	||jd	f}
t|
}|| _td
| n
t|ddS )a)  
    This is a handler to force virtual host style s3 addressing no matter
    the signature version (which is taken in consideration for the default
    case). If the bucket is not DNS compatible an InvalidDNSName is thrown.

    :param request: A AWSRequest object that is about to be sent.
    :param signature_version: The signature version to sign with
    :param default_endpoint_url: The endpoint to use when switching to a
        virtual style. If None is supplied, the virtual host will be
        constructed from the url of the request.
    NzKRequest is GetBucketLocation operation, not checking for DNS compatibility.rh   r3   z*Checking for DNS compatible bucket for: %s   ro   rn   ra   zURI updated to: %s)r  )Z	auth_path_is_get_bucket_location_requestr   r   r   rt   rk   rp   netlocr   r  removers   schemequeryr   r"   )r   r  r  r   r  
path_partsr  rk   Zglobal_endpointhostZ	new_tupleZnew_urirE   rE   rF   r  /  s<    



r  c                 C   s   | j dS )Nz	?location)rt   r   r   rE   rE   rF   r  o  s    r  c                    s"    j t  fdd}|S )a  Method decorator for caching method calls to a single instance.

    **This is not a general purpose caching decorator.**

    In order to use this, you *must* provide an ``_instance_cache``
    attribute on the instance.

    This decorator is used to cache method calls.  The cache is only
    scoped to a single instance though such that multiple instances
    will maintain their own cache.  In order to keep things simple,
    this decorator requires that you provide an ``_instance_cache``
    attribute on your instance.

    c                    s\   |f}|r&t t| }||f}| j|}|d k	r>|S  | f||}|| j|< |S r   )tuplesortedr*  Z_instance_cacherS   )r   argsr   	cache_keyZkwarg_itemsresultfunc	func_namerE   rF   _cache_guard  s    

z$instance_cache.<locals>._cache_guard)r   	functoolswraps)r  r  rE   r  rF   instance_caches  s    r  c                 K   sh   t | jjd}dd |D }d}t|dkrB|d|d 7 }|d7 }|dkrVdS t| |d	d
 dS )z?Switches the current s3 endpoint with an S3 Accelerate endpointrn   c                 S   s   g | ]}|t kr|qS rE   S3_ACCELERATE_WHITELISTrX   prE   rE   rF   rZ     s      z-switch_host_s3_accelerate.<locals>.<listcomp>zhttps://s3-accelerate.r   amazonaws.com)ZListBucketsCreateBucketZDeleteBucketNF)use_new_scheme)r   rt   r  rp   r   rs   _switch_hosts)r   Zoperation_namer   r  r   rE   rE   rF   switch_host_s3_accelerate  s    r  c                 C   s2   t | jd}||r.|| }t| | dS )zBSwitches the host using a parameter value from a JSON request bodyr2  N)r   r   datare  rS   r  )r   
param_nameZrequest_jsonnew_endpointrE   rE   rF   switch_host_with_param  s    
r  c                 C   s   t | j||}|| _d S r   )_get_new_endpointrt   )r   r  r  final_endpointrE   rE   rF   r    s      r  c                 C   sV   t |}t | }|j}|r |j}||j|j|jdf}t|}td|  d|  |S Nra   zUpdating URI from  to )r   r  r  rk   r  r   r   r   )Zoriginal_endpointr  r  Znew_endpoint_componentsZoriginal_endpoint_componentsr  Zfinal_endpoint_componentsr  rE   rE   rF   r    s    r  c                 C   sR   |D ]H}|| kr@t | | tr@t || tr@t| | ||  q|| | |< qdS )zDeeply two dictionaries, overriding existing keys in the base.

    :param base: The base dictionary which will be merged into.
    :param extra: The dictionary to merge into the base. Keys from this
        dictionary will take precedence.
    N)r?   r  
deep_merge)baseextrarY   rE   rE   rF   r    s    r  c                 C   s   |  dd S )zcTranslate the form used for event emitters.

    :param service_id: The service_id to convert.
     -)rc   rB   )Z
service_idrE   rE   rF   hyphenize_service_id  s    r  c                   @   sL   e Zd ZdZdddZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dS )S3RegionRedirectorv2a  Updated version of S3RegionRedirector for use when
    EndpointRulesetResolver is in use for endpoint resolution.

    This class is considered private and subject to abrupt breaking changes or
    removal without prior announcement. Please do not use it directly.
    Nc                 C   s   |pi | _ t|| _d S r   )_cacheweakrefproxy_clientr   Zendpoint_bridgeclientcacherE   rE   rF   r     s    
zS3RegionRedirectorv2.__init__c                 C   sF   t d |p| jjj}|d| j |d| j |d| j d S )Nz(Registering S3 region redirector handlerneeds-retry.s3before-parameter-build.s3zbefore-endpoint-resolution.s3)	r   r   r  metar<   registerredirect_from_errorannotate_request_contextredirect_from_cacher   event_emitterZemitterrE   rE   rF   r    s    
  zS3RegionRedirectorv2.registerc                 K   s  |dkrdS | di  di }t| dr>td dS | drVtd dS |d  d	i }| d
}|d  di }|dko|jdk}	|dko|jdkod| di k}
|dkod|k}|d dk	o|d jdk}|dk}t|	|||
|gsdS |d d d }|d  d}| ||}|dkrFtd||f  dS td|||f  || j	|< | j
j}|j||d d d |d d}| |d |j|d< d|d d d< |j d}|dk	r||}|\}}||d d< |d  di ||d d< dS )   
        An S3 request sent to the wrong region will return an error that
        contains the endpoint the request should be sent to. This handler
        will add the redirect information to the signing context and then
        redirect the request.
        Nr   s3_redirectbucketzBS3 request was previously for an Accesspoint ARN, not redirecting.
redirected6S3 request was previously redirected, not redirecting.r3   Errorr   ResponseMetadataZ301Z400
HeadObject
HeadBucketx-amz-bucket-regionHTTPHeadersAuthorizationHeaderMalformedRegionr   i-  i.  i3  PermanentRedirectclient_regionS3 client configured for region %s but the bucket %s is not in that region and the proper region could not be automatically determined.S3 client configured for region %s but the bucket %s is in region %s; Please configure the proper region to avoid multiple unnecessary redirects and signing attempts.params)Zoperation_modelZ	call_argsrequest_contextrt   TZauthSchemes	auth_typesigning)rS   	ArnParseris_arnr   r   rf   r   anyget_bucket_regionr  r  Z_ruleset_resolverconstruct_endpointset_request_urlrt   Z
propertiesZauth_schemes_to_signing_ctx)r   request_dictr   	operationr   Zredirect_ctxr   
error_coderesponse_metadatais_special_head_objectis_special_head_bucketis_wrong_signing_regionis_redirect_statusis_permanent_redirectr  r  
new_regionZep_resolverZep_infoZauth_schemesZ	auth_infor  signing_contextrE   rE   rF   r    s    


	

 

z(S3RegionRedirectorv2.redirect_from_errorc              
   C   s   |d }|d d }d|kr$|d S | di  dd}|dk	rD|S z| jj|d}|d d }W n0 tk
r } z|jd d }W 5 d}~X Y nX | dd}|S 	a.  
        There are multiple potential sources for the new region to redirect to,
        but they aren't all universally available for use. This will try to
        find region from response elements, but will fall back to calling
        HEAD on the bucket if all else fails.

        :param bucket: The bucket to find the region for. This is necessary if
            the region is not available in the error response.
        :param response: A response representing a service request that failed
            due to incorrect region configuration.
        r3   r  r  r  r  r  N)BucketrS   r  Zhead_bucketr   r   r   r  r   Zservice_responseresponse_headersr   r_   r   rE   rE   rF   r  h  s     z&S3RegionRedirectorv2.get_bucket_regionc                 K   s   t ||dS )z
        Splice a new endpoint into an existing URL. Note that some endpoints
        from the the endpoint provider have a path component which will be
        discarded by this function.
        F)r  )r   old_urlr  r   rE   rE   rF   r    s    z$S3RegionRedirectorv2.set_request_urlc                 K   s4   | d}|dk	r0|| jkr0| j |}||d< dS )a  
        If a bucket name has been redirected before, it is in the cache. This
        handler will update the AWS::Region endpoint resolver builtin param
        to use the region from cache instead of the client region to avoid the
        redirect.
        r  NzAWS::Region)rS   r  )r   builtinsr  r   r  r  rE   rE   rF   r    s    
z(S3RegionRedirectorv2.redirect_from_cachec                 K   s   | d}d||d|d< dS )zStore the bucket name in context for later use when redirecting.
        The bucket name may be an access point ARN or alias.
        r  F)r  r  r  r  Nr   )r   r  r   r   r  rE   rE   rF   r    s
    
z-S3RegionRedirectorv2.annotate_request_context)N)N)r   r   r   r   r   r  r  r  r  r  r  rE   rE   rE   rF   r    s   

l!r  c                   @   sL   e Zd ZdZdddZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dS )S3RegionRedirectorzThis handler has been replaced by S3RegionRedirectorv2. The original
    version remains in place for any third-party libraries that import it.
    Nc                 C   s:   || _ || _| jd kri | _t|| _tjdtd d S )NzThe S3RegionRedirector class has been deprecated for a new internal replacement. A future version of botocore may remove this class.category)_endpoint_resolverr  r  r  r  warningswarnFutureWarningr  rE   rE   rF   r     s    
zS3RegionRedirector.__init__c                 C   s<   |p| j jj}|d| j |d| j |d| j d S )Nr  zbefore-call.s3r  )r  r  r<   r  r  r  r  r  rE   rE   rF   r    s    zS3RegionRedirector.registerc                 K   s  |dkrdS |  |di r,td dS |di drLtd dS |d di }|d}|d d	i }|d
ko|jdk}|d
ko|jdkod|di k}	|dkod|k}
|d dk	o|d jdk}|dk}t||
||	|gsdS |d d d }|d d}| ||}|dkr<td||f  dS td|||f  | j	d|}|d }|||d}||d d< || j
|< | ||d  d|d d< dS )r  Nr   z=S3 request was previously to an accesspoint, not redirecting.Zs3_redirectedr  r3   r  r   r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  s3r  )r   r  r   T)_is_s3_accesspointrS   r   r   rf   r   r  r  r  resolver  r  )r   r  r   r  r   r   r  r  r  r  r   r  r  r  r  r  r   r  rE   rE   rF   r    s    

	

z&S3RegionRedirector.redirect_from_errorc              
   C   s   |d }|d d }d|kr$|d S | di  dd}|dk	rD|S z| jj|d}|d d }W n0 tk
r } z|jd d }W 5 d}~X Y nX | dd}|S r  r  r  rE   rE   rF   r  $  s     z$S3RegionRedirector.get_bucket_regionc                 K   s4   | di  dd }|d k	r0t|d |d|d< d S )Nr  r   rt   F)rS   r  r   r  r   r   r   rE   rE   rF   r  E  s    z"S3RegionRedirector.set_request_urlc                 K   sF   |  |rdS |d}| j|}|dk	r6||d< nd|i|d< dS )z
        This handler retrieves a given bucket's signing context from the cache
        and adds it into the request context.
        Nr  r  r  )r  rS   r  )r   r  r   r   r  r  rE   rE   rF   r  J  s    


z&S3RegionRedirector.redirect_from_cachec                 C   s   d|kS Nr   rE   )r   r   rE   rE   rF   r  X  s    z%S3RegionRedirector._is_s3_accesspoint)N)N)r   r   r   r   r   r  r  r  r  r  r  rE   rE   rE   rF   r    s   

_!r  c                   @   s   e Zd ZdS )InvalidArnExceptionN)r   r   r   rE   rE   rE   rF   r  \  s   r  c                   @   s    e Zd Zdd Zedd ZdS )r  c                 C   sH   | dd}t|dk r$td| |d |d |d |d |d d	S )
N:      zUProvided ARN: %s must be of the format: arn:partition:service:region:account:resourcer3   r  r     )	partitionservicer   accountresource)rp   r   r  )r   arnZ	arn_partsrE   rE   rF   	parse_arna  s    zArnParser.parse_arnc                 C   sJ   t | tr| dsdS t }z||  W dS  tk
rD   Y dS X d S )Nzarn:FT)r?   rA   
startswithr  r"  r  )r   
arn_parserrE   rE   rF   r  p  s    
zArnParser.is_arnN)r   r   r   r"  staticmethodr  rE   rE   rE   rF   r  `  s   r  c                   @   s`   e Zd ZedZedZdgZdddZdd Z	d	d
 Z
dd Zdd Zdd Zdd ZdS )S3ArnParamHandlerzA^(?P<resource_type>accesspoint|outpost)[/:](?P<resource_name>.+)$zc^(?P<outpost_name>[a-zA-Z0-9\-]{1,63})[/:]accesspoint[/:](?P<accesspoint_name>[a-zA-Z0-9\-]{1,63}$)r  Nc                 C   s   || _ |d krt | _ d S r   _arn_parserr  r   r$  rE   rE   rF   r     s    zS3ArnParamHandler.__init__c                 C   s   | d| j d S )Nr  r  
handle_arnr   r  rE   rE   rF   r    s    zS3ArnParamHandler.registerc                 K   s`   |j | jkrd S | |}|d kr&d S |d dkrB| ||| n|d dkr\| ||| d S )Nresource_typeaccesspointoutpost)rf   _BLACKLISTED_OPERATIONS"_get_arn_details_from_bucket_param_store_accesspoint_store_outpost)r   r  modelr   r   arn_detailsrE   rE   rF   r+    s    
zS3ArnParamHandler.handle_arnc                 C   sH   d|krDz&|d }| j |}| || |W S  tk
rB   Y nX d S )Nr  )r(  r"  _add_resource_type_and_namer  )r   r  r!  r5  rE   rE   rF   r1    s    z4S3ArnParamHandler._get_arn_details_from_bucket_paramc                 C   s@   | j |d }|r2|d|d< |d|d< n
t|dd S )Nr   r-  resource_name)r!  )_RESOURCE_REGEXr  groupr/   )r   r!  r5  r  rE   rE   rF   r6    s
    z-S3ArnParamHandler._add_resource_type_and_namec                 C   s8   |d |d< |d |d |d |d |d d|d< d S )	Nr7  r  r  r  r   r  )rf   r  r  r   r  r   rE   r   r  r   r5  rE   rE   rF   r2    s    z$S3ArnParamHandler._store_accesspointc                 C   sd   |d }| j |}|s"t|d|d}||d< |d||d |d |d |d	 d
|d< d S )Nr7  )r7  accesspoint_namer  outpost_namer  r  r   r  )r<  rf   r  r  r   r  r   )_OUTPOST_RESOURCE_REGEXr  r-   r9  )r   r  r   r5  r7  r  r;  rE   rE   rF   r3    s    

z S3ArnParamHandler._store_outpost)N)r   r   r   rd   r  r8  r=  r0  r   r  r+  r1  r6  r2  r3  rE   rE   rE   rF   r&  |  s   
r&  c                   @   s   e Zd ZdZdZd7ddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zed1d2 Zed3d4 Zed5d6 ZdS )8S3EndpointSetterawsr  NFc                 C   sF   || _ || _|| _|| _|d kr&i | _|| _|| _|d krB| j| _d S r   r  _region
_s3_config_use_fips_endpoint_endpoint_url
_partition_DEFAULT_PARTITIONr   endpoint_resolverr   	s3_configr  r  use_fips_endpointrE   rE   rF   r     s    
zS3EndpointSetter.__init__c                 C   s.   | d| j | d| j | d| j d S )Nzbefore-sign.s3zchoose-signer.s3z%before-call.s3.WriteGetObjectResponse)r  set_endpoint
set_signer#update_endpoint_to_s3_object_lambdar,  rE   rE   rF   r    s    zS3EndpointSetter.registerc                 K   sh   | j rtdd| |d | jr&d S | j}|d| j}dj|d |d d}t|d |d	|d< d S )
NzOS3 client does not support accelerate endpoints for S3 Object Lambda operationsmsgs3-object-lambdazhttps://{host_prefix}{hostname}host_prefixr  )rQ  r  rt   F)	_use_accelerate_endpointr0   _override_signing_namerD  r  r  rA  formatr  )r   r  r   r   resolverresolvedr  rE   rE   rF   rM    s"     z4S3EndpointSetter.update_endpoint_to_s3_object_lambdac                 K   s   |  |rL| | | | | | | |}| | | || d S | jrz| jrht	d| j
 dtf d|i| | jr| jf d|i| d S )Nz{Client is configured to use the FIPS psuedo region for "%s", but S3 Accelerate does not have any FIPS compatible endpoints.rN  r   )_use_accesspoint_endpoint_validate_accesspoint_supported_validate_fips_supported_validate_global_regions(_resolve_region_for_accesspoint_endpoint._resolve_signing_name_for_accesspoint_endpoint_switch_to_accesspoint_endpointrR  rC  r0   rA  r  _s3_addressing_handler)r   r   r   r  rE   rE   rF   rK    s(    




zS3EndpointSetter.set_endpointc                 C   s
   d|j kS r  r   r   rE   rE   rF   rW  %  s    z*S3EndpointSetter._use_accesspoint_endpointc                 C   s   | j s
d S d|jd d kr(tdhdd|jd krFtd| j d|jd d }|| jkr| jdd	std
| j|f dd S )Nfipsr   r   ,Invalid ARN, FIPS region not allowed in ARN.rN  r<  zhClient is configured to use the FIPS psuedo-region "%s", but outpost ARNs do not support FIPS endpoints.use_arn_regionTzClient is configured to use the FIPS psuedo-region for "%s", but the access-point ARN provided is for the "%s" region. For clients using a FIPS psuedo-region calls to access-point ARNs in another region are not allowed.)rC  r   r.   rA  rB  rS   r   r   Zaccesspoint_regionrE   rE   rF   rY  (  s(    
z)S3EndpointSetter._validate_fips_supportedc                 C   s0   | j ddrd S | jdkr,td| j dd S )Nrb  T)z
aws-globalzs3-external-1zClient is configured to use the global psuedo-region "%s". When providing access-point ARNs a regional endpoint must be specified.rN  )rB  rS   rA  r.   r   rE   rE   rF   rZ  H  s    
z)S3EndpointSetter._validate_global_regionsc                 C   s   | j rtdd|jd d }|| jkr<td| j|f d|jd d}|dkrj| jdrjtd	d|jd d
}|r| jdrtdd| | d S )NzZClient does not support s3 accelerate configuration when an access-point ARN is specified.rN  r   r  zClient is configured for "%s" partition, but access-point ARN provided is for "%s" partition. The client and  access-point partition must be the same.r  rP  use_dualstack_endpointzjClient does not support s3 dualstack configuration when an S3 Object Lambda access point ARN is specified.r<  zTClient does not support s3 dualstack configuration when an outpost ARN is specified.)rR  r.   r   rE  rS   rB  _validate_mrap_s3_config)r   r   Zrequest_partitionZ
s3_servicer<  rE   rE   rF   rX  T  s0    
z0S3EndpointSetter._validate_accesspoint_supportedc                 C   s@   t |jsd S | jdr&tddn| jdr<tddd S )NZ$s3_disable_multiregion_access_pointszCInvalid configuration, Multi-Region Access Point ARNs are disabled.rN  rd  zeClient does not support s3 dualstack configuration when a Multi-Region Access Point ARN is specified.)r   r   rB  rS   r.   r   rE   rE   rF   re  z  s    
z)S3EndpointSetter._validate_mrap_s3_configc                 C   sJ   t |jr| |d n,| jddrD|jd d }| || |S | jS )Nr|   rb  Tr   r   )r   r   _override_signing_regionrB  rS   rA  rc  rE   rE   rF   r[    s    
z9S3EndpointSetter._resolve_region_for_accesspoint_endpointc                 K   s   t |rtrdS tddd S )NZs3v4azzUsing S3 with an MRAP arn requires an additional dependency. You will need to pip install botocore[crt] before proceeding.rN  )r   r   r*   )r   r   r   rE   rE   rF   rL    s    zS3EndpointSetter.set_signerc                 C   s    |j d d }| |j | d S )Nr   r  r   rS  )r   r   Zaccesspoint_servicerE   rE   rF   r\    s    z?S3EndpointSetter._resolve_signing_name_for_accesspoint_endpointc                 C   sX   t |j}t|j| |j|| |j|j|jdf}t	
d|j d|  ||_d S r  )r   rt   r   r  _get_netlocr   _get_accesspoint_pathrk   r  r   r   )r   r   r  original_componentsZaccesspoint_endpointrE   rE   rF   r]    s     
 z0S3EndpointSetter._switch_to_accesspoint_endpointc                 C   s"   t |r| |S | ||S d S r   )r   _get_mrap_netloc_get_accesspoint_netloc)r   r  r  rE   rE   rF   rh    s    
zS3EndpointSetter._get_netlocc                 C   s\   |d }d}|d g}| j r4t| j j}|| n|d }|d|| |g d|S )Nr   z	s3-globalrf   r  r.  rn   )rD  r   r  rr   r  _get_partition_dns_suffixrs   )r   r  r   r  Zmrap_netloc_componentsendpoint_url_netlocr  rE   rE   rF   rk    s    
z!S3EndpointSetter._get_mrap_netlocc           	      C   s   |d }d |d |d g}|d}| jrT|r<|| t| jj}|| n||rl|dg}|| n:|d dkr| d|}|| n| d	|}|| | jd
r|d ||| 	|g d
|S )Nr   z{}-{}rf   r  r<  s3-outpostsr  rP  zs3-accesspointrd  r6   rn   )rT  rS   rD  rr   r   r  r  _inject_fips_if_neededrB  _get_dns_suffixrs   )	r   r  r  r   Zaccesspoint_netloc_componentsr<  rn  Zoutpost_host	componentrE   rE   rF   rl    s:    

  

z(S3EndpointSetter._get_accesspoint_netlocc                 C   s   | j rd| S |S )Nz%s-fipsrC  )r   rr  r  rE   rE   rF   rp    s    z'S3EndpointSetter._inject_fips_if_neededc                 C   s"   |d d }| d| ddp dS )Nr   rf   rh   ra   r3   )rc   )r   Zoriginal_pathr  rf   rE   rE   rF   ri    s    z&S3EndpointSetter._get_accesspoint_pathc                 C   s   | j |}|d kr| j}|S r   )r  get_partition_dns_suffix_DEFAULT_DNS_SUFFIX)r   Zpartition_name
dns_suffixrE   rE   rF   rm   	  s    z*S3EndpointSetter._get_partition_dns_suffixc                 C   s,   | j d|}| j}|r(d|kr(|d }|S Nr  Z	dnsSuffixr  r  ru  r   r  rV  rv  rE   rE   rF   rq  	  s     z S3EndpointSetter._get_dns_suffixc                 C   s$   |j di }||d< ||j d< d S Nr  r   r   rS   r   r   r  r  rE   rE   rF   rf  	  s    z)S3EndpointSetter._override_signing_regionc                 C   s    | di }||d< ||d< d S Nr  signing_namer   )r   r   r~  r  rE   rE   rF   rS  	  s    z'S3EndpointSetter._override_signing_namec                 C   s   | j drdS | jd krdS t| jj}|ds8dS |d}|d dkrRdS |dd	 }t|tt|krvdS t	d
d |D S )Nuse_accelerate_endpointTFr  rn   r   s3-accelerater3   c                 s   s   | ]}|t kV  qd S r   r  r  rE   rE   rF   	<genexpr>D	  s     z<S3EndpointSetter._use_accelerate_endpoint.<locals>.<genexpr>)
rB  rS   rD  r   r  r   rp   r   setall)r   r  r  feature_partsrE   rE   rF   rR  #	  s    


z)S3EndpointSetter._use_accelerate_endpointc                 C   s"   | j r
dS | jd}|r|S d S )NvirtualZaddressing_style)rR  rB  rS   )r   Zconfigured_addressing_stylerE   rE   rF   _addressing_styleF	  s
    z"S3EndpointSetter._addressing_stylec                 C   sH   | j dkrtd tS | j dks,| jd k	r:td d S td tS )Nr  z'Using S3 virtual host style addressing.rk   zUsing S3 path style addressing.zSDefaulting to S3 virtual host style addressing with path style addressing fallback.)r  r   r   r  rD  r  r   rE   rE   rF   r^  R	  s    


z'S3EndpointSetter._s3_addressing_handler)NNNNF)r   r   r   rF  ru  r   r  rM  rK  rW  rY  rZ  rX  re  r[  rL  r\  r]  rh  rk  rl  rp  ri  rm  rq  rf  rS  rj  rR  r  r^  rE   rE   rE   rF   r>    sF        
 & 				
"
r>  c                   @   s   e Zd ZdZdZedZd6ddZdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdS )7S3ControlEndpointSetterr?  r  z^[a-zA-Z0-9\-]{1,63}$NFc                 C   sF   || _ || _|| _|| _|d kr&i | _|| _|| _|d krB| j| _d S r   r@  rG  rE   rE   rF   r   p	  s    	z S3ControlEndpointSetter.__init__c                 C   s   | d| j d S )Nzbefore-sign.s3-control)r  rK  r,  rE   rE   rF   r  	  s    z S3ControlEndpointSetter.registerc                 K   s|   |  |r@| | | |}| | | || | | n8| |rx| | | |d | 	| j
}| || d S Nro  )_use_endpoint_from_arn_details-_validate_endpoint_from_arn_details_supported _resolve_region_from_arn_details&_resolve_signing_name_from_arn_details"_resolve_endpoint_from_arn_details_add_headers_from_arn_details_use_endpoint_from_outpost_id#_validate_outpost_redirection_validrS  _construct_outpost_endpointrA  _update_request_netloc)r   r   r   r  
new_netlocrE   rE   rF   rK  	  s    





z$S3ControlEndpointSetter.set_endpointc                 C   s
   d|j kS )Nr5  r_  r   rE   rE   rF   r  	  s    z6S3ControlEndpointSetter._use_endpoint_from_arn_detailsc                 C   s
   d|j kS )N
outpost_idr_  r   rE   rE   rF   r  	  s    z5S3ControlEndpointSetter._use_endpoint_from_outpost_idc                 C   s   d|j d d kr(t|j d d dd| jddsf|j d d }|| jkrfd	|| jf }t|d
|j d d }|| jkrtd| j|f d
| jdrtdd
d|j d kr| | d S )Nr`  r5  r   r  ra  r!  rO  rb  FzpThe use_arn_region configuration is disabled but received arn for "%s" when the client is configured to use "%s"rN  r  zClient is configured for "%s" partition, but arn provided is for "%s" partition. The client and arn partition must be the same.r  7S3 control client does not support accelerate endpointsr<  )r   r1   rB  rS   rA  r2   rE  r  )r   r   
arn_region	error_msgZrequest_partionrE   rE   rF   r  	  s2    


zES3ControlEndpointSetter._validate_endpoint_from_arn_details_supportedc                 C   s   | j drtddd S )Nrd  zPClient does not support s3 dualstack configuration when an outpost is specified.rN  )rB  rS   r2   r   rE   rE   rF   r  	  s    z;S3ControlEndpointSetter._validate_outpost_redirection_validc                 C   s2   | j ddr,|jd d }| || |S | jS )Nrb  Fr5  r   )rB  rS   r   rf  rA  )r   r   r  rE   rE   rF   r  	  s
    z8S3ControlEndpointSetter._resolve_region_from_arn_detailsc                 C   s   |j d d }| || |S )Nr5  r  rg  )r   r   Zarn_servicerE   rE   rF   r  	  s    z>S3ControlEndpointSetter._resolve_signing_name_from_arn_detailsc                 C   s   |  ||}| || d S r   ) _resolve_netloc_from_arn_detailsr  )r   r   r  r  rE   rE   rF   r  	  s
     z:S3ControlEndpointSetter._resolve_endpoint_from_arn_detailsc                 C   sD   t |j}t|j||j|jdf}td|j d|  ||_d S r  )r   rt   r   r  rk   r  r   r   )r   r   r  rj  Zarn_details_endpointrE   rE   rF   r  	  s    
	z.S3ControlEndpointSetter._update_request_netlocc                 C   s0   |j d }d|kr| |S |d }| ||S )Nr5  r<  r  )r   r  _construct_s3_control_endpoint)r   r   r  r5  r  rE   rE   rF   r  	  s
    

z8S3ControlEndpointSetter._resolve_netloc_from_arn_detailsc                 C   s   | j |S r   )_HOST_LABEL_REGEXr  )r   labelrE   rE   rF   _is_valid_host_label	  s    z,S3ControlEndpointSetter._is_valid_host_labelc                 G   s"   |D ]}|  |st|dqd S )N)r  )r  r%   )r   labelsr  rE   rE   rF   _validate_host_labels	  s    
z-S3ControlEndpointSetter._validate_host_labelsc                 C   s\   |  || | jr(t| jj}||g}n*|dg}| | | |}|||g | |S )Nz
s3-control)r  rD  r   r  _add_dualstackrq  r  _construct_netloc)r   r  r  rn  r  rv  rE   rE   rF   r  	  s    


z6S3ControlEndpointSetter._construct_s3_control_endpointc                 C   s@   |  | | jrt| jjS d|| |g}| | | |S r  )r  rD  r   r  rq  	_add_fipsr  )r   r  r  rE   rE   rF   r  
  s    

z3S3ControlEndpointSetter._construct_outpost_endpointc                 C   s
   d |S )Nrn   )rs   r   r  rE   rE   rF   r  
  s    z)S3ControlEndpointSetter._construct_netlocc                 C   s   | j r|d d |d< d S )Nr   z-fipsrs  r  rE   rE   rF   r  
  s    z!S3ControlEndpointSetter._add_fipsc                 C   s   | j dr|d d S )Nrd  r6   )rB  rS   rr   r  rE   rE   rF   r  
  s    z&S3ControlEndpointSetter._add_dualstackc                 C   s,   | j d|}| j}|r(d|kr(|d }|S rw  rx  ry  rE   rE   rF   rq  
  s     z'S3ControlEndpointSetter._get_dns_suffixc                 C   s$   |j di }||d< ||j d< d S rz  r{  r|  rE   rE   rF   rf  &
  s    z0S3ControlEndpointSetter._override_signing_regionc                 C   s$   |j di }||d< ||j d< d S r}  r{  )r   r   r~  r  rE   rE   rF   rS  /
  s    z.S3ControlEndpointSetter._override_signing_namec                 C   s(   |j d }|d}|r$| || d S )Nr5  r<  )r   rS   _add_outpost_id_header)r   r   r5  r<  rE   rE   rF   r  8
  s    

z5S3ControlEndpointSetter._add_headers_from_arn_detailsc                 C   s   ||j d< d S )Nzx-amz-outpost-id)r_   )r   r   r<  rE   rE   rF   r  >
  s    z.S3ControlEndpointSetter._add_outpost_id_header)NNNNF) r   r   r   rF  ru  rd   r  r  r   r  rK  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rq  rf  rS  r  r  rE   rE   rE   rF   r  k	  s@   
     
 					r  c                   @   s   e Zd ZdZedZdddZdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )S3ControlArnParamHandlerzThis handler has been replaced by S3ControlArnParamHandlerv2. The
    original version remains in place for any third-party importers.
    z[/:]Nc                 C   s(   || _ |d krt | _ tjdtd d S )NzThe S3ControlArnParamHandler class has been deprecated for a new internal replacement. A future version of botocore may remove this class.r  )r(  r  r  r  r  r)  rE   rE   rF   r   I
  s    z!S3ControlArnParamHandler.__init__c                 C   s   | d| j d S )Nz!before-parameter-build.s3-controlr*  r,  rE   rE   rF   r  T
  s    z!S3ControlArnParamHandler.registerc                 K   s:   |j dkr| ||| n| ||| | ||| d S )N)r  ZListRegionalBuckets)rf   _handle_outpost_id_param_handle_name_param_handle_bucket_param)r   r  r4  r   r   rE   rE   rF   r+  Z
  s    
z#S3ControlArnParamHandler.handle_arnc                 C   sX   ||krd S z0|| }| j |}||d< | ||d< |W S  tk
rR   Y d S X d S )Nr  	resources)r(  r"  _split_resourcer  )r   r  r  r!  r5  rE   rE   rF   _get_arn_details_from_paramd
  s    z4S3ControlArnParamHandler._get_arn_details_from_paramc                 C   s   | j |d S )Nr   )_RESOURCE_SPLIT_REGEXrp   )r   r5  rE   rE   rF   r  p
  s    z(S3ControlArnParamHandler._split_resourcec                 C   sD   |d }d|kr8|d |kr8d|d  }t |d |d||d< d S )Nr  Z	AccountIdzGAccount ID in arn does not match the AccountId parameter provided: "%s"r  r  )r1   )r   r  r5  Z
account_idr  rE   rE   rF   _override_account_id_params
  s    z3S3ControlArnParamHandler._override_account_id_paramc                 C   s   d|krd S |d |d< d S )NZ	OutpostIdr  rE   )r   r  r4  r   rE   rE   rF   r  
  s    z1S3ControlArnParamHandler._handle_outpost_id_paramc                 C   sX   |j dkrd S | |d}|d kr&d S | |r@| ||| nd}t|d |dd S NZCreateAccessPointNamez4The Name parameter does not support the provided ARNr  r  )rf   r  _is_outpost_accesspoint_store_outpost_accesspointr1   r   r  r4  r   r5  r  rE   rE   rF   r  
  s    

z+S3ControlArnParamHandler._handle_name_paramc                 C   s@   |d dkrdS |d }t |dkr(dS |d dko>|d d	kS )
Nr  ro  Fr  r  r   r/  r  r.  r   r   r5  r  rE   rE   rF   r  
  s    z0S3ControlArnParamHandler._is_outpost_accesspointc                 C   sD   |  || |d d }||d< ||d< |d d |d< ||d< d S )Nr  r  r  r;  r3   r<  r5  r  )r   r  r   r5  r;  rE   rE   rF   r  
  s    z3S3ControlArnParamHandler._store_outpost_accesspointc                 C   sJ   |  |d}|d krd S | |r2| ||| nd}t|d |dd S Nr  z6The Bucket parameter does not support the provided ARNr  r  )r  _is_outpost_bucket_store_outpost_bucketr1   r  rE   rE   rF   r  
  s    
z-S3ControlArnParamHandler._handle_bucket_paramc                 C   s@   |d dkrdS |d }t |dkr(dS |d dko>|d d	kS )
Nr  ro  Fr  r  r   r/  r  r  r  r  rE   rE   rF   r  
  s    z+S3ControlArnParamHandler._is_outpost_bucketc                 C   sD   |  || |d d }||d< ||d< |d d |d< ||d< d S )Nr  r  r  r  r3   r<  r5  r  )r   r  r   r5  r  rE   rE   rF   r  
  s    z.S3ControlArnParamHandler._store_outpost_bucket)N)r   r   r   r   rd   r  r  r   r  r+  r  r  r  r  r  r  r  r  r  r  rE   rE   rE   rF   r  B
  s   


		r  c                   @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )S3ControlArnParamHandlerv2a  Updated version of S3ControlArnParamHandler for use when
    EndpointRulesetResolver is in use for endpoint resolution.

    This class is considered private and subject to abrupt breaking changes or
    removal without prior announcement. Please do not use it directly.
    Nc                 C   s   || _ |d krt | _ d S r   r'  r)  rE   rE   rF   r   
  s    z#S3ControlArnParamHandlerv2.__init__c                 C   s   | d| j d S )Nz%before-endpoint-resolution.s3-controlr*  r,  rE   rE   rF   r  
  s    z#S3ControlArnParamHandlerv2.registerc                 C   sl   |j dkrd S | |d}|d kr&d S | | | | | |rT| ||| nd}t|d |dd S r  )rf   r  _raise_for_fips_pseudo_region_raise_for_accelerate_endpointr  r  r1   r  rE   rE   rF   r  
  s    



z-S3ControlArnParamHandlerv2._handle_name_paramc                 C   s   |  || d S r   r  r:  rE   rE   rF   r  
  s    z5S3ControlArnParamHandlerv2._store_outpost_accesspointc                 C   s^   |  |d}|d krd S | | | | | |rF| ||| nd}t|d |dd S r  )r  r  r  r  r  r1   r  rE   rE   rF   r  
  s    


z/S3ControlArnParamHandlerv2._handle_bucket_paramc                 C   s   |  || d S r   r  r:  rE   rE   rF   r     s    z0S3ControlArnParamHandlerv2._store_outpost_bucketc                 C   s0   |d }| ds|dr,t|d ddd S )Nr   zfips-r  ra  r  )r#  r   r1   )r   r5  r  rE   rE   rF   r    s    z8S3ControlArnParamHandlerv2._raise_for_fips_pseudo_regionc                 C   s&   |d j pi }|dr"tddd S )Nclient_configr  r  rN  )r  rS   r2   )r   r   rI  rE   rE   rF   r    s
    
z9S3ControlArnParamHandlerv2._raise_for_accelerate_endpoint)N)r   r   r   r   r   r  r  r  r  r  r  r  rE   rE   rE   rF   r  
  s   
	r  c                   @   sr   e Zd ZdZdZdZdZeddgZdej	fdd	Z
dd
dZdd Zdd Zdd ZdddZdd Zdd ZdS )ContainerMetadataFetcherr  r  r3   z169.254.170.2	localhostz	127.0.0.1Nc                 C   s(   |d krt jj| jd}|| _|| _d S )N)r   )r[   r   r   TIMEOUT_SECONDSr   _sleep)r   rL   sleeprE   rE   rF   r     s    z!ContainerMetadataFetcher.__init__c                 C   s   |  | | ||S )zRetrieve JSON metadata from container metadata.

        :type full_url: str
        :param full_url: The full URL of the metadata service.
            This should include the scheme as well, e.g
            "http://localhost:123/foo"

        )_validate_allowed_url_retrieve_credentials)r   full_urlr_   rE   rE   rF   retrieve_full_uri$  s    	
z*ContainerMetadataFetcher.retrieve_full_uric                 C   s:   t j|}| |j}|s6td|jd| jf d S )NzGUnsupported host '%s'.  Can only retrieve metadata from these hosts: %sz, )r[   compatr   _check_if_whitelisted_hostr  r   rs   _ALLOWED_HOSTS)r   r  parsedZis_whitelisted_hostrE   rE   rF   r  0  s    z.ContainerMetadataFetcher._validate_allowed_urlc                 C   s   || j krdS dS )NTF)r  )r   r  rE   rE   rF   r  :  s    
z3ContainerMetadataFetcher._check_if_whitelisted_hostc                 C   s   |  |}| |S )zRetrieve JSON metadata from ECS metadata.

        :type relative_uri: str
        :param relative_uri: A relative URI, e.g "/foo/bar?id=123"

        :return: The parsed JSON response.

        )r  r  )r   relative_urir  rE   rE   rF   retrieve_uri?  s    	
z%ContainerMetadataFetcher.retrieve_uric              
   C   s   ddi}|d k	r| | d}z| ||| jW S  tk
r } z4tjd|dd | | j |d7 }|| jkrr W 5 d }~X Y qX qd S )NAcceptzapplication/jsonr   zAReceived error when attempting to retrieve container metadata: %sTr   r3   )	rZ  _get_responser  r)   r   r   r  
SLEEP_TIMERETRY_ATTEMPTS)r   r  extra_headersr_   attemptsr   rE   rE   rF   r  K  s(    
  
z.ContainerMetadataFetcher._retrieve_credentialsc           
   
   C   s   zt jj}|d||d}| j| }|jd}|jdkrRt	d|j|f dzt
|W W S  tk
r   d}td|| t	|dY nX W n4 tk
r }	 zd	|	 }t	|dW 5 d }	~	X Y nX d S )
Nr   r   r2  r   z4Received non 200 response (%s) from ECS metadata: %sr  z8Unable to parse JSON returned from ECS metadata servicesz%s:%sz;Received error when attempting to retrieve ECS metadata: %s)r[   r\   r   r   r   r   r   re  r   r)   r   r   r   r   r   r   )
r   r  r_   r   r   r   r   Zresponse_textr  r   rE   rE   rF   r  a  s0    
z&ContainerMetadataFetcher._get_responsec                 C   s   d| j  | S )Nzhttp://)
IP_ADDRESS)r   r  rE   rE   rF   r  }  s    z!ContainerMetadataFetcher.full_url)N)N)r   r   r   r  r  r  r  r  timer  r   r  r  r  r  r  r  r  rE   rE   rE   rF   r    s   



r  c                 C   s   t | ri S t S d S r   )should_bypass_proxiesr   rt   rE   rE   rF   r     s    r   c              	   C   s8   zt t| jrW dS W n ttjfk
r2   Y nX dS )z:
    Returns whether we should bypass proxies or not.
    TF)r   r   r  r:  socketgaierrorr  rE   rE   rF   r    s    
r  c              	   C   s   | sdS z
t | W S  ttfk
r*   Y nX t| drt| drz0|  }| dd |  }| | || W S  tjk
r   Y nX d S )Nr   seektellr  )r   AttributeErrorr:  rR   r  r  ioUnsupportedOperation)rT  Zorig_posZend_file_posrE   rE   rF   determine_content_length  s     


r  
ISO-8859-1c                 C   sJ   |  d}|sdS tj }||d< |d}|dk	r:|S d|krF|S dS )zReturns encodings from given HTTP Header Dict.

    :param headers: dictionary to extract encoding from.
    :param default: default encoding if the content-type is text
    zcontent-typeNcharsetr   )rS   r:   messager   	get_param)r_   defaultcontent_typer  r  rE   rE   rF   get_encoding_from_headers  s    


r  c                 K   s0   t | ttfrt| }nt| }t|dS )Nra  )r?   r3  	bytearray_calculate_md5_from_bytes_calculate_md5_from_filebase64	b64encodere  )rT  r   Z
binary_md5rE   rE   rF   calculate_md5  s    
r  c                 C   s   t | }| S r   )r   r\  )Z
body_bytesmd5rE   rE   rF   r    s    r  c                    sB      }t }t fdddD ]}|| q  | | S )Nc                      s
     dS rP  rR  rE   fileobjrE   rF   rU    rV  z*_calculate_md5_from_file.<locals>.<lambda>rV  )r  r   rY  rZ  r  r\  )r  Zstart_positionr  r^  rE   r  rF   r    s    
r  c                 K   s   | d }| d }|  di  di }| d}|r>|dkr>dS |D ]}t|rB dS qBtr|dk	rd|krt|f|}|| d d< dS )	z1Only add a Content-MD5 if the system supports it.r_   rT  r   r]  Zrequest_algorithmzconditional-md5NzContent-MD5)rS   CHECKSUM_HEADER_PATTERNr  r   r  )r  r   r_   rT  Zchecksum_contextZchecksum_algorithmrP   Z
md5_digestrE   rE   rF   conditionally_calculate_md5  s    

r  c                   @   s    e Zd ZefddZdd ZdS )FileWebIdentityTokenLoaderc                 C   s   || _ || _d S r   )_web_identity_token_pathr!  )r   Zweb_identity_token_pathr!  rE   rE   rF   r     s    z#FileWebIdentityTokenLoader.__init__c              
   C   s,   |  | j}| W  5 Q R  S Q R X d S r   )r!  r  r  )r   Z
token_filerE   rE   rF   __call__  s    z#FileWebIdentityTokenLoader.__call__N)r   r   r   openr   r  rE   rE   rE   rF   r    s   r  c                   @   s2   e Zd Zd
ddZdd ZdddZddd	ZdS )SSOTokenLoaderNc                 C   s   |d kri }|| _ d S r   )r  )r   r  rE   rE   rF   r     s    zSSOTokenLoader.__init__c                 C   s$   |}|d k	r|}t |d S )Nr2  )rW  sha1r4  r[  )r   	start_urlsession_namer5  rE   rE   rF   _generate_cache_key
  s    z"SSOTokenLoader._generate_cache_keyc                 C   s   |  ||}|| j|< d S r   )r  r  )r   r  r   r  r  rE   rE   rF   
save_token  s    zSSOTokenLoader.save_tokenc                 C   s   |  ||}td|  || jkrL|}|d k	r6|}d| d}t|d| j| }d|ksfd|kr|d| d}t|d|S )NzChecking for cached token at: z
Token for z does not existr  ZaccessTokenZ	expiresAtz is invalid)r  r   r   r  r,   )r   r  r  r  rf   r  r   rE   rE   rF   r    s    



zSSOTokenLoader.__call__)N)N)N)r   r   r   r   r  r  r  rE   rE   rE   rF   r    s   

r  c                   @   s@   e Zd ZdZdZdddZdd Zdd	 Zd
d ZdddZ	dS )EventbridgeSignerSetterr?  r  Nc                 C   s   || _ || _|| _d S r   )r  rA  rD  )r   rH  r   r  rE   rE   rF   r   )  s    z EventbridgeSignerSetter.__init__c                 C   s    | d| j | d| j d S )Nz'before-parameter-build.events.PutEventszbefore-call.events.PutEvents)r  check_for_global_endpointset_endpoint_urlr,  rE   rE   rF   r  .  s     z EventbridgeSignerSetter.registerc                 K   s6   d|kr2|d }t d|d  d|  ||d< d S )Neventbridge_endpointzRewriting URL from rt   r  )r   r   r  rE   rE   rF   r  7  s    z(EventbridgeSignerSetter.set_endpoint_urlc           	      K   s   | d}|d krd S t|dkr,tddts:tdd| d}d }|d k	rl|jr`tdd|jrldg}| jd krtd	| }|j	|krtd
d| j
||d}n| j}||d< d|d< d S )NZ
EndpointIdr   z+EndpointId must not be a zero length stringrN  zqUsing EndpointId requires an additional dependency. You will need to pip install botocore[crt] before proceeding.r  z>FIPS is not supported with EventBridge multi-region endpoints.r6   https://z-EndpointId is not a valid hostname component.endpoint_variant_tagsr  Zv4ar  )rS   r   r#   r   r*   rJ  rd  rD  r   r  _get_global_endpoint)	r   r  r   r   r   r8   r  r  Zresolved_endpointrE   rE   rF   r  =  sB    



 z1EventbridgeSignerSetter.check_for_global_endpointc                 C   sN   | j }|| j}|d kr | j}|j||d}|d kr<| j}d| d| dS )Nr  r   z.endpoint.events.rh   )r  Zget_partition_for_regionrA  rF  rt  ru  )r   r   r  rU  r  rv  rE   rE   rF   r  i  s     z,EventbridgeSignerSetter._get_global_endpoint)NN)N)
r   r   r   rF  ru  r   r  r  r  r  rE   rE   rE   rF   r  %  s   
	,r  c                 C   s   | dkrdS t | }|jdr*|jdkr.dS |jd}|d dkrJdS |dd	 }t|tt|krndS td
d |D S )zDoes the URL match the S3 Accelerate endpoint scheme?

    Virtual host naming style with bucket names in the netloc part of the URL
    are not allowed by this function.
    NFr  )httpshttprn   r   r  r3   r  c                 s   s   | ]}|t kV  qd S r   r  r  rE   rE   rF   r    s     z'is_s3_accelerate_url.<locals>.<genexpr>)r   r  r   r  rp   r   r  r  )rt   Z	url_partsr  r  rE   rE   rF   is_s3_accelerate_urlx  s     r  c                   @   sr   e Zd ZdZejejddddZedfddZ	d	d
 Z
dd Zdd Zdd Zdd Zdd ZdddZdS )JSONFileCachezJSON file cache.
    This provides a dict like interface that stores JSON serializable
    objects.
    The objects are serialized to JSON and stored in a file.  These
    values can be retrieved at a later time.
    ~z.awsZbotor  Nc                 C   s   || _ |d kr| j}|| _d S r   )_working_dir_default_dumps_dumps)r   Zworking_dirZ
dumps_funcrE   rE   rF   r     s    zJSONFileCache.__init__c                 C   s   t j|| jdS )N)r  )r   dumps_serialize_if_needed)r   rn  rE   rE   rF   r
    s    zJSONFileCache._default_dumpsc                 C   s   |  |}tj|S r   )_convert_cache_keyr   rk   isfile)r   r  
actual_keyrE   rE   rF   __contains__  s    
zJSONFileCache.__contains__c              
   C   s\   |  |}z,t|}t|W  5 Q R  W S Q R X W n  ttfk
rV   t|Y nX dS )z Retrieve value from a cache key.N)r  r  r   loadr  r   KeyError)r   r  r  r"  rE   rE   rF   __getitem__  s    

"zJSONFileCache.__getitem__c                 C   s@   |  |}zt|}|  W n tk
r:   t|Y nX d S r   )r  r   unlinkFileNotFoundErrorr  )r   r  r  Zkey_pathrE   rE   rF   __delitem__  s    
zJSONFileCache.__delitem__c              	   C   s   |  |}z| |}W n& ttfk
r>   td| Y nX tj| jsZt| j t	t
|tjtjB dd}|  || W 5 Q R X d S )Nz3Value cannot be cached, must be JSON serializable: i  w)r  r  r:  r   r   rk   isdirr	  makedirsfdopenr  O_WRONLYO_CREATtruncatewrite)r   r  r   Zfull_keyZfile_contentr"  rE   rE   rF   __setitem__  s     

 zJSONFileCache.__setitem__c                 C   s   t j| j|d }|S )Nz.json)r   rk   rs   r	  )r   r  	full_pathrE   rE   rF   r    s    z JSONFileCache._convert_cache_keyFc                 C   s&   t |tjr"|r| S |dS |S )Nz%Y-%m-%dT%H:%M:%S%Z)r?   r  	isoformatr  )r   r   ZisorE   rE   rF   r    s
    
z"JSONFileCache._serialize_if_needed)F)r   r   r   r   r   rk   
expanduserrs   Z	CACHE_DIRr   r
  r  r  r  r   r  r  rE   rE   rE   rF   r    s   	r  )T)F)N)F)N)N)T)T)r  )r  rc  r  email.messager:   r  rW  r  loggingr   r  rd   r  r  r  r  pathlibr   urllib.requestr   r   Zdateutil.parserr;  Zdateutil.tzr   Zurllib3.exceptionsr   r[   Zbotocore.awsrequestZbotocore.httpsessionZbotocore.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zbotocore.exceptionsr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   	getLoggerr   r   r   r   r   rK   Z
SAFE_CHARSr  r  r   r  ZEVENT_ALIASESr  r  rG   rM   rV   r`   rg   rl   rm   ri   r~   r   r   	Exceptionr   r   r   r   r  r  r  r  r  r$  r  r1  r,  r@  rB  rE  rO  r_  rh  rb  rj  rq  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r&  r>  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  rE   rE   rE   rF   <module>   s  @d

N
	
 ; ?&
	&-

!d
 
 
@!

 E 4U    X Mm
	
!S!