a
    w=icg                     @   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
ZeeZdZdZdZdZd	Zd
ZeeeeeefZdjejd ZdZdZdd Zd-ddZdd Zd.ddZd/ddZ dd Z!d0ddZ"d1ddZ#d2dd Z$d3d!d"Z%d4d#d$Z&d%d& Z'd'd( Z(d)d* Z)d5d+d,Z*dS )6zgApplication default credentials.

Implements application default credentials and project ID detection.
    N)environment_vars)
exceptionsZauthorized_userservice_accountZexternal_account external_account_authorized_userZimpersonated_service_accountZgdch_service_accountzCould not automatically determine credentials. Please set {env} or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
)enva  Your application has authenticated using end user credentials from Google Cloud SDK without a quota project. You might receive a "quota exceeded" or "API not enabled" error. We recommend you rerun `gcloud auth application-default login` and make sure a quota project is added. Or you can use service accounts instead. For more information about service accounts, see https://cloud.google.com/docs/authentication/z+urn:ietf:params:aws:token-type:aws4_requestc                 C   s&   ddl m} | j|jkr"tt dS )zDetermines if the credentials are problematic.

    Credentials from the Cloud SDK that are associated with Cloud SDK's project
    are problematic because they may not have APIs enabled and have limited
    quota. If this is the case, warn about it.
    r   
_cloud_sdkN)google.authr   Z	client_idZCLOUD_SDK_CLIENT_IDwarningswarn_CLOUD_SDK_CREDENTIALS_WARNING)credentialsr    r   e/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/google/auth/_default.py#_warn_about_problematic_credentialsI   s    r   c           	      C   s   t j| std| t| db}zt	|}W nB t
yz } z*td| |}t|| W Y d}~n
d}~0 0 W d   n1 s0    Y  t| |||||S )a  Loads Google credentials from a file.

    The credentials file must be a service account key, stored authorized
    user credentials, external account credentials, or impersonated service
    account credentials.

    Args:
        filename (str): The full path to the credentials file.
        scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
            specified, the credentials will automatically be scoped if
            necessary
        default_scopes (Optional[Sequence[str]]): Default scopes passed by a
            Google client library. Use 'scopes' for user-defined scopes.
        quota_project_id (Optional[str]):  The project ID used for
            quota and billing.
        request (Optional[google.auth.transport.Request]): An object used to make
            HTTP requests. This is used to determine the associated project ID
            for a workload identity pool resource (external account credentials).
            If not specified, then it will use a
            google.auth.transport.requests.Request client to make requests.

    Returns:
        Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
            credentials and the project ID. Authorized user credentials do not
            have the project ID information. External account credentials project
            IDs may not always be determined.

    Raises:
        google.auth.exceptions.DefaultCredentialsError: if the file is in the
            wrong format or is missing.
    zFile {} was not found.rz!File {} is not a valid json file.N)ospathexistsr   DefaultCredentialsErrorformatioopenjsonload
ValueErrorsix
raise_from_load_credentials_from_info)	filenamescopesdefault_scopesquota_project_idrequestZfile_objinfo
caught_excnew_excr   r   r   load_credentials_from_fileV   s    "
@r'   c           
      C   s   ddl m} |d}|tkr0t| ||\}}	n|tkrLt| |||\}}	n|tkrlt|| |||d\}}	nb|t	krt
| ||\}}	nH|tkrt| ||\}}	n.|tkrt| |\}}	ntdj| |tdt||rt||}||	fS )Nr   CredentialsWithQuotaProjecttype)r    r!   r#   zZThe file {file} does not have a valid type. Type is {type}, expected one of {valid_types}.)filer*   Zvalid_types)google.auth.credentialsr)   get_AUTHORIZED_USER_TYPE _get_authorized_user_credentials_SERVICE_ACCOUNT_TYPE _get_service_account_credentials_EXTERNAL_ACCOUNT_TYPE!_get_external_account_credentials&_EXTERNAL_ACCOUNT_AUTHORIZED_USER_TYPE1_get_external_account_authorized_user_credentials"_IMPERSONATED_SERVICE_ACCOUNT_TYPE-_get_impersonated_service_account_credentials_GDCH_SERVICE_ACCOUNT_TYPE%_get_gdch_service_account_credentialsr   r   r   _VALID_TYPES
isinstance_apply_quota_project_id)
r   r$   r    r!   r"   r#   r)   Zcredential_typer   
project_idr   r   r   r      sH    






