o
    "`^h                 	   @   sd	  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 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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN eOePZQdZRdZSdZTdZUdZVeWdZXeGe<e:e;fZYdgZZi ddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9i d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdOdUdVdWdXdYdZi d[d\d]d^d_d^d`d5dad7dbd?dcdddedfdgdhdidjdkdldmdndodpdqdZdrdsdtdudvdui dwdxdydjdzd{d|d}d~ddddddddddddlddddddddddddddddIddZ[ejWdej\dZ]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd ZfdVddZgdd ZhG dd deiZjG dd deiZkG dd dZlG dd delZmG dd dZnG dd delZodWddZpdd ZqerfddZsddĄ ZteVfddƄZueVfddȄZvddʄ Zwdd̄ Zxdd΄ ZyddЄ ZzdXdd҄Z{dWddԄZ|ddք Z}dd؄ Z~G ddڄ dڃZG dd܄ d܃Zddބ Zdd Zdd Zdd Zdd Zdd Z	dXddZ	dXddZdd Zdd Zdd Zdd Zdd ZdVddZdVddZdd Zdd ZG dd  d ZG dd deZG dd dZG dd dZG dd dZG d	d
 d
eZG dd dZG dd dZG dd dZG dd dZG dd dZG dd deZG dd dZdd Zdd Zdd ZdYd d!Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZG d2d3 d3ZG d4d5 d5ZG d6d7 d7Zd8d9 ZG d:d; d;Zd<d= ZddiZi ddd d!d>d?d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d:d;d@dAd<d=dBdCdDdEdFdGi d@dAdHdIdJdKdRdSdTdOdUdVd[d\d]d^dBdCd`d5dad7dbd?dcdddDdEdFdGdHdIdedfi dJdKdgdhdidjdLdMdkdldNdOdmdndodpdrdsdqdZdvdudwdxdPdQdddddddRdSdddddTdddUZdS (Z      N)datetime)
ip_address)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api.mediatailormediatailorzapi.pricingpricingzapi.sagemaker	sagemaker
apigatewayzapi-gatewayzapplication-autoscalingzapplication-auto-scaling
appstream2	appstreamautoscalingzauto-scalingzautoscaling-planszauto-scaling-planscezcost-explorer
cloudhsmv2zcloudhsm-v2cloudsearchdomainzcloudsearch-domainzcognito-idpzcognito-identity-providerconfigzconfig-servicecurzcost-and-usage-report-servicezdata.iotziot-data-planezdata.jobs.iotziot-jobs-data-planezdata.mediastorezmediastore-datadatapipelinezdata-pipeline
devicefarmzdevice-farmzdevices.iot1clickziot-1click-devices-servicedirectconnectzdirect-connect	discoveryzapplication-discovery-servicedmszdatabase-migration-servicedszdirectory-servicedynamodbstreamszdynamodb-streamselasticbeanstalkzelastic-beanstalkelasticfilesystemefselasticloadbalancingzelastic-load-balancingelasticmapreduceemrelastictranscoderzelastic-transcoderelbelbv2zelastic-load-balancing-v2emailseszentitlement.marketplacezmarketplace-entitlement-serviceeszelasticsearch-serviceeventseventbridgezcloudwatch-eventsziot-dataziot-jobs-dataziot1click-devicesziot1click-projectsziot-1click-projectskinesisanalyticszkinesis-analyticskinesisvideozkinesis-videoz
lex-modelszlex-model-building-servicezlex-runtimezlex-runtime-servicelogszcloudwatch-logsmachinelearningzmachine-learningzmarketplace-entitlementmarketplacecommerceanalyticszmarketplace-commerce-analyticszmetering.marketplacezmarketplace-meteringmeteringmarketplacemghzmigration-hubz
models.lex
monitoring
cloudwatchzmturk-requestermturkzopsworks-cm
opsworkscmzprojects.iot1clickresourcegroupstaggingapizresource-groups-tagging-apiroute53zroute-53route53domainszroute-53-domainszruntime.lexzruntime.sagemakerzsagemaker-runtimesdbsimpledbsecretsmanagerzsecrets-managerserverlessreposerverlessapplicationrepositoryservicecatalogzservice-catalogstatessfnstepfunctionszstorage-gateway)storagegatewayzstreams.dynamodbtaggingz^X-Amz-Checksum-([a-z0-9]*)$)flagsc                 C   s(   t | tr| S t | tr|  dk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
    trueF)
isinstanceboolstrlowerval r{   L/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/botocore/utils.pyensure_boolean   s
   

r}   c                 C   sP   |  d}|dur| }|tvr|td}tdi ||S |  dr&d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)modevalid_modesimds_use_ipv6r7   r6   r{   )get_config_variablerx   METADATA_ENDPOINT_MODESr)   )sessionendpoint_modelendpoint_modeerror_msg_kwargsr{   r{   r|   resolve_imds_endpoint_mode   s   
r   c                 C   s2   t | do| jddo| jddko| 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
    serialization	jsonvalueFlocationheaderstring)hasattrr   get	type_name)shaper{   r{   r|   is_json_value_header   s   

r   c                 C   s<   | du rdS t |tjjr| |v S |  dd | D v S )z&Case-insensitive check for header key.NFc                 S   s   g | ]}|  qS r{   rx   ).0keyr{   r{   r|   
<listcomp>   s    zhas_header.<locals>.<listcomp>)ru   botocore
awsrequestHeadersDictrx   keys)header_nameheadersr{   r{   r|   
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
    serviceAbbreviationserviceFullNameAmazon AWSz\W+)metadatar   service_namereplaceresub)service_modelnamer{   r{   r|   get_service_module_name   s   r   c                 C   s   | sdS t | S )N/)remove_dot_segmentspathr{   r{   r|   normalize_url_path  s   r   c                 C   s   | du r| S t | S )zLReturns None if val is None, otherwise ensure value
    converted to booleanN)r}   ry   r{   r{   r|   normalize_boolean  s   r   c                 C   s   | sdS |  d}g }|D ]}|r%|dkr%|dkr |r|  q|| q| d dkr/d}nd}| d dkr<|r<d}nd}|d| | S )Nr   r   .z..r   )splitpopappendjoin)url	input_urloutput_listxfirstlastr{   r{   r|   r     s&   

r   c                 C   s6   | r| dkrt | ddD ]}|| v rt | dqd S )Nr   
expression)[]*)r&   )r   invalidr{   r{   r|   validate_jmespath_for_set:  s   

r   Tc                 C   s|   |rt | |dd}|d t|dkr|d nd}}|s$t|d|r8|| vr.i | |< t| | ||ddS || |< d S )Nr   r5   r   r   r   F)is_first)r   r   lenr&   set_value_from_jmespath)sourcer   valuer   bitscurrent_key	remainderr{   r{   r|   r   E  s   "
r   c                 C   s   |  di }| ddk}|S )z9Determine if request is intended for an MRAP accesspoint.s3_accesspointregionr   r   )contextr   	is_globalr{   r{   r|   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__r{   r{   r{   r|   r   j  s    r   c                   @   s   e Zd Zdd ZdS )BadIMDSRequestErrorc                 C   
   || _ d S Nrequestselfr   r{   r{   r|   __init__q     
zBadIMDSRequestError.__init__N)r   r   r   r   r{   r{   r{   r|   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 Zd!ddZdS )"IMDSFetcherzlatest/api/token21600r5   Nc                 C   s   || _ || _|d u ri }| ||| _|| _|d u rtj }|dd	 dk| _
|d| _|| _tjj| j t| jd| _d S )NAWS_EC2_METADATA_DISABLEDfalsert   ec2_metadata_v1_disabled)timeoutproxies)_timeout_num_attempts_select_base_url	_base_url_configosenvironcopyr   rx   	_disabled_imds_v1_disabled_user_agentr   httpsessionURLLib3Sessionget_environ_proxies_session)r   r   num_attemptsbase_urlenv
user_agentrC   r{   r{   r|   r   z  s    	
zIMDSFetcher.__init__c                 C   s   | j S r   )r   r   r{   r{   r|   get_base_url  s   zIMDSFetcher.get_base_urlc                 C   s   |d u ri }| ddk}| d}|r|rtd d }|tkr$|}n|r)|}n|r.t}nt}td|  t|sAt|d|S )Nr~   r7   ec2_metadata_service_endpointzFCustom endpoint and IMDS_USE_IPV6 are both set. Using custom endpoint.zIMDS ENDPOINT: )endpoint)r   loggerwarningMETADATA_BASE_URLMETADATA_BASE_URL_IPv6debugis_valid_urir(   )r   r   rC   requires_ipv6custom_metadata_endpointchosen_base_urlr{   r{   r|   r     s*   

zIMDSFetcher._select_base_urlc                 C   s,   d}| j r| j dsd}| j  | | S )Nr   r   )r   endswith)r   r   sepr{   r{   r|   _construct_url  s   zIMDSFetcher._construct_urlc                 C   s  |    | | j}d| ji}| | tjjd||d}t| j	D ]i}z'| j
| }|jdkr8|jW   S |jdv rAW  d S |jdv rJt|W q" tyV   Y  d S  typ } ztjd||dd	 W Y d }~q"d }~w ty } zt|jd
trt||d d }~ww d S )Nz$x-aws-ec2-metadata-token-ttl-secondsPUTmethodr   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#   ru   kwargsr   r   r(   )r   r   r   r   iresponseer{   r{   r|   _fetch_metadata_token  sF   



z!IMDSFetcher._fetch_metadata_tokenc           
      C   s   |    |s
|   |du r| j}| |}i }|dur ||d< | | t| jD ]9}ztjj	d||d}| j
| }||sG|W   S W q* tyc }	 ztjd||	dd W Y d}	~	q*d}	~	ww |  )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  _assert_v1_enabled_default_retryr	  r  r  r   r   r   r  r   r  r  r  r   r  _RETRIES_EXCEEDED_ERROR_CLS)
r   url_path
retry_functokenr   r   r  r   r  r   r{   r{   r|   _get_request  s:   


zIMDSFetcher._get_requestc                 C   s   | j d ur| j |d< d S d S )Nz
User-Agent)r   )r   r   r{   r{   r|   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   r{   r{   r|   r    s   
zIMDSFetcher._assert_enabledc                 C   s   | j rtddd S )NzLUnable to retrieve token for use in IMDSv2 call and IMDSv1 has been disabled	error_msg)r   r+   r   r{   r{   r|   r#    s
   zIMDSFetcher._assert_v1_enabledc                 C      |  |p	| |S r   _is_non_ok_response	_is_emptyr   r  r{   r{   r|   r$       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_responser0  r{   r{   r|   r.     s   
zIMDSFetcher._is_non_ok_responsec                 C   s   |j s| j|ddd dS dS )Nzno bodyTr2  F)contentr4  r0  r{   r{   r|   r/  &  s   zIMDSFetcher._is_emptyFc                 C   s@   d}||j |jg}|r|d7 }||j tj|g|R   d S )NzHMetadata service returned %s response with status code of %s for url: %sz, content body: %s)r  r   r   r5  r   r  )r   r  reason_to_logr3  	statementlogger_argsr{   r{   r|   r4  ,  s   zIMDSFetcher._log_imds_responser   F)r   r   r   r   r%  r  r   DEFAULT_METADATA_SERVICE_TIMEOUTr   r   r   r   r	  r!  r)  r  r  r#  r$  r.  r/  r4  r{   r{   r{   r|   r   u  s.    

$,r   c                   @   s\   e Zd ZdZg d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=|   }| |}| ||}| |r-||d |d |d |d d}| | |W S d|v r;d|v r;td| i W S  | jyO   td	| j Y i S  t	yh } ztd
|j
 W Y d }~i S d }~ww )Nr<  r=  r>  r?  )	role_name
access_key
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   r{   r{   r|   retrieve_iam_role_credentialsA  s<   



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(  r{   r{   r|   rG  j  s   z%InstanceMetadataFetcher._get_iam_rolec                 C   s$   | j | j| | j|d}t|jS rM  )r)  rO  _needs_retry_for_credentialsr   loadsr  )r   r@  r(  rr{   r{   r|   rH  q  s   z(InstanceMetadataFetcher._get_credentialsc                 C   s4   z	t |j W dS  ty   | |d Y dS w )NFzinvalid jsonT)r   rR  r  
ValueErrorr4  r0  r{   r{   r|   _is_invalid_jsony  s   z(InstanceMetadataFetcher._is_invalid_jsonc                 C   r,  r   r-  r0  r{   r{   r|   rP    r1  z2InstanceMetadataFetcher._needs_retry_for_role_namec                 C   s   |  |p| |p| |S r   )r.  r/  rU  r0  r{   r{   r|   rQ    s
   
z4InstanceMetadataFetcher._needs_retry_for_credentialsc                 C   s*   | j D ]}||vrtd|  dS qdS )Nz3Retrieved credentials is missing required field: %sFT)_REQUIRED_CREDENTIAL_FIELDSr   r  )r   rK  fieldr{   r{   r|   rI    s   
z7InstanceMetadataFetcher._contains_all_credential_fieldsc           
      C   s   | d}|d u rd S zHtj|d}| j dd}tdd}|| }tj }tj|d}|| }||krQ|| }	|	d|d< t	
d|d d	d
 W d S W d S  tyg   t	d|d   Y d S w )NrC  z%Y-%m-%dT%H:%M:%SZ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 )r   r   strptimer   randomrandintutcnow	timedeltastrftimer   inforT  r  )
r   rK  
expirationrefresh_intervaljitterrefresh_interval_with_jittercurrent_timerefresh_offsetextension_timenew_timer{   r{   r|   rJ    sB   



z,InstanceMetadataFetcher._evaluate_expirationr   )r   r   r   rO  rV  rL  rG  rH  rU  rP  rQ  rI  rJ  r{   r{   r{   r|   r;  8  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u 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   r   r   fetcherr{   r{   r|   r     s
   
zIMDSRegionProvider.__init__c                 C   s   |   }|S )z#Provide the region value from IMDS.)_get_instance_metadata_region)r   instance_regionr{   r{   r|   provide  s   zIMDSRegionProvider.providec                 C   s   |   }| }|S r   )_get_fetcherretrieve_region)r   ro  r   r{   r{   r|   rp    s   z0IMDSRegionProvider._get_instance_metadata_regionc                 C   s   | j d u r
|  | _ | j S r   )rn  _create_fetcherr   r{   r{   r|   rs    s   

zIMDSRegionProvider._get_fetcherc                 C   sX   | j d}| j d}| j dt| j | j dd}t||| j| j  |d}|S )Nmetadata_service_timeoutmetadata_service_num_attemptsr   r   )r   r~   r   )r   r   r   r   rC   )r   r   r   InstanceMetadataRegionFetcherrm  r   )r   metadata_timeoutmetadata_num_attemptsimds_configro  r{   r{   r|   ru    s0   z"IMDSRegionProvider._create_fetcherNN)r   r   r   r   rr  rp  rs  ru  r{   r{   r{   r|   rl    s    
rl  c                   @       e Zd ZdZdd Zdd ZdS )rx  z-latest/meta-data/placement/availability-zone/c                 C   s4   z|   }|W S  | jy   td| j Y dS w )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.
        rF  N)_get_regionr%  r   r  r   )r   r   r{   r{   r|   rt    s   z-InstanceMetadataRegionFetcher.retrieve_regionc                 C   s2   |   }| j| j| j|d}|j}|d d }|S )NrN  r   )r!  r)  rO  r$  r  )r   r(  r  availability_zoner   r{   r{   r|   r~    s   z)InstanceMetadataRegionFetcher._get_regionN)r   r   r   rO  rt  r~  r{   r{   r{   r|   rx    s    rx  Fc                 C   s   |D ]M}t || tr$|| v r||v rt| | ||  q|| | |< qt || trI|rI|| v rBt | | trB| | ||  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)ru   dictmerge_dictslistextend)dict1dict2append_listsr   r{   r{   r|   r     s   r  c                 C   s"   i }| D ]
}| | ||  < q|S )zDCopies the given dictionary ensuring all keys are lowercase strings.r   )originalr   r   r{   r{   r|   lowercase_dict=  s   r  c                 C   sZ   z || }|  }t|W  d    W S 1 sw   Y  W d S  ty,   t| dw )Nr   )readparse_key_val_file_contentsOSErrorr   )filename_openfcontentsr{   r{   r|   parse_key_val_fileE  s   
(
r  c                 C   sH   i }|   D ]}d|vrq|dd\}}| }| }|||< q|S )N=r5   )
splitlinesr   strip)r  finalliner   rz   r{   r{   r|   r  N  s   
r  c                 C   s   g }t | dr|  }n| }|D ]+\}}t|tr-|D ]}|t| dt|  qq|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  &)r   r  ru   r  r   percent_encoder   )mappingsafeencoded_pairspairsr   r   elementr{   r{   r|   percent_encode_sequence]  s   



r  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  )ru   bytesrw   encoder   )	input_strr  r{   r{   r|   r    s
   

r  c              	   C   s6   t j ddddddt d}|| }|t j| d S )a  Parse numerical epoch timestamps (seconds since 1970) into a
    ``datetime.datetime`` in UTC using ``datetime.timedelta``. This is intended
    as fallback when ``fromtimestamp`` raises ``OverflowError`` or ``OSError``.

    :type value: float or int
    :param value: The Unix timestamps as number.

    :type tzinfo: callable
    :param tzinfo: A ``datetime.tzinfo`` class or compatible callable.
      r5   r   tzinforZ  )r   r   
astimezonera  )r   r  
epoch_zeroepoch_zero_localizedr{   r{   r|   _epoch_seconds_to_datetime  s   r  c              
   C   s   t | ttfrtj| | S ztjt| | W S  ttfy%   Y nw ztjj	| dt
 idW S  ttfyJ } z
td|  d| d}~ww )z.Parse timestamp with pluggable tzinfo options.GMT)tzinfoszInvalid timestamp "z": N)ru   intfloatr   fromtimestamp	TypeErrorrT  dateutilparserparser   )r   r  r   r{   r{   r|   _parse_timestamp_with_tzinfo  s   r  c                 C   s   t  }|D ]'}zt| |W   S  ttfy, } ztjd|j|d W Y d}~qd}~ww zt| }W n tt	fy>   Y n.w z|D ]
}t
||d  W S W n ttfyk } ztjd|j|d W Y d}~nd}~ww td|  d)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  Nr  zHUnable to parse timestamp using fallback method with "%s" timezone info.z1Unable to calculate correct timezone offset for "")r   r  r  OverflowErrorr   r  r   r  r  rT  r  RuntimeError)r   tzinfo_optionsr  r   numeric_valuer{   r{   r|   parse_timestamp  s@   
r  c                 C   sD   t | tr| }nt| }|jdu r|jt d}|S |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.

    Nr  )ru   _DatetimeClassr  r  r   r   r  )r   datetime_objr{   r{   r|   parse_to_aware_datetime  s   

r  c                 C   sR   t  ddd}| jdu r|du rt }| j|d} | j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
    r  r5   Nr  )r   r  r   r   	utcoffsettotal_seconds)dtdefault_timezoneepochdr{   r{   r|   datetime2timestamp  s   

r  c                    s>   t  }t fdddD ]}|| q|r| S | 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                      
     dS N   r  r{   bodyr{   r|   <lambda>>     
 z"calculate_sha256.<locals>.<lambda>    )hashlibsha256iterupdate	hexdigestdigest)r  as_hexchecksumchunkr{   r  r|   calculate_sha256+  s   r  c                    s   g }dt j}t fdddD ]}|||  q|s%|d S t|dkrSg }t|D ]\}}|durE||||   q1|| q1|}t|dks+t	|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

    r  c                      s
     S r   r  r{   r  required_chunk_sizer{   r|   r  W  r  z%calculate_tree_hash.<locals>.<lambda>r  r5   Nr   ascii)r  r  r  r   r  r  r   	_in_pairsbinasciihexlifydecode)r  chunksr  r  
new_chunksr   secondr{   r  r|   calculate_tree_hashF  s    	r  c                 C   s   t | }t||S r   )r  r   )iterableshared_iterr{   r{   r|   r  g  s   	
r  c                   @   r}  )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   r   r   )_fget)r   fgetr{   r{   r|   r     r   zCachedProperty.__init__c                 C   s(   |d u r| S |  |}||j| j j< |S r   )r  __dict__r   )r   objclscomputed_valuer{   r{   r|   __get__  s
   
zCachedProperty.__get__N)r   r   r   r   r   r  r{   r{   r{   r|   r  x  s    r  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   r   r   )_use_member_names)r   use_member_namesr{   r{   r|   r     r   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   r   stackr{   r{   r|   generate_skeleton  s   
z#ArgumentGenerator.generate_skeletonr   c                 C   s>  | |j z|jdkr| ||W |  S |jdkr'| ||W |  S |jdkr7| ||W |  S |jdkr[| jrF|W |  S |jrTt	
|jW |  S W |  dS |jdv rgW |  dS |jdv rsW |  d	S |jd
krW |  dS |jdkrtddddddW |  S W |  d S |  w )N	structurer  mapr   r   )integerlongr   )r  doubleg        booleanT	timestampr  r5   )r   r   r   _generate_type_structurer   _generate_type_list_generate_type_mapr  enumr^  choicer   r   r   r  r   r{   r{   r|   r    sD   














z$ArgumentGenerator._generate_skeletonc                 C   sF   | |jdkr
i S t }|j D ]\}}| j|||d||< q|S )Nr5   )r   )countr   r   membersr  r  )r   r   r  skeletonmember_namemember_shaper{   r{   r|   r    s   z*ArgumentGenerator._generate_type_structurec                 C   s$   d}| j r	|jj}| |j||gS )Nr   )r  memberr   r  r  r{   r{   r|   r    s
   z%ArgumentGenerator._generate_type_listc                 C   s0   |j }|j}|jdksJ td| ||fgS )Nr   KeyName)r   r   r   r   r  )r   r   r  	key_shapevalue_shaper{   r{   r|   r    s   z$ArgumentGenerator._generate_type_mapNr9  )r   )
r   r   r   r   r   r  r  r  r  r  r{   r{   r{   r|   r    s    



r  c                 C   s.   t | rdS dt| j d}t|d uS )NFr   r   )r   intersectionr   hostnamer   matchendpoint_urlr	  r{   r{   r|   is_valid_ipv6_endpoint_url  s   
r  c                 C   s   t | j}t|d uS r   )r   r	  r   r
  r  r{   r{   r|   is_valid_ipv4_endpoint_url  s   
r  c                 C   sh   t | rdS t| }|j}|du rdS t|dkrdS |d dkr(|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   r   r   z;^((?!-)[A-Z\d-]{1,63}(?<!-)\.)*((?!-)[A-Z\d-]{1,63}(?<!-))$)	r   r  r   r	  r   r   compile
IGNORECASEr
  )r  partsr	  allowedr{   r{   r|   is_valid_endpoint_url  s   

r  c                 C   s   t | pt| S r   )r  r  )r  r{   r{   r|   r    s   
r  c                 C   s2   | du 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)r   r  r
  r*   )r  valid_host_labelvalidr{   r{   r|   validate_region_name  s   


r  c                 C   sR   d| v rdS t | }|dk s|dkrdS t| }|du s%| t | kr'd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.
    r   F   ?   NT)r   LABEL_REr
  end)bucket_namenr
  r{   r{   r|   check_dns_name)  s   
r  c              
   K   sb   | j ddr	d}z	t| || W dS  ty0 } z|jd }td| W Y d}~dS d}~ww )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.

    use_global_endpointFzs3.amazonaws.comr  z2Not changing URI, bucket is not DNS compatible: %sN)r   r   switch_to_virtual_host_styler$   r  r   r  )r   signature_versionr  default_endpoint_urlr  r   r  r{   r{   r|   fix_s3_host@  s   

r$  c                 K   s  | j durdS t| rtd dS t| j}|j| _ |jd}|du r(|j}t	|dkr|d }|s6dS td| j t
|rt	|dkrU| j d dkrU|  j d7  _ || d|p`d}|}|d | }	|j|	||jd	f}
t|
}|| _td
| dS 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.r   r5   z*Checking for DNS compatible bucket for: %s   r   r   r   zURI updated to: %s)r  )	auth_path_is_get_bucket_location_requestr   r  r   r   r   r   netlocr   r  remover   schemequeryr   r$   )r   r"  r#  r  r  
path_partsr  r   global_endpointhost	new_tuplenew_urir{   r{   r|   r!  ]  s>   



r!  c                 C   s   | j dS )Nz	?location)r   r  r   r{   r{   r|   r'       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                    sb   |f}|rt t| }||f}| j|}|d ur|S  | g|R i |}|| j|< |S r   )tuplesortedr  _instance_cacher   )r   argsr  	cache_keykwarg_itemsresultfunc	func_namer{   r|   _cache_guard  s   

z$instance_cache.<locals>._cache_guard)r   	functoolswraps)r:  r<  r{   r9  r|   instance_cache  s   r?  c                         fdd}|S )a  
    Version of functools.lru_cache that stores a weak reference to ``self``.

    Serves the same purpose as :py:func:`instance_cache` but uses Python's
    functools implementation which offers ``max_size`` and ``typed`` properties.

    lru_cache is a global cache even when used on a method. The cache's
    reference to ``self`` will prevent garbace collection of the object. This
    wrapper around functools.lru_cache replaces the reference to ``self`` with
    a weak reference to not interfere with garbage collection.
    c                    s>   t ji  fddt  fdd}j|_|S )Nc                    s    |  g|R i |S r   r{   )weakref_to_selfr5  r  )r:  r{   r|   func_with_weakref  s   z=lru_cache_weakref.<locals>.wrapper.<locals>.func_with_weakrefc                    s    t | g|R i |S r   )weakrefref)r   r5  r  )rB  r{   r|   inner  s   z1lru_cache_weakref.<locals>.wrapper.<locals>.inner)r=  	lru_cacher>  
