o
    ưi8                     @   s^   d Z ddlZddlZddlmZmZmZmZmZ ddl	m
Z
 G dd de
ZG dd dZdS )	a  
Databricks integration utilities for LiteLLM.

This module provides authentication, telemetry, and security utilities
for the Databricks LLM provider integration.

Authentication priority:
1. OAuth M2M (DATABRICKS_CLIENT_ID + DATABRICKS_CLIENT_SECRET) - Recommended for production
2. PAT (DATABRICKS_API_KEY) - Supported for development
3. Databricks SDK automatic auth - Fallback (uses unified auth)
    N)AnyDictLiteralOptionalTuple)BaseLLMExceptionc                   @   s   e Zd ZdS )DatabricksExceptionN)__name__
__module____qualname__ r   r   [/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/databricks/common_utils.pyr      s    r   c                   @   st  e Zd ZdZedejdfedejdfedejdfedejdfedejdfed	ejdfgZed
e	de	fddZ
edeeef deeef fddZed$dee defddZdee defddZdedededefddZdee dee dee deeef fddZ	d$dee dee ded  d!ee dee dee deeef fd"d#ZdS )%DatabricksBasezk
    Base class for Databricks integration with authentication,
    telemetry, and security utilities.
    z(Bearer\s+)[A-Za-z0-9\-_\.]+z\1[REDACTED]z(Authorization:\s*)[^\s,}]+z (api[_-]?key["\s:=]+)[^\s,}"\']+z&(client[_-]?secret["\s:=]+)[^\s,}"\']+z(dapi[a-zA-Z0-9]{32,})z[REDACTED_PAT]z%(access[_-]?token["\s:=]+)[^\s,}"\']+datareturnc                    s   |du rdS t |tr|} jD ]
\}}|||}q|S t |trJi }| D ]\}}| tfdddD r@d||< q( |||< q(|S t |t	rX fdd|D S |S )a  
        Redact sensitive information (tokens, secrets) from data before logging.

        Handles strings, dicts, and lists recursively. Keys containing sensitive
        terms (authorization, api_key, token, secret, password, credential) are
        fully redacted.

        Args:
            data: String, dict, or other data structure to redact

        Returns:
            Redacted version of the data safe for logging
        Nc                 3   s    | ]}| v V  qd S Nr   ).0Z	sensitive)	lower_keyr   r   	<genexpr>M   s
    
z7DatabricksBase.redact_sensitive_data.<locals>.<genexpr>)authorizationapi_keyZapikeytokensecretpasswordZ
credential
[REDACTED]c                    s   g | ]}  |qS r   )redact_sensitive_data)r   item)clsr   r   
<listcomp>_   s    z8DatabricksBase.redact_sensitive_data.<locals>.<listcomp>)

isinstancestrSENSITIVE_PATTERNSsubdictitemsloweranyr   list)r   r   resultpatternreplacementredactedkeyvaluer   )r   r   r   r   1   s(   



z$DatabricksBase.redact_sensitive_dataheadersc                 C   sl   |si S i }h d}|  D ]%\}}| |v r/t|dkr*|dd  d||< qd||< q|||< q|S )aR  
        Create a copy of headers with sensitive values redacted for safe logging.

        Shows first 8 characters of sensitive values for debugging purposes,
        with the rest redacted.

        Args:
            headers: HTTP headers dictionary

        Returns:
            New dictionary with sensitive headers redacted
        >   zx-databricks-tokenz	x-api-keyr   zapi-key
   N   z...[REDACTED]r   )r$   r%   len)r   r.   r+   Zsensitive_headersr,   r-   r   r   r   redact_headers_for_loggingc   s   

z)DatabricksBase.redact_headers_for_loggingNcustom_user_agentc                 C   s   zddl m} W n ty   d}Y nw | r?|  } d| v r(| dd  }n| }|r?|dddd r?| d| S d	| S )
a'  
        Build the User-Agent string for Databricks API calls.

        If a custom user agent is provided, the partner name (part before /)
        is extracted and prefixed to the litellm user agent with an underscore.
        The custom version is ignored; LiteLLM's version is always used.

        Args:
            custom_user_agent: Optional custom user agent string (e.g., "mycompany/1.0.0")

        Returns:
            User-Agent string in format:
            - Default: "litellm/{version}"
            - With custom: "{partner}_litellm/{version}"

        Examples:
            - None -> "litellm/1.79.1"
            - "mycompany/1.0.0" -> "mycompany_litellm/1.79.1"
            - "partner_product/2.0.0" -> "partner_product_litellm/1.79.1"
            - "acme" -> "acme_litellm/1.79.1"
        r   )versionz0.0.0/_ -z	_litellm/zlitellm/)Zlitellm._versionr4   	Exceptionstripsplitreplaceisalnum)r3   r4   Zpartner_namer   r   r   _build_user_agent   s    