r   c                 C   s\   ddl m} td | }tj|s8td dS t|| d\}}|sT|	 }||fS )z7Gets the credentials and project ID from the Cloud SDK.r   r   z9Checking Cloud SDK credentials as part of auth process...z7Cloud SDK credentials not found on disk; not using themNNr"   )
r	   r   _LOGGERdebug(get_application_default_credentials_pathr   r   isfiler'   get_project_id)r"   r   Zcredentials_filenamer   r=   r   r   r   _get_gcloud_sdk_credentials   s    


rE   c                 C   s   ddl m} | }tjtj}t	d| |durT||krTt	d| t
| dS |dur|ttjtj | d\}}||fS dS dS )zRGets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment
    variable.r   r   z?Checking %s for explicit credentials as part of auth process...NzrExplicit credentials path %s is the same as Cloud SDK credentials path, fall back to Cloud SDK credentials flow...r?   r>   )r	   r   rB   r   environr-   r   CREDENTIALSr@   rA   rE   r'   )r"   r   Zcloud_sdk_adc_pathZexplicit_filer   r=   r   r   r   !_get_explicit_environ_credentials   s$    

rH   c                  C   s   t jtjdkrdS z td ddlm  m	}  W n t
yT   td Y dS 0 z|  }|  }||fW S  ty   td Y dS 0 dS )z?Gets Google App Engine App Identity credentials and project ID.python27r>   z:Checking for App Engine runtime as part of auth process...r   Nz)Import of App Engine auth library failed.z]No App Engine library was found so cannot authentication via App Engine Identity Credentials.)r   rF   r-   r   ZLEGACY_APPENGINE_RUNTIMEr@   rA   Zgoogle.auth.app_engineauth
app_engineImportErrorwarningCredentialsrD   EnvironmentError)rK   r   r=   r   r   r   _get_gae_credentials   s"    


rP   c                 C   s   zddl m} ddlm} W n ty:   td Y dS 0 | du rRtjj	j
 } |j| drz|j| d}W n tjy   d}Y n0 | |fS td dS dS )	z>Gets credentials and project ID from the GCE Metadata Service.r   )compute_engine)	_metadataz-Import of Compute Engine auth library failed.r>   Nr#   z]Authentication failed using Compute Engine authentication due to unavailable metadata server.)r	   rQ   Zgoogle.auth.compute_enginerR   rL   r@   rM   googlerJ   	transportZ_http_clientRequestZpingrD   r   ZTransportErrorrN   )r#   rQ   rR   r=   r   r   r   _get_gce_credentials  s$    	

rW   c           
      C   s   |  dtkr.ddlm} |jj| ||d}n|  ddurp|  d ddurpddlm} |jj| ||d}nFz"dd	lm} |jj| ||d}W n" ty   t	
d
|Y n0 |du rddl}	|	jjj }||j|dfS )a{  Loads external account Credentials from the parsed external account info.

    The credentials information must correspond to a supported external account
    credentials.

    Args:
        info (Mapping[str, str]): The external account info in Google format.
        filename (str): The full path to the credentials file.
        scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If
            specified, the credentials will automatically be scoped if
            necessary.
        default_scopes (Optional[Sequence[str]]): Default scopes passed by a
            Google client library. Use 'scopes' for user-defined scopes.
        request (Optional[google.auth.transport.Request]): An object used to make
            HTTP requests. This is used to determine the associated project ID
            for a workload identity pool resource (external account credentials).
            If not specified, then it will use a
            google.auth.transport.requests.Request client to make requests.

    Returns:
        Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded
            credentials and the project ID. External account credentials project
            IDs may not always be determined.

    Raises:
        google.auth.exceptions.DefaultCredentialsError: if the info dictionary
            is in the wrong format or is missing required information.
    Zsubject_token_typer   )awsr    r!   Zcredential_sourceN
