a
    w=icwr                     @   s   d 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 ddlmZ ddlmZ dd	lmZ d
ZdZdZdZG dd deZdd Zdd Zdd Zdi fddZG dd dejZdS )a  AWS Credentials and AWS Signature V4 Request Signer.

This module provides credentials to access Google Cloud resources from Amazon
Web Services (AWS) workloads. These credentials are recommended over the
use of service account credentials in AWS as they do not involve the management
of long-live service account private keys.

AWS Credentials are initialized using external_account arguments which are
typically loaded from the external credentials JSON file.
Unlike other Credentials that can be initialized with a list of explicit
arguments, secrets or credentials, external account clients use the
environment and hints/guidelines provided by the external_account JSON
file to retrieve credentials and exchange them for Google access tokens.

This module also provides a basic implementation of the
`AWS Signature Version 4`_ request signing algorithm.

AWS Credentials use serialized signed requests to the
`AWS STS GetCallerIdentity`_ API that can be exchanged for Google access tokens
via the GCP STS endpoint.

.. _AWS Signature Version 4: https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
.. _AWS STS GetCallerIdentity: https://docs.aws.amazon.com/STS/latest/APIReference/API_GetCallerIdentity.html
    N)http_client)urllib)urljoin)_helpers)environment_vars)
exceptions)external_accountzAWS4-HMAC-SHA256aws4_requestzx-amz-security-tokenz
x-amz-datec                   @   s&   e Zd ZdZdd Zdi fddZdS )RequestSignerzImplements an AWS request signer based on the AWS Signature Version 4 signing
    process.
    https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
    c                 C   s
   || _ dS )zInstantiates an AWS request signer used to compute authenticated signed
        requests to AWS APIs based on the AWS Signature Version 4 signing process.

        Args:
            region_name (str): The AWS region to use.
        N)_region_name)selfregion_name r   `/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/google/auth/aws.py__init__I   s    zRequestSigner.__init__ c                 C   s   | d}| d}| d}|p$i }tj|}	tjt|t|	j}
|	jr\|	j	dkrdt
dt|	j|
jprdt|	j|| j|||||d
}| d|	jd	}d
|v r| d
|t< |D ]}|| ||< q|dur||t< |||d}|r||d< |S )a^  Generates the signed request for the provided HTTP request for calling
        an AWS API. This follows the steps described at:
        https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html

        Args:
            aws_security_credentials (Mapping[str, str]): A dictionary containing
                the AWS security credentials.
            url (str): The AWS service URL containing the canonical URI and
                query string.
            method (str): The HTTP method used to call this API.
            request_payload (Optional[str]): The optional request payload if
                available.
            additional_headers (Optional[Mapping[str, str]]): The optional
                additional headers needed for the requested AWS API.

        Returns:
            Mapping[str, str]: The AWS signed request dictionary object.
        access_key_idsecret_access_keysecurity_tokenhttpszInvalid AWS service URL/)
hostcanonical_uricanonical_querystringmethodregion
access_key
secret_keyr   request_payloadadditional_headersauthorization_header)Authorizationr   amz_dateNurlr   headersdata)getr   parseurlparser   	posixpathnormpathpathhostnamescheme
ValueError#_generate_authentication_header_map_get_canonical_querystringqueryr   _AWS_DATE_HEADER_AWS_SECURITY_TOKEN_HEADER)r   aws_security_credentialsr$   r   r   r   r   r   r   uriZnormalized_uriZ
header_mapr%   keyZsigned_requestr   r   r   get_request_optionsS   sF    


z!RequestSigner.get_request_optionsN)__name__
__module____qualname____doc__r   r8   r   r   r   r   r
   C   s
   r
   c                 C   s   t j| }i }|D ]P}t jj|dd}g ||< || D ]}|| t jj|dd q8||   qt| }|  g }|D ]$}|| D ]}|d|| qqd	|S )a  Generates the canonical query string given a raw query string.
    Logic is based on
    https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

    Args:
        query (str): The raw query string.

    Returns:
        str: The canonical query string.
    z-_.~)safez{}={}&)
r   r(   parse_qsquoteappendsortlistkeysformatjoin)r2   ZquerystringZquerystring_encoded_mapr7   Z	quote_keyitemZsorted_keysZquerystring_encoded_pairsr   r   r   r1      s"    r1   c                 C   s   t | |dtj S )zCreates the HMAC-SHA256 hash of the provided message using the provided
    key.

    Args:
        key (str): The HMAC-SHA256 key to use.
        msg (str): The message to hash.

    Returns:
        str: The computed hash bytes.
    utf-8)hmacnewencodehashlibsha256digest)r7   msgr   r   r   _sign   s    rP   c                 C   s6   t d|  d|}t ||}t ||}t |d}|S )a  Calculates the signing key used to calculate the signature for
    AWS Signature Version 4 based on:
    https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html

    Args:
        key (str): The AWS secret access key.
        date_stamp (str): The '%Y%m%d' date format.
        region_name (str): The AWS region.
        service_name (str): The AWS service name, eg. sts.

    Returns:
        str: The signing key bytes.
    ZAWS4rH   r	   )rP   rK   )r7   
date_stampr   service_nameZk_dateZk_regionZ	k_serviceZ	k_signingr   r   r   _get_signing_key   s
    


rS   r   c
              
   C   sb  |  dd }
t }|d}|d}i }|	D ]}|	| || < q2|durX||t< | |d< d|vrp||t< d}t| }|	  |D ]}d	
|||| }qd
|}t|pdd }d
||||||}d
|||
t}d
t||t|d }t||||
}t||dtj }d
t||||}d|i}d|vr^||d< |S )a
  Generates the authentication header map needed for generating the AWS
    Signature Version 4 signed request.

    Args:
        host (str): The AWS service URL hostname.
        canonical_uri (str): The AWS service URL path name.
        canonical_querystring (str): The AWS service URL query string.
        method (str): The HTTP method used to call this API.
        region (str): The AWS region.
        access_key (str): The AWS access key ID.
        secret_key (str): The AWS secret access key.
        security_token (Optional[str]): The AWS security session token. This is
            available for temporary sessions.
        request_payload (Optional[str]): The optional request payload if
            available.
        additional_headers (Optional[Mapping[str, str]]): The optional
            additional headers needed for the requested AWS API.

    Returns:
        Mapping[str, str]: The AWS authentication header dictionary object.
            This contains the x-amz-date and authorization header information.
    .r   z%Y%m%dT%H%M%SZz%Y%m%dNr   dater   z{}{}:{}
;rH   z{}
{}
{}
{}
{}
{}z{}/{}/{}/{}z{}
{}
{}
{}z3{} Credential={}/{}, SignedHeaders={}, Signature={}r    r"   )splitr   utcnowstrftimelowerr4   r3   rC   rD   rB   rE   rF   rL   rM   rK   	hexdigest_AWS_REQUEST_TYPE_AWS_ALGORITHMrS   rI   rJ   )r   r   r   r   r   r   r   r   r   r   rR   current_timer"   rQ   Zfull_headersr7   Zcanonical_headersheader_keysZsigned_headersZpayload_hashZcanonical_requestZcredential_scopeZstring_to_signZsigning_key	signaturer    Zauthentication_headerr   r   r   r0      sb    $



	

r0   c                       sj   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	e
 fddZe
 fddZ  ZS )CredentialszAWS external account credentials.
    This is used to exchange serialized AWS signature v4 signed requests to
    AWS STS GetCallerIdentity service for Google access tokens.
    Nc           
         s   t t| j|||||d| |p&i }|dp4d| _|d| _|d| _|d| _|d| _d| _	d| _
|| _td	| j}|r| \}}	nd
\}}	|dks| jdu rtdnt|	pddkrtd|	dS )a]  Instantiates an AWS workload external account credentials object.

        Args:
            audience (str): The STS audience field.
            subject_token_type (str): The subject token type.
            token_url (str): The STS endpoint URL.
            credential_source (Mapping): The credential source dictionary used
                to provide instructions on how to retrieve external credential
                to be exchanged for Google access tokens.
            args (List): Optional positional arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
            kwargs (Mapping): Optional keyword arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.

        Raises:
            google.auth.exceptions.RefreshError: If an error is encountered during
                access token retrieval logic.
            ValueError: For invalid parameters.

        .. note:: Typically one of the helper constructors
            :meth:`from_file` or
            :meth:`from_info` are used instead of calling the constructor directly.
        )audiencesubject_token_type	token_urlcredential_sourceZenvironment_idr   Z
region_urlr$   Zregional_cred_verification_urlZimdsv2_session_token_urlNz^(aws)([\d]+)$)NNZawsz)No valid AWS 'credential_source' provided   z7aws version '{}' is not supported in the current build.)superra   r   r'   Z_environment_id_region_url_security_credentials_url_cred_verification_url_imdsv2_session_token_url_region_request_signer_target_resourcerematchgroupsr/   intrE   )
r   rb   rc   rd   re   argskwargsmatchesZenv_idZenv_version	__class__r   r   r   \  sD    

zCredentials.__init__c           
      C   s  |durJ| j durJddi}|| j d|d}|jdkrBtd|j|j}nd}| jdu rv| || j|| _t	| j| _| 
||}| j|| jd| jd	}|d
}| j|d< i }|d|d< |d|d< g |d
< t| D ]}	|d
 |	||	 d qtjtj|dddS )ai  Retrieves the subject token using the credential_source object.
        The subject token is a serialized `AWS GetCallerIdentity signed request`_.

        The logic is summarized as:

        Retrieve the AWS region from the AWS_REGION or AWS_DEFAULT_REGION
        environment variable or from the AWS metadata server availability-zone
        if not found in the environment variable.

        Check AWS credentials in environment variables. If not found, retrieve
        from the AWS metadata server security-credentials endpoint.

        When retrieving AWS credentials from the metadata server
        security-credentials endpoint, the AWS role needs to be determined by
        calling the security-credentials endpoint without any argument. Then the
        credentials can be retrieved via: security-credentials/role_name

        Generate the signed request to AWS STS GetCallerIdentity action.

        Inject x-goog-cloud-target-resource into header and serialize the
        signed request. This will be the subject-token to pass to GCP STS.

        .. _AWS GetCallerIdentity signed request:
            https://cloud.google.com/iam/docs/access-resources-aws#exchange-token

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
        Returns:
            str: The retrieved subject token.
        Nz$X-aws-ec2-metadata-token-ttl-secondsZ300PUTr#      z$Unable to retrieve AWS Session Tokenz{region}POSTr%   zx-goog-cloud-target-resourcer$   r   )r7   value),:T)
separators	sort_keys)rk   statusr   RefreshErrorr&   rm   _get_regionrh   rl   r
   _get_security_credentialsr8   rj   replacer'   rn   sortedrD   rA   r   r(   r@   jsondumps)
r   requestr%   Zimdsv2_session_token_responseimdsv2_session_tokenr5   Zrequest_optionsZrequest_headersZaws_signed_reqr7   r   r   r   retrieve_subject_token  sL    !



z"Credentials.retrieve_subject_tokenc                 C   s   t jtj}|dur|S t jtj}|dur4|S | jsDtdd}|durXd|i}|| jd|d}t	|j
dr|j
dn|j
}|jdkrtd	||dd
 S )a  Retrieves the current AWS region from either the AWS_REGION or
        AWS_DEFAULT_REGION environment variable or from the AWS metadata server.

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
            url (str): The AWS metadata server region URL.
            imdsv2_session_token (str): The AWS IMDSv2 session token to be added as a
                header in the requests to AWS metadata endpoint.

        Returns:
            str: The current AWS region.

        Raises:
            google.auth.exceptions.RefreshError: If an error occurs while
                retrieving the AWS region.
        NzUnable to determine AWS regionX-aws-ec2-metadata-tokenGETr#   decoderH   ry   zUnable to retrieve AWS region)osenvironr'   r   Z
AWS_REGIONZAWS_DEFAULT_REGIONrh   r   r   hasattrr&   r   r   )r   r   r$   r   Zenv_aws_regionr%   responseresponse_bodyr   r   r   r     s*    


zCredentials._get_regionc                 C   sv   t jtj}t jtj}t jtj}|r>|r>|||dS | ||}| |||}|d|d|ddS )a  Retrieves the AWS security credentials required for signing AWS
        requests from either the AWS security credentials environment variables
        or from the AWS metadata server.

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
            imdsv2_session_token (str): The AWS IMDSv2 session token to be added as a
                header in the requests to AWS metadata endpoint.

        Returns:
            Mapping[str, str]: The AWS security credentials dictionary object.

        Raises:
            google.auth.exceptions.RefreshError: If an error occurs while
                retrieving the AWS security credentials.
        )r   r   r   ZAccessKeyIdZSecretAccessKeyToken)	r   r   r'   r   ZAWS_ACCESS_KEY_IDZAWS_SECRET_ACCESS_KEYZAWS_SESSION_TOKEN_get_metadata_role_name"_get_metadata_security_credentials)r   r   r   Zenv_aws_access_key_idZenv_aws_secret_access_keyZenv_aws_session_token	role_namecredentialsr   r   r   r   A  s$    z%Credentials._get_security_credentialsc                 C   st   ddi}|dur||d< |d | j|d|d}t|jdrH|jd	n|j}|jtjkrft	d
|t
|}|S )aJ  Retrieves the AWS security credentials required for signing AWS
        requests from the AWS metadata server.

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
            role_name (str): The AWS role name required by the AWS metadata
                server security_credentials endpoint in order to return the
                credentials.
            imdsv2_session_token (str): The AWS IMDSv2 session token to be added as a
                header in the requests to AWS metadata endpoint.

        Returns:
            Mapping[str, str]: The AWS metadata server security credentials
                response.

        Raises:
            google.auth.exceptions.RefreshError: If an error occurs while
                retrieving the AWS security credentials.
        zContent-Typezapplication/jsonNr   z{}/{}r   r#   r   rH   z+Unable to retrieve AWS security credentials)rE   ri   r   r&   r   r   r   OKr   r   r   loads)r   r   r   r   r%   r   r   Zcredentials_responser   r   r   r   q  s$    	

z.Credentials._get_metadata_security_credentialsc                 C   sr   | j du rtdd}|dur(d|i}|| j d|d}t|jdrP|jdn|j}|jtjkrntd||S )	a  Retrieves the AWS role currently attached to the current AWS
        workload by querying the AWS metadata server. This is needed for the
        AWS metadata server security credentials endpoint in order to retrieve
        the AWS security credentials needed to sign requests to AWS APIs.

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
            imdsv2_session_token (str): The AWS IMDSv2 session token to be added as a
                header in the requests to AWS metadata endpoint.

        Returns:
            str: The AWS role name.

        Raises:
            google.auth.exceptions.RefreshError: If an error occurs while
                retrieving the AWS role name.
        NzIUnable to determine the AWS metadata server security credentials endpointr   r   r#   r   rH   z Unable to retrieve AWS role name)	ri   r   r   r   r&   r   r   r   r   )r   r   r   r%   r   r   r   r   r   r     s&    

z#Credentials._get_metadata_role_namec                    s   t t| j|fi |S )a  Creates an AWS Credentials instance from parsed external account info.

        Args:
            info (Mapping[str, str]): The AWS external account info in Google
                format.
            kwargs: Additional arguments to pass to the constructor.

        Returns:
            google.auth.aws.Credentials: The constructed credentials.

        Raises:
            ValueError: For invalid parameters.
        )rg   ra   	from_info)clsinfort   rv   r   r   r     s    zCredentials.from_infoc                    s   t t| j|fi |S )aH  Creates an AWS Credentials instance from an external account json file.

        Args:
            filename (str): The path to the AWS external account json file.
            kwargs: Additional arguments to pass to the constructor.

        Returns:
            google.auth.aws.Credentials: The constructed credentials.
        )rg   ra   	from_file)r   filenamert   rv   r   r   r     s    zCredentials.from_file)N)r9   r:   r;   r<   r   r   r   r   r   r   classmethodr   r   __classcell__r   r   rv   r   ra   V  s   
 Dk601.ra   )r<   rL   rI   r   r   r*   ro   Z	six.movesr   r   Zsix.moves.urllib.parser   Zgoogle.authr   r   r   r   r]   r\   r4   r3   objectr
   r1   rP   rS   r0   ra   r   r   r   r   <module>   s2   ]$
o