
    `gj                         d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  ej                  e      ZdZh dZddZdd	Zdd
ZddddZddZddZy)zPreventive SSL CA certificate checks for Hermes Agent.

This module catches broken CA bundle paths before OpenAI/httpx turns them into
opaque ``FileNotFoundError: [Errno 2] No such file or directory`` failures.
    )annotationsN)Path)SSLConfigurationError)HERMES_CA_BUNDLESSL_CERT_FILEREQUESTS_CA_BUNDLECURL_CA_BUNDLE>   1onyestruec                 r    t        j                  dd      j                         j                         t        v S )NHERMES_SKIP_SSL_GUARD )osgetenvstriplower_SKIP_VALUES     B/root/.hermes/venv/lib/python3.12/site-packages/agent/ssl_guard.py_skip_ssl_guard_enabledr      s+    99,b1779??A\QQr   c                      	 y)NzRepair: python -m pip install --force-reinstall certifi openai httpx
If you configured a custom corporate CA bundle, fix or unset the broken CA bundle environment variable.r   r   r   r   _repair_hintr       s    	1r   c                2    t        |  dt                      S )z=Create a consistent, user-actionable SSL configuration error.
)r   r   )messages    r   _ssl_errr   (   s     G9B|~.>!?@@r   Frequire_substantialc                  t        |      j                         }|j                         st        |  d|       |j	                         st        |  d|       |r.|j                         j                  dk  rt        |  d| d      	 t        j                  t        |            }|j                         st        |  d| d	      y # t        $ r}t        |  d| d|       |d }~ww xY w)
Nz  points to a missing CA bundle: z% does not point to a CA bundle file: i   z at z appears corrupted (too small))cafilez CA bundle at z cannot be loaded: z did not load any certificates)r   
expanduserexistsr   is_filestatst_sizesslcreate_default_contextstr	Exceptionget_ca_certs)labelvaluer!   pathctxexcs         r   _validate_bundle_pathr3   -   s    ;!!#D;;=% @HII<<>% EeWMNNtyy{22T9%UG+IJKKY((D	: %ug5STUU   Y%ug5HNOUXXYs   C 	C.C))C.c                 :   t               rt        j                  d       yt        D ]&  } t	        j
                  |       }|st        | |       ( 	 ddl}t        |j                               }t        d|d       y# t        $ r}t        d|       |d}~ww xY w)a  Verify configured and bundled CA certificates are present and loadable.

    Raises:
        SSLConfigurationError: If an explicit CA-bundle environment variable
            points at a bad path, or if certifi's bundled ``cacert.pem`` is
            missing/corrupt.
    z5SSL CA bundle guard skipped via HERMES_SKIP_SSL_GUARDNr   zcertifi is not importable: certifiTr    )r   loggerdebug_CA_BUNDLE_ENV_VARSr   r   r3   r5   r,   r   r+   where)env_varr/   r5   r2   	ca_bundles        r   verify_ca_bundler<   =   s      LM& 2		'"!'512
E GMMO$I)YDI	  E4SE:;DEs   A= =	BBBc                     t                y)a  Backward-compatible wrapper for older call sites.

    The old PR name mentioned a platform fallback, but allowing startup with a
    broken certifi bundle still leaves httpx/OpenAI and requests call sites
    failing later. Keep the wrapper name but enforce the same check.
    N)r<   r   r   r   verify_ca_bundle_with_fallbackr>   W   s
     r   )returnbool)r?   r+   )r   r+   r?   r   )r.   r+   r/   r+   r!   r@   r?   None)r?   rA   )__doc__
__future__r   loggingr   r)   pathlibr   agent.errorsr   	getLogger__name__r6   r8   r   r   r   r   r3   r<   r>   r   r   r   <module>rI      sh    #  	 
  .			8	$  *RA
 RW V J4r   