executable)	pluggable)identity_poolz3Failed to load external account credentials from {}rS   )r-   _AWS_SUBJECT_TOKEN_TYPEr	   rX   rN   	from_infor[   r\   r   r   r   r   google.auth.transport.requestsrJ   rU   requestsrV   rD   )
r$   r   r    r!   r#   rX   r   r[   r\   rT   r   r   r   r3   0  s4     

r3   c                 C   sH   zddl m} |j|}W n" ty>   td| Y n0 |d fS )Nr   )r   zCFailed to load external account authorized user credentials from {})r	   r   rN   r^   r   r   r   r   )r   r$   r    r!   r#   r   r   r   r   r   r5   v  s    
r5   c              
   C   sp   ddl m} z|jj||d}W nF tyf } z.d| }t||}t	|| W Y d }~n
d }~0 0 |d fS )Nr   r   )r    z2Failed to load authorized user credentials from {})
google.oauth2r   rN   Zfrom_authorized_user_infor   r   r   r   r   r   )r   r$   r    r   r%   msgr&   r   r   r   r/     s    

"r/   c           	   
   C   sx   ddl m} z|jj|||d}W nF tyh } z.d| }t||}t	|| W Y d }~n
d }~0 0 ||
dfS )Nr   )r   rY   z2Failed to load service account credentials from {}r=   )rb   r   rN   from_service_account_infor   r   r   r   r   r   r-   )	r   r$   r    r!   r   r   r%   rc   r&   r   r   r   r1     s    

"r1   c              
   C   s0  ddl m} z|d}|d}|tkr:t| |\}}n&|tkrRt| |\}}ntd||d}|	d}	|
d}
|	d	ks|
d	ks|	|
krtd
|||	d |
 }|d}|d}|j|||||d}W nH ty& } z.d| }t||}t|| W Y d }~n
d }~0 0 |d fS )Nr   )impersonated_credentialssource_credentialsr*   z.source credential of type {} is not supported.Z!service_account_impersonation_url/z:generateAccessTokenz'Cannot extract target principal from {}   	delegatesr"   r?   z?Failed to load impersonated service account credentials from {})r	   re   r-   r.   r/   r0   r1   r   r   rfindfindrN   r   r   r   r   )r   r$   r    re   Zsource_credentials_infoZsource_credentials_typerf   _Zimpersonation_urlstart_indexZ	end_indexZtarget_principalrj   r"   r   r%   rc   r&   r   r   r   r7     sR    









"r7   c              
   C   sr   ddl m} z|j|}W nF tyb } z.d| }t||}t	|| W Y d }~n
d }~0 0 ||
dfS )Nr   )gdch_credentialsz7Failed to load GDCH service account credentials from {}project)rb   ro   ZServiceAccountCredentialsrd   r   r   r   r   r   r   r-   )r   r$   ro   r   r%   rc   r&   r   r   r   r9     s    
"r9   c                 C   s8   |r|  |} ddlm} t| |jr4| js4t|  | S )Nr   ra   )with_quota_projectrb   r   r;   rN   r"   r   )r   r"   Zauthorized_user_credentialsr   r   r   r<     s    
r<   c                    s
  ddl m} ddl m} tjtjtjtj} fdd fddt	fddf}|D ]}| \}	}
