fix - proxy refactor user_api_key_auth

This commit is contained in:
Ishaan Jaff 2024-06-15 10:33:58 -07:00
parent 40155ade0c
commit da3ae00bd6
5 changed files with 1265 additions and 1186 deletions

View file

@ -698,7 +698,7 @@ class SlackAlerting(CustomLogger):
return
if "budget_alerts" not in self.alert_types:
return
_id: str = "default_id" # used for caching
_id: Optional[str] = "default_id" # used for caching
user_info_json = user_info.model_dump(exclude_none=True)
for k, v in user_info_json.items():
user_info_str = "\n{}: {}\n".format(k, v)

View file

@ -1358,7 +1358,7 @@ class CallInfo(LiteLLMBase):
spend: float
max_budget: Optional[float] = None
token: str = Field(description="Hashed value of that key")
token: Optional[str] = Field(default=None, description="Hashed value of that key")
customer_id: Optional[str] = None
user_id: Optional[str] = None
team_id: Optional[str] = None
@ -1575,3 +1575,37 @@ class ManagementEndpointLoggingPayload(LiteLLMBase):
exception: Optional[Any] = None
start_time: Optional[datetime] = None
end_time: Optional[datetime] = None
class ProxyException(Exception):
# NOTE: DO NOT MODIFY THIS
# This is used to map exactly to OPENAI Exceptions
def __init__(
self,
message: str,
type: str,
param: Optional[str],
code: Optional[int],
):
self.message = message
self.type = type
self.param = param
self.code = code
# rules for proxyExceptions
# Litellm router.py returns "No healthy deployment available" when there are no deployments available
# Should map to 429 errors https://github.com/BerriAI/litellm/issues/2487
if (
"No healthy deployment available" in self.message
or "No deployments available" in self.message
):
self.code = 429
def to_dict(self) -> dict:
"""Converts the ProxyException instance to a dictionary."""
return {
"message": self.message,
"type": self.type,
"param": self.param,
"code": self.code,
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -2754,47 +2754,6 @@ def _is_valid_team_configs(team_id=None, team_config=None, request_data=None):
return
def _is_user_proxy_admin(user_id_information: Optional[list]):
if user_id_information is None:
return False
if len(user_id_information) == 0 or user_id_information[0] is None:
return False
_user = user_id_information[0]
if (
_user.get("user_role", None) is not None
and _user.get("user_role") == LitellmUserRoles.PROXY_ADMIN.value
):
return True
# if user_id_information contains litellm-proxy-budget
# get first user_id that is not litellm-proxy-budget
for user in user_id_information:
if user.get("user_id") != "litellm-proxy-budget":
_user = user
break
if (
_user.get("user_role", None) is not None
and _user.get("user_role") == LitellmUserRoles.PROXY_ADMIN.value
):
return True
return False
def _get_user_role(user_id_information: Optional[list]):
if user_id_information is None:
return None
if len(user_id_information) == 0 or user_id_information[0] is None:
return None
_user = user_id_information[0]
return _user.get("user_role")
def encrypt_value(value: str, master_key: str):
import hashlib
import nacl.secret