
    0j                         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dS )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truereturnboolc                     t          j        dd                                                                          t          v S )NHERMES_SKIP_SSL_GUARD )osgetenvstriplower_SKIP_VALUES     5/home/rurouni/.hermes/hermes-agent/agent/ssl_guard.py_skip_ssl_guard_enabledr      s2    9,b117799??AA\QQr   strc                     	 dS )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   messager   c                B    t          |  dt                                 S )z=Create a consistent, user-actionable SSL configuration error.
)r   r   )r   s    r   _ssl_errr"   (   s"     G!?!?|~~!?!?@@@r   Frequire_substantiallabelvaluer$   Nonec               >   t          |                                          }|                                st          |  d|           |                                st          |  d|           |r2|                                j        dk     rt          |  d| d          	 t          j        t          |                    }n*# t          $ r}t          |  d| d|           |d }~ww xY w|                                st          |  d| d	          d S )
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_contextr   	Exceptionget_ca_certs)r%   r&   r$   pathctxexcs         r   _validate_bundle_pathr6   -   s[   ;;!!##D;;== J%HHHHIII<<>> O%MMeMMNNN Ltyy{{2T99%JJUJJJKKKY(D		::: Y Y Y%NNuNNNNOOUXXY V%TTuTTTUUUV Vs   '"C
 

C1C,,C1c                 v   t                      rt                              d           dS t          D ](} t	          j        |           }|rt          | |           )	 ddl}n%# t          $ r}t          d|           |d}~ww xY wt          |                                          }t          d|d           dS )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   r6   r8   r1   r"   r   where)env_varr&   r8   r5   	ca_bundles        r   verify_ca_bundler?   =   s        LMMM& 2 2	'"" 	2!'5111E E E E:S::;;DE GMMOO$$I)YDIIIIIIs   A! !
B+A>>Bc                 "    t                       dS )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_fallbackrA   W   s     r   )r   r   )r   r   )r   r   r   r   )r%   r   r&   r   r$   r   r   r'   )r   r'   )__doc__
__future__r   loggingr   r/   pathlibr   agent.errorsr   	getLogger__name__r9   r;   r   r   r   r"   r6   r?   rA   r   r   r   <module>rI      s.    # " " " " "  				 



       . . . . . .		8	$	$  *))R R R R   A A A A
 RW V V V V V V J J J J4     r   