|	durZ||	| |d	}	|
st
t|	d
drdu rddl}|jjj |	jd}
 rt|	|r|	 }	|p|
}|stdtj |	|f  S qZttdS )a  Gets the default credentials for the current environment.

    `Application Default Credentials`_ provides an easy way to obtain
    credentials to call Google APIs for server-to-server or local applications.
    This function acquires credentials from the environment in the following
    order:

    1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
       to the path of a valid service account JSON private key file, then it is
       loaded and returned. The project ID returned is the project ID defined
       in the service account file if available (some older files do not
       contain project ID information).

       If the environment variable is set to the path of a valid external
       account JSON configuration file (workload identity federation), then the
       configuration file is used to determine and retrieve the external
       credentials from the current environment (AWS, Azure, etc).
       These will then be exchanged for Google access tokens via the Google STS
       endpoint.
       The project ID returned in this case is the one corresponding to the
       underlying workload identity pool resource if determinable.

       If the environment variable is set to the path of a valid GDCH service
       account JSON file (`Google Distributed Cloud Hosted`_), then a GDCH
       credential will be returned. The project ID returned is the project
       specified in the JSON file.
    2. If the `Google Cloud SDK`_ is installed and has application default
       credentials set they are loaded and returned.

       To enable application default credentials with the Cloud SDK run::

            gcloud auth application-default login

       If the Cloud SDK has an active project, the project ID is returned. The
       active project can be set using::

            gcloud config set project

    3. If the application is running in the `App Engine standard environment`_
       (first generation) then the credentials and project ID from the
       `App Identity Service`_ are used.
    4. If the application is running in `Compute Engine`_ or `Cloud Run`_ or
       the `App Engine flexible environment`_ or the `App Engine standard
       environment`_ (second generation) then the credentials and project ID
       are obtained from the `Metadata Service`_.
    5. If no credentials are found,
       :class:`~google.auth.exceptions.DefaultCredentialsError` will be raised.

    .. _Application Default Credentials: https://developers.google.com            /identity/protocols/application-default-credentials
    .. _Google Cloud SDK: https://cloud.google.com/sdk
    .. _App Engine standard environment: https://cloud.google.com/appengine
    .. _App Identity Service: https://cloud.google.com/appengine/docs/python            /appidentity/
    .. _Compute Engine: https://cloud.google.com/compute
    .. _App Engine flexible environment: https://cloud.google.com            /appengine/flexible
    .. _Metadata Service: https://cloud.google.com/compute/docs            /storing-retrieving-metadata
    .. _Cloud Run: https://cloud.google.com/run
    .. _Google Distributed Cloud Hosted: https://cloud.google.com/blog/topics            /hybrid-cloud/announcing-google-distributed-cloud-edge-and-hosted

    Example::

        import google.auth

        credentials, project_id = google.auth.default()

    Args:
        scopes (Sequence[str]): The list of scopes for the credentials. If
            specified, the credentials will automatically be scoped if
            necessary.
        request (Optional[google.auth.transport.Request]): An object used to make
            HTTP requests. This is used to either detect whether the application
            is running on Compute Engine or to determine the associated project
            ID for a workload identity pool resource (external account
            credentials). If not specified, then it will either use the standard
            library http client to make requests for Compute Engine credentials
            or a google.auth.transport.requests.Request client for external
            account credentials.
        quota_project_id (Optional[str]): The project ID used for
            quota and billing.
        default_scopes (Optional[Sequence[str]]): Default scopes passed by a
            Google client library. Use 'scopes' for user-defined scopes.
    Returns:
        Tuple[~google.auth.credentials.Credentials, Optional[str]]:
            the current environment's credentials and project ID. Project ID
            may be None, which indicates that the Project ID could not be
            ascertained from the environment.

    Raises:
        ~google.auth.exceptions.DefaultCredentialsError:
            If no credentials were found, or if the credentials found were
            invalid.
    r   )with_scopes_if_requiredr(   c                      s
   t  dS Nr?   )rH   r   r?   r   r   <lambda>Y      zdefault.<locals>.<lambda>c                      s
   t  dS rs   )rE   r   r?   r   r   rt   Z  ru   c                      s   t  S )N)rW   r   rS   r   r   rt   \  ru   N)r!   rD   rS   zvNo project ID could be determined. Consider running `gcloud config set project` or setting the %s environment variable)r,   rr   r)   r   rF   r-   r   ZPROJECTZLEGACY_PROJECTrP   callablegetattrr_   rJ   rU   r`   rV   rD   r;   rq   r@   rM   r   r   _HELP_MESSAGE)r    r#   r"   r!   rr   r)   Zexplicit_project_idcheckerscheckerr   r=   rT   Zeffective_project_idr   )r"   r#   r   default  sF    a	





r{   )NNNN)N)N)N)NNN)NNN)N)NN)NNNN)+__doc__r   r   loggingr   r
   r   r	   r   r   Z"google.auth.transport._http_clientrT   	getLogger__name__r@   r.   r0   r2   r4   r6   r8   r:   r   rG   striprx   r   r]   r   r'   r   rE   rH   rP   rW   r3   r5   r/   r1   r7   r9   r<   r{   r   r   r   r   <module>   s\   



	 
41

!
# 
G 


.