
    kCin&                     "   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJrJ	r	  SSK
Jr  SSK
Jr  SSKJr  \R                  " \5      rSrSS	/rS
rSrSrSrSr\" \\\-  -
  \-  5      r\/\-  \/\-  -   rS rS rS rS rS r S r!S r"S r#S r$g)z'Helpers for Agent Identity credentials.    N)quoteurlparse)environment_vars)
exceptions)_mtls_helperzThe cryptography library is required for certificate-based authentication.Please install it with `pip install google-auth[cryptography]`.z+^agents\.global\.org-\d+\.system\.id\.goog$z,^agents\.global\.proj-\d+\.system\.id\.goog$z=/var/run/secrets/workload-spiffe-credentials/certificates.pem2   g?g      ?   c                     U =(       aH    [         R                  R                  U 5      =(       a"    [         R                  R                  U 5      S:  $ )z)Checks if a file exists and is not empty.r   )ospathexistsgetsize)r   s    ]/var/www/html/land-ocr/venv/lib/python3.13/site-packages/google/auth/_agent_identity_utils.py_is_certificate_file_readyr   =   s1    FBGGNN4(FRWW__T-BQ-FF    c                     SSK n [        R                  R                  [        R
                  5      nU(       d  gSn[         H  n [        US5       nU R                  U5      nUR                  S0 5      R                  S0 5      R                  S5      n[        U5      (       a  UsSSS5        s  $  SSS5        [        [         5      (       a  [         s  $ ["        R$                  " U5        M     [&        R(                  " S
[        R*                   S35      e! , (       d  f       Nl= f! [        [        [        4 a6    U(       d,  [        R                  SU[        R
                  [        5        S	n Nf = f)a  Gets the certificate path from the certificate config file.

The path to the certificate config file is read from the
GOOGLE_API_CERTIFICATE_CONFIG environment variable. This function
implements a retry mechanism to handle cases where the environment
variable is set before the files are available on the filesystem.

Returns:
    str: The path to the leaf certificate file.

Raises:
    google.auth.exceptions.RefreshError: If the certificate config file
        or the certificate file cannot be found after retries.
r   NFrcert_configsworkload	cert_pathzfCertificate config file not found at %s (from %s environment variable). Retrying for up to %s seconds.TzCertificate config or certificate file not found after multiple retries. Token binding protection is failing. You can turn off this protection by setting z) to false to fall back to unbound tokens.)jsonr   environgetr   GOOGLE_API_CERTIFICATE_CONFIG_POLLING_INTERVALSopenloadr   IOError
ValueErrorKeyError_LOGGERwarning_TOTAL_TIMEOUT_WELL_KNOWN_CERT_PATHtimesleepr   RefreshError7GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES)r   cert_config_pathhas_logged_warningintervalfcert_configr   s          r   #get_agent_identity_certificate_pathr.   B   sG    zz~~&6&T&TU&	&,"iilOONB7SR(S% 
 .i88$ -, 9 -, &&;<<(( 	

8A 'D 
!
!	\SS
T U*	* A -, X. 
	%@$$BB" &*"
	s8   D*AD&	D*3D*
D'	#D*'D**AE54E5c                  6   [         R                  R                  [        R                  S5      R                  5       S:H  n U (       a  g[        5       nU(       d  g[        US5       nUR                  5       nSSS5        [        W5      $ ! , (       d  f       N= f)a  Gets and parses the agent identity certificate if not opted out.

Checks if the user has opted out of certificate-bound tokens. If not,
it gets the certificate path, reads the file, and parses it.

Returns:
    The parsed certificate object if found and not opted out, otherwise None.
truefalseNrb)
r   r   r   r   r(   lowerr.   r   readparse_certificate)is_opted_outr   	cert_file
cert_bytess       r   (get_and_parse_agent_identity_certificater9      s     	

TT	
 %'		  35I	i	)^^%
 
 Z(( 
	s   &B


Bc                 v     SSK Jn  UR                  U 5      $ ! [         a  n[        [        5      UeSnAff = f)zParses a PEM-encoded certificate.

Args:
    cert_bytes (bytes): The PEM-encoded certificate bytes.

Returns:
    cryptography.x509.Certificate: The parsed certificate object.
r   x509N)cryptographyr<   load_pem_x509_certificateImportErrorCRYPTOGRAPHY_NOT_FOUND_ERROR)r8   r<   es      r   r5   r5      s9    ?%--j99 ?67Q>?s    
838c                     SSK Jn  SSKJn   U R                  R                  UR                  5      nUR                  R                  UR                  5      nU HW  n[        U5      nUR                  S:X  d  M   UR                  n[         H!  n[        R                   " X5      (       d  M       g   MY     g! UR                   a     gf = f! ["         a  n	[#        [$        5      U	eSn	A	ff = f)az  Checks if a certificate is an Agent Identity certificate.

This is determined by checking the Subject Alternative Name (SAN) for a
SPIFFE ID with a trust domain matching Agent Identity patterns.

Args:
    cert (cryptography.x509.Certificate): The parsed certificate object.

Returns:
    bool: True if the certificate is an Agent Identity certificate,
        False otherwise.
r   r;   )ExtensionOIDFspiffeTN)r=   r<   cryptography.x509.oidrC   
extensionsget_extension_for_oidSUBJECT_ALTERNATIVE_NAMEExtensionNotFoundvalueget_values_for_typeUniformResourceIdentifierr   schemenetloc,_AGENT_IDENTITY_SPIFFE_TRUST_DOMAIN_PATTERNSrematchr?   r@   )
certr<   rC   exturisuri
parsed_uritrust_domainpatternrA   s
             r   _is_agent_identity_certificaterY      s    ?%6	//7755C
 yy,,T-K-KLC!#J  H,)00KGxx66#  L	   %% 		  ?67Q>?sL   C %B7 AC ;.C -C 0C 7C
C 	C

C 
C,C''C,c                 h    SSK Jn  U R                  UR                  R                  5      n[
        R                  " U5      R                  5       n[        R                  " U5      R                  S5      nUR                  S5      n[        U5      $ ! [         a  n[        [        5      UeSnAff = f)a   Calculates the URL-encoded, unpadded, base64-encoded SHA256 hash of a
DER-encoded certificate.

Args:
    cert (cryptography.x509.Certificate): The parsed certificate object.

Returns:
    str: The URL-encoded, unpadded, base64-encoded SHA256 fingerprint.
r   )serializationzutf-8=N)cryptography.hazmat.primitivesr[   public_bytesEncodingDERhashlibsha256digestbase64	b64encodedecoderstripr   r?   r@   )rR   r[   der_certfingerprintbase64_fingerprintunpadded_base64_fingerprintrA   s          r   !calculate_certificate_fingerprintrl      s    ?@$$]%;%;%?%?@nnX.557 $--k:AA'J&8&?&?&D#011 ?67Q>?s   BB 
B1B,,B1c                     [        U 5      n[        R                  R                  [        R
                  S5      R                  5       S:H  nU=(       a    U$ )ak  Determines if a bound token should be requested.

This is based on the GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES
environment variable and whether the certificate is an agent identity cert.

Args:
    cert (cryptography.x509.Certificate): The parsed certificate object.

Returns:
    bool: True if a bound token should be requested, False otherwise.
r0   )rY   r   r   r   r   r(   r3   )rR   is_agent_certis_opted_ins      r   should_request_bound_tokenrp      sM     348M


TT	
 %'		  ([(r   c                  6    [         R                  " SS9u  pp#X4$ )zCCalls the client cert callback and returns the certificate and key.T)generate_encrypted_key)r   get_client_ssl_credentials)_r8   	key_bytes
passphrases       r   call_client_cert_callbackrw   
  s%    +7+R+R#,(A9   r   c                 V    U (       a  [        U 5      n[        U5      nU$ [        S5      e)z2Returns the fingerprint of the cached certificate.z"mTLS connection is not configured.)r5   rl   r   )cached_certcert_objcached_cert_fingerprints      r   get_cached_cert_fingerprintr|     s0    $[1"CH"M #" =>>r   )%__doc__rd   ra   loggingr   rP   r%   urllib.parser   r   google.authr   r   google.auth.transportr   	getLogger__name__r!   r@   rO   r$   _FAST_POLL_CYCLES_FAST_POLL_INTERVAL_SLOW_POLL_INTERVALr#   int_SLOW_POLL_CYCLESr   r   r.   r9   r5   rY   rl   rp   rw   r|    r   r   <module>r      s    .    	 	  ( ( " . 

H
%F  330 ,
 X      (+>>?CVV  ++.??-- 
G
>B)>?""?J?6).!#r   