
    &g9                     R   d Z ddlZddlZddlmZmZ ddlZddlZddlm	Z	 dZ
dZdZdZ ej        d	ej                  Z ej        d
ej                  Z ej        e          Z ej        dej                  Zd Zd ZddZddZd Zd Zd Zd ZddZde
efdZddZ d Z!dS )z/Helper functions for getting mTLS cert and key.    N)environpath)
exceptionsz,~/.secureConnect/context_aware_metadata.jsonz(~/.config/gcloud/certificate_config.jsonGOOGLE_API_CERTIFICATE_CONFIGcert_provider_commands:   -----BEGIN CERTIFICATE-----.+-----END CERTIFICATE-----?
?sH   -----BEGIN [A-Z ]*PRIVATE KEY-----.+-----END [A-Z ]*PRIVATE KEY-----?
?s6   -----BEGIN PASSPHRASE-----(.+)-----END PASSPHRASE-----c                     t          j        |           } t          j        |           st                              d|            dS | S )aJ  Checks for config file path. If it exists, returns the absolute path with user expansion;
    otherwise returns None.

    Args:
        config_path (str): The config file path for either context_aware_metadata.json or certificate_config.json for example

    Returns:
        str: absolute path if exists and None otherwise.
    z%s is not found.N)r   
expanduserexists_LOGGERdebug)config_paths    T/var/www/api/venv/lib/python3.11/site-packages/google/auth/transport/_mtls_helper.py_check_config_pathr   3   sE     /+..K;{## (+666t    c                     	 t          |           5 }t          j        |          }ddd           n# 1 swxY w Y   n)# t          $ r}t	          j        |          }||d}~ww xY w|S )a\  Reads and loads JSON from the given path. Used to read both X509 workload certificate and
    secure connect configurations.

    Args:
        path (str): the path to read from.

    Returns:
        Dict[str, str]: The JSON stored at the file.

    Raises:
        google.auth.exceptions.ClientCertError: If failed to parse the file as JSON.
    N)openjsonload
ValueErrorr   ClientCertError)r   f	json_data
caught_excnew_excs        r   _load_json_filer   D   s    &$ZZ 	%1	!I	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% & & &,Z88:%& s-   > 2> 6> 6> 
A$AA$c                 R    t          |           \  }}||dS t          ||          S )a  Read the workload identity cert and key files specified in the certificate config provided.
    If no config path is provided, check the environment variable: "GOOGLE_API_CERTIFICATE_CONFIG"
    first, then the well known gcloud location: "~/.config/gcloud/certificate_config.json".

    Args:
        certificate_config_path (string): The certificate config path. If no path is provided,
        the environment variable will be checked first, then the well known gcloud location.

    Returns:
        Tuple[Optional[bytes], Optional[bytes]]: client certificate bytes in PEM format and key
            bytes in PEM format.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when retrieving
        the certificate or key information.
    NNN) _get_workload_cert_and_key_paths_read_cert_and_key_files)certificate_config_path	cert_pathkey_paths      r   _get_workload_cert_and_keyr#   [   s7    $ ;;RSSIxX-z#Ix888r   c                     | ,t          j        t          d          }|	|dk    r|} nt          } t	          j        |           } t	          j        |           sdS | S )a   Get the certificate configuration path based on the following order:

    1: Explicit override, if set
    2: Environment variable, if set
    3: Well-known location

    Returns "None" if the selected config file does not exist.

    Args:
        certificate_config_path (string): The certificate config path. If provided, the well known
        location and environment variable will be ignored.

    Returns:
        The absolute path of the certificate config file, and None if the file does not exist.
    N )r   get_CERTIFICATE_CONFIGURATION_ENV&CERTIFICATE_CONFIGURATION_DEFAULT_PATHr   r	   r
   )r    env_paths     r   _get_cert_config_pathr*   u   sh    " &;=tDDHNN&.##&L#"o.EFF;.// t""r   c                    t          |           }|dS t          |          }d|vr't          j        d                    |                    |d         }d|vr't          j        d                    |                    |d         }d|vr't          j        d                    |                    |d         }d|vr't          j        d	                    |                    |d         }||fS )
Nr   cert_configszWCertificate config file {} is in an invalid format, a "cert configs" object is expectedworkloadzXCertificate config file {} is in an invalid format, a "workload" cert config is expectedr!   ziCertificate config file {} is in an invalid format, a "cert_path" is expected in the workload cert configr"   zhCertificate config file {} is in an invalid format, a "key_path" is expected in the workload cert config)r*   r   r   r   format)r   absolute_pathdatar,   r-   r!   r"   s          r   r   r      s:   )+66Mz=))DT!!(ell 
 
 	

 'L%%(fmm 
 
 	

 J'H(""(w~~ 
 
 	

 %I!!(v}} 
 
 	

 
#Hhr   c                 F    t          |           }t          |          }||fS N)_read_cert_file_read_key_file)r!   r"   	cert_datakey_datas       r   r   r      s'    	**Ih''Hhr   c                 2   t          | d          5 }|                                }d d d            n# 1 swxY w Y   t          j        t          |          }t          |          dk    r't          j        d                    |                     |d         S )Nrb   z[Certificate file {} is in an invalid format, a single PEM formatted certificate is expectedr   )	r   readrefindall_CERT_REGEXlenr   r   r.   )r!   	cert_filer5   
cert_matchs       r   r3   r3      s    	i		 %)NN$$	% % % % % % % % % % % % % % % K33J
:!(ipp 
 
 	

 a=   266c                 2   t          | d          5 }|                                }d d d            n# 1 swxY w Y   t          j        t          |          }t          |          dk    r't          j        d                    |                     |d         S )Nr8   r9   z[Private key file {} is in an invalid format, a single PEM formatted private key is expectedr   )	r   r:   r;   r<   
