
    @wj)                     b   d Z ddlZddlZddlm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  ej                  e      Z G d d	ej                         Zdd
Zd Zd Zd Z	 ddej,                  dededee   ddf
dZdej,                  defdZdeej,                     fdZ	 ddee   defdZy)zUtilites for mutual TLS.    N)getenv)Optional)environment_vars)
exceptions)_mtls_helperc                       e Zd ZdZdZdZy)UseMtlsEndpointModealwaysneverautoN)__name__
__module____qualname__ALWAYSNEVERAUTO     X/root/dashboard-youtube/.venv/lib/python3.12/site-packages/google/auth/transport/mtls.pyr	   r	      s    FEDr   r	   c                     t        j                  |       }|y| r%t        j                  t         j                        	 yy)a2  Check if default client SSL credentials exists on the device.

    Args:
       include_context_aware (bool): include_context_aware indicates if context_aware
       path location will be checked or should it be skipped.

    Returns:
        bool: indicating if the default client cert source exists.
    include_context_awareTF)r   _get_cert_config_path_check_config_pathCONTEXT_AWARE_METADATA_PATH)r   	cert_paths     r   has_default_client_cert_sourcer   $   sI     223I ++L,T,TU r   c                  N    t        d      st        j                  d      d } | S )a  Get a callback which returns the default client SSL credentials.

    Returns:
        Callable[[], [bytes, bytes]]: A callback which returns the default
            client certificate bytes and private key bytes, both in PEM format.

    Raises:
        google.auth.exceptions.MutualTLSChannelError: If the default
            client SSL credentials don't exist or are malformed.
    Tr   z(Default client cert source doesn't existc                      	 t        j                         \  } }}||fS # t        t        t        f$ r}t        j                  |      }||d }~ww xY wN)r   get_client_cert_and_keyOSErrorRuntimeError
ValueErrorr   MutualTLSChannelError)_
cert_bytes	key_bytes
caught_excnew_excs        r   callbackz,default_client_cert_source.<locals>.callbackM   sZ    	*'3'K'K'M$Az9
 9$$	 z2 	* 66zBGz)	*s    AA

Ar   r   r%   )r+   s    r   default_client_cert_sourcer-   =   s.     *E..6
 	
% Or   c                 X     t        d      st        j                  d       fd}|S )am  Get a callback which returns the default encrpyted client SSL credentials.

    Args:
        cert_path (str): The cert file path. The default client certificate will
            be written to this file when the returned callback is called.
        key_path (str): The key file path. The default encrypted client key will
            be written to this file when the returned callback is called.

    Returns:
        Callable[[], [str, str, bytes]]: A callback which generates the default
            client certificate, encrpyted private key and passphrase. It writes
            the certificate and private key into the cert_path and key_path, and
            returns the cert_path, key_path and passphrase bytes.

    Raises:
        google.auth.exceptions.MutualTLSChannelError: If any problem
            occurs when loading or saving the client certificate and key.
    Tr   z2Default client encrypted cert source doesn't existc                     	 t        j                  d      \  } }}}t        d      5 }|j                  |       d d d        t        	d      5 }|j                  |       d d d        	|fS # 1 sw Y   4xY w# 1 sw Y   xY w# t        j
                  t        f$ r}t	        j                  |      }||d }~ww xY w)NT)generate_encrypted_keywb)r   get_client_ssl_credentialsopenwriter   ClientCertErrorr"   r%   )
r&   r'   r(   passphrase_bytes	cert_filekey_filer)   r*   r   key_paths
           r   r+   z6default_client_encrypted_cert_source.<locals>.callbackq   s    	* 77tT i& ,)
+,h% *y)* ($444, ,* ***G4 	* 66zBGz)	*sE   'B A/B A;"B /A84B ;B B B= B88B=r,   )r   r9   r+   s   `` r   $default_client_encrypted_cert_sourcer:   Y   s/    & *E..@
 	
5$ Or   c                  *    t        j                         S )a  Returns boolean for whether the client certificate should be used for mTLS.

    This is a wrapper around _mtls_helper.check_use_client_cert().
    If GOOGLE_API_USE_CLIENT_CERTIFICATE is set to true or false, a corresponding
    bool value will be returned
    If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred by
    reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG or
    CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH, or the default path
    like ~/.config/gcloud/certificate_config.json, and verifying it
    contains a "workload" section. If so, the function will return True,
    otherwise False.

    Returns:
       bool: indicating whether the client certificate should be used for mTLS.
    )r   check_use_client_certr   r   r   should_use_client_certr=      s      --//r   ctxr'   r(   