cache_info)r:  rE  
cache_argscache_kwargs)r:  rB  r|   wrapper  s   z"lru_cache_weakref.<locals>.wrapperr{   )rI  rJ  rK  r{   rH  r|   lru_cache_weakref  s   rL  c                 K   sh   t | jjd}dd |D }d}t|dkr!|d|d 7 }|d7 }|dv r+dS t| |d	d
 dS )z?Switches the current s3 endpoint with an S3 Accelerate endpointr   c                 S   s   g | ]}|t v r|qS r{   S3_ACCELERATE_WHITELISTr   pr{   r{   r|   r     s    z-switch_host_s3_accelerate.<locals>.<listcomp>zhttps://s3-accelerate.r   amazonaws.com)ListBucketsCreateBucketDeleteBucketNF)use_new_scheme)r   r   r(  r   r   r   _switch_hosts)r   operation_namer  r  r   r{   r{   r|   switch_host_s3_accelerate  s   rX  c                 C   s6   t | jd}||r|| }t| | dS dS )zBSwitches the host using a parameter value from a JSON request bodyr  N)r   rR  datar  r   rV  )r   
param_namerequest_jsonnew_endpointr{   r{   r|   switch_host_with_param  s
   
r]  c                 C   s   t | j||}|| _d S r   )_get_new_endpointr   )r   r\  rU  final_endpointr{   r{   r|   rV    s   
rV  c                 C   sV   t |}t | }|j}|r|j}||j|j|jdf}t|}td|  d|  |S Nr   zUpdating URI from  to )r   r*  r(  r   r+  r   r   r  )original_endpointr\  rU  new_endpoint_componentsoriginal_endpoint_componentsr*  final_endpoint_componentsr_  r{   r{   r|   r^     s   r^  c                 C   sR   |D ]$}|| v 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)ru   r  
deep_merge)baseextrar   r{   r{   r|   rf    s   rf  c                 C   s   |  dd S )zcTranslate the form used for event emitters.

    :param service_id: The service_id to convert.
     -)r   rx   )