_KEY_REGEXr>   r   r   r.   )r"   key_filer6   	key_matchs       r   r4   r4      s    	h		 #==??# # # # # # # # # # # # # # # 
:x00I
9~~(ipp 
 
 	
 Q<rA   Fc                    	 t          j        | t           j        t           j                  }|                                \  }}n)# t          $ r}t          j        |          }||d}~ww xY w|j        dk    rt          j        d|j        z            t          j	        t          |          }t          |          dk    rt          j        d          t          j	        t          |          }t          |          dk    rt          j        d          t          j	        t          |          }	|rnt          |	          dk    rt          j        d          d	|d         vrt          j        d
          |d         |d         |	d                                         fS d	|d         v rt          j        d          t          |	          dk    rt          j        d          |d         |d         dfS )a!  Run the provided command, and return client side mTLS cert, key and
    passphrase.

    Args:
        command (List[str]): cert provider command.
        expect_encrypted_key (bool): If encrypted private key is expected.

    Returns:
        Tuple[bytes, bytes, bytes]: client certificate bytes in PEM format, key
            bytes in PEM format and passphrase bytes.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when running
            the cert provider command or generating cert, key and passphrase.
    )stdoutstderrNr   z5Cert provider command returns non-zero status code %sr9   z,Client SSL certificate is missing or invalidz$Client SSL key is missing or invalidz Passphrase is missing or invalids	   ENCRYPTEDz!Encrypted private key is expectedz%Encrypted private key is not expectedzPassphrase is not expected)
subprocessPopenPIPEcommunicateOSErrorr   r   
returncoder;   r<   r=   r>   rC   _PASSPHRASE_REGEXstrip)
commandexpect_encrypted_keyprocessrG   rH   r   r   r@   rE   passphrase_matchs
             r   _run_cert_provider_commandrU      s    &"JOJO
 
 
 !,,.. & & &,Z88:%&
 Q(CgFXX
 
 	

 K00J
:!()WXXX
:v..I
9~~()OPPPz"3V<< H  A%%,-OPPPy|++,-PQQQ!}il,<Q,?,E,E,G,GGGy|##()PQQQ
q  ()EFFFa=)A,,,s   AA 
A+A&&A+c                 `   t          |          }|rt          |          \  }}|r|rd||dfS t          |          }|rot          |          }t          |vrt	          j        d          |t                   }| rd|vr|                    d           t          ||           \  }}}	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.
        2. Certificate and key specified by context aware metadata (i.e. SecureConnect).

    Args:
        generate_encrypted_key (bool): If set to True, encrypted private key
            and passphrase will be generated; otherwise, unencrypted private key
            will be generated and passphrase will be None. This option only
            affects keys obtained via context_aware_metadata.json.
        context_aware_metadata_path (str): The context_aware_metadata.json file path.
        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.
    TNz"Cert provider command is not foundz--with_passphrase)rR   )FNNN)r   r#   r   _CERT_PROVIDER_COMMANDr   r   appendrU   )
generate_encrypted_keycontext_aware_metadata_pathr    cert_config_pathcertkeymetadata_pathmetadata_jsonrQ   
passphrases
             r   get_client_ssl_credentialsra     s    > **ABB )./?@@	c 	)C 	)sD(( ''BCCM +'66!66,-QRRR 67! 	0&9&H&HNN./// !;*@!
 !
 !
c: T3
**""r   c                 ^    | r |             \  }}d||fS t          d          \  }}}}|||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.
    TF)rY   )ra   )client_cert_callbackr\   r]   has_cert_s        r   get_client_cert_and_keyrf   T  sP    (  ((**	cT37uUUUHdCT3r   c                 ~    ddl m} |                    |j        | |          }|                    |j        |          S )a#  A helper function to decrypt the private key with the given passphrase.
    google-auth library doesn't support passphrase protected private key for
    mutual TLS channel. This helper function can be used to decrypt the
    passphrase protected private key in order to estalish mutual TLS channel.

    For example, if you have a function which produces client cert, passphrase
    protected private key and passphrase, you can convert it to a client cert
    callback function accepted by google-auth::

        from google.auth.transport import _mtls_helper

        def your_client_cert_function():
            return cert, encrypted_key, passphrase

        # callback accepted by google-auth for mutual TLS channel.
        def client_cert_callback():
            cert, encrypted_key, passphrase = your_client_cert_function()
            decrypted_key = _mtls_helper.decrypt_private_key(encrypted_key,
                passphrase)
            return cert, decrypted_key

    Args:
        key (bytes): The private key bytes in PEM format.
        passphrase (bytes): The passphrase bytes.

    Returns:
        bytes: The decrypted private key in PEM format.

    Raises:
        ImportError: If pyOpenSSL is not installed.
        OpenSSL.crypto.Error: If there is any problem decrypting the private key.
    r   )crypto)r`   )OpenSSLrh   load_privatekeyFILETYPE_PEMdump_privatekey)r]   r`   rh   pkeys       r   decrypt_private_keyrn   p  sQ    B  !!&"5sz!RRD !!&"5t<<<r   r2   )F)"__doc__r   loggingosr   r   r;   rI   google.authr   CONTEXT_AWARE_METADATA_PATHr(   r'   rW   compileDOTALLr=   rC   	getLogger__name__r   rO   r   r   r#   r*   r   r   r3   r4   rU   ra   rf   rn    r   r   <module>ry      s   6 5           				     " " " " " "L )S &!@ 0 bjCRY  RZQI 

 '
H
%
% BJ=ry  
  "  .9 9 9 94# # # #<' ' 'T      3- 3- 3- 3-n ! ;B:# :# :# :#z   8'= '= '= '= '=r   