passphrasereturnc                    t        | t        j                        st        j                  d      	 t        j                  |||      5 \  }}}||t        j                  d      | j                  |||       ddd       y# 1 sw Y   yxY w# t        j                  t        t        t        t        f$ r}t        j                  |      }||d}~ww xY w)a  Load a client certificate and key into an SSL context.

    Args:
        ctx (ssl.SSLContext): The SSL context to load the certificate and key into.
        cert_bytes (bytes): The client certificate bytes in PEM format.
        key_bytes (bytes): The client private key bytes in PEM format.
        passphrase (Optional[bytes]): The passphrase for the client private key.

    Raises:
        google.auth.exceptions.MutualTLSChannelError: If the SSL context is invalid,
            or if loading the certificate and key fails.
    zFailed to load client certificate and key for mTLS. The provided context object is invalid or does not support loading certificate chains.)r?   NzKFailed to generate temporary file paths for the client certificate and key.)certfilekeyfilepassword)
isinstancessl
SSLContextr   r%   r   secure_cert_key_pathsload_cert_chainSSLErrorr"   r$   r#   	TypeError)	r>   r'   r(   r?   r   r9   passphrase_valr)   r*   s	            r   _load_client_cert_into_contextrM      s    $ c3>>*..P
 	

&//	j
 	
 H$4 66a  "H~   	 	 	 	 & 22:>:%&s5   B 	2B;B B	B B (C8CCc                 
   t               r
t               sy	 t        j                         \  }}}}|syt	        | |||       y# t
        j                  t        t        t        f$ r}t        j                  |      }||d}~ww xY w)a  Load the default client certificate and key into an SSL context if configured.

    If client certificates are enabled and a default client certificate source is
    found, the certificate and key are loaded into the SSL context.

    Args:
        ctx (ssl.SSLContext): The SSL context to load the default client certificate
            and key into.

    Returns:
        bool: True if client certificates are enabled and the default client
            certificate was successfully loaded. False if client certificates
            are disabled or if no default certificate source is configured.

    Raises:
        google.auth.exceptions.MutualTLSChannelError: If the default client certificate
            or key is malformed.
    FTN)r=   r   r   r2   rM   r   r5   r"   r#   r$   r%   )r>   has_certr'   r(   r?   r)   r*   s          r   load_default_client_certrP      s    & "#+I+K 335	
 &sJ	:N 	""	 & 22:>:%&s   A #B%A==Bc                      t               r
t               syt        j                  t        j                  j
                        } t        |       r| S dS )a  Get a default SSL context loaded with the default client certificate.

    Returns:
        ssl.SSLContext: An SSL context loaded with the default client
            certificate, or None if client certificates are not configured
            or available.

    Raises:
        google.auth.exceptions.MutualTLSChannelError: If the default client certificate
            or key is malformed.
    N)r=   r   rF   create_default_contextPurposeSERVER_AUTHrP   )r>   s    r   get_default_ssl_contextrU      s?     "#+I+K

$
$S[[%<%<
=C*3/39T9r   client_cert_availablec                    | 
t               } t        t        j                        }|xs dj	                         j                         }	 t        |      }|t        j                  k(  ry|t        j                  k(  ry|t        j                  k(  r| S y# t        $ r+ t        j                  dt        j                   d| d      w xY w)aT  Determine whether to use an mTLS endpoint.

    This relies on the GOOGLE_API_USE_MTLS_ENDPOINT environment variable. If set to
    "always", returns True. If set to "never", returns False. If set to "auto"
    or unset, returns whether a client certificate is available.

    Args:
        client_cert_available (Optional[bool]): indicating if a client certificate
            is available. If None, this is determined by checking if client
            certificates are enabled using :func:`should_use_client_cert`.

    Returns:
        bool: indicating if an mTLS endpoint should be used.
    Nr   zUnsupported z value 'z('. Accepted values: never, auto, always.TF)r=   r   r   GOOGLE_API_USE_MTLS_ENDPOINTstriplowerr	   r$   r   r%   r   r   r   )rV   use_mtls_endpointmodes      r   should_use_mtls_endpointr]   	  s    " $ 6 8/LLM*4f;;=CCE
"#45 ")))"((("'''$$ (  
..+HHI J!""JL
 	

s   	B 4C)Tr    )__doc__enumloggingosr   rF   typingr   google.authr   r   google.auth.transportr   	getLoggerr   _LOGGEREnumr	   r   r-   r:   r=   rG   bytesrM   boolrP   rU   r]   r   r   r   <module>rj      s        
  ( " .
'

H
%$)) 28*Z0. #'	/&	/&/& /& 	/&
 
/&d(#.. (T (V:#..!9 :( -1#%#D>#%	#%r   