service_idr{   r{   r|   hyphenize_service_id'  s   rl  c                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
IdentityCachezBase IdentityCache implementation for storing and retrieving
    highly accessed credentials.

    This class is not intended to be instantiated in user code.
    base_identity_cachec                 C      || _ || _d S r   _client_credential_clsr   clientcredential_clsr{   r{   r|   r   8     
zIdentityCache.__init__c                 K   s2   | j di |}| }| jj||| jddd}|S )N-   
   )r   refresh_usingr  advisory_timeoutmandatory_timeoutr{   )build_refresh_callbackrr  create_from_metadataMETHOD)r   r  callbackr   credential_entryr{   r{   r|   get_credentials<  s   zIdentityCache.get_credentialsc                  K   s   t  )zCallback to be implemented by subclasses.

        Returns a set of metadata to be converted into a new
        credential instance.
        )NotImplementedError)r  r{   r{   r|   r|  H  s   z$IdentityCache.build_refresh_callbackN)r   r   r   r   r~  r   r  r|  r{   r{   r{   r|   rm  /  s    rm  c                       sJ   e Zd ZdZdZdd Zejdd fddZd	d
 Z	dddZ
  ZS )S3ExpressIdentityCachezS3Express IdentityCache for retrieving and storing
    credentials from CreateSession calls.

    This class is not intended to be instantiated in user code.
    	s3expressc                 C   ro  r   rp  rs  r{   r{   r|   r   Z  rv  zS3ExpressIdentityCache.__init__d   )maxsizec                    s   t  j|dS )Nbucket)superr  )r   r  	__class__r{   r|   r  ^  s   z&S3ExpressIdentityCache.get_credentialsc                    r@  )Nc                     sB   j j d} | d }j|d dd}|d |d |d |d	S )
