o
    "`^hG                     @   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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!InvalidEndpointConfigurationError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"   N/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/botocore/regions.pyconstruct_endpoint7   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_partitionsT   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_endpoints[   s   z,BaseEndpointResolver.get_available_endpointsN)r'   F)__name__
__module____qualname____doc__r$   r&   r*   r"   r"   r"   r#   r   4   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_datar4   r"   r"   r#   __init__v   s   	
zEndpointResolver.__init__r'   c                 C   sB   | j d D ]}|d |krq|d }||vrq|| d   S d S )Nr1   	partitionservicesr   )r3   )r   r    r(   r7   r8   r"   r"   r#   get_service_endpoints_data   s   z+EndpointResolver.get_service_endpoints_datac                 C   s&   g }| j d D ]	}||d  q|S )Nr1   r7   )r3   append)r   resultr7   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 )Nr1   r7   r8   r   regions)r3   _retrieve_variant_datar:   )r   r    r(   r)   endpoint_variant_tagsr;   r7   r8   service_endpointsendpoint_nameis_regional_endpoint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 )Nr1   r7   defaults	dnsSuffix)r3   r=   get)r   r(   r>   r7   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-1r1   r7   T)r3   _endpoint_for_partition!_UNSUPPORTED_DUALSTACK_PARTITIONS)	r   r    r!   r(   use_dualstack_endpointuse_fips_endpointvalid_partitionr7   r;   r"   r"   r#   r$      sN   	z#EndpointResolver.construct_endpointc                 C   s4   | j d D ]}| ||r|d   S qt|dd)Nr1   r7   z,No partition found for provided region_name.)r!   	error_msg)r3   _region_matchr   )r   r!   r7   r"   r"   r#   get_partition_for_region   s   z)EndpointResolver.get_partition_for_regionc                 C   s  |d }|r|| j v rd| d}tdg|d|d |t}	|d u r0d|	v r-|	d }nt |||	|||d}
||	d	 v rG| jdi |
S | ||sO|r|	d}|	d
d}|rr|srtd||| ||
d< | jdi |
S td|| | jdi |
S d S )Nr7   z4Dualstack endpoints are currently not supported for z
 partition	dualstacktagsrN   r8   partitionEndpoint)r7   r    service_datar@   rK   rL   r   isRegionalizedTz'Using partition endpoint for %s, %s: %sr@   z*Creating a regex based endpoint for %s, %sr"   )	rJ   r   rE   DEFAULT_SERVICE_DATAr   _resolverO   LOGdebug)r   r7   r    r!   rK   rL   force_partitionr(   rN   rU   resolve_kwargspartition_endpointis_regionalizedr"   r"   r#   rI      sX   	



z(EndpointResolver._endpoint_for_partitionc                 C   s0   ||d v rdS d|v rt |d |S dS )Nr<   TregionRegexF)recompilematch)r   r7   r!   r"   r"   r#   rO   :  s
   zEndpointResolver._region_matchc                 C   s>   | dg }|D ]}t|d t|kr| }|  S qd S )NvariantsrS   )rE   setcopy)r   r5   rS   rc   rF   r;   r"   r"   r#   r=   A  s   z'EndpointResolver._retrieve_variant_datac                 C   s$   g }|r	| d |r| d |S )NrQ   fips)r:   )r   rK   rL   rS   r"   r"   r#   _create_tag_listH  s   

z!EndpointResolver._create_tag_listc                 C   s4   i }|||fD ]}|  ||}|r| || q|S r+   )r=   _merge_keys)r   rS   r5   service_defaultspartition_defaultsr;   rc   rF   r"   r"   r#   _resolve_variantP  s   z!EndpointResolver._resolve_variantc                 C   s$  | di  |i }| drtd|  | di }| di }	| ||}
|
rL| |
|||	}|i krEd| d| }t|
|d| || n|}d|vrX|d |d< |d	 |d	< ||d
< | || | |	| | ||d |||d |d< d|v r| ||d |||d |d< |S )Nr   
deprecatedz3Client is configured with the deprecated endpoint: rC   zEndpoint does not exist for z in region rR   rD   r7   endpointNamehostnamesslCommonName)rE   rY   warningrg   rk   r   rh   _expand_template)r   r7   r    rU   r@   rK   rL   r5   ri   rj   rS   r;   rN   r"   r"   r#   rX   Z  sX   	
zEndpointResolver._resolvec                 C   s"   |D ]}||vr|| ||< qd S r+   r"   )r   	from_datar;   keyr"   r"   r#   rh     s
   zEndpointResolver._merge_keysc                 C   s   |j |||dS )N)serviceregionrD   )format)r   r7   templater    r@   rD   r"   r"   r#   rq     s   z!EndpointResolver._expand_template)F)r'   )r'   FNr+   )NNFF)r,   r-   r.   r/   rJ   r6   r9   r&   r*   rG   r$   rP   rI   rO   r=   rg   rk   rX   rh   rq   r"   r"   r"   r#   r0   q   s6    

		


