Ë
    uPêjA4  ã                   ó²   — d dl Z d dlZd dlmZm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 d dlmZmZmZ d dlmZ dd	lmZmZ dd
lmZ  G d„ de«      Zy)é    N)ÚAnyÚFinal)Úquote)Úverbose_logger)ÚInMemoryCache)Ú_get_httpx_clientÚget_async_httpx_clientÚhttpxSpecialProvider)ÚKeyManagementSystemé   )ÚBaseSecretManagerÚraise_if_unsafe_secret_name)Ústr_to_boolc                   óž  — e Zd Zd„ Zdefd„Zdefd„Zdeddfd„Zdedefd„Z		 	 dded	edz  d
e
ej                  z  dz  dedz  fd„Z	 	 dded	edz  d
e
ej                  z  dz  dedz  fd„Z	 	 	 	 ddedededz  d	edz  d
e
ej                  z  dz  deez  dz  deeef   fd„Z	 	 	 ddededz  d	edz  d
e
ej                  z  dz  def
d„Zy)ÚCyberArkSecretManagerc                 óT  — ddl m}m} t        j                  dd«      | _        t        j                  dd«      | _        t        j                  dd«      | _        t        j                  d	d
«      | _        t        j                  dd
«      | _	        t        j                  dd
«      | _
        t        t        j                  d«      «      }|�|nd| _        | j                  s#| j                  r| j                  st        d«      ‚| t        _        t         j"                  t        _        t'        t        j(                  j+                  dd«      «      }t-        |¬«      | _        |dur"t        d|j0                  j2                  › �«      ‚| j                  st5        j6                  d«       y y )Nr   )ÚCommonProxyErrorsÚpremium_userÚCYBERARK_API_BASEzhttp://127.0.0.1:8080ÚCYBERARK_ACCOUNTÚdefaultÚCYBERARK_USERNAMEÚadminÚCYBERARK_API_KEYÚ ÚCYBERARK_CLIENT_CERTÚCYBERARK_CLIENT_KEYÚCYBERARK_SSL_VERIFYTzƒMissing CyberArk credentials. Please set CYBERARK_API_KEY or both CYBERARK_CLIENT_CERT and CYBERARK_CLIENT_KEY in your environment.ÚCYBERARK_REFRESH_INTERVALÚ300)Údefault_ttlz=CyberArk secret manager is only available for premium users. zzCyberArk SSL verification is disabled. This is insecure and should only be used for testing with self-signed certificates.)Úlitellm.proxy.proxy_serverr   r   ÚosÚgetenvÚconjur_addrÚconjur_accountÚconjur_usernameÚconjur_api_keyÚtls_cert_pathÚtls_key_pathr   Ú
ssl_verifyÚ
ValueErrorÚlitellmÚsecret_manager_clientr   ÚCYBERARKÚ_key_management_systemÚintÚenvironÚgetr   ÚcacheÚnot_premium_userÚvaluer   Úwarning)Úselfr   r   Ússl_verify_envÚ_refresh_intervals        út/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/secret_managers/cyberark_secret_manager.pyÚ__init__zCyberArkSecretManager.__init__   sg  € ßNô Ÿ9™9Ð%8Ð:QÓRˆÔÜ Ÿi™iÐ(:¸IÓFˆÔÜ!Ÿy™yÐ)<¸gÓFˆÔÜ Ÿi™iÐ(:¸BÓ?ˆÔô  ŸY™YÐ'=¸rÓBˆÔÜŸI™IÐ&;¸RÓ@ˆÔô !,¬B¯I©IÐ6KÓ,LÓ MˆØ2@Ð2L¡ÐRVˆŒð ×"Ò"¨D×,>Ò,>À4×CTÒCTÜð Vóð ð )-ŒÔ%Ü)<×)EÑ)EŒÔ&ô $'¤r§z¡z§~¡~Ð6QÐSXÓ'YÓ#ZÐÜ"Ð/@ÔAˆŒ
à˜tÑ#ÜØOÐPa×PrÑPr×PxÑPxÐOyÐzóð ð �ŠÜ×"Ñ"ð Mõð ó    Úreturnc                 óV  — | j                   j                  d«      }|�|S t        j                  d«       | j                  › d| j
                  › d| j                  › d�}	 | j                  ra| j                  rUt        j                  | j                  | j                  f| j                  ¬«      }|j                  || j                  ¬«      }n?t        d	| j                  i¬
