o
    9_Wc|                     @   s  d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZ dd	lmZm Z  e!e"Z#d
Z$di iZ%G dd dZ&G dd de&Z'G dd de(eZ)G dd dZ*dS )zResolves regions and endpoints.

This module implements endpoint resolution, including resolving endpoints for a
given service and region and resolving the available endpoints for a service
in a specific AWS partition.
    N)Enum)UNSIGNED
xform_name)AUTH_TYPE_MAPSHAS_CRTCRT_SUPPORTED_AUTH_TYPES)EndpointProvider)EndpointProviderErrorEndpointVariantErrorInvalidHostLabelErrorMissingDependencyExceptionNoRegionErrorParamValidationError$UnknownEndpointResolutionBuiltInNameUnknownRegionErrorUnknownSignatureVersionError*UnsupportedS3AccesspointConfigurationErrorUnsupportedS3ConfigurationErrorUnsupportedS3ControlArnError&UnsupportedS3ControlConfigurationError)ensure_booleaninstance_cachez{service}.{region}.{dnsSuffix}	endpointsc                   @   s.   e Zd ZdZdddZdd Z	dd	d
ZdS )BaseEndpointResolverz3Resolves regions and endpoints. Must be subclassed.Nc                 C      t )a7  Resolves an endpoint for a service and region combination.

        :type service_name: string
        :param service_name: Name of the service to resolve an endpoint for
            (e.g., s3)

        :type region_name: string
        :param region_name: Region/endpoint name to resolve (e.g., us-east-1)
            if no region is provided, the first found partition-wide endpoint
            will be used if available.

        :rtype: dict
        :return: Returns a dict containing the following keys:
            - partition: (string, required) Resolved partition name
            - endpointName: (string, required) Resolved endpoint name
            - hostname: (string, required) Hostname to use for this endpoint
            - sslCommonName: (string) sslCommonName to use for this endpoint.
            - credentialScope: (dict) Signature version 4 credential scope
              - region: (string) region name override when signing.
              - service: (string) service name override when signing.
            - signatureVersions: (list<string>) A list of possible signature
              versions, including s3, v4, v2, and s3v4
            - protocols: (list<string>) A list of supported protocols
              (e.g., http, https)
            - ...: Other keys may be included as well based on the metadata
        NotImplementedError)selfservice_nameregion_name r!   2D:\Flask\env\Lib\site-packages\botocore/regions.pyconstruct_endpoint5   s   z'BaseEndpointResolver.construct_endpointc                 C   r   )zLists the partitions available to the endpoint resolver.

        :return: Returns a list of partition names (e.g., ["aws", "aws-cn"]).
        r   r   r!   r!   r"   get_available_partitionsR   s   z-BaseEndpointResolver.get_available_partitionsawsFc                 C   r   )a  Lists the endpoint names of a particular partition.

        :type service_name: string
        :param service_name: Name of a service to list endpoint for (e.g., s3)

        :type partition_name: string
        :param partition_name: Name of the partition to limit endpoints to.
            (e.g., aws for the public AWS endpoints, aws-cn for AWS China
            endpoints, aws-us-gov for AWS GovCloud (US) Endpoints, etc.

        :type allow_non_regional: bool
        :param allow_non_regional: Set to True to include endpoints that are
             not regional endpoints (e.g., s3-external-1,
             fips-us-gov-west-1, etc).
        :return: Returns a list of endpoint names (e.g., ["us-east-1"]).
        r   )r   r   partition_nameallow_non_regionalr!   r!   r"   get_available_endpointsY   s   z,BaseEndpointResolver.get_available_endpointsN)r&   F)__name__
__module____qualname____doc__r#   r%   r)   r!   r!   r!   r"   r   2   s    
r   c                   @   s   e Zd ZdZddgZd%ddZd&dd	Zd
d Z			d'ddZ	d(ddZ					d)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$ ZdS )*EndpointResolverz7Resolves endpoints based on partition endpoint metadatazaws-isoz	aws-iso-bFc                 C   s    d|vrt d|| _|| _dS )a  
        :type endpoint_data: dict
        :param endpoint_data: A dict of partition data.

        :type uses_builtin_data: boolean
        :param uses_builtin_data: Whether the endpoint data originates in the
            package's data directory.
        
