o
    Jh"                     @   s  d Z ddlZddlZddlZddlmZ ddl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! 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)register_feature_id)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#   p/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/botocore/regions.pyconstruct_endpoint:   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_partitionsW   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   7   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_datar5   r#   r#   r$   __init__y   s   	
zEndpointResolver.__init__r(   c                 C   sB   | j d D ]}|d |krq|d }||vrq|| d   S d S )Nr2   	partitionservicesr   )r4   )r    r!   r)   r8   r9   r#   r#   r$   get_service_endpoints_data   s   z+EndpointResolver.get_service_endpoints_datac                 C   s&   g }| j d D ]	}||d  q|S )Nr2   r8   )r4   append)r    resultr8   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 )Nr2   r8   r9   r   regions)r4   _retrieve_variant_datar;   )r    r!   r)   r*   endpoint_variant_tagsr<   r8   r9   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 )Nr2   r8   defaults	dnsSuffix)r4   r>   get)r    r)   r?   r8   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-1r2   r8   T)r4   _endpoint_for_partition!_UNSUPPORTED_DUALSTACK_PARTITIONS)	r    r!   r"   r)   use_dualstack_endpointuse_fips_endpointvalid_partitionr8   r<   r#   r#   r$   r%      sN   	z#EndpointResolver.construct_endpointc                 C   s4   | j d D ]}| ||r|d   S qt|dd)Nr2   r8   z,No partition found for provided region_name.)r"   	error_msg)r4   _region_matchr   )r    r"   r8   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 )Nr8   z4Dualstack endpoints are currently not supported for z
 partition	dualstacktagsrO   r9   partitionEndpoint)r8   r!   service_datarA   rL   rM   r   isRegionalizedTz'Using partition endpoint for %s, %s: %srA   z*Creating a regex based endpoint for %s, %sr#   )	rK   r   rF   DEFAULT_SERVICE_DATAr   _resolverP   LOGdebug)r    r8   r!   r"   rL   rM   force_partitionr)   rO   rV   resolve_kwargspartition_endpointis_regionalizedr#   r#   r$   rJ      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    r8   r"   r#   r#   r$   rP   =  s
   zEndpointResolver._region_matchc                 C   s>   | dg }|D ]}t|d t|kr| }|  S qd S )NvariantsrT   )rF   setcopy)r    r6   rT   rd   rG   r<   r#   r#   r$   r>   D  s   z'EndpointResolver._retrieve_variant_datac                 C   s$   g }|r	| d |r| d |S )NrR   fips)r;   )r    rL   rM   rT   r#   r#   r$   _create_tag_listK  s   

z!EndpointResolver._create_tag_listc                 C   s4   i }|||fD ]}|  ||}|r| || q|S r,   )r>   _merge_keys)r    rT   r6   service_defaultspartition_defaultsr<   rd   rG   r#   r#   r$   _resolve_variantS  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: rD   zEndpoint does not exist for z in region rS   rE   r8   endpointNamehostnamesslCommonName)rF   rZ   warningrh   rl   r   ri   _expand_template)r    r8   r!   rV   rA   rL   rM   r6   rj   rk   rT   r<   rO   r#   r#   r$   rY   ]  sX   	
zEndpointResolver._resolvec                 C   s"   |D ]}||vr|| ||< qd S r,   r#   )r    	from_datar<   keyr#   r#   r$   ri     s
   zEndpointResolver._merge_keysc                 C   s   |j |||dS )N)serviceregionrE   )format)r    r8   templater!   rA   rE   r#   r#   r$   rr     s   z!EndpointResolver._expand_template)F)r(   )r(   FNr,   )NNFF)r-   r.   r/   r0   rK   r7   r:   r'   r+   rH   r%   rQ   rJ   rP   r>   rh   rl   rY   ri   rr   r#   r#   r#   r$   r1   t   s6    

		


2
A
Ar1   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$   ry     s    ry   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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&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$   r7     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#   ).0rt   valr#   r#   r$   
<dictcomp>  s    z>EndpointRulesetResolver.construct_endpoint.<locals>.<dictcomp>)headersr#   )_get_provider_paramsrZ   r[   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   sx   i }|  |||}| j D ]+\}}| j|||d}|du r+|jdur+| j|j|d}|dur9|||< | || 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_register_endpoint_feature_ids)	r    r   r   r   r   customized_builtinsr   	param_def	param_valr#   r#   r$   r     s(   z,EndpointRulesetResolver._get_provider_paramsc                 C   sV   |  ||}|d ur|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_operation_context_param&_resolve_param_as_client_context_param)r    r   r   r   staticdynamicoperation_context_paramsr#   r#   r$   r   =  s"   
z3EndpointRulesetResolver._resolve_param_from_contextc                 C   s   |  |}||S r,   )_get_static_context_paramsrF   )r    r   r   static_ctx_paramsr#   r#   r$   r   S  s   

z>EndpointRulesetResolver._resolve_param_as_static_context_paramc                 C   s(   |  |}||v r|| }||S d S r,   )_get_dynamic_context_paramsrF   )r    r   r   r   dynamic_ctx_paramsmember_namer#   r#   r$   r   Y  s
   

z?EndpointRulesetResolver._resolve_param_as_dynamic_context_paramc                 C   s(   |   }||v r|| }| j|S d S r,   )_get_client_context_paramsr   rF   )r    r   client_ctx_paramsclient_ctx_varnamer#   r#   r$   r   a  s
   z>EndpointRulesetResolver._resolve_param_as_client_context_paramc                 C   s*   |j }||v r|| d }t||S d S )Npath)operation_context_parametersjmespathsearch)r    r   r   r   operation_ctx_paramsr   r#   r#   r$   r   g  s
   zAEndpointRulesetResolver._resolve_param_as_operation_context_paramc                 C   s4   |t j vrt|d||}t|r| S |S )Nname)ry   __members__valuesr   rF   callable)r    r   r   r   r#   r#   r$   r   o  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   z      zFEndpointRulesetResolver._get_static_context_params.<locals>.<dictcomp>)static_context_parametersr    r   r#   r#   r$   r   w     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     r   zGEndpointRulesetResolver._get_dynamic_context_params.<locals>.<dictcomp>)context_parametersr   r#   r#   r$   r     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    zFEndpointRulesetResolver._get_client_context_params.<locals>.<dictcomp>)r   client_context_parametersr&   r#   r#   r$   r     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	hyphenizerf   r   r   emit)r    r   r   r   r   r   r#   r#   r$   r     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   )rF   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signingRegionrv   signingRegionSet,signingName)signing_namedisableDoubleEncodingz?Selected auth type "%s" as "%s" with signing context params: %sr   )
isinstancelistlen	TypeErrorrZ   r[   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   rI      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: `   )labelrI   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.)kwargsrF   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_exceptionc                 C   s6   |dkrt d|   d S |dkrt d d S d S )NAccountIdEndpointModeACCOUNT_ID_MODE_	AccountIdRESOLVED_ACCOUNT_ID)r   upper)r    r   r   r#   r#   r$   r   \  s
   z6EndpointRulesetResolver._register_endpoint_feature_ids)TN)r-   r.   r/   r0   r7   r%   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r#   r#   r#   r$   r     s2    

2"


c 7r   )/r0   rf   loggingra   enumr   r   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.useragentr   botocore.utilsr   r   	getLoggerr-   rZ   DEFAULT_URI_TEMPLATErX   r   r1   strry   r   r#   r#   r#   r$   <module>   s,   @
=  9#