«      }|j                  j                  || j                  ¬«      }|j!                  «        |j"                  }t%        j&                  |j)                  «       «      j+                  «       }t        j                  d«       | j                   j-                  d|¬«       |S # t.        $ r}t1        d|› �«      ‚d}~ww xY w)zì
        Authenticate with CyberArk Conjur and get a session token.

        The token is a JSON object that must be base64-encoded for use in subsequent requests.

        Returns:
            str: Base64-encoded session token
        Úcyberark_auth_tokenNz&Authenticating with CyberArk Conjur...z/authn/ú/z/authenticate)ÚcertÚverify)Úcontentr+   ©Úparamsz0Successfully authenticated with CyberArk Conjur.)Úkeyr6   z+Could not authenticate to CyberArk Conjur: )r4   Ú	get_cacher   Údebugr%   r&   r'   r)   r*   ÚhttpxÚClientr+   Úpostr(   r   ÚclientÚraise_for_statusÚtextÚbase64Ú	b64encodeÚencodeÚdecodeÚ	set_cacheÚ	ExceptionÚRuntimeError)	r8   Úcached_tokenÚauth_urlÚhttp_clientÚrespÚhttp_handlerÚ
token_jsonÚ	token_b64Úes	            r;   Ú_authenticatez#CyberArkSecretManager._authenticateA   s‚  € ð #Ÿj™j×2Ñ2Ð3HÓIˆØÐ#ØÐä×ÑÐEÔFØ!×-Ñ-Ð.¨g°d×6IÑ6IÐ5JÈ!ÈD×L`ÑL`ÐKaÐanÐoˆð	RØ×!Ò! d×&7Ò&7ä%*§\¡\Ø×,Ñ,¨d×.?Ñ.?Ð@ØŸ?™?ô&�ð #×'Ñ'¨¸$×:MÑ:MÐ'ÓN‘ô '8ÀÈdÏoÉoÐ?^Ô&_�Ø#×*Ñ*×/Ñ/°À$×BUÑBUÐ/ÓV�à×!Ñ!Ô#ð !%§	¡	ˆJÜ%×/Ñ/°
×0AÑ0AÓ0CÓD×KÑKÓMˆIä× Ñ Ð!SÔTð �J‰J× Ñ Ð%:À)Ð ÔLàÐøÜò 	RÜÐ!LÈQÈCÐPÓQÐQûð	Rús   ÁD,F Æ	F(ÆF#Æ#F(c                 ó2   — | j                  «       }dd|› d�iS )z—
        Get headers for CyberArk API requests including authentication.

        Returns:
            dict: Headers with authentication token
        ÚAuthorizationzToken token="ú")r_   )r8   Útokens     r;   Ú_get_request_headersz*CyberArkSecretManager._get_request_headersn   s&   € ð ×)Ñ)Ó+ˆØ =°°°qÐ!9Ð:Ð:r=   Úsecret_nameNc                 óþ  — t        |«       | j                  › d| j                  › d�}t        j                  |d¬«      j                  «       }d|› d�}	 t        d| j                  i¬«      }|j                  j                  |i | j                  «       ¥d	d
i¥|¬«      }|j                  «        t        j                  d|«       y# t        j                  $ r}}|j                   j"                  dv rt        j                  d|«       nDt        j$                  d|j                   j"                  |j                   j&                  «       Y d}~yY d}~yd}~wt(        $ r }t        j$                  d|«       Y d}~yd}~ww xY w)z±
        Ensure a variable exists in CyberArk Conjur by creating a policy entry if needed.

        Args:
            secret_name: Name of the variable to ensure exists
        z
