o
    ưi/                     @   sD  d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ e ZG dd dZej deegdgdeefdedededefddZ!ej"deegdgdeefdededefddZ#ej"deegdgedej"deegdgede	ddddeefdeded e$d!ee$ def
d"d#Z%ej&d$eegdgde	dddeefdeded e$defd%d&Z'	d/d'ed(ed)ee$ d*efd+d,Z(ej)d$eegdgde	dddeefdededed e$def
d-d.Z*dS )0z2
CRUD endpoints for storing reusable credentials.
    )Optional)	APIRouterDependsHTTPExceptionRequestResponsePathN)verbose_proxy_logger)CredentialAccessor)_get_masked_values)CommonProxyErrorsUserAPIKeyAuth)user_api_key_auth)encrypt_value_helper)handle_exception_on_proxyjsonify_object)CreateCredentialItemCredentialItemc                   @   s,   e Zd Zeddedee defddZdS )CredentialHelperUtilsN
credentialnew_encryption_keyreturnc                 C   s@   i }| j pi  D ]\}}t||||< q	t| j|| jpi dS )z<Encrypt values in credential.credential_values and add to DBcredential_namecredential_valuescredential_info)r   itemsr   r   r   r   )r   r   Zencrypted_credential_valueskeyvalue r   c/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/credential_endpoints/endpoints.pyencrypt_credential_values   s   z/CredentialHelperUtils.encrypt_credential_valuesN)__name__
__module____qualname__staticmethodr   r   strr!   r   r   r   r    r      s    "r   z/credentialszcredential management)dependenciestagsrequestfastapi_responser   user_api_key_dictc              
      s>  ddl m}m} z|du rtddtjjid|jrH|du r%tddd||j}|du r5tdd	d|	|j}|du rEtdd	d||_
|j
du rStd
ddt|j|j
|jd}t|}	|	 }
t|
}|jjji ||j|jddI dH  t|g dddW S  ty } z	t| t|d}~ww )zz
    [BETA] endpoint. This might change unexpectedly.
    Stores credential in DB.
    Reloads credentials in memory.
    r   )
llm_routerprisma_clientN  errorstatus_codedetailzELLM router not found. Please ensure you have a valid router instance.  Model not foundi  zPCredential values are required. Unable to infer credential values from model ID.r   )Z
created_by
updated_by)dataTzCredential created successfullysuccessmessage)litellm.proxy.proxy_serverr-   r.   r   r   db_not_connected_errorr   model_idget_deploymentget_deployment_credentialsr   r   r   r   r   r!   
model_dumpr   dblitellm_credentialstablecreateuser_idr
   Zupsert_credentials	Exceptionr	   	exceptionr   )r*   r+   r   r,   r-   r.   modelr   Zprocessed_credentialencrypted_credentialZcredentials_dictZcredentials_dict_jsonifieder   r   r    create_credential'   sj   

	
rJ   c              
      sL   zdd t jD }d|dW S  ty% } z
t|W  Y d}~S d}~ww ):
    [BETA] endpoint. This might change unexpectedly.
    c                 S   s"   g | ]}|j t|j|jd qS )r   )r   r   r   r   ).0r   r   r   r    
<listcomp>~   s    z#get_credentials.<locals>.<listcomp>T)r9   credentialsN)litellmcredential_listrE   r   )r*   r+   r,   Zmasked_credentialsrI   r   r   r    get_credentialsp   s   rQ   z+/credentials/by_name/{credential_name:path})r(   r)   Zresponse_modelz /credentials/by_model/{model_id}.z9The credential name, percent-decoded; may contain slashes)descriptionr   r=   c              
      s  ddl m} zo|rH|du rtddd||}|du r#tddd||}|du r2tdddt|d	d	d
}td|j||i d}	|	W S |rqt	j
D ]}	|	j|krht|	jt|	jd	d	d
|	jd}
|
  W S qMtdd| dtddd ty } z	t| t|d}~ww )rK   r   )r-   Nr/   zLLM router not foundr1   r4   r5      )Zunmasked_lengthZnumber_of_asterisksz{}-credential-{}r   z+Credential not found. Got credential name: z$Credential name or model ID required)r;   r-   r   r>   r?   r   r   formatZ
model_namerO   rP   r   r   r   rE   r	   rF   r   )r*   r+   r   r=   r,   r-   rG   r   Zmasked_credential_valuesr   Zmasked_credentialrI   r   r   r    get_credential   sb   




	
rU   z#/credentials/{credential_name:path}c              
      s   ddl m} z,|du rtddtjjid|jjjd idI dH   fd	d
t	j
D t	_
dddW S  tyI } z
t|W  Y d}~S d}~ww )rK   r   r.   Nr/   r0   r1   r   wherec                    s   g | ]	}|j  kr|qS r   r   )rL   credrY   r   r    rM      s
    
z%delete_credential.<locals>.<listcomp>TzCredential deleted successfullyr8   )r;   r.   r   r   r<   r   rA   rB   deleterO   rP   rE   r   )r*   r+   r   r,   r.   rI   r   rY   r    delete_credential   s&   

r\   db_credentialupdated_patchr   r   c                 C   s~   t | j| j| jd}t||}|jr|j|_|jr*dd |j D }|j| |jr=	 d|jvr6i |_|j|j |S )z(
    Update a credential in the DB.
    )r   r   r   c                 S   s   i | ]\}}||qS r   r   )rL   kvr   r   r    
<dictcomp>  s    
z(update_db_credential.<locals>.<dictcomp>r   )r   r   r   r   r   r!   r   update)r]   r^   r   merged_credentialrH   Zencrypted_paramsr   r   r    update_db_credential   s,   
rd   c           
   
      s   ddl m} zK|du rtddtjjid|jjjd|idI dH }|du r-td	d
dt	||}t
| }|jjjd|ii |d|jidI dH  dddW S  tyh }	 z
t|	W  Y d}	~	S d}	~	ww )rK   r   rV   Nr/   r0   r1   r   rW   r4   zCredential not found in DB.r6   )rX   r7   TzCredential updated successfullyr8   )r;   r.   r   r   r<   r   rA   rB   Zfind_uniquerd   r   r@   rb   rD   rE   r   )
r*   r+   r   r   r,   r.   r]   rc   Zcredential_object_jsonifiedrI   r   r   r    update_credential  s8   

re   r"   )+__doc__typingr   Zfastapir   r   r   r   r   r   rO   Zlitellm._loggingr	   Z.litellm.litellm_core_utils.credential_accessorr
   Z*litellm.litellm_core_utils.litellm_loggingr   Zlitellm.proxy._typesr   r   Z$litellm.proxy.auth.user_api_key_authr   Z0litellm.proxy.common_utils.encrypt_decrypt_utilsr   Zlitellm.proxy.utilsr   r   Zlitellm.types.utilsr   r   Zrouterr   postrJ   getrQ   r'   rU   r[   r\   rd   patchre   r   r   r   r    <module>   s     	D
	<
!
'
	