NBucketCredentialsr?  T)isor<  r=  SessionToken)rA  rB  r(  rC  )rq  create_session_serialize_if_needed)r  credsrd  r  r   r{   r|   	refresherc  s   z@S3ExpressIdentityCache.build_refresh_callback.<locals>.refresherr{   )r   r  r  r{   r  r|   r|  b  s   z-S3ExpressIdentityCache.build_refresh_callbackFc                 C   $   t |tr|r| S |dS |S Nz%Y-%m-%dT%H:%M:%S%Zru   r  	isoformatrb  r   r   r  r{   r{   r|   r  r  
   

z+S3ExpressIdentityCache._serialize_if_neededr9  )r   r   r   r   r~  r   r=  rF  r  r|  r  __classcell__r{   r{   r  r|   r  Q  s    
r  c                   @   s0   e Zd Zd
ddZd
ddZdd Zdd	 ZdS )S3ExpressIdentityResolverNc                 C   s*   t || _|d u rt| j|}|| _d S r   )rC  proxyrq  r  _cache)r   rt  ru  cacher{   r{   r|   r   {  s   
z"S3ExpressIdentityResolver.__init__c                 C   s8   t d |p| jjj}|d| j |d| j d S )Nz'Registering S3Express Identity Resolverbefore-call.s3before-sign.s3)r   r  rq  metarX   registerapply_signing_cache_keyresolve_s3express_identityr   event_emitteremitterr{   r{   r|   r    s   
z"S3ExpressIdentityResolver.registerc                 K   s^   | di }| dd }| di  d}|dkr+|d ur-|di  ||d d< d S d S d S )Nendpoint_propertiesbackendinput_paramsr  	S3Expresssigningr6  )r   
setdefault)r   paramsr   r  r  r  r  r{   r{   r|   r    s   z1S3ExpressIdentityResolver.apply_signing_cache_keyc           	      K   sl   |j di }|d}|dkr0|dr2| j|d< d|vr4|j di di d	|d< d S d S d S d S )
Nr  signing_namer  zv4-s3expressidentity_cacher6  s3_redirectr  r  )r   r   
startswithr  )	r   r   r  r  r"  request_signerrW  r  signing_contextr{   r{   r|   r    s   



z4S3ExpressIdentityResolver.resolve_s3express_identityr   )r   r   r   r   r  r  r  r{   r{   r{   r|   r  z  s
    


r  c                   @   L   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   )r  rC  r  rq  r   endpoint_bridgert  r  r{   r{   r|   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  rq  r  rX   r  redirect_from_errorannotate_request_contextredirect_from_cacher  r{   r{   r|   r    s   
zS3RegionRedirectorv2.registerc              	   K   s  |du rdS | di  di }t| drtd dS | dr+td dS |d  d	i }| d
}|d  di }|dv oH|jdk}	|dv oY|jdkoYd| di v }
|dkoad|v }|d duon|d jdv }|dk}t|	|||
|gs~dS |d d d }|d  d}| ||}|du rtd| d| d dS td| d| d| d || j	|< | j
j}|j||d d d |d d}| |d |j|d< d|d d d< |j d }|dur
||}|\}}||d d!< i |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   r  r  zBS3 request was previously for an Accesspoint ARN, not redirecting.
redirected6S3 request was previously redirected, not redirecting.r5   ErrorrD  ResponseMetadata301400
HeadObject
HeadBucketx-amz-bucket-regionHTTPHeadersAuthorizationHeaderMalformedRegionr   i-  i.  i3  PermanentRedirectclient_region S3 client configured for region  but the bucket S is not in that region and the proper region could not be automatically determined. is in region b; Please configure the proper region to avoid multiple unnecessary redirects and signing attempts.r  )operation_model	call_argsrequest_contextr   TauthSchemes	auth_typer  )r   	ArnParseris_arnr   r  r   r  anyget_bucket_regionr  rq  _ruleset_resolverconstruct_endpointset_request_urlr   
propertiesauth_schemes_to_signing_ctx)r   request_dictr  	operationr  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_regionep_resolverep_infoauth_schemes	auth_infor  r  r{   r{   r|   r    s   


	



z(S3RegionRedirectorv2.redirect_from_errorc              
   C      |d }|d d }d|v r|d S | di  dd}|dur"|S z| jj|d}|d d }W n tyJ } z|jd d }W Y d}~nd}~ww | 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.
        r5   r  r  r  r  r  Nr  r   rq  head_bucketr   r  r   r  r  service_responseresponse_headersr   r   r   r{   r{   r|   r  ,      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  r{   r{   r|   r  M  s   z$S3RegionRedirectorv2.set_request_urlc                 K   s<   | d}|dur|| jv r| j |}||d< dS dS 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)r   r  )r   builtinsr  r  r  r  r{   r{   r|   r  U  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  r{   r{   r|   r  a  s
   
z-S3RegionRedirectorv2.annotate_request_contextr   )r   r   r   r   r   r  r  r  r  r  r  r{   r{   r{   r|   r    s    

k!r  c                   @   r  )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 u 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  rC  r  rq  warningswarnFutureWarningr  r{   r{   r|   r   r  s   

zS3RegionRedirector.__init__c                 C   s<   |p| j jj}|d| j |d| j |d| j d S )Nr  r  r  )rq  r  rX   r  r  r  r  r  r{   r{   r|   r    s   zS3RegionRedirector.registerc              	   K   s  |du rdS |  |di rtd dS |di dr&td dS |d di }|d}|d d	i }|d
v oC|jdk}|d
v oT|jdkoTd|di v }	|dko\d|v }
|d duoi|d jdv }|dk}t||
||	|gsydS |d d d }|d d}| ||}|du rtd| d| d dS td| d| d| d | 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.s3_redirectedr  r5   r  rD  r  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_accesspointr   r   r  r   r  r  r  r   resolver  r  )r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r{   r{   r|   r    s   

	
z&S3RegionRedirector.redirect_from_errorc              
   C   r  r  r  r  r{   r{   r|   r    r  z$S3RegionRedirector.get_bucket_regionc                 K   s8   | di  dd }|d urt|d |d|d< d S d S )Nr  r   r   F)r   r^  r   r  r   r  r   r{   r{   r|   r    s   z"S3RegionRedirector.set_request_urlc                 K   sH   |  |rdS |d}| j|}|dur||d< dS 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  r   r  )r   r  r   r  r  r  r{   r{   r|   r    s   

z&S3RegionRedirector.redirect_from_cachec                 C   s   d|v S Nr   r{   )r   r   r{   r{   r|   r    s   z%S3RegionRedirector._is_s3_accesspointr   )r   r   r   r   r   r  r  r  r  r  r  r{   r{   r{   r|   r  m  s    

^!r  c                   @   s   e Zd ZdS )InvalidArnExceptionN)r   r   r   r{   r{   r{   r|   r
    s    r
  c                   @   s    e Zd Zdd Zedd ZdS )r  c                 C   sL   | dd}t|dk rtd| d|d |d |d |d	 |d d
S )N:      zProvided ARN: zE must be of the format: arn:partition:service:region:account:resourcer5   r%  r     )	partitionservicer   accountresource)r   r   r
  )r   arn	arn_partsr{   r{   r|   	parse_arn$  s   
zArnParser.parse_arnc                 C   sD   t | tr
| dsdS t }z||  W dS  ty!   Y dS w )Nzarn:FT)ru   rw   r  r  r  r
  )r   