/policies/z/policy/rootrb   )Údefault_stylez- !variable ú
r+   rE   zContent-Typezapplication/x-yaml)ÚheadersrD   z%Created policy entry for variable: %s)i™  i¦  z8Variable %s already exists or policy conflict (expected)z)Could not ensure variable exists: %s - %sNz"Error ensuring variable exists: %s)r   r%   r&   ÚyamlÚ	safe_dumpÚstripr   r+   rM   rL   rd   rN   r   rI   rJ   ÚHTTPStatusErrorÚresponseÚstatus_coder7   rO   rU   )r8   re   Ú
policy_urlÚquoted_nameÚpolicy_yamlrM   rZ   r^   s           r;   Ú_ensure_variable_existsz-CyberArkSecretManager._ensure_variable_existsx   s_  € ô 	$ KÔ0Ø#×/Ñ/Ð0°
¸4×;NÑ;NÐ:OÈ|Ð\ˆ
ä!Ÿ^™^¨KÀsÔK×QÑQÓSˆØ+¨K¨=¸Ð;ˆð	LÜ-°lÀDÇOÁOÐ5TÔUˆFØ Ÿ-™-×,Ñ,ØðØ×/Ñ/Ó1ðà"Ð$8ñð $ð -ó ˆDð ×!Ñ!Ô#Ü× Ñ Ð!HÈ+ÕVøÜ×$Ñ$ò 	à�z‰z×%Ñ%¨Ñ3Ü×$Ñ$Ð%_ÐalÕmä×&Ñ&Ø?ÀÇÁ×AWÑAWÐYZ×YcÑYc×YhÑYh÷ñ ô nûô
 ò 	LÜ×"Ñ"Ð#GÈ×KÑKûð	Lús&   ÁA0C ÃE<ÃA.EÅE<ÅE7Å7E<c                 óX   — t        |d¬«      }| j                  › d| j                  › d|› �S )zÖ
        Build the URL for accessing a secret in CyberArk Conjur.

        Args:
            secret_name: Name of the secret (will be URL-encoded)

        Returns:
            str: Full URL for the secret
        r   )Úsafez	/secrets/z
/variable/)r   r%   r&   )r8   re   Úencoded_names      r;   Úget_urlzCyberArkSecretManager.get_urlž   s5   € ô $ K°bÔ9ˆØ×"Ñ"Ð# 9¨T×-@Ñ-@Ð,AÀÈLÈ>ÐZÐZr=   Úoptional_paramsÚtimeoutc              ƒ   óÌ  K  — | j                   j                  |«      �| j                   j                  |«      S t        t        j                  d| j
                  i¬«      }	 | j                  |«      }|j                  || j                  «       ¬«      ƒ d{  –—† }|j                  «        |j                  }| j                   j                  ||«       |S 7 Œ># t        j                  $ rP}|j                  j                  dk(  rt!        j"                  d|«       nt!        j$                  d|«       Y d}~yd}~wt&        $ r }t!        j$                  d|«       Y d}~yd}~ww xY w­w)a_  
        Reads a secret from CyberArk Conjur using an async HTTPX client.

        Args:
            secret_name: Name/path of the secret to read
            optional_params: Additional parameters (not used for Conjur)
            timeout: Request timeout

        Returns:
            Optional[str]: The secret value if found, None otherwise
        Nr+   ©Úllm_providerrF   ©ri   é”  ú&Secret %s not found in CyberArk Conjurú-Error reading secret from CyberArk Conjur: %s)r4   rH   r	   r
   ÚSecretManagerr+   rw   r3   rd   rN   rO   rT   rJ   rm   rn   ro   r   rI   Ú	exceptionrU   )	r8   re   rx   ry   Úasync_clientÚurlrn   Úsecret_valuer^   s	            r;   Úasync_read_secretz'CyberArkSecretManager.async_read_secret¬   s)  è ø€ ð$ �:‰:×Ñ Ó,Ð8Ø—:‘:×'Ñ'¨Ó4Ð4ä4Ü-×;Ñ;Ø  $§/¡/Ð2ô