z DatabricksBase._build_user_agentapi_basec                 C   sN   |du r%zddl m} | }|jj d}|W S  ty$   tdddw |S )zx
        Get the Databricks API base URL.

        If not provided, attempts to get it from the Databricks SDK.
        Nr   )WorkspaceClient/serving-endpoints  z~Either set the DATABRICKS_API_BASE and DATABRICKS_API_KEY environment variables, or install the databricks-sdk Python library.status_codemessage)databricks.sdkr@   confighostImportErrorr   )selfr?   r@   databricks_clientr   r   r   _get_api_base   s   zDatabricksBase._get_api_base	client_idclient_secretc           
   
   C   s   ddl }|d}d|v r|dd}| d}z|j|ddd	||fd
didd}W n |jyC } ztddt| dd}~ww |jdkrTt|jd|j d|	 }	|	d S )a  
        Obtain an OAuth M2M access token using client credentials flow.

        This is the recommended authentication method for production integrations
        per Databricks Partner requirements.

        Args:
            api_base: Databricks workspace URL
            client_id: OAuth client ID (Service Principal application ID)
            client_secret: OAuth client secret

        Returns:
            Access token string

        Raises:
            DatabricksException: If token request fails
        r   Nr5   rA   r7   z/oidc/v1/tokenZclient_credentialszall-apis)Z
grant_typescopeContent-Typez!application/x-www-form-urlencoded   )r   authr.   timeouti  z OAuth M2M token request failed: rC      access_token)
requestsrstripr<   postRequestExceptionr   r    rD   textjson)
rJ   r?   rM   rN   rV   Zworkspace_urlZ	token_urlresponseeZ
token_datar   r   r   _get_oauth_m2m_token   s:   





z#DatabricksBase._get_oauth_m2m_tokenr   c                 C   s   |pddi}z-ddl m}m} |d | }|p|jj d}|du r/|j }i ||}||fW S  ty@   tdd	d
w )a  
        Get Databricks credentials using the Databricks SDK.

        Also registers LiteLLM as a partner for proper telemetry attribution
        in Databricks system.access.audit table.

        Args:
            api_key: Optional API key (PAT)
            api_base: Optional API base URL
            headers: Optional existing headers

        Returns:
            Tuple of (api_base, headers)
        rP   application/jsonr   )r@   	useragentZlitellmrA   NrB   a  If the Databricks base URL and API key are not set, the databricks-sdk Python library must be installed. Please install the databricks-sdk, set {LLM_PROVIDER}_API_BASE and {LLM_PROVIDER}_API_KEY environment variables, or provide the base URL and API key as arguments.rC   )	rF   r@   r`   Zwith_partnerrG   rH   ZauthenticaterI   r   )rJ   r   r?   r.   r@   r`   rK   Zdatabricks_auth_headersr   r   r   _get_databricks_credentials  s"   

z*DatabricksBase._get_databricks_credentialsendpoint_type)chat_completions
embeddingscustom_endpointc                 C   s  ddl m} td}td}	|du rtd}|r;|	r;|r;|d | |||	}
|p.i }d|
 |d	< d
|d< n|du rZ|sZ|du rKtddd|d | j|||d\}}|du rp|rftddd| j|||d\}}|du r}d|d
d}n|dur|	d	d|i |durd| |d	< | 
||d< |d| |  |dkr|durd|}||fS |dkr|durd|}||fS )a!  
        Validate and configure the Databricks environment.

        Authentication priority:
        1. OAuth M2M (DATABRICKS_CLIENT_ID + DATABRICKS_CLIENT_SECRET) - Recommended
        2. PAT (DATABRICKS_API_KEY) - Supported for development
        3. Databricks SDK automatic auth - Fallback (uses unified auth)

        Args:
            api_key: Personal access token (PAT)
            api_base: Databricks workspace URL with /serving-endpoints
            endpoint_type: Type of endpoint (chat_completions or embeddings)
            custom_endpoint: Whether using a custom endpoint URL
            headers: Existing headers dict
            custom_user_agent: Optional custom user agent to prefix

        Returns:
            Tuple of (api_base, headers) with authentication configured
        r   )verbose_loggerZDATABRICKS_CLIENT_IDZDATABRICKS_CLIENT_SECRETNZDATABRICKS_API_BASEz-Using OAuth M2M authentication for DatabrickszBearer Authorizationr_   rP   TrB   zMissing API Key - A call is being made to LLM Provider but no key is set either in the environment variables ({LLM_PROVIDER}_API_KEY) or via paramsrC   z'Using Databricks SDK for authentication)r?   r   r.   zMissing API Base - A call is being made to LLM Provider but no api base is set either in the environment variables ({LLM_PROVIDER}_API_KEY) or via paramsz	Bearer {})rg   rP   z
User-AgentzDatabricks request headers: rc   z{}/chat/completionsrd   z{}/embeddings)Zlitellm._loggingrf   osgetenvdebugr^   r   ra   formatupdater>   r2   )rJ   r   r?   rb   re   r.   r3   rf   rM   rN   rU   r   r   r   databricks_validate_environment5  s`   









z.DatabricksBase.databricks_validate_environmentr   )r	   r
   r   __doc__recompile
IGNORECASEr!   classmethodr   r   r   r    r2   staticmethodr   r>   rL   r^   r#   r   ra   r   boolrm   r   r   r   r   r      sp    1$#.
:

5
r   )rn   rh   ro   typingr   r   r   r   r   Z)litellm.llms.base_llm.chat.transformationr   r   r   r   r   r   r   <module>   s    