partitionsz%Missing "partitions" in endpoint dataN)
ValueError_endpoint_datauses_builtin_data)r   endpoint_datar3   r!   r!   r"   __init__t   s   	
zEndpointResolver.__init__r&   c                 C   sB   | j d D ]}|d |krq|d }||vrq|| d   S d S )Nr0   	partitionservicesr   )r2   )r   r   r'   r6   r7   r!   r!   r"   get_service_endpoints_data   s   z+EndpointResolver.get_service_endpoints_datac                 C   s&   g }| j d D ]	}||d  q|S )Nr0   r6   )r2   append)r   resultr6   r!   r!   r"   r%      s   z)EndpointResolver.get_available_partitionsNc                 C   s   g }| j d D ]@}|d |krq|d }||vrq|| d }|D ]%}	|	|d v }
|r=|
r=| ||	 |}|r<||	 q!|sA|
rF||	 q!q|S )Nr0   r6   r7   r   regions)r2   _retrieve_variant_datar9   )r   r   r'   r(   endpoint_variant_tagsr:   r6   r7   Zservice_endpointsendpoint_nameZis_regional_endpointZvariant_datar!   r!   r"   r)      s,   

z(EndpointResolver.get_available_endpointsc                 C   s\   | j d D ]&}|d |kr+|r%| |d|}|r$d|v r$|d   S q|d   S qd S )Nr0   r6   defaults	dnsSuffix)r2   r<   get)r   r'   r=   r6   variantr!   r!   r"   get_partition_dns_suffix   s   
	z)EndpointResolver.get_partition_dns_suffixc           	      C   s   |dkr|r|d u rd}|d ur4d }| j d D ]
}|d |kr!|}q|d ur2| |||||d}|S d S | j d D ]}|rE|d | jv rEq9| |||||}|rT|  S q9d S )Ns3z	us-east-1r0   r6   T)r2   _endpoint_for_partition!_UNSUPPORTED_DUALSTACK_PARTITIONS)	r   r   r    r'   use_dualstack_endpointuse_fips_endpointZvalid_partitionr6   r:   r!   r!   r"   r#      sT   	z#EndpointResolver.construct_endpointc                 C   s4   | j d D ]}| ||r|d   S qt|dd)Nr0   r6   z,No partition found for provided region_name.)r    	error_msg)r2   _region_matchr   )r   r    r6   r!   r!   r"   get_partition_for_region   s   z)EndpointResolver.get_partition_for_regionc                 C   s  |d }|r|| j v rd| }tdg|d|d |t}	|d u r.d|	v r+|	d }nt |||	|||d}
||	d v rE| jdi |
S | ||sM|r|	d}|	d	d
}|rp|sptd||| ||
d< | jdi |
S td|| | jdi |
S d S )Nr6   z@Dualstack endpoints are currently not supported for %s partition	dualstacktagsrI   r7   ZpartitionEndpoint)r6   r   service_datar>   rG   rH   r   ZisRegionalizedTz'Using partition endpoint for %s, %s: %sr>   z*Creating a regex based endpoint for %s, %sr!   )	rF   r   rA   DEFAULT_SERVICE_DATAr   _resolverJ   LOGdebug)r   r6   r   r    rG   rH   Zforce_partitionr'   rI   rO   Zresolve_kwargsZpartition_endpointZis_regionalizedr!   r!   r"   rE      sZ   	


z(EndpointResolver._endpoint_for_partitionc                 C   s0   ||d v rdS d|v rt |d |S dS )Nr;   TZregionRegexF)recompilematch)r   r6   r    r!   r!   r"   rJ   8  s
   zEndpointResolver._region_matchc                 C   s>   | dg }|D ]}t|d t|kr| }|  S qd S )NvariantsrN   )rA   setcopy)r   r4   rN   rW   rB   r:   r!   r!   r"   r<   ?  s   z'EndpointResolver._retrieve_variant_datac                 C   s$   g }|r	| d |r| d |S )NrL   Zfips)r9   )r   rG   rH   rN   r!   r!   r"   _create_tag_listF  s   