arn_parserr{   r{   r|   r  3  s   
zArnParser.is_arnN)r   r   r   r  staticmethodr  r{   r{   r{   r|   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}$)rS  Nc                 C      || _ |d u rt | _ d S d S r   _arn_parserr  r   r  r{   r{   r|   r   I     zS3ArnParamHandler.__init__c                 C      | d| j d S )Nr  r  
handle_arnr   r  r{   r{   r|   r  N     zS3ArnParamHandler.registerc                 K   sf   |j | jv rd S | |}|d u rd S |d dkr"| ||| d S |d dkr1| ||| d S d S )Nresource_typeaccesspointoutpost)r   _BLACKLISTED_OPERATIONS"_get_arn_details_from_bucket_param_store_accesspoint_store_outpost)r   r  modelr   r  arn_detailsr{   r{   r|   r   Q  s   
zS3ArnParamHandler.handle_arnc                 C   sH   d|v r"z|d }| j |}| || |W S  ty!   Y d S w d S )Nr  )r  r  _add_resource_type_and_namer
  )r   r  r  r+  r{   r{   r|   r'  \  s   z4S3ArnParamHandler._get_arn_details_from_bucket_paramc                 C   s>   | j |d }|r|d|d< |d|d< d S t|d)Nr  r#  resource_name)r  )_RESOURCE_REGEXr
  groupr1   )r   r  r+  r
  r{   r{   r|   r,  g  s
   
z-S3ArnParamHandler._add_resource_type_and_namec                 C   s8   |d |d< |d |d |d |d |d d|d< d S )	Nr-  r  r  r  r   r  )r   r  r  r   r  r   r{   r   r  r   r+  r{   r{   r|   r(  o  s   z$S3ArnParamHandler._store_accesspointc                 C   sd   |d }| j |}|st|d|d}||d< |d||d |d |d |d	 d
|d< d S )Nr-  )r-  accesspoint_namer  outpost_namer  r  r   r  )r2  r   r  r  r   r  r   )_OUTPOST_RESOURCE_REGEXr
  r/   r/  )r   r  r   r+  r-  r
  r1  r{   r{   r|   r)    s   

z S3ArnParamHandler._store_outpostr   )r   r   r   r   r  r.  r3  r&  r   r  r   r'  r,  r(  r)  r{   r{   r{   r|   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awsrQ  NFc                 C   J   || _ || _|| _|| _|d u ri | _|| _|| _|d u r#| j| _d S d S r   r   _region
_s3_config_use_fips_endpoint_endpoint_url
_partition_DEFAULT_PARTITIONr   endpoint_resolverr   	s3_configr  r  use_fips_endpointr{   r{   r|   r     s   
zS3EndpointSetter.__init__c                 C   s.   | d| j | d| j | d| j d S )Nr  zchoose-signer.s3z%before-call.s3.WriteGetObjectResponse)r  set_endpoint
set_signer#update_endpoint_to_s3_object_lambdar!  r{   r{   r|   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	  )rH  r	  r   F)	_use_accelerate_endpointr2   _override_signing_namer;  r   r  r8  formatr^  )r   r  r   r  resolverresolvedr\  r{   r{   r|   rD    s    z4S3EndpointSetter.update_endpoint_to_s3_object_lambdac                 K   s   |  |r&| | | | | | | |}| | | || d S | jr?| jr6t	d| j
 ddtdd|i| | jrN| jdd|i| d S d S )Nz8Client is configured to use the FIPS psuedo region for "zA", but S3 Accelerate does not have any FIPS compatible endpoints.rE  r   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_endpointrI  r:  r2   r8  rX  _s3_addressing_handler)r   r   r  r  r{   r{   r|   rB    s*   




zS3EndpointSetter.set_endpointc                 C   
   d|j v S r	  r   r   r{   r{   r|   rN    r   z*S3EndpointSetter._use_accesspoint_endpointc                 C   s   | j sd S d|jd d v rtdhdd|jd v r%td| j dd|jd d }|| jkrE| jd	d
sGtd| j d| ddd S d S )Nfipsr   r   ,Invalid ARN, FIPS region not allowed in ARN.rE  r2  z4Client is configured to use the FIPS psuedo-region "z2", but outpost ARNs do not support FIPS endpoints.use_arn_regionTz8Client is configured to use the FIPS psuedo-region for "z1", but the access-point ARN provided is for the "zn" region. For clients using a FIPS psuedo-region calls to access-point ARNs in another region are not allowed.)r:  r   r0   r8  r9  r   r   r   accesspoint_regionr{   r{   r|   rP    s,   
z)S3EndpointSetter._validate_fips_supportedc                 C   s4   | j ddr	d S | jdv rtd| j ddd S )NrZ  T)z
aws-globalzs3-external-1z6Client is configured to use the global psuedo-region "zJ". When providing access-point ARNs a regional endpoint must be specified.rE  )r9  r   r8  r0   r   r{   r{   r|   rQ  		  s   
z)S3EndpointSetter._validate_global_regionsc                 C   s   | j rtdd|jd d }|| jkr!td| j d| dd|jd d}|d	kr8| jd
r8tdd|jd d}|rM| jd
rMtdd| | d S )NzZClient does not support s3 accelerate configuration when an access-point ARN is specified.rE  r   r  Client is configured for "z3" partition, but access-point ARN provided is for "zE" partition. The client and  access-point partition must be the same.r  rG  use_dualstack_endpointzjClient does not support s3 dualstack configuration when an S3 Object Lambda access point ARN is specified.r2  zTClient does not support s3 dualstack configuration when an outpost ARN is specified.)rI  r0   r   r<  r   r9  _validate_mrap_s3_config)r   r   request_partition
s3_servicer2  r{   r{   r|   rO  	  s0   

z0S3EndpointSetter._validate_accesspoint_supportedc                 C   s>   t |jsd S | jdrtdd| jdrtddd S )N$s3_disable_multiregion_access_pointszCInvalid configuration, Multi-Region Access Point ARNs are disabled.rE  r^  zeClient does not support s3 dualstack configuration when a Multi-Region Access Point ARN is specified.)r   r   r9  r   r0   r   r{   r{   r|   r_  :	  s   
z)S3EndpointSetter._validate_mrap_s3_configc                 C   sN   t |jr| |d | jS | jddr$|jd d }| || |S | jS )Nr   rZ  Tr   r   )r   r   _override_signing_regionr9  r   r8  r[  r{   r{   r|   rR  L	  s   
z9S3EndpointSetter._resolve_region_for_accesspoint_endpointc                 K   s   t |rtrdS tddd S )Ns3v4azzUsing S3 with an MRAP arn requires an additional dependency. You will need to pip install botocore[crt] before proceeding.rE  )r   r   r,   )r   r   r  r{   r{   r|   rC  Y	  s   zS3EndpointSetter.set_signerc                 C   s    |j d d }| |j | d S )Nr   r  r   rJ  )r   r   accesspoint_servicer{   r{   r|   rS  d	  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   r   r   r*  _get_netlocr   _get_accesspoint_pathr   r+  r   r  )r   r   r  original_componentsaccesspoint_endpointr{   r{   r|   rT  h	  s   

z0S3EndpointSetter._switch_to_accesspoint_endpointc                 C   s   t |r	| |S | ||S r   )r   _get_mrap_netloc_get_accesspoint_netloc)r   r  r  r{   r{   r|   rg  z	  s   
zS3EndpointSetter._get_netlocc                 C   s\   |d }d}|d g}| j rt| j j}|| n|d }|d|| |g d|S )Nr   z	s3-globalr   r  r$  r   )r;  r   r(  r   r  _get_partition_dns_suffixr   )r   r  r   r  mrap_netloc_componentsendpoint_url_netlocr  r{   r{   r|   rk  	  s   

z!S3EndpointSetter._get_mrap_netlocc           	      C   s   |d }d |d |d g}|d}| jr*|r|| t| jj}|| n>|r6|dg}|| n|d dkrH| d|}|| n| d	|}|| | jd
r^|d ||| 	|g d
|S )Nr   z{}-{}r   r  r2  s3-outpostsr  rG  zs3-accesspointr^  r8   r   )rK  r   r;  r   r   r(  r  _inject_fips_if_neededr9  _get_dns_suffixr   )	r   r  r  r   accesspoint_netloc_componentsr2  ro  outpost_host	componentr{   r{   r|   rl  	  s6   




z(S3EndpointSetter._get_accesspoint_netlocc                 C   s   | j r| dS |S )N-fipsr:  )r   ru  r  r{   r{   r|   rq  	  s   
z'S3EndpointSetter._inject_fips_if_neededc                 C   s"   |d d }| d| ddpdS )Nr   r   r   r   r5   )r   )r   original_pathr  r   r{   r{   r|   rh  	  s   z&S3EndpointSetter._get_accesspoint_pathc                 C   s   | j |}|d u r| j}|S r   )r   get_partition_dns_suffix_DEFAULT_DNS_SUFFIX)r   partition_name
dns_suffixr{   r{   r|   rm  	  s   z*S3EndpointSetter._get_partition_dns_suffixc                 C   ,   | j d|}| j}|rd|v r|d }|S Nr  	dnsSuffixr   r  rz  r   r  rM  r|  r{   r{   r|   rr  	     z S3EndpointSetter._get_dns_suffixc                 C   $   |j di }||d< ||j d< d S Nr  r   r   r   r   r   r  r  r{   r{   r|   rc  	     z)S3EndpointSetter._override_signing_regionc                 C   s    | di }||d< ||d< d S Nr  r  r   )r   r   r  r  r{   r{   r|   rJ  	  s   z'S3EndpointSetter._override_signing_namec                 C   s   | j drdS | jd u rdS t| jj}|dsdS |d}|d dkr)dS |dd	 }t|tt|kr;dS t	d
d |D S )Nuse_accelerate_endpointTFrQ  r   r   s3-accelerater5   c                 s       | ]}|t v V  qd S r   rM  rO  r{   r{   r|   	<genexpr>
      z<S3EndpointSetter._use_accelerate_endpoint.<locals>.<genexpr>)
r9  r   r;  r   r(  r  r   r   setall)r   r(  r  feature_partsr{   r{   r|   rI  	  s   


z)S3EndpointSetter._use_accelerate_endpointc                 C   s"   | j rdS | jd}|r|S d S )Nvirtualaddressing_style)rI  r9  r   )r   configured_addressing_styler{   r{   r|   _addressing_style
  s   z"S3EndpointSetter._addressing_stylec                 C   sH   | j dkrtd tS | j dks| jd urtd d S td tS )Nr  z'Using S3 virtual host style addressing.r   zUsing S3 path style addressing.zSDefaulting to S3 virtual host style addressing with path style addressing fallback.)r  r   r  r!  r;  r$  r   r{   r{   r|   rU  
  s   


z'S3EndpointSetter._s3_addressing_handlerNNNNF)r   r   r   r=  rz  r   r  rD  rB  rN  rP  rQ  rO  r_  rR  rC  rS  rT  rg  rk  rl  rq  rh  rm  rr  rc  rJ  r  rI  r  rU  r{   r{   r{   r|   r4    sH    
% 				
"
r4  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S3ControlEndpointSetterr5  rQ  z^[a-zA-Z0-9\-]{1,63}$NFc                 C   r6  r   r7  r>  r{   r{   r|   r   0
  s   	z S3ControlEndpointSetter.__init__c                 C   r  )Nzbefore-sign.s3-control)r  rB  r!  r{   r{   r|   r  D
  r"  z S3ControlEndpointSetter.registerc                 K   s   |  |r!| | | |}| | | || | | d S | |r?| | | |d | 	| j
}| || d S d S Nrp  )_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_validrJ  _construct_outpost_endpointr8  _update_request_netloc)r   r   r  r  
new_netlocr{   r{   r|   rB  G
  s   





z$S3ControlEndpointSetter.set_endpointc                 C   rV  )Nr+  rW  r   r{   r{   r|   r  T
  r   z6S3ControlEndpointSetter._use_endpoint_from_arn_detailsc                 C   rV  )N
outpost_idrW  r   r{   r{   r|   r  W
  r   z5S3ControlEndpointSetter._use_endpoint_from_outpost_idc                 C   s   d|j d d v rt|j d d dd| jdds6|j d d }|| jkr6d	| d
| j d}t|d|j d d }|| jkrOtd| j d| dd| jdrZtddd|j d v rh| | d S d S )NrX  r+  r   r  rY  r  rF  rZ  FzCThe use_arn_region configuration is disabled but received arn for "z(" when the client is configured to use "r  rE  r  r]  z&" partition, but arn provided is for "z;" partition. The client and arn partition must be the same.r  7S3 control client does not support accelerate endpointsr2  )r   r3   r9  r   r8  r4   r<  r  )r   r   
arn_regionr+  request_partionr{   r{   r|   r  Z
  s:   



zES3ControlEndpointSetter._validate_endpoint_from_arn_details_supportedc                 C   s   | j drtddd S )Nr^  zPClient does not support s3 dualstack configuration when an outpost is specified.rE  )r9  r   r4   r   r{   r{   r|   r  y
  s
   z;S3ControlEndpointSetter._validate_outpost_redirection_validc                 C   s2   | j ddr|jd d }| || |S | jS )NrZ  Fr+  r   )r9  r   r   rc  r8  )r   r   r  r{   r{   r|   r  
  s
   z8S3ControlEndpointSetter._resolve_region_from_arn_detailsc                 C   s   |j d d }| || |S )Nr+  r  re  )r   r   arn_servicer{   r{   r|   r  
  s   z>S3ControlEndpointSetter._resolve_signing_name_from_arn_detailsc                 C   s   |  ||}| || d S r   ) _resolve_netloc_from_arn_detailsr  )r   r   r  r  r{   r{   r|   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   r   r   r*  r   r+  r   r  )r   r   r  ri  arn_details_endpointr{   r{   r|   r  
  s   
	
z.S3ControlEndpointSetter._update_request_netlocc                 C   s0   |j d }d|v r| |S |d }| ||S )Nr+  r2  r  )r   r  _construct_s3_control_endpoint)r   r   r  r+  r  r{   r{   r|   r  
  s
   

z8S3ControlEndpointSetter._resolve_netloc_from_arn_detailsc                 C   s   | j |S r   )_HOST_LABEL_REGEXr
  )r   labelr{   r{   r|   _is_valid_host_label
  r1  z,S3ControlEndpointSetter._is_valid_host_labelc                 G   s"   |D ]}|  |st|dqd S )N)r  )r  r'   )r   labelsr  r{   r{   r|   _validate_host_labels
  s
   

z-S3ControlEndpointSetter._validate_host_labelsc                 C   s\   |  || | jrt| jj}||g}n|dg}| | | |}|||g | |S )N
s3-control)r  r;  r   r(  _add_dualstackrr  r  _construct_netloc)r   r  r  ro  r(  r|  r{   r{   r|   r  
  s   



z6S3ControlEndpointSetter._construct_s3_control_endpointc                 C   s@   |  | | jrt| jjS d|| |g}| | | |S r  )r  r;  r   r(  rr  	_add_fipsr  )r   r  r(  r{   r{   r|   r  
  s   


z3S3ControlEndpointSetter._construct_outpost_endpointc                 C   s
   d |S )Nr   )r   r   r(  r{   r{   r|   r  
  r   z)S3ControlEndpointSetter._construct_netlocc                 C   s   | j r|d d |d< d S d S )Nr   rv  rw  r  r{   r{   r|   r  
  s   z!S3ControlEndpointSetter._add_fipsc                 C   s   | j dr|d d S d S )Nr^  r8   )r9  r   r   r  r{   r{   r|   r  
  s   z&S3ControlEndpointSetter._add_dualstackc                 C   r}  r~  r  r  r{   r{   r|   rr  
  r  z'S3ControlEndpointSetter._get_dns_suffixc                 C   r  r  r  r  r{   r{   r|   rc  
  r  z0S3ControlEndpointSetter._override_signing_regionc                 C   r  r  r  )r   r   r  r  r{   r{   r|   rJ  
  r  z.S3ControlEndpointSetter._override_signing_namec                 C   s,   |j d }|d}|r| || d S d S )Nr+  r2  )r   r   _add_outpost_id_header)r   r   r+  r2  r{   r{   r|   r  
  s
   

z5S3ControlEndpointSetter._add_headers_from_arn_detailsc                 C   s   ||j d< d S )Nzx-amz-outpost-id)r   )r   r   r2  r{   r{   r|   r  
  s   z.S3ControlEndpointSetter._add_outpost_id_headerr  ) r   r   r   r=  rz  r   r  r  r   r  rB  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rr  rc  rJ  r  r  r{   r{   r{   r|   r  +
  sB    

					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 u 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  r{   r{   r|   r     s   
z!S3ControlArnParamHandler.__init__c                 C   r  )Nz!before-parameter-build.s3-controlr  r!  r{   r{   r|   r       z!S3ControlArnParamHandler.registerc                 K   s<   |j dv r| ||| d S | ||| | ||| d S )N)rS  ListRegionalBuckets)r   _handle_outpost_id_param_handle_name_param_handle_bucket_param)r   r  r*  r   r  r{   r{   r|   r     s   
z#S3ControlArnParamHandler.handle_arnc                 C   sR   ||vrd S z|| }| j |}||d< | ||d< |W S  ty(   Y d S w )Nr  	resources)r  r  _split_resourcer
  )r   r  rZ  r  r+  r{   r{   r|   _get_arn_details_from_param#  s   z4S3ControlArnParamHandler._get_arn_details_from_paramc                 C   s   | j |d S )Nr  )_RESOURCE_SPLIT_REGEXr   )r   r+  r{   r{   r|   r  /     z(S3ControlArnParamHandler._split_resourcec                 C   sF   |d }d|v r|d |krd |d }t|d |d||d< d S )Nr  	AccountIdzGAccount ID in arn does not match the AccountId parameter provided: "{}"r  r  )rK  r3   )r   r  r+  
account_idr+  r{   r{   r|   _override_account_id_param2  s   
z3S3ControlArnParamHandler._override_account_id_paramc                 C   s   d|vrd S |d |d< d S )N	OutpostIdr  r{   )r   r  r*  r   r{   r{   r|   r  ?  s   z1S3ControlArnParamHandler._handle_outpost_id_paramc                 C   sV   |j dkrd S | |d}|d u rd S | |r!| ||| d S d}t|d |dNCreateAccessPointNamez4The Name parameter does not support the provided ARNr  r  )r   r  _is_outpost_accesspoint_store_outpost_accesspointr3   r   r  r*  r   r+  r+  r{   r{   r|   r  D  s   

z+S3ControlArnParamHandler._handle_name_paramc                 C   @   |d dkrdS |d }t |dkrdS |d dko|d d	kS )
Nr  rp  Fr  r  r   r%  r%  r$  r   r   r+  r  r{   r{   r|   r  T     z0S3ControlArnParamHandler._is_outpost_accesspointc                 C   D   |  || |d d }||d< ||d< |d d |d< ||d< d S )Nr  r  r  r1  r5   r2  r+  r  )r   r  r   r+  r1  r{   r{   r|   r  ]     z3S3ControlArnParamHandler._store_outpost_accesspointc                 C   sH   |  |d}|d u rd S | |r| ||| d S d}t|d |dNr  z6The Bucket parameter does not support the provided ARNr  r  )r  _is_outpost_bucket_store_outpost_bucketr3   r  r{   r{   r|   r  e  s   
z-S3ControlArnParamHandler._handle_bucket_paramc                 C   r  )
Nr  rp  Fr  r  r   r%  r%  r  r  r  r{   r{   r|   r  t  r  z+S3ControlArnParamHandler._is_outpost_bucketc                 C   r  )Nr  r  r  r  r5   r2  r+  r  )r   r  r   r+  r  r{   r{   r|   r  }  r  z.S3ControlArnParamHandler._store_outpost_bucketr   )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    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   r  r   r  r  r{   r{   r|   r     r  z#S3ControlArnParamHandlerv2.__init__c                 C   r  )Nz%before-endpoint-resolution.s3-controlr  r!  r{   r{   r|   r    r  z#S3ControlArnParamHandlerv2.registerc                 C   sj   |j dkrd S | |d}|d u rd S | | | | | |r+| ||| d S d}t|d |dr  )r   r  _raise_for_fips_pseudo_region_raise_for_accelerate_endpointr  r  r3   r  r{   r{   r|   r    s   



z-S3ControlArnParamHandlerv2._handle_name_paramc                 C      |  || d S r   r  r0  r{   r{   r|   r    r  z5S3ControlArnParamHandlerv2._store_outpost_accesspointc                 C   s\   |  |d}|d u rd S | | | | | |r$| ||| d S d}t|d |dr  )r  r  r  r  r  r3   r  r{   r{   r|   r    s   


z/S3ControlArnParamHandlerv2._handle_bucket_paramc                 C   r  r   r  r0  r{   r{   r|   r    r  z0S3ControlArnParamHandlerv2._store_outpost_bucketc                 C   s0   |d }| ds|drt|d ddd S )Nr   zfips-r  rY  r  )r  r  r3   )r   r+  r  r{   r{   r|   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  rE  )r  r   r4   )r   r   r@  r{   r{   r|   r    s   
z9S3ControlArnParamHandlerv2._raise_for_accelerate_endpointr   )r   r   r   r   r   r  r  r  r  r  r  r  r{   r{   r{   r|   r    s    
	r  c                   @   s|   e Zd ZdZdZdZdZed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 ZdddZdd Zdd ZdS )ContainerMetadataFetcherr%  r  r5   z169.254.170.2z169.254.170.23zfd00:ec2::23	localhostNc                 C   s(   |d u rt jj| jd}|| _|| _d S )N)r   )r   r   r   TIMEOUT_SECONDSr   _sleep)r   r   sleepr{   r{   r|   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   r{   r{   r|   retrieve_full_uri  s   
	z*ContainerMetadataFetcher.retrieve_full_uric                 C   sN   t j|}| |jrd S | |j}|s%td|j dd| j d S )NzUnsupported host 'zN'.  Can only retrieve metadata from a loopback address or one of these hosts: z, )	r   compatr   _is_loopback_addressr	  _check_if_whitelisted_hostrT  r   _ALLOWED_HOSTS)r   r  parsedis_whitelisted_hostr{   r{   r|   r    s   

z.ContainerMetadataFetcher._validate_allowed_urlc                 C   s&   zt |}|jW S  ty   Y dS w )NF)r   is_loopbackrT  )r   r	  ipr{   r{   r|   r    s   z-ContainerMetadataFetcher._is_loopback_addressc                 C   s   || j v rdS dS )NTF)r  )r   r.  r{   r{   r|   r    s   
z3ContainerMetadataFetcher._check_if_whitelisted_hostc                 C   s   |  |}| |S )zRetrieve JSON metadata from container 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  r{   r{   r|   retrieve_uri
  s   
	
z%ContainerMetadataFetcher.retrieve_uric              
   C   s   ddi}|d ur| | d}	 z	| ||| jW S  tyC } ztjd|dd | | j |d7 }|| jkr9 W Y d }~nd }~ww q)NAcceptzapplication/jsonr   TzAReceived error when attempting to retrieve container metadata: %sr  r5   )	r  _get_responser  r+   r   r  r  
