
    gj              	           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ddlZ ej        e          Zej        defd            Z	 ddeded	ee         d
ej        fdZd Zd Z	 ddZddZdS )z<
Helper functions for mTLS in async for discovery of certs.
    N)Optional)
exceptionscontentc              #     K   t          j                    \  }}	 t          j        |d          5 }|                    |            ddd           n# 1 swxY w Y   |V  t          j                            |          rt          j        |           dS dS # t          j                            |          rt          j        |           w w xY w)zCreates a temporary file with the given content.

    Args:
        content (bytes): The content to write to the file.

    Yields:
        str: The path to the temporary file.
    wbN)tempfilemkstemposfdopenwritepathexistsremove)r   fd	file_pathfs       b/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/google/auth/aio/transport/mtls.py_create_temp_filer   "   s      $&&MB	!Yr4   	AGGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 7>>)$$ 	!Ii     	! 	!27>>)$$ 	!Ii    	!s.   B AB AB AB 6C
cert_bytes	key_bytes
passphrasereturnc           	         t          |           5 }t          |          5 }	 t          j        t          j        j                  }|                    |||           |cddd           cddd           S # t          j        t          t          t          t          f$ r}t          j        d          |d}~ww xY w# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )a  Creates an SSLContext with the given client certificate and key.
    This function writes the certificate and key to temporary files so that
    ssl.create_default_context can load them, as the ssl module requires
    file paths for client certificates. These temporary files are deleted
    immediately after the SSL context is created.
    Args:
        cert_bytes (bytes): The client certificate content in PEM format.
        key_bytes (bytes): The client private key content in PEM format.
        passphrase (Optional[bytes]): The passphrase for the private key, if any.
    Returns:
        ssl.SSLContext: The configured SSL context with client certificate.

    Raises:
        google.auth.exceptions.TransportError: If there is an error loading the certificate.
    )certfilekeyfilepasswordNz3Failed to load client certificate and key for mTLS.)r   sslcreate_default_contextPurposeSERVER_AUTHload_cert_chainSSLErrorOSErrorIOError
ValueErrorRuntimeErrorr   TransportError)r   r   r   	cert_pathkey_pathcontextexcs          r   make_client_cert_ssl_contextr,   8   s   $ 
:	&	& )5F6 6 			01HIIG##"Hz $                   gw
LI 	 	 	+E 	                         sQ   CB<<A7C7(B9B44B99B<<C 	 CC 	CCCc                    K   	 t          j        | g|R   d{V S # t          $ r+ t          j                    } |j        d| g|R   d{V cY S w xY w)zRun a blocking function in an executor to avoid blocking the event loop.

    This implements the non-blocking execution strategy for disk I/O operations.
    N)asyncio	to_threadAttributeErrorget_running_looprun_in_executor)funcargsloops      r   _run_in_executorr6   Y   s      
=&t3d3333333333 = = =')))T)$<t<<<<<<<<<<<<=s    2AAc                      t           j        j        j                            d          st          j        d          d } | S )a  Get a callback which returns the default client SSL credentials.

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

    Raises:
        google.auth.exceptions.DefaultClientCertSourceError: If the default
            client SSL credentials don't exist or are malformed.
    F)include_context_awarez(Default client cert source doesn't existc                     K   	 t                       d {V \  } }}n6# t          t          t          f$ r}t	          j        |          }||d }~ww xY w||fS N)get_client_cert_and_keyr#   r&   r%   r   MutualTLSChannelError)_r   r   
caught_excnew_excs        r   callbackz,default_client_cert_source.<locals>.callbacky   s|      	*-D-F-F'F'F'F'F'F'F$Az99z2 	* 	* 	* 6zBBGz)	* 9$$s    AAA)googleauth	transportmtlshas_default_client_cert_sourcer   r<   )r@   s    r   default_client_cert_sourcerF   g   s]     ; %DD# E   
 .6
 
 	
% % % O    c                    K   t          t          j        j        j        j        | d           d{V \  }}|r|rd||dfS dS )a  Returns the client side certificate, private key and passphrase.

    We look for certificates and keys with the following order of priority:
        1. Certificate and key specified by certificate_config.json.
               Currently, only X.509 workload certificates are supported.

    Args:
        certificate_config_path (str): The certificate_config.json file path.

    Returns:
        Tuple[bool, bytes, bytes, bytes]:
            A boolean indicating if cert, key and passphrase are obtained, the
            cert bytes and key bytes both in PEM format, and passphrase bytes.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when getting
            the cert, key and passphrase.
    FNT)FNNN)r6   rA   rB   rC   _mtls_helper_get_workload_cert_and_key)certificate_config_pathcertkeys      r   get_client_ssl_credentialsrN      ss      . '*E       ID#  % %T3$$""rG   c                    K   | r1 |             }	 | d{V \  }}n# t           $ r |\  }}Y nw xY wd||fS t                       d{V \  }}}}|||fS )a  Returns the client side certificate and private key. The function first
    tries to get certificate and key from client_cert_callback; if the callback
    is None or doesn't provide certificate and key, the function tries application
    default SSL credentials.

    Args:
        client_cert_callback (Optional[Callable[[], (bytes, bytes)]]): An
            optional callback which returns client certificate bytes and private
            key bytes both in PEM format.

    Returns:
        Tuple[bool, bytes, bytes]:
            A boolean indicating if cert and key are obtained, the cert bytes
            and key bytes both in PEM format.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when getting
            the cert and key.
    NT)	TypeErrorrN   )client_cert_callbackresultrL   rM   has_certr=   s         r   r;   r;      s      (  %%''	$ID## 	 	 	ID###	T3#=#?#???????HdCT3s    ..r:   )__doc__r.   
contextlibloggingr
   r   r   typingr   google.authr   "google.auth.transport._mtls_helperrA   google.auth.transport.mtls	getLogger__name___LOGGERcontextmanagerbytesr   
SSLContextr,   r6   rF   rN   r;    rG   r   <module>rb      s`          				 



        " " " " " " ) ) ) ) ! ! ! !
'
H
%
% !u ! ! ! !, HL "'5=e_^   B= = =  > ! #  #  #  #F     rG   