ˆð
	ØŸ™ kÓ2ˆCØ$0×$4Ñ$4°SÀ$×B[ÑB[ÓB]Ð$4Ó$^×^ˆHØ×%Ñ%Ô'ð #+§-¡-ˆLØ�J‰J× Ñ  ¨lÔ;ØÐð _ùô ×$Ñ$ò 	Ø�z‰z×%Ñ%¨Ò,Ü×$Ñ$Ð%MÈ{Õ[ä×(Ñ(Ð)XÐZ[Ô\ÜûÜò 	Ü×$Ñ$Ð%TÐVWÔXÜûð	üsU   ‚AE$Á!5C ÂCÂ=C ÃE$ÃC ÃE!Ã*AD5Ä0E$Ä5E!ÅEÅE$ÅE!Å!E$c                 ó¦  — | j                   j                  |«      �| j                   j                  |«      S t        d| j                  i¬«      }	 | j	                  |«      }|j
                  j                  || j                  «       ¬«      }|j                  «        |j                  }| j                   j                  ||«       |S # t        j                  $ rP}|j                  j                  dk(  rt        j                   d|«       nt        j"                  d|«       Y d}~yd}~wt$        $ r }t        j"                  d|«       Y d}~yd}~ww xY w)a]  
        Reads a secret from CyberArk Conjur using a sync HTTPX client.

        Args:
            secret_name: Name/path of the secret to read
            optional_params: Additional parameters (not used for Conjur)
            timeout: Request timeout

        Returns:
            Optional[str]: The secret value if found, None otherwise
        Nr+   rE   r}   r~   r   r€   )r4   rH   r   r+   rw   rM   r3   rd   rN   rO   rT   rJ   rm   rn   ro   r   rI   r‚   rU   )	r8   re   rx   ry   Úsync_clientr„   rn   r…   r^   s	            r;   Úsync_read_secretz&CyberArkSecretManager.sync_read_secretÚ   s  € ð$ �:‰:×Ñ Ó,Ð8Ø—:‘:×'Ñ'¨Ó4Ð4ä.°|ÀTÇ_Á_Ð6UÔVˆð	ØŸ™ kÓ2ˆCØ)×0Ñ0×4Ñ4°SÀ$×B[ÑB[ÓB]Ð4Ó^ˆHØ×%Ñ%Ô'ð #+§-¡-ˆLØ�J‰J× Ñ  ¨lÔ;ØÐøä×$Ñ$ò 	Ø�z‰z×%Ñ%¨Ò,Ü×$Ñ$Ð%MÈ{Õ[ä×(Ñ(Ð)XÐZ[Ô\ÜûÜò 	Ü×$Ñ$Ð%TÐVWÔXÜûð	ús&   ÁA5C ÃEÃAD$Ä$EÄ0EÅEr…   ÚdescriptionÚtagsc              ƒ   óÔ  K  — t        t        j                  d| j                  i¬«      }	 | j	                  |«       | j                  |«      }|j                  || j                  «       |¬«      ƒ d{  –—† }	|	j                  «        | j                  j                  ||«       dd|› d�dœS 7 Œ9# t        $ r.}
t        j                  d	|
«       d
t        |
«      dœcY d}
~
S d}
~
ww xY w­w)aï  
        Writes a secret to CyberArk Conjur using an async HTTPX client.

        Args:
            secret_name: Name/path of the secret to write
            secret_value: Value to store
            description: Optional description (not used by Conjur)
            optional_params: Additional parameters
            timeout: Request timeout
            tags: Optional tags (not used by Conjur)

        Returns:
            dict: Response containing status and details of the operation
        r+   r{   )r„   ri   rD   NÚsuccesszSecret z written successfully©ÚstatusÚmessagez+Error writing secret to CyberArk Conjur: %sÚerror)r	   r
   r�   r+   rs   rw   rL   rd   rN   r4   rT   rU   r   r‚   Ústr)r8   re   r…   rŠ   rx   ry   r‹   rƒ   r„   rn   r^   s              r;   Úasync_write_secretz(CyberArkSecretManager.async_write_secret  sä   è ø€ ô. 5Ü-×;Ñ;Ø  $§/¡/Ð2ô
ˆð
	:à×(Ñ(¨Ô5ð Ÿ™ kÓ2ˆCØ)×.Ñ.°3À×@YÑ@YÓ@[ÐeqÐ.Ór×rˆHØ×%Ñ%Ô'ð �J‰J× Ñ  ¨lÔ;ð $Ø$ [ MÐ1FÐGñð ð sùô ò 	:Ü×$Ñ$Ð%RÐTUÔVØ%´#°a³&Ñ9Õ9ûð	:üsG   ‚(C(«AB. Á2B,Á38B. Â+C(Â,B. Â.	C%Â7#C ÃC%ÃC(Ã C%Ã%C(Úrecovery_window_in_daysc              ƒ   ót   K  — t        j                  d«       | j                  j                  |«       dddœS ­w)aŸ  
        CyberArk Conjur does not support direct secret deletion via API.
        Secrets can only be removed through policy updates.

        Args:
            secret_name: Name of the secret
            recovery_window_in_days: Not used
            optional_params: Additional parameters
            timeout: Request timeout

        Returns:
            dict: Response indicating operation not supported
        zhCyberArk Conjur does not support direct secret deletion. Secrets must be removed through policy updates.Únot_supportedz`CyberArk Conjur does not support direct secret deletion. Use policy updates to remove variables.rŽ   )r   r7   r4   Údelete_cache)r8   re   r”   rx   ry   s        r;   Úasync_delete_secretz)CyberArkSecretManager.async_delete_secret5  s?   è ø€ ô( 	×ÑØvô	
ð
 	�
‰
×Ñ Ô,ð &Øyñ
ð 	
ùs   ‚68)NN)NNNN)é   NN)Ú__name__Ú
__module__Ú__qualname__r<   r’   r_   Údictrd   rs   rw   ÚfloatrJ   ÚTimeoutr†   r‰   Úlistr   r“   r1   r˜   © r=   r;   r   r      s¹  „ ò'ðR+R˜só +RðZ; dó ;ð$L°3ð $L¸4ó $LðL[ 3ð [¨3ó [ð" (,Ø04ñ	,àð,ð  ™ð,ð ˜Ÿ™Ñ&¨Ñ-ð	,ð
 
ˆt‰ó,ðb (,Ø04ñ	)àð)ð  ™ð)ð ˜Ÿ™Ñ&¨Ñ-ð	)ð
 
ˆt‰ó)ð^ #'Ø'+Ø04Ø#'ñ.:àð.:ð ð.:ð ˜4‘Zð	.:ð
  ™ð.:ð ˜Ÿ™Ñ&¨Ñ-ð.:ð �T‰k˜DÑ ð.:ð 
ˆc�3ˆh‰ó.:ðf /0Ø'+Ø04ñ
àð
ð "% t¡ð
ð  ™ð	
ð
 ˜Ÿ™Ñ&¨Ñ-ð
ð 
ô
r=   r   )rP   r#   Útypingr   r   Úurllib.parser   rJ   rj   r-   Úlitellm._loggingr   Úlitellm.cachingr   Ú&litellm.llms.custom_httpx.http_handlerr   r	   r
   Úlitellm.proxy._typesr   Úbase_secret_managerr   r   Úmainr   r   r¡   r=   r;   ú<module>rª      sD   ðÛ Û 	ß Ý ã Û ã Ý +Ý )÷ñ õ
 5ç OÝ ô|
Ð-õ |
r=   