o
    diG                     @   s   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
mZmZmZmZmZ d dlmZ d dlmZ G dd de	Zd	d
 Zdd Z	d!ddZ	d"ddZ	d"ddZ	d"ddZG dd de
Zdd Z	d#ddZdd Z	d$dd ZdS )%    N)get_current_datetime)ParamValidationErrorUnknownClientMethodError)RequestSignerS3PostPresignerUnknownSignatureVersionError UnsupportedSignatureVersionError_should_use_global_endpointcreate_request_objectprepare_request_dict)FrozenAuthToken)	ArnParserc                   @   sR   e Zd ZdddZ				dddZdd Z		dd	d
ZeZ			dddZdS )AioRequestSignerNc                    s   |  ||I d H S N)sign)selfoperation_namerequestkwargs r   G/home/ubuntu/.local/lib/python3.10/site-packages/aiobotocore/signers.pyhandler   s   zAioRequestSigner.handlerstandardc              
      sf  |}|d u r
| j }|d u r| j}| |||jI d H }| jjd| j  d| ||| j || |dI d H  |tj	kr|||d}	|d urI||	d< |j
di }
|s]|

dr]|
d |	d< |

d	rh|
d	 |	d	< |

d
rs|
d
 |	d
< |

dd ur| |	|
d |
d  z| jdi |	I d H }W n ty } z|dkrt|d|d }~ww || d S d S )Nzbefore-sign..)r   signing_nameregion_namesignature_versionrequest_signerr   )r   r   r   expiressigningregionr   r   request_credentialsidentity_cache	cache_keyr   r   r   )_region_name_signing_name_choose_signercontext_event_emitteremit_service_id	hyphenizebotocoreUNSIGNEDget_resolve_identity_cacheget_auth_instancer   r   add_auth)r   r   r   r   signing_type
expires_inr   explicit_region_namer   r   signing_contextauther   r   r   r      sh   	




zAioRequestSigner.signc                    s   ddd}| |d}| dp| j}| di }| d| j}| d| j}	|tjur6||s6||7 }| jjd	| j	
  d
| ||	||dI d H \}
}|d urc|}|tjurc||sc||7 }|S )Nz-presign-postz-query)presign-postpresign-url 	auth_typer   r   r    zchoose-signer.r   )r   r   r   r(   )r/   _signature_versionr&   r%   r-   r.   endswithr)   emit_until_responser+   r,   )r   r   r3   r(   signing_type_suffix_mapsuffixr   r   r   r   r   responser   r   r   r'   ^   s6   

zAioRequestSigner._choose_signerc                    s  |d u r| j }tjj|}|d u rt|d|jdu r8| jr/t| jt	s/| j
 I d H }n| j}||}|S |p<| j}	t|dd du rX|d }
|d }|
|I d H }	|d= d }|	d ure|	 I d H }||d< |jr~| jd u rvtj ||d< ||d< |d	i |}|S )
Nr$   TREQUIRES_IDENTITY_CACHEr"   r#   credentialsr   service_namer   )r=   r-   r7   AUTH_TYPE_MAPSr/   r   REQUIRES_TOKEN_auth_token
isinstancer   get_frozen_token_credentialsgetattrget_credentialsget_frozen_credentialsREQUIRES_REGIONr%   
exceptionsNoRegionError)r   r   r   r   r!   r   clsfrozen_tokenr7   rD   cachekeyfrozen_credentialsr   r   r   r1      sB   



z"AioRequestSigner.get_auth_instance  c                    s2   t |}| |||d||I d H  |  |jS )Nr:   )r
   r   prepareurl)r   request_dictr   r4   r   r   r   r   r   r   generate_presigned_url   s   
	z'AioRequestSigner.generate_presigned_url)NN)Nr   NN)rW   NN)	__name__
__module____qualname__r   r   r'   r1   get_authr[   r   r   r   r   r      s     

B+
/r   c                 K      t | d< d S )Ngenerate_db_auth_token)ra   class_attributesr   r   r   r   add_generate_db_auth_token      rd   c                 K   s   t | d< t| d< d S )Ngenerate_db_connect_auth_token$generate_db_connect_admin_auth_token)#dsql_generate_db_connect_auth_token)dsql_generate_db_connect_admin_auth_tokenrb   r   r   r   'add_dsql_generate_db_auth_token_methods   s   
rj   c                    s|   |}|du r| j j}d|d}ddi |dd}d}| | d	| }	t||	 | jjd||d
ddI dH }
|
t|d S )a  Generates an auth token used to connect to a db with IAM credentials.

    :type DBHostname: str
    :param DBHostname: The hostname of the database to connect to.

    :type Port: int
    :param Port: The port number the database is listening on.

    :type DBUsername: str
    :param DBUsername: The username to log in as.

    :type Region: str
    :param Region: The region the database is in. If None, the client
        region will be used.

    :return: A presigned url which can be used as an auth token.
    Nconnect)ActionDBUser/r;   GETurl_pathquery_stringheadersbodymethodhttps://:  zrds-dbr   rZ   r   r4   r   )metar   r   _request_signerr[   len)r   
DBHostnamePort
DBUsernameRegionr    paramsrZ   schemeendpoint_urlpresigned_urlr   r   r   ra      s0   
ra   rx   c           
         s   d}||vrt d| dd| d|du r| jj}ddi d	|id