SLEEP_TIMERETRY_ATTEMPTS)r   r  extra_headersr   attemptsr   r{   r{   r|   r    s.   

z.ContainerMetadataFetcher._retrieve_credentialsc           
   
   C   s   zGt jj}|d||d}| j| }|jd}|jdkr+t	d|j d| dzt
|W W S  tyG   d}td	|| t	|dw  ty] }	 z
d
|	 }t	|dd }	~	ww )Nr"  r  r  r  zReceived non 200 response z from container metadata: r*  z>Unable to parse JSON returned from container metadata servicesz%s:%sz?Received error when attempting to retrieve container metadata: )r   r   r  r   r  r  r5  r  r  r+   r   rR  rT  r   r  r  )
r   r  r   r   r  r   r  response_textr+  r   r{   r{   r|   r  ,  s4   



z&ContainerMetadataFetcher._get_responsec                 C   s   d| j  | S )Nzhttp://)
IP_ADDRESS)r   r  r{   r{   r|   r  F  r  z!ContainerMetadataFetcher.full_urlr   )r   r   r   r  r  r  r  r  timer  r   r  r  r  r  r  r  r  r  r{   r{   r{   r|   r    s&    

r  c                 C   s   t | ri S t S r   )should_bypass_proxiesr   r   r{   r{   r|   r   J  s   r   c              	   C   s6   zt t| jrW dS W dS  ttjfy   Y dS w )z:
    Returns whether we should bypass proxies or not.
    TF)r   r   r(  r  socketgaierrorr  r{   r{   r|   r  Q  s   r  c              	   C   s   | sdS zt | W S  ttfy   Y nw t| drCt| drCz|  }| dd |  }| | || W S  tjyB   Y d S w d S )Nr   seektellr%  )r   AttributeErrorr  r   r  r  ioUnsupportedOperation)r  orig_posend_file_posr{   r{   r|   determine_content_lengthf  s&   


r  
ISO-8859-1c                 C   sJ   |  d}|s	dS tj }||d< |d}|dur|S d|v r#|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  )r   rU   messagerE  	get_param)r   defaultcontent_typer  r  r{   r{   r|   get_encoding_from_headers  s   