2
A
Ar0   c                   @   s@   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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::EndpointzAWS::Auth::AccountIdz AWS::Auth::AccountIdEndpointModeN)r,   r-   r.   
AWS_REGIONAWS_USE_FIPSAWS_USE_DUALSTACKAWS_STS_USE_GLOBAL_ENDPOINTAWS_S3_USE_GLOBAL_ENDPOINTAWS_S3_ACCELERATEAWS_S3_FORCE_PATH_STYLEAWS_S3_USE_ARN_REGIONAWS_S3CONTROL_USE_ARN_REGIONAWS_S3_DISABLE_MRAPSDK_ENDPOINT
ACCOUNT_IDACCOUNT_ID_ENDPOINT_MODEr"   r"   r"   r#   rx     s    rx   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)ruleset_datapartition_data)r	   	_providerruleset
parameters_param_definitions_service_model	_builtins_client_context_event_emitter_use_ssl_requested_auth_scheme_instance_cache)	r   endpoint_ruleset_datar   service_modelbuiltinsclient_contextevent_emitteruse_sslrequested_auth_schemer"   r"   r#   r6     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 r9 ||d}~ww td|j  | jsa|j	dra|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: zEndpoint provider result: zhttps://zhttp://   )urlc                 S   s   i | ]	\}}||d  qS )r   r"   ).0rs   valr"   r"   r#   
<dictcomp>  s    z>EndpointRulesetResolver.construct_endpoint.<locals>.<dictcomp>)headersr"   )_get_provider_paramsrY   rZ   r   resolve_endpointr
   #ruleset_error_to_botocore_exceptionr   r   
startswith_replacer   items)r   operation_model	call_argsrequest_contextprovider_paramsprovider_resultex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_namer   )_get_customized_builtinsr   r   _resolve_param_from_contextbuiltin_resolve_param_as_builtin)	r   r   r   r   r   customized_builtinsr   	param_def	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   staticdynamicr"   r"   r#   r   8  s   
z3EndpointRulesetResolver._resolve_param_from_contextc                 C   s   |  |}||S r+   )_get_static_context_paramsrE   )r   r   r   static_ctx_paramsr"   r"   r#   r   G  s   

z>EndpointRulesetResolver._resolve_param_as_static_context_paramc                 C   s(   |  |}||v r|| }||S d S r+   )_get_dynamic_context_paramsrE   )r   r   r   r   dynamic_ctx_paramsmember_namer"   r"   r#   r   M  s
   

z?EndpointRulesetResolver._resolve_param_as_dynamic_context_paramc                 C   s(   |   }||v r|| }| j|S d S r+   )_get_client_context_paramsr   rE   )r   r   client_ctx_paramsclient_ctx_varnamer"   r"   r#   r   U  s
   z>EndpointRulesetResolver._resolve_param_as_client_context_paramc                 C   s"   |t j vrt|d||S )Nname)rx   __members__valuesr   rE   )r   r   r   r"   r"   r#   r   [  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r   paramr"   r"   r#   r   c      zFEndpointRulesetResolver._get_static_context_params.<locals>.<dictcomp>)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#   r   k  r   zGEndpointRulesetResolver._get_dynamic_context_params.<locals>.<dictcomp>)context_parametersr   r"   r"   r#   r   h  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#   r   s  s    zFEndpointRulesetResolver._get_client_context_params.<locals>.<dictcomp>)r   client_context_parametersr%   r"   r"   r#   r   p  s   z2EndpointRulesetResolver._get_client_context_paramsc                 C   s8   | j j }t| j}| jjd| ||||d |S )Nzbefore-endpoint-resolution.)r   modelparamscontext)r   
service_id	hyphenizere   r   r   emit)r   r   r   r   r   r   r"   r"   r#   r   x  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rd|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   )rE   r   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r   schemer%   r"   r#   r     s    Nc                 3   s,    | ]}   j|d  r j|fV  qdS r   N)._does_botocore_authname_match_ruleset_authnamer   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)signature_versionsigningRegionru   signingRegionSet,signingName)signing_namedisableDoubleEncodingz?Selected auth type "%s" as "%s" with signing context params: %sr   )
isinstancelistlen	TypeErrorrY   rZ   joinr   r   nextStopIterationr   anyr   r   updater   )r   auth_schemesr   r   fixable_with_crtauth_type_optionssigning_contextr"   r%   r#   auth_schemes_to_signing_ctx  sx   


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   	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   rH      N)r   splitr   )r   botonamersnamer"   r"   r#   r     s
   
zFEndpointRulesetResolver._does_botocore_authname_match_ruleset_authnamec                 C   sh  |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kr|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: `   )labelrH   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:)report	s3controlzInvalid ARN:Bucket)arnr   z!AccountId is required but not seteventszUInvalid Configuration: FIPS is not supported with EventBridge multi-region endpoints.z&EndpointId must be a valid host label.)kwargsrE   r   r   
IndexErrorr   r   r    r   r   lowerr   r   r   r   )r   ruleset_exceptionr   r   r  r    r	  r"   r"   r#   r     s`   










z;EndpointRulesetResolver.ruleset_error_to_botocore_exception)TN)r,   r-   r.   r/   r6   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r     s.    

2!


c r   ),r/   re   loggingr`   enumr   botocorer   r   botocore.authr   r   botocore.crtr   botocore.endpoint_providerr	   botocore.exceptionsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   botocore.utilsr   r   	getLoggerr,   rY   DEFAULT_URI_TEMPLATErW   r   r0   strrx   r   r"   r"   r"   r#   <module>   s(   @
=  9"