d}d}| | }t|| | jj||||ddI dH }	|	t|d S )a!  Generate a DSQL database token for an arbitrary action.
    :type Hostname: str
    :param Hostname: The DSQL endpoint host name.
    :type Action: str
    :param Action: Action to perform on the cluster (DbConnectAdmin or DbConnect).
    :type Region: str
    :param Region: The AWS region where the DSQL Cluster is hosted. If None, the client region will be used.
    :type ExpiresIn: int
    :param ExpiresIn: The token expiry duration in seconds (default is 900 seconds).
    :return: A presigned url which can be used as an auth token.
    )	DbConnectDbConnectAdminz	Received z! for action but expected one of: z, )reportNrn   r;   rl   ro   rp   rv   dsqlry   )r   joinrz   r   r   r{   r[   r|   )
r   Hostnamerl   r   	ExpiresInpossible_actionsrZ   r   r   r   r   r   r   _dsql_generate_db_auth_token  s4   	
r   c                       t | |d||I dH S )a  Generate a DSQL database token for the "DbConnect" action.
    :type Hostname: str
    :param Hostname: The DSQL endpoint host name.
    :type Region: str
    :param Region: The AWS region where the DSQL Cluster is hosted. If None, the client region will be used.
    :type ExpiresIn: int
    :param ExpiresIn: The token expiry duration in seconds (default is 900 seconds).
    :return: A presigned url which can be used as an auth token.
    r   Nr   r   r   r   r   r   r   r   rh   ?     

rh   c                    r   )a  Generate a DSQL database token for the "DbConnectAdmin" action.
    :type Hostname: str
    :param Hostname: The DSQL endpoint host name.
    :type Region: str
    :param Region: The AWS region where the DSQL Cluster is hosted. If None, the client region will be used.
    :type ExpiresIn: int
    :param ExpiresIn: The token expiry duration in seconds (default is 900 seconds).
    :return: A presigned url which can be used as an auth token.
    r   Nr   r   r   r   r   ri   P  r   ri   c                   @   s   e Zd Z				dddZdS )AioS3PostPresignerNrW   c                    s   |d u ri }|d u rg }i }t  }|tj|d }|tjj|d< g |d< |D ]	}	|d |	 q)t|}
||
j	d< ||
j	d< | j
d|
|dI d H  |
j|dS )	N)seconds
expiration
conditionszs3-presign-post-fieldszs3-presign-post-policy	PutObjectr9   )rY   fields)r   datetime	timedeltastrftimer-   r7   ISO8601appendr
   r(   r{   r   rY   )r   rZ   r   r   r4   r   policydatetime_nowexpire_date	conditionr   r   r   r   generate_presigned_postb  s&   


z*AioS3PostPresigner.generate_presigned_post)NNrW   N)r\   r]   r^   r   r   r   r   r   r   a  s    r   c                 K   r`   )Nr[   )r[   rb   r   r   r   add_generate_presigned_url  re   r   rW   c                    s   |}|}|du ri }|}|}dt | d}	| j}
z| j| }W n ty,   t|dw | jj|}| j|||	dI dH }t	
|dd}| j|||	| dI dH \}}}| j||||	|d	d
I dH }|durn||d< |
j|||dI dH S )ax  Generate a presigned url given a client, its method, and arguments

    :type ClientMethod: string
    :param ClientMethod: The client method to presign for

    :type Params: dict
    :param Params: The parameters normally passed to
        ``ClientMethod``.

    :type ExpiresIn: int
    :param ExpiresIn: The number of seconds the presigned url is valid
        for. By default it expires in an hour (3600 seconds)

    :type HttpMethod: string
    :param HttpMethod: The http method to use on the generated url. By
        default, the http method is whatever is used in the method's model.

    :returns: The presigned url
    NTis_presign_requestuse_global_endpoint)method_name
api_paramsoperation_modelr(   Bucketr;   ignore_signing_regionFr   r   r   r(   rs   set_user_agent_headerru   )rZ   r4   r   )r	   r{   _PY_TO_OP_NAMEKeyErrorr   rz   service_modelr   _emit_api_paramsr   is_arnr/   _resolve_endpoint_ruleset_convert_to_request_dictr[   )r   ClientMethodParamsr   
HttpMethodclient_methodr   r4   http_methodr(   r   r   r   bucket_is_arnr   additional_headers
propertiesrZ   r   r   r   r[     s`   


r[   c                 K   r`   )Nr   )r   rb   r   r   r   add_generate_presigned_post  re   r   c                    s,  |}|}|}|}	|}
|d u ri }n|  }|	d u rg }	dt| d}t| j}| jjd}| jd|i||dI d H }t	|
dd}| j|||| dI d H \}}}| j|||||dd	I d H }|	d
|i |dr|	dd|d td  g n|	d|i ||d< |j|||	|
dI d H S )NTr   CreateBucketr   r   r;   r   Fr   bucketz${filename}zstarts-withz$keyrU   )rZ   r   r   r4   )copyr	   r   r{   rz   r   r   r   r   r   r/   r   r   r   r>   r|   r   )r   r   KeyFields
Conditionsr   r   rU   r   r   r4   r(   post_presignerr   r   r   r   r   r   rZ   r   r   r   r     sf   



 r   r   )Nrx   )NrW   N)NNrW   ) r   r-   botocore.authbotocore.compatr   botocore.exceptionsr   r   botocore.signersr   r   r   r   r	   r
   r   botocore.tokensr   botocore.utilsr   r   rd   rj   ra   r   rh   ri   r   r   r[   r   r   r   r   r   r   <module>   s6    $	 9

9
/

(
O