r
  c                 K   s0   t | ttfrt| }nt| }t|dS )Nr  )ru   r  	bytearray_calculate_md5_from_bytes_calculate_md5_from_filebase64	b64encoder  )r  r  
binary_md5r{   r{   r|   calculate_md5  s   
r  c                 C   s   t | }| S r   )r   r  )
body_bytesmd5r{   r{   r|   r    s   r  c                    sB      }t }t fdddD ]}|| q | | S )Nc                      r  r  r  r{   fileobjr{   r|   r    r  z*_calculate_md5_from_file.<locals>.<lambda>r  )r  r   r  r  r  r  )r  start_positionr  r  r{   r  r|   r    s   
r  c                 C   s"   |  di  di }| ddkS )Nr   r  r  r  r   )r  r  r{   r{   r|   _is_s3express_request  s   r  c                 C   s2   | d }d|v r
dS |D ]
}t |r dS qdS )Nr   Content-MD5TF)CHECKSUM_HEADER_PATTERNr
  )r  r   r   r{   r{   r|   _has_checksum_header  s   
r  c                 K   s0   t | st| fi | t| fi | d S d S r   )r  conditionally_calculate_md5conditionally_enable_crc32)r  r  r{   r{   r|    conditionally_calculate_checksum  s   r  c                 K   sb   |  di  di }| d}t| r+| d d ur-|dv r/ddddd	i| d d< d S d S d S d S )
Nr   r  request_algorithmr  )Nconditional-md5crc32r   zx-amz-checksum-crc32)	algorithminr   )r   r  )r  r  checksum_contextchecksum_algorithmr{   r{   r|   r    s   
r  c                 K   s   | d }|  di  di }| d}|r|dkrdS t| r!dS t| r'dS tr=|dur?t|fi |}|| d d< dS dS dS )	z1Only add a Content-MD5 if the system supports it.r  r   r  r  r  Nr   r  )r   r  r  r   r  )r  r  r  r#  r$  
md5_digestr{   r{   r|   r    s   
r  c                   @   s    e Zd ZefddZdd ZdS )FileWebIdentityTokenLoaderc                 C   ro  r   )_web_identity_token_pathr  )r   web_identity_token_pathr  r{   r{   r|   r     rv  z#FileWebIdentityTokenLoader.__init__c                 C   s8   |  | j}| W  d    S 1 sw   Y  d S r   )r  r'  r  )r   
token_filer{   r{   r|   __call__  s   $z#FileWebIdentityTokenLoader.__call__N)r   r   r   openr   r*  r{   r{   r{   r|   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 u ri }|| _ d S r   )r  )r   r  r{   r{   r|   r        
zSSOTokenLoader.__init__c                 C   s$   |}|d ur|}t |d S )Nr  )r  sha1r  r  )r   	start_urlsession_namer  r{   r{   r|   _generate_cache_key  s   z"SSOTokenLoader._generate_cache_keyc                 C   s   |  ||}|| j|< d S r   )r1  r  )r   r/  r(  r0  r6  r{   r{   r|   
save_token
  s   zSSOTokenLoader.save_tokenc                 C   s   |  ||}td|  || jvr&|}|d ur|}d| d}t|d| j| }d|vs3d|vr>d| d}t|d|S )NzChecking for cached token at: z
Token for z does not existr*  accessToken	expiresAtz is invalid)r1  r   r  r  r.   )r   r/  r0  r6  r   r+  r(  r{   r{   r|   r*    s   



zSSOTokenLoader.__call__r   )r   r   r   r   r1  r2  r*  r{   r{   r{   r|   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 )EventbridgeSignerSetterr5  rQ  Nc                 C   s   || _ || _|| _d S r   )r   r8  r;  )r   r?  r   r  r{   r{   r|   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!  r{   r{   r|   r  (  s   z EventbridgeSignerSetter.registerc                 K   s:   d|v r|d }t d|d  d|  ||d< d S d S )Neventbridge_endpointzRewriting URL from r   ra  )r   r  r  r{   r{   r|   r7  1  s
   z(EventbridgeSignerSetter.set_endpoint_urlc           	      K   s   | d}|d u rd S t|dkrtddtstdd| d}d }|d ur6|jr0tdd|jr6dg}| jd u rTtd	| }|j	|krLtd
d| j
||d}n| j}||d< d|d< d S )N
EndpointIdr   z+EndpointId must not be a zero length stringrE  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.r8   https://z-EndpointId is not a valid hostname component.endpoint_variant_tagsr8  v4ar  )r   r   r%   r   r,   rA  r^  r;  r   r	  _get_global_endpoint)	r   r  r   r  r   rC   r<  r  resolved_endpointr{   r{   r|   r6  7  s@   



z1EventbridgeSignerSetter.check_for_global_endpointc                 C   sN   | j }|| j}|d u r| j}|j||d}|d u r| j}d| d| dS )Nr;  r:  z.endpoint.events.r   )r   get_partition_for_regionr8  r=  ry  rz  )r   r   r<  rL  r  r|  r{   r{   r|   r>  c  s   z,EventbridgeSignerSetter._get_global_endpointr|  r   )
r   r   r   r=  rz  r   r  r7  r6  r>  r{   r{   r{   r|   r5    s    
	,r5  c                 C   s   | du rdS t | }|jdr|jdvrdS |jd}|d dkr%dS |dd	 }t|tt|kr7d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.
    NFrQ  )httpshttpr   r   r  r5   r  c                 s   r  r   rM  rO  r{   r{   r|   r    r  z'is_s3_accelerate_url.<locals>.<genexpr>)r   r(  r  r*  r   r   r  r  )r   	url_partsr  r  r{   r{   r|   is_s3_accelerate_urlr  s   
rD  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.awsbotor  Nc                 C   s   || _ |d u r
| j}|| _d S r   )_working_dir_default_dumps_dumps)r   working_dir
dumps_funcr{   r{   r|   r     s   
zJSONFileCache.__init__c                 C   s   t j|| jdS )N)r  )r   dumpsr  )r   r  r{   r{   r|   rI    r  zJSONFileCache._default_dumpsc                 C   s   |  |}tj|S r   )_convert_cache_keyr   r   isfile)r   r6  
actual_keyr{   r{   r|   __contains__  s   
zJSONFileCache.__contains__c              	   C   s`   |  |}zt|}t|W  d   W S 1 sw   Y  W dS  ttfy/   t|w )z Retrieve value from a cache key.N)rN  r+  r   loadr  rT  KeyError)r   r6  rP  r  r{   r{   r|   __getitem__  s   

(zJSONFileCache.__getitem__c                 C   s8   |  |}zt|}|  W d S  ty   t|w r   )rN  r   unlinkFileNotFoundErrorrS  )r   r6  rP  key_pathr{   r{   r|   __delitem__  s   
zJSONFileCache.__delitem__c              	   C   s   |  |}z| |}W n ttfy   td| w tj| js*t| j t	t
|tjtjB dd}|  || W d    d S 1 sNw   Y  d S )Nz3Value cannot be cached, must be JSON serializable: i  w)rN  rJ  r  rT  r   r   isdirrH  makedirsfdopenr+  O_WRONLYO_CREATtruncatewrite)r   r6  r   full_keyfile_contentr  r{   r{   r|   __setitem__  s&   
"zJSONFileCache.__setitem__c                 C   s   t j| j|d }|S )Nz.json)r   r   r   rH  )r   r6  	full_pathr{   r{   r|   rN    s   z JSONFileCache._convert_cache_keyFc                 C   r  r  r  r  r{   r{   r|   r    r  z"JSONFileCache._serialize_if_neededr9  )r   r   r   r   r   r   
expanduserr   	CACHE_DIRr   rI  rQ  rT  rX  rc  rN  r  r{   r{   r{   r|   rE    s    	rE  c                 C   s   | d u rdS |  dS )NFz--x-s3)r  r  r{   r{   r|   is_s3express_bucket  r-  rg  appmeshzapp-meshzds-datazdirectory-service-dataglobalacceleratorzglobal-acceleratorziotevents-dataziot-events-data	ioteventsz
iot-eventsiotwirelessziot-wirelesskinesisanalyticsv2zkinesis-analytics-v2zlexv2-modelszlex-models-v2zlexv2-runtimezlex-runtime-v2z	sms-voicezpinpoint-sms-voice	s3controlr  zservice-catalog-appregistry)rh   rj   rk   rm   zservicecatalog-appregistryrp   rq   )Tr9  r   )r  )r  r  r   email.messagerU   r=  r  r  loggingr   r^  r   r  r  r  rC  r  	ipaddressr   pathlibr   urllib.requestr   r   dateutil.parserr  dateutil.tzr   urllib3.exceptionsr   r   botocore.awsrequestbotocore.httpsessionbotocore.compatr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   botocore.exceptionsr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   	getLoggerr   r   r:  r   r  r   
SAFE_CHARSr  r  r  rN  EVENT_ALIASESr  r  r}   r   r   r   r   r   r   r   r   r   r   	Exceptionr   r   r   r;  rl  rx  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?  rL  rX  r]  rV  r^  rf  rl  rm  r  r  r  r  rT  r
  r  r  r4  r  r  r  r  r   r  r  r
  r  r  r  r  r  r  r  r  r&  r,  r5  rD  rE  rg  SERVICE_NAME_ALIASES.CLIENT_NAME_TO_HYPHENIZED_SERVICE_ID_OVERRIDESr{   r{   r{   r|   <module>   s  @d

	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEL
	 D B&	&0-!d
@!")0 D 3U    W Mw	
!S!E	
 !"#$%&'()*+,-./012345