
    h                     b    d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ  G d d	e      Zy)
z
Prometheus Auth Middleware
    )Request)JSONResponse)BaseHTTPMiddlewareN)SpecialHeaders)user_api_key_authc                   B    e Zd ZdZdefdZedefd       Zed        Zy)PrometheusAuthMiddlewarea  
    Middleware to authenticate requests to the metrics endpoint

    By default, auth is not run on the metrics endpoint

    Enabled by setting the following in proxy_config.yaml:

    ```yaml
    litellm_settings:
        require_auth_for_metrics_endpoint: true
    ```
    requestc                   K   | j                  |      r]| j                         du rK	 t        ||j                  j	                  t
        j                  j                        xs d       d {     ||       d {   }|S 7 # t        $ r.}t        ddt        |dt        |                   cY d }~S d }~ww xY w7 Bw)NT )r
   api_keyi  z)Unauthorized access to metrics endpoint: message)status_codecontent)_is_prometheus_metrics_endpoint$_should_run_auth_on_metrics_endpointr   headersgetr   openai_authorizationvalue	Exceptionr   getattrstr)selfr
   	call_nexteresponses        o/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/middleware/prometheus_auth_middleware.pydispatchz!PrometheusAuthMiddleware.dispatch   s      //888:dB+ ' ' 3 3*??EE! !    #7++! ! '$'"KGTUW`befgbhLiKj k  ,sS   $CAB ,B-B 1C<B?=CB 	B<#B71B<2C7B<<Cc                 V    	 d| j                   j                  v ryy# t        $ r Y yw xY w)Nz/metricsTF)urlpathr   )r
   s    r   r   z8PrometheusAuthMiddleware._is_prometheus_metrics_endpoint3   s1    	W[[--- 		s    	((c                  "    t         j                  S )z
        Returns True if auth should be run on the metrics endpoint

        False by default, set to True in proxy_config.yaml to enable

        ```yaml
        litellm_settings:
            require_auth_for_metrics_endpoint: true
        ```
        )litellm!require_auth_for_metrics_endpoint     r   r   z=PrometheusAuthMiddleware._should_run_auth_on_metrics_endpoint<   s     888r'   N)	__name__
__module____qualname____doc__r   r   staticmethodr   r   r&   r'   r   r	   r	      sB    g 0    9 9r'   r	   )r+   fastapir   fastapi.responsesr   starlette.middleware.baser   r$   litellm.proxy._typesr   $litellm.proxy.auth.user_api_key_authr   r	   r&   r'   r   <module>r2      s+     * 8  / B;91 ;9r'   