z!EndpointResolver._create_tag_listc                 C   s4   i }|||fD ]}|  ||}|r| || q|S r*   )r<   _merge_keys)r   rN   r4   service_defaultspartition_defaultsr:   rW   rB   r!   r!   r"   _resolve_variantN  s   z!EndpointResolver._resolve_variantc                 C   s"  | di  |i }| drtd|  | di }| di }	| ||}
|
rK| |
|||	}|i krDd| d| }t|
|d| || n|}d|vrW|d |d< |d	 |d	< ||d
< | || | |	| | ||d |||d |d< d|v r| ||d |||d |d< |S )Nr   
deprecatedz5Client is configured with the deprecated endpoint: %sr?   zEndpoint does not exist for z in region rM   r@   r6   ZendpointNamehostnameZsslCommonName)rA   rR   warningrZ   r^   r   r[   _expand_template)r   r6   r   rO   r>   rG   rH   r4   r\   r]   rN   r:   rI   r!   r!   r"   rQ   X  s\   	
zEndpointResolver._resolvec                 C   s"   |D ]}||vr|| ||< qd S r*   r!   )r   	from_datar:   keyr!   r!   r"   r[     s
   zEndpointResolver._merge_keysc                 C   s   |j |||dS )N)Zserviceregionr@   )format)r   r6   templater   r>   r@   r!   r!   r"   rb     s   z!EndpointResolver._expand_template)F)r&   )r&   FNr*   )NNFF)r+   r,   r-   r.   rF   r5   r8   r%   r)   rC   r#   rK   rE   rJ   r<   rZ   r^   rQ   r[   rb   r!   r!   r!   r"   r/   o   s6    

		


2
A
Br/   c                   @   s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdS )EndpointResolverBuiltinszAWS::RegionzAWS::UseFIPSzAWS::UseDualStackzAWS::STS::UseGlobalEndpointzAWS::S3::UseGlobalEndpointzAWS::S3::AcceleratezAWS::S3::ForcePathStylezAWS::S3::UseArnRegionzAWS::S3Control::UseArnRegionz'AWS::S3::DisableMultiRegionAccessPointszSDK::EndpointN)r+   r,   r-   Z
AWS_REGIONZAWS_USE_FIPSZAWS_USE_DUALSTACKZAWS_STS_USE_GLOBAL_ENDPOINTZAWS_S3_USE_GLOBAL_ENDPOINTZAWS_S3_ACCELERATEZAWS_S3_FORCE_PATH_STYLEZAWS_S3_USE_ARN_REGIONZAWS_S3CONTROL_USE_ARN_REGIONZAWS_S3_DISABLE_MRAPZSDK_ENDPOINTr!   r!   r!   r"   rh     s    rh   c                   @   s   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edd Zedd Zedd Zdd Zdd Zdd Zd d! Zd"d# ZdS )%EndpointRulesetResolverz5Resolves endpoints using a service's endpoint rulesetTNc	           	      C   sH   t ||d| _| jjj| _|| _|| _|| _|| _|| _	|| _
i | _d S )N)Zruleset_datapartition_data)r	   	_providerZruleset
parameters_param_definitions_service_model	_builtins_client_context_event_emitter_use_ssl_requested_auth_schemeZ_instance_cache)	r   Zendpoint_ruleset_datarj   Zservice_modelbuiltinsZclient_contextZevent_emitterZuse_sslZrequested_auth_schemer!   r!   r"   r5     s   
z EndpointRulesetResolver.__init__c              
   C   s   |du ri }|du ri }|  |||}td|  z| jjdi |}W n ty? } z| ||}|du r8 ||d}~ww td|j  | js_|j	dr_|j
d|jdd  d}|j
dd	 |j D d
}|S )zAInvokes the provider with params defined in the service's rulesetNz-Calling endpoint provider with parameters: %szEndpoint provider result: %szhttps://zhttp://   )urlc                 S   s   i | ]	\}}||d  qS )r   r!   ).0rd   valr!   r!   r"   
<dictcomp>  s    z>EndpointRulesetResolver.construct_endpoint.<locals>.<dictcomp>)headersr!   )_get_provider_paramsrR   rS   rk   Zresolve_endpointr
   #ruleset_error_to_botocore_exceptionrv   rr   
startswith_replacerz   items)r   operation_model	call_argsrequest_contextprovider_paramsZprovider_resultexZbotocore_exceptionr!   r!   r"   r#     sD   

z*EndpointRulesetResolver.construct_endpointc           	      C   sl   i }|  |||}| j D ]%\}}| j|||d}|du r+|jdur+| j|j|d}|dur3|||< q|S )a  Resolve a value for each parameter defined in the service's ruleset

        The resolution order for parameter values is:
        1. Operation-specific static context values from the service definition
        2. Operation-specific dynamic context values from API parameters
        3. Client-specific context parameters
        4. Built-in values such as region, FIPS usage, ...
        )
param_namer   r   N)builtin_namert   )_get_customized_builtinsrm   r   _resolve_param_from_contextbuiltin_resolve_param_as_builtin)	r   r   r   r   r   customized_builtinsr   Z	param_defZ	param_valr!   r!   r"   r{     s&   z,EndpointRulesetResolver._get_provider_paramsc                 C   s<   |  ||}|d ur|S | |||}|d ur|S | |S r*   )&_resolve_param_as_static_context_param'_resolve_param_as_dynamic_context_param&_resolve_param_as_client_context_param)r   r   r   r   ZstaticZdynamicr!   r!   r"   r   4  s   
z3EndpointRulesetResolver._resolve_param_from_contextc                 C   s   |  |}||S r*   )_get_static_context_paramsrA   )r   r   r   Zstatic_ctx_paramsr!   r!   r"   r   C  s   

z>EndpointRulesetResolver._resolve_param_as_static_context_paramc                 C   s(   |  |}||v r|| }||S d S r*   )_get_dynamic_context_paramsrA   )r   r   r   r   Zdynamic_ctx_paramsmember_namer!   r!   r"   r   I  s
   

z?EndpointRulesetResolver._resolve_param_as_dynamic_context_paramc                 C   s(   |   }||v r|| }| j|S d S r*   )_get_client_context_paramsrp   rA   )r   r   Zclient_ctx_paramsZclient_ctx_varnamer!   r!   r"   r   Q  s
   z>EndpointRulesetResolver._resolve_param_as_client_context_paramc                 C   s"   |t j vrt|d||S )Nname)rh   __members__valuesr   rA   )r   r   rt   r!   r!   r"   r   W  s   

z1EndpointRulesetResolver._resolve_param_as_builtinc                 C      dd |j D S )z=Mapping of param names to static param value for an operationc                 S      i | ]}|j |jqS r!   )r   valuerw   paramr!   r!   r"   ry   _      zFEndpointRulesetResolver._get_static_context_params.<locals>.<dictcomp>)Zstatic_context_parametersr   r   r!   r!   r"   r   \     z2EndpointRulesetResolver._get_static_context_paramsc                 C   r   )z7Mapping of param names to member names for an operationc                 S   r   r!   )r   r   r   r!   r!   r"   ry   g  r   zGEndpointRulesetResolver._get_dynamic_context_params.<locals>.<dictcomp>)Zcontext_parametersr   r!   r!   r"   r   d  r   z3EndpointRulesetResolver._get_dynamic_context_paramsc                 C   s   dd | j jD S )z7Mapping of param names to client configuration variablec                 S   s   i | ]	}|j t|j qS r!   )r   r   r   r!   r!   r"   ry   o  s    zFEndpointRulesetResolver._get_client_context_params.<locals>.<dictcomp>)rn   Zclient_context_parametersr$   r!   r!   r"   r   l  s   z2EndpointRulesetResolver._get_client_context_paramsc                 C   s6   | j j }t| j}| jjd| ||||d |S )Nzbefore-endpoint-resolution.%s)rt   modelparamscontext)rn   
service_idZ	hyphenizerY   ro   rq   emit)r   r   r   r   r   r   r!   r!   r"   r   t  s   z0EndpointRulesetResolver._get_customized_builtinsc                    s  t |trt|dkrtdtdddd |D  j  jtkr(di fS  fdd|D } jd	urSzt	 fd
d|D \}}W nE t
yR   d	i f Y S w zt	dd |D \}}W n* t
y   d}dd |D }ts{tdd |D }|rtddtd|dw i }d|v r|d |d< nd|v rt|d dkr|d d |d< d|v r|j|d d d|v rt|d |d< td|d || ||fS )a  Convert an Endpoint's authSchemes property to a signing_context dict

        :type auth_schemes: list
        :param auth_schemes: A list of dictionaries taken from the
            ``authSchemes`` property of an Endpoint object returned by
            ``EndpointProvider``.

        :rtype: str, dict
        :return: Tuple of auth type string (to be used in
            ``request_context['auth_type']``) and signing context dict (for use
            in ``request_context['signing']``).
        r   z&auth_schemes must be a non-empty list.z_Selecting from endpoint provider's list of auth schemes: %s. User selected auth scheme is: "%s"z, c                 S   s   g | ]}d | d d qS )"r   )rA   rw   sr!   r!   r"   
<listcomp>  s    zGEndpointRulesetResolver.auth_schemes_to_signing_ctx.<locals>.<listcomp>nonec                    s&   g | ]}i |d   |d  iqS r   )_strip_sig_prefixrw   schemer$   r!   r"   r     s    Nc                 3   s,    | ]}   j|d  r j|fV  qdS r   N)._does_botocore_authname_match_ruleset_authnamers   r   r$   r!   r"   	<genexpr>  s    

zFEndpointRulesetResolver.auth_schemes_to_signing_ctx.<locals>.<genexpr>c                 s   s(    | ]}|d  t v r|d  |fV  qdS r   )r   r   r!   r!   r"   r     s    


Fc                 S   s   g | ]}|d  qS r   r!   r   r!   r!   r"   r     s    c                 s   s    | ]}|t v V  qd S r*   r   r   r!   r!   r"   r     s
    
zbThis operation requires an additional dependency. Use pip install botocore[crt] before proceeding.msg)Zsignature_versionZsigningRegionre   ZsigningRegionSetZsigningName)Zsigning_nameZdisableDoubleEncodingz?Selected auth type "%s" as "%s" with signing context params: %sr   )
isinstancelistlen	TypeErrorrR   rS   joinrs   r   nextStopIterationr   anyr   r   updater   )r   Zauth_schemesr   r   Zfixable_with_crtZauth_type_optionsZsigning_contextr!   r$   r"   auth_schemes_to_signing_ctx  st   


z3EndpointRulesetResolver.auth_schemes_to_signing_ctxc                 C   s   | dr|dd S |S )z6Normalize auth type names by removing any "sig" prefixsig   N)r}   )r   Z	auth_namer!   r!   r"   r     s   z)EndpointRulesetResolver._strip_sig_prefixc                 C   s>   |  |}|dd }|dkr|dr|dd }||kS )a\  
        Whether a valid string provided as signature_version parameter for
        client construction refers to the same auth methods as a string
        returned by the endpoint ruleset provider. This accounts for:

        * The ruleset prefixes auth names with "sig"
        * The s3 and s3control rulesets don't distinguish between v4[a] and
          s3v4[a] signers
        * The v2, v3, and HMAC v1 based signers (s3, s3-*) are botocore legacy
          features and do not exist in the rulesets
        * Only characters up to the first dash are considered

        Example matches:
        * v4, sigv4
        * v4, v4
        * s3v4, sigv4
        * s3v7, sigv7 (hypothetical example)
        * s3v4a, sigv4a
        * s3v4-query, sigv4

        Example mismatches:
        * v4a, sigv4
        * s3, sigv4
        * s3-presign-post, sigv4
        -r   rD      N)r   splitr}   )r   ZbotonameZrsnamer!   r!   r"   r     s
   
zFEndpointRulesetResolver._does_botocore_authname_match_ruleset_authnamec                 C   s:  |j d}|du rdS |dr+z	|dd }W n ty%   |}Y nw t|dS | jj}|dkro|dks;|d	kr@t|d
S |ds^|ds^|ds^|ds^|ds^|drct	|d
S |
 drot|dS |dkr|dr|d}t||dS |ds|drt|d
S |dkrt|dS dS )zAttempts to translate ruleset errors to pre-existing botocore
        exception types by string matching exception strings.
        r   NzInvalid region in ARN: `   )labelrD   z/S3 Object Lambda does not support S3 Acceleratez#Accelerate cannot be used with FIPSr   zS3 Outposts does not supportzS3 MRAP does not supportz!S3 Object Lambda does not supportzAccess Points do not supportzInvalid configuration:z#Client was configured for partitionzinvalid arn:)reportZ	s3controlzInvalid ARN:ZBucket)arnr   z!AccountId is required but not set)kwargsrA   r}   r   
IndexErrorr   rn   r   r   r   lowerr   r   r   )r   Zruleset_exceptionr   r   r   r   r   r!   r!   r"   r|     sV   








z;EndpointRulesetResolver.ruleset_error_to_botocore_exception)TN)r+   r,   r-   r.   r5   r#   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r|   r!   r!   r!   r"   ri     s.    

2!


a ri   )+r.   rY   loggingrT   enumr   Zbotocorer   r   Zbotocore.authr   r   Zbotocore.crtr   Zbotocore.endpoint_providerr	   Zbotocore.exceptionsr
   r   r   r   r   r   r   r   r   r   r   r   r   Zbotocore.utilsr   r   	getLoggerr+   rR   ZDEFAULT_URI_TEMPLATErP   r   r/   strrh   ri   r!   r!   r!   r"   <module>   s(   <
=  :