mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
feat(proxy): validate callback_settings definitions at boot and gate reference changes on update
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Modules / fmt, validate, test (aws) (push) Has been cancelled
Terraform Modules / fmt, validate, test (gcp) (push) Has been cancelled
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Modules / fmt, validate, test (aws) (push) Has been cancelled
Terraform Modules / fmt, validate, test (gcp) (push) Has been cancelled
A named definition now fails boot when it is named after a callback, names an unknown backend, or carries a field no callback accepts. A non-admin editing a key that references a definition must send the stored reference back unchanged. A logging entry without callback_vars is rejected at write time instead of being stored and ignored, and a team-level entry naming a definition is rejected since references only resolve on keys. Adds a spend-log test proving the resolved secret never reaches the row.
This commit is contained in:
parent
c3a5e29cc5
commit
d1b0dea353
12 changed files with 477 additions and 27 deletions
|
|
@ -6,14 +6,14 @@ per-integration checks here.
|
|||
"""
|
||||
|
||||
import math
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from collections.abc import Callable, Iterator, Mapping, Sequence
|
||||
from types import MappingProxyType
|
||||
from typing import Final, cast # noqa: TID251 # preserve arbitrary keys from validated callback mappings
|
||||
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
import litellm
|
||||
from litellm.types.utils import CAPTURE_MESSAGE_CONTENT_VAR, EXCLUDED_SERVICES_VAR
|
||||
from litellm.types.utils import CAPTURE_MESSAGE_CONTENT_VAR, EXCLUDED_SERVICES_VAR, StandardCallbackDynamicParams
|
||||
|
||||
_NEWRELIC_CALLBACK: Final = "newrelic"
|
||||
_NEWRELIC_VAR_PREFIX: Final = "newrelic_"
|
||||
|
|
@ -25,7 +25,11 @@ _ARIZE_SAMPLING_RATE_VARS: Final[frozenset[str]] = frozenset(
|
|||
{"arize_success_sampling_rate", "arize_error_sampling_rate"}
|
||||
)
|
||||
_GENERIC_API_CALLBACK_TYPE: Final = "generic_api"
|
||||
_CALLBACK_TYPE_FIELD: Final = "callback_type"
|
||||
_STRING_KEYED_MAPPING: Final = TypeAdapter(Mapping[str, object])
|
||||
_DEFINITION_VAR_KEYS: Final[frozenset[str]] = frozenset(
|
||||
{*StandardCallbackDynamicParams.__annotations__, CAPTURE_MESSAGE_CONTENT_VAR, EXCLUDED_SERVICES_VAR}
|
||||
)
|
||||
|
||||
|
||||
def is_named_callback_definition(definition: object) -> bool:
|
||||
|
|
@ -53,6 +57,48 @@ def callback_backend_name(callback_name: str) -> str:
|
|||
return callback_name if definition is None else str(definition["callback_type"])
|
||||
|
||||
|
||||
def callback_settings_definition_error(callback_settings: Mapping[str, object]) -> str | None:
|
||||
"""Why a named ``callback_settings`` definition can never work from a key, checked at config load.
|
||||
|
||||
A definition named after a callback would be handed to that callback's constructor
|
||||
and would reroute every inline key entry naming that callback to the definition's
|
||||
backend with its secrets merged in. Other mistakes would only surface on
|
||||
``/key/health`` of a key that already depends on the definition.
|
||||
"""
|
||||
return next((error for error in _definition_errors(callback_settings) if error is not None), None)
|
||||
|
||||
|
||||
def _definition_errors(callback_settings: Mapping[str, object]) -> Iterator[str | None]:
|
||||
for name, definition in callback_settings.items():
|
||||
if is_named_callback_definition(definition):
|
||||
yield _definition_error(name, _STRING_KEYED_MAPPING.validate_python(definition))
|
||||
|
||||
|
||||
def _definition_error(name: str, definition: Mapping[str, object]) -> str | None:
|
||||
known_callbacks: Final = litellm._known_custom_logger_compatible_callbacks # pyright: ignore[reportPrivateUsage] # the registry of callback names has no public accessor
|
||||
if name in known_callbacks:
|
||||
return f"callback_settings.{name} is named after the {name} callback, so a key could not tell them apart"
|
||||
backend: Final = str(definition[_CALLBACK_TYPE_FIELD])
|
||||
if backend not in known_callbacks:
|
||||
return f"callback_settings.{name}.callback_type {backend!r} is not a known callback"
|
||||
unknown: Final = tuple(key for key in definition if key != _CALLBACK_TYPE_FIELD and key not in _DEFINITION_VAR_KEYS)
|
||||
if unknown:
|
||||
return f"callback_settings.{name} sets {', '.join(unknown)}, which no callback accepts as a callback var"
|
||||
return None
|
||||
|
||||
|
||||
def team_callback_reference_error(logging_entries: object) -> str | None:
|
||||
"""A ``callback_settings`` reference only resolves on a key, so a team entry naming one is refused."""
|
||||
return next(
|
||||
(
|
||||
f"callback_settings.{name} can only be referenced from a key's logging metadata, not a team's"
|
||||
for name, _ in stored_callback_entries(logging_entries)
|
||||
if name is not None and named_callback_definition(name) is not None
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
def callback_config_error(
|
||||
callback_name: str | None, callback_vars: Mapping[str, str] | None, callback_type: str | None = None
|
||||
) -> str | None:
|
||||
|
|
@ -396,9 +442,11 @@ def _logging_entry_error(entry: object) -> str | None:
|
|||
)
|
||||
)
|
||||
callback_name: Final = typed_entry.get("callback_name")
|
||||
callback_vars_raw: Final = typed_entry.get("callback_vars")
|
||||
if not isinstance(callback_name, str) or not isinstance(callback_vars_raw, Mapping):
|
||||
if not isinstance(callback_name, str):
|
||||
return None
|
||||
callback_vars_raw: Final = typed_entry.get("callback_vars")
|
||||
if not isinstance(callback_vars_raw, Mapping):
|
||||
return f"logging entry {callback_name!r} needs a callback_vars object, pass {{}} when it has no values"
|
||||
callback_vars: Final[Mapping[object, object]] = cast( # cast-ok: Mapping check preserves arbitrary key types
|
||||
Mapping[object, object], callback_vars_raw
|
||||
)
|
||||
|
|
|
|||
|
|
@ -21,12 +21,12 @@ import traceback
|
|||
from collections.abc import Awaitable, Callable, Iterator, Mapping, Sequence
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Final, Literal, Optional, Protocol, TypeVar, cast
|
||||
from typing import TYPE_CHECKING, Final, Literal, Optional, Protocol, TypeAlias, TypeVar, cast
|
||||
|
||||
import fastapi
|
||||
import yaml
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query, Request, status
|
||||
from pydantic import TypeAdapter
|
||||
from pydantic import TypeAdapter, ValidationError
|
||||
from typing_extensions import ReadOnly, TypedDict
|
||||
|
||||
import litellm
|
||||
|
|
@ -434,6 +434,15 @@ _KEY_METADATA_REQUEST_FIELDS: Final = frozenset(
|
|||
|
||||
|
||||
_DECODED_JSON: Final = TypeAdapter(object)
|
||||
_STORED_METADATA: Final[TypeAdapter[Mapping[str, object] | None]] = TypeAdapter(Mapping[str, object] | None)
|
||||
|
||||
|
||||
def _stored_metadata(metadata: object) -> Mapping[str, object] | None:
|
||||
"""A key row's metadata with its callback vars decrypted, or None when the row carries no metadata object."""
|
||||
try:
|
||||
return _STORED_METADATA.validate_python(_DECODED_JSON.validate_python(decrypt_callback_vars(metadata)))
|
||||
except ValidationError:
|
||||
return None
|
||||
|
||||
|
||||
def _decode_json_string_column(column: str, value: object) -> object:
|
||||
|
|
@ -934,33 +943,59 @@ def raise_on_invalid_key_logging_config(metadata: Mapping[str, object] | None) -
|
|||
raise HTTPException(status_code=400, detail={"error": error})
|
||||
|
||||
|
||||
_CallbackReference: TypeAlias = tuple[str, Mapping[str, str], Mapping[str, object]]
|
||||
|
||||
|
||||
def raise_on_unauthorized_callback_reference(
|
||||
metadata: Mapping[str, object] | None, user_api_key_dict: UserAPIKeyAuth
|
||||
metadata: Mapping[str, object] | None,
|
||||
user_api_key_dict: UserAPIKeyAuth,
|
||||
*,
|
||||
existing_metadata: Mapping[str, object] | None = None,
|
||||
) -> None:
|
||||
"""Only a proxy admin may point a key at a ``callback_settings`` definition.
|
||||
"""Only a proxy admin may point a key at a ``callback_settings`` definition, or change or drop that reference.
|
||||
|
||||
The definition carries the operator's secret-manager references, so a key that
|
||||
names it must not be writable by the team admins and members who can otherwise
|
||||
edit key metadata, and must not override the values the definition sets.
|
||||
edit key metadata, and must not override the values the definition sets. Those
|
||||
callers may still edit the rest of the key's metadata as long as they send the
|
||||
stored reference back unchanged.
|
||||
"""
|
||||
references: Final = tuple(
|
||||
if metadata is None:
|
||||
return
|
||||
references: Final = _callback_references(metadata)
|
||||
if user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN.value:
|
||||
overridden: Final = next(
|
||||
(error for reference in references if (error := _overridden_definition_var_error(reference)) is not None),
|
||||
None,
|
||||
)
|
||||
if overridden is not None:
|
||||
raise HTTPException(status_code=400, detail={"error": overridden})
|
||||
return
|
||||
stored: Final = _callback_references(existing_metadata or {})
|
||||
if _reference_keys(references) == _reference_keys(stored):
|
||||
return
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail={
|
||||
"error": (
|
||||
"Only a proxy admin can point a key at a callback_settings definition"
|
||||
if not stored
|
||||
else "Only a proxy admin can change or remove a key's callback_settings reference"
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _callback_references(metadata: Mapping[str, object]) -> tuple[_CallbackReference, ...]:
|
||||
return tuple(
|
||||
(callback_name, callback_vars, definition)
|
||||
for callback_name, callback_vars in stored_callback_entries((metadata or {}).get("logging"))
|
||||
for callback_name, callback_vars in stored_callback_entries(metadata.get("logging"))
|
||||
if callback_name is not None and (definition := named_callback_definition(callback_name)) is not None
|
||||
)
|
||||
if not references:
|
||||
return
|
||||
if user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN.value:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail={"error": "Only a proxy admin can point a key at a callback_settings definition"},
|
||||
)
|
||||
overridden: Final = next(
|
||||
(error for reference in references if (error := _overridden_definition_var_error(reference)) is not None),
|
||||
None,
|
||||
)
|
||||
if overridden is not None:
|
||||
raise HTTPException(status_code=400, detail={"error": overridden})
|
||||
|
||||
|
||||
def _reference_keys(references: Sequence[_CallbackReference]) -> tuple[tuple[str, tuple[tuple[str, str], ...]], ...]:
|
||||
return tuple((name, tuple(sorted(callback_vars.items()))) for name, callback_vars, _ in references)
|
||||
|
||||
|
||||
def _overridden_definition_var_error(
|
||||
|
|
@ -981,6 +1016,7 @@ def common_key_access_checks(
|
|||
llm_router: Router | None,
|
||||
premium_user: bool,
|
||||
user_id: str | None = None,
|
||||
existing_metadata: Mapping[str, object] | None = None,
|
||||
) -> Literal[True]:
|
||||
"""
|
||||
Check if user is allowed to make a key request, for this key
|
||||
|
|
@ -1007,7 +1043,7 @@ def common_key_access_checks(
|
|||
llm_router=llm_router,
|
||||
premium_user=premium_user,
|
||||
)
|
||||
raise_on_unauthorized_callback_reference(data.metadata, user_api_key_dict)
|
||||
raise_on_unauthorized_callback_reference(data.metadata, user_api_key_dict, existing_metadata=existing_metadata)
|
||||
return True
|
||||
|
||||
|
||||
|
|
@ -3816,6 +3852,7 @@ async def _validate_update_key_data(
|
|||
user_id=user_api_key_dict.user_id if acting_as_team_admin else existing_key_row.user_id,
|
||||
llm_router=llm_router,
|
||||
premium_user=premium_user,
|
||||
existing_metadata=_stored_metadata(existing_key_row.metadata), # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # LiteLLM_VerificationToken.metadata is a bare dict
|
||||
)
|
||||
|
||||
await TeamMemberPermissionChecks.can_team_member_execute_key_management_endpoint(
|
||||
|
|
@ -6509,7 +6546,11 @@ def _check_regenerate_guardrail_opt_out(
|
|||
user_api_key_dict,
|
||||
existing_metadata=existing_metadata,
|
||||
)
|
||||
raise_on_unauthorized_callback_reference(data.metadata, user_api_key_dict)
|
||||
raise_on_unauthorized_callback_reference(
|
||||
data.metadata,
|
||||
user_api_key_dict,
|
||||
existing_metadata=_stored_metadata(existing_metadata),
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ from litellm.proxy.common_utils.callback_config_validation import (
|
|||
conflicting_span_scope_error,
|
||||
cross_entry_family_error,
|
||||
stored_callback_entries,
|
||||
team_callback_reference_error,
|
||||
)
|
||||
from litellm.proxy.common_utils.callback_utils import ( # noqa: F401 # legacy module exports
|
||||
_CALLBACK_VAR_ENCRYPTED_PREFIX, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export
|
||||
|
|
@ -78,6 +79,9 @@ def _callback_config_error(message: str) -> HTTPException:
|
|||
|
||||
|
||||
def _validate_team_callback(data: "AddTeamCallback") -> None:
|
||||
reference_error: Final = team_callback_reference_error([data.model_dump()])
|
||||
if reference_error is not None:
|
||||
raise _callback_config_error(reference_error)
|
||||
error: Final = callback_config_error(data.callback_name, data.callback_vars, data.callback_type)
|
||||
if error is not None:
|
||||
raise _callback_config_error(error)
|
||||
|
|
|
|||
|
|
@ -131,6 +131,7 @@ from litellm.proxy.common_utils.auth_cache_invalidation_pubsub import (
|
|||
evict_shared,
|
||||
publish_auth_cache_invalidation,
|
||||
)
|
||||
from litellm.proxy.common_utils.callback_config_validation import team_callback_reference_error
|
||||
from litellm.proxy.common_utils.callback_utils import encrypt_callback_vars
|
||||
from litellm.proxy.common_utils.json_merge_patch import apply_json_merge_patch
|
||||
from litellm.proxy.common_utils.openai_error_payload import proxy_exception_for
|
||||
|
|
@ -292,6 +293,12 @@ class _TeamIdGroupRow(TypedDict):
|
|||
_count: _TeamIdKeyCount
|
||||
|
||||
|
||||
def _raise_on_team_callback_reference(metadata: Mapping[str, object] | None) -> None:
|
||||
error: Final = team_callback_reference_error((metadata or {}).get("logging"))
|
||||
if error is not None:
|
||||
raise HTTPException(status_code=400, detail={"error": error})
|
||||
|
||||
|
||||
def _as_object(value: object) -> object:
|
||||
return value
|
||||
|
||||
|
|
@ -1907,6 +1914,7 @@ async def new_team(
|
|||
if isinstance(data.metadata, dict):
|
||||
TeamMemberBudgetHandler.strip_system_managed_metadata_keys(data.metadata)
|
||||
|
||||
_raise_on_team_callback_reference(data.metadata) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # request models declare `metadata` as bare dict
|
||||
await validate_team_metadata_if_configured(
|
||||
operation="create",
|
||||
metadata=data.metadata,
|
||||
|
|
@ -2616,6 +2624,7 @@ async def update_team(
|
|||
metadata_sent="metadata" in data.model_fields_set,
|
||||
existing_metadata=_existing_team_metadata if isinstance(_existing_team_metadata, dict) else None, # pyright: ignore[reportUnknownArgumentType] # existing_team_row.metadata is a bare dict
|
||||
)
|
||||
_raise_on_team_callback_reference(data.metadata) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # request models declare `metadata` as bare dict
|
||||
|
||||
if data.soft_budget is not None:
|
||||
max_budget_to_check = data.max_budget if data.max_budget is not None else existing_team_row.max_budget
|
||||
|
|
|
|||
|
|
@ -167,7 +167,10 @@ from litellm.proxy._types import (
|
|||
TransformRequestBody,
|
||||
UserAPIKeyAuth,
|
||||
)
|
||||
from litellm.proxy.common_utils.callback_config_validation import is_named_callback_definition
|
||||
from litellm.proxy.common_utils.callback_config_validation import (
|
||||
callback_settings_definition_error,
|
||||
is_named_callback_definition,
|
||||
)
|
||||
from litellm.proxy.common_utils.callback_utils import (
|
||||
is_sensitive_callback_key,
|
||||
normalize_callback_names,
|
||||
|
|
@ -6868,6 +6871,11 @@ class ProxyConfig:
|
|||
## Callback settings
|
||||
callback_settings: Final = config.get("callback_settings", {})
|
||||
if callback_settings:
|
||||
definition_error: Final = callback_settings_definition_error(
|
||||
_SETTINGS_ADAPTER.validate_python(callback_settings)
|
||||
)
|
||||
if definition_error is not None:
|
||||
raise ValueError(definition_error)
|
||||
litellm.callback_settings = callback_settings
|
||||
|
||||
## LITELLM MODULE SETTINGS (e.g. litellm.drop_params=True,..)
|
||||
|
|
|
|||
|
|
@ -6,10 +6,12 @@ import pytest
|
|||
import litellm
|
||||
from litellm.proxy.common_utils.callback_config_validation import (
|
||||
callback_config_error,
|
||||
callback_settings_definition_error,
|
||||
conflicting_callback_var_error,
|
||||
conflicting_span_scope_error,
|
||||
cross_entry_family_error,
|
||||
logging_metadata_config_error,
|
||||
team_callback_reference_error,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -567,3 +569,83 @@ class TestCallbackReferenceEntries:
|
|||
}
|
||||
|
||||
assert logging_metadata_config_error({"logging": [reference, inline]}) is None
|
||||
|
||||
|
||||
class TestCallbackSettingsDefinitionError:
|
||||
def test_a_well_formed_definition_next_to_plain_settings_passes(self):
|
||||
assert (
|
||||
callback_settings_definition_error(
|
||||
{
|
||||
"arize_space_a": {
|
||||
"callback_type": "arize",
|
||||
"arize_api_key": "os.environ/LIT10061_ARIZE_API_KEY",
|
||||
"arize_space_id": "os.environ/LIT10061_ARIZE_SPACE_ID",
|
||||
"excluded_services": "postgres,redis",
|
||||
},
|
||||
"audit_sink": {"callback_type": "generic_api", "endpoint": "https://sink.local", "headers": {}},
|
||||
"otel": {"message_logging": False},
|
||||
}
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
def test_a_definition_named_after_a_callback_is_rejected(self):
|
||||
error: Final = callback_settings_definition_error(
|
||||
{"langfuse": {"callback_type": "arize", "arize_api_key": "k"}}
|
||||
)
|
||||
|
||||
assert error is not None and "callback_settings.langfuse" in error
|
||||
|
||||
def test_an_unknown_backend_is_rejected(self):
|
||||
error: Final = callback_settings_definition_error(
|
||||
{"arize_space_a": {"callback_type": "arise", "arize_api_key": "k"}}
|
||||
)
|
||||
|
||||
assert error is not None and "'arise'" in error
|
||||
|
||||
def test_a_field_no_callback_accepts_is_rejected(self):
|
||||
error: Final = callback_settings_definition_error(
|
||||
{"arize_space_a": {"callback_type": "arize", "arize_api_kye": "k", "arize_space_id": "s"}}
|
||||
)
|
||||
|
||||
assert error is not None and "arize_api_kye" in error and "arize_space_id" not in error
|
||||
|
||||
|
||||
class TestTeamCallbackReferenceError:
|
||||
@pytest.fixture(autouse=True)
|
||||
def definitions(self, monkeypatch) -> None:
|
||||
monkeypatch.setattr(
|
||||
litellm, "callback_settings", {"arize_space_a": {"callback_type": "arize", "arize_api_key": "k"}}
|
||||
)
|
||||
|
||||
def test_a_team_entry_naming_a_definition_is_refused(self):
|
||||
error: Final = team_callback_reference_error(
|
||||
[{"callback_name": "arize_space_a", "callback_type": "success", "callback_vars": {}}]
|
||||
)
|
||||
|
||||
assert error is not None and "arize_space_a" in error
|
||||
|
||||
def test_inline_team_entries_are_not_affected(self):
|
||||
inline: Final = {"callback_name": "arize", "callback_type": "success", "callback_vars": {"arize_api_key": "k"}}
|
||||
|
||||
assert team_callback_reference_error([inline]) is None
|
||||
assert team_callback_reference_error(None) is None
|
||||
|
||||
|
||||
def test_a_logging_entry_without_callback_vars_is_refused_at_write_time(monkeypatch):
|
||||
"""Such an entry fails model validation at request time and is dropped, so accepting it would store a no-op."""
|
||||
monkeypatch.setattr(
|
||||
litellm, "callback_settings", {"arize_space_a": {"callback_type": "arize", "arize_api_key": "k"}}
|
||||
)
|
||||
|
||||
error: Final = logging_metadata_config_error(
|
||||
{"logging": [{"callback_name": "arize_space_a", "callback_type": "success"}]}
|
||||
)
|
||||
|
||||
assert error is not None and "callback_vars" in error and "arize_space_a" in error
|
||||
assert (
|
||||
logging_metadata_config_error(
|
||||
{"logging": [{"callback_name": "arize_space_a", "callback_type": "success", "callback_vars": {}}]}
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
|
|
|||
|
|
@ -552,3 +552,68 @@ class TestCallbackReferenceKeyWrites:
|
|||
assert raise_on_unauthorized_callback_reference(metadata, UserAPIKeyAuth(user_role="proxy_admin")) is None
|
||||
finally:
|
||||
is_otel_v2_enabled.cache_clear()
|
||||
|
||||
|
||||
class TestCallbackReferenceKeyUpdates:
|
||||
"""A non-admin may edit a referencing key only by sending its stored reference back unchanged."""
|
||||
|
||||
_STORED: Final = {
|
||||
"logging": [
|
||||
{
|
||||
"callback_name": "arize_space_a",
|
||||
"callback_type": "success",
|
||||
"callback_vars": {"turn_off_message_logging": "true"},
|
||||
}
|
||||
],
|
||||
"note": "original",
|
||||
}
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def definitions(self, monkeypatch) -> None:
|
||||
monkeypatch.setattr(litellm, "callback_settings", _ARIZE_REFERENCE_SETTINGS)
|
||||
|
||||
@staticmethod
|
||||
def _check(metadata, role: str):
|
||||
from litellm.proxy._types import UserAPIKeyAuth
|
||||
from litellm.proxy.management_endpoints.key_management_endpoints import (
|
||||
raise_on_unauthorized_callback_reference,
|
||||
)
|
||||
|
||||
return raise_on_unauthorized_callback_reference(
|
||||
metadata, UserAPIKeyAuth(user_role=role), existing_metadata=TestCallbackReferenceKeyUpdates._STORED
|
||||
)
|
||||
|
||||
def test_a_non_admin_can_edit_other_metadata_while_resending_the_stored_reference(self):
|
||||
assert self._check({**self._STORED, "note": "edited"}, "internal_user") is None
|
||||
|
||||
def test_a_non_admin_sending_no_metadata_leaves_the_reference_alone(self):
|
||||
assert self._check(None, "internal_user") is None
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"new_metadata",
|
||||
[
|
||||
{"note": "edited"},
|
||||
{"logging": [], "note": "edited"},
|
||||
{"logging": [{"callback_name": "arize_space_a", "callback_type": "success", "callback_vars": {}}]},
|
||||
{
|
||||
"logging": [
|
||||
{
|
||||
"callback_name": "arize",
|
||||
"callback_type": "success",
|
||||
"callback_vars": {"arize_api_key": "mine", "arize_space_id": "space-b"},
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
ids=["dropped", "emptied", "vars-changed", "swapped-for-inline"],
|
||||
)
|
||||
def test_a_non_admin_cannot_drop_or_change_the_stored_reference(self, new_metadata):
|
||||
from fastapi import HTTPException
|
||||
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
self._check(new_metadata, "internal_user")
|
||||
assert exc.value.status_code == 403
|
||||
assert "change or remove" in str(exc.value.detail)
|
||||
|
||||
def test_a_proxy_admin_can_remove_the_reference(self):
|
||||
assert self._check({"note": "edited"}, "proxy_admin") is None
|
||||
|
|
|
|||
|
|
@ -24265,3 +24265,52 @@ async def test_key_health_reports_a_callback_settings_secret_it_cannot_read(monk
|
|||
assert health["key"] == "unhealthy"
|
||||
assert health["logging_callbacks"]["callbacks"] == ["arize_space_b"]
|
||||
assert "callback_settings.arize_space_b.arize_api_key" in str(health["logging_callbacks"]["details"])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_key_by_a_non_admin_cannot_drop_a_stored_callback_settings_reference(monkeypatch):
|
||||
"""The gate has to see the stored metadata, or sending ``metadata`` without ``logging`` silently removes the reference."""
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from litellm.proxy._types import LitellmUserRoles, UpdateKeyRequest, UserAPIKeyAuth
|
||||
from litellm.proxy.management_endpoints.key_management_endpoints import _validate_update_key_data
|
||||
|
||||
monkeypatch.setattr(
|
||||
litellm,
|
||||
"callback_settings",
|
||||
{"arize_space_a": {"callback_type": "arize", "arize_api_key": "os.environ/LIT10061_ARIZE_API_KEY"}},
|
||||
)
|
||||
mock_prisma_client = AsyncMock()
|
||||
mock_prisma_client.jsonify_object = lambda data: data
|
||||
mock_prisma_client.db = MagicMock()
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.user_api_key_cache", MagicMock())
|
||||
existing_key_row = MagicMock(
|
||||
token="hashed_alice_personal_key",
|
||||
user_id="alice",
|
||||
team_id=None,
|
||||
created_by="alice",
|
||||
max_budget=None,
|
||||
organization_id=None,
|
||||
project_id=None,
|
||||
metadata={
|
||||
"logging": [{"callback_name": "arize_space_a", "callback_type": "success", "callback_vars": {}}],
|
||||
"note": "original",
|
||||
},
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await _validate_update_key_data(
|
||||
data=UpdateKeyRequest(key="sk-alice-personal", metadata={"note": "edited"}),
|
||||
existing_key_row=existing_key_row,
|
||||
user_api_key_dict=UserAPIKeyAuth(
|
||||
user_role=LitellmUserRoles.INTERNAL_USER, api_key="sk-alice", user_id="alice"
|
||||
),
|
||||
llm_router=None,
|
||||
premium_user=True,
|
||||
prisma_client=mock_prisma_client,
|
||||
user_api_key_cache=MagicMock(),
|
||||
)
|
||||
|
||||
assert exc.value.status_code == 403
|
||||
assert "callback_settings reference" in str(exc.value.detail)
|
||||
|
|
|
|||
|
|
@ -1897,3 +1897,25 @@ def test_add_team_callback_rejects_non_string_destination_callback_vars(callback
|
|||
with pytest.raises(ValidationError) as exc:
|
||||
AddTeamCallback(callback_name="newrelic", callback_vars=callback_vars)
|
||||
assert f"{callback_var} must be a string, got {type(value).__name__}" in str(exc.value)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_team_callbacks_refuses_a_callback_settings_reference(patched_prisma, monkeypatch):
|
||||
"""A reference only resolves on a key, so storing one on a team must fail loudly instead of logging nowhere."""
|
||||
monkeypatch.setattr(
|
||||
litellm,
|
||||
"callback_settings",
|
||||
{"arize_space_a": {"callback_type": "arize", "arize_api_key": "os.environ/LIT10061_ARIZE_API_KEY"}},
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
await add_team_callbacks(
|
||||
data=AddTeamCallback(callback_name="arize_space_a", callback_type="success", callback_vars={}),
|
||||
http_request=Mock(spec=Request),
|
||||
team_id="team-victim",
|
||||
user_api_key_dict=_admin_auth(),
|
||||
)
|
||||
|
||||
assert exc.value.status_code == 400
|
||||
assert "arize_space_a" in str(exc.value.detail)
|
||||
patched_prisma.db.litellm_teamtable.update.assert_not_called()
|
||||
|
|
|
|||
|
|
@ -19450,3 +19450,61 @@ async def test_team_info_keeps_the_auth_label_for_a_caller_outside_the_team(monk
|
|||
)
|
||||
|
||||
assert (exc_info.value.code, exc_info.value.type) == ("403", "auth_error")
|
||||
|
||||
|
||||
_REFERENCE_LOGGING: Final = {
|
||||
"logging": [{"callback_name": "arize_space_a", "callback_type": "success", "callback_vars": {}}]
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_new_team_refuses_a_callback_settings_reference_in_its_logging_metadata(
|
||||
mock_db_client: MagicMock,
|
||||
mock_admin_auth: UserAPIKeyAuth,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(litellm, "default_team_settings", None)
|
||||
monkeypatch.setattr(litellm, "default_team_params", {})
|
||||
monkeypatch.setattr(
|
||||
litellm, "callback_settings", {"arize_space_a": {"callback_type": "arize", "arize_api_key": "k"}}
|
||||
)
|
||||
mock_team_create: Final = _wire_new_team_prisma(mock_db_client)
|
||||
|
||||
exc: Final[pytest.ExceptionInfo[ProxyException]]
|
||||
with pytest.raises(ProxyException) as exc:
|
||||
await new_team(
|
||||
data=NewTeamRequest(team_alias="t", metadata=dict(_REFERENCE_LOGGING)),
|
||||
http_request=MagicMock(spec=Request),
|
||||
user_api_key_dict=mock_admin_auth,
|
||||
)
|
||||
|
||||
assert str(exc.value.code) == "400"
|
||||
assert "arize_space_a" in str(exc.value.message)
|
||||
mock_team_create.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_team_refuses_a_callback_settings_reference_in_its_logging_metadata(
|
||||
mock_db_client: MagicMock,
|
||||
mock_admin_auth: UserAPIKeyAuth,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
litellm, "callback_settings", {"arize_space_a": {"callback_type": "arize", "arize_api_key": "k"}}
|
||||
)
|
||||
existing: Final[MagicMock] = MagicMock()
|
||||
existing.metadata = {}
|
||||
existing.model_dump.return_value = {"team_id": "t1", "organization_id": "org-1", "metadata": {}}
|
||||
mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing)
|
||||
|
||||
exc: Final[pytest.ExceptionInfo[ProxyException]]
|
||||
with pytest.raises(ProxyException) as exc:
|
||||
await update_team(
|
||||
data=UpdateTeamRequest(team_id="t1", metadata=dict(_REFERENCE_LOGGING)),
|
||||
http_request=MagicMock(spec=Request),
|
||||
user_api_key_dict=mock_admin_auth,
|
||||
)
|
||||
|
||||
assert str(exc.value.code) == "400"
|
||||
assert "arize_space_a" in str(exc.value.message)
|
||||
mock_db_client.db.litellm_teamtable.update.assert_not_called()
|
||||
|
|
|
|||
|
|
@ -6621,3 +6621,25 @@ async def test_proxy_config_validates_advertised_mcp_versions_at_load(tmp_path,
|
|||
return
|
||||
with pytest.raises(ValidationError):
|
||||
await ProxyConfig().load_config(router=None, config_file_path=str(config))
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ProxyConfig_load_config_fails_on_a_callback_definition_no_key_could_use(tmp_path, monkeypatch):
|
||||
f = tmp_path / "c.yaml"
|
||||
f.write_text(
|
||||
"model_list: []\n"
|
||||
"general_settings: {}\n"
|
||||
"callback_settings:\n"
|
||||
" arize_space_a:\n"
|
||||
" callback_type: arize\n"
|
||||
" arize_api_kye: os.environ/LIT10061_ARIZE_API_KEY\n"
|
||||
)
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", None)
|
||||
monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", False)
|
||||
monkeypatch.delenv("LITELLM_CONFIG_BUCKET_NAME", raising=False)
|
||||
monkeypatch.setattr(litellm, "callback_settings", {})
|
||||
|
||||
with pytest.raises(ValueError, match="arize_api_kye"):
|
||||
await ProxyConfig().load_config(router=None, config_file_path=str(f))
|
||||
|
||||
assert litellm.callback_settings == {}
|
||||
|
|
|
|||
|
|
@ -11574,3 +11574,45 @@ def test_caller_tags_reach_litellm_metadata_whether_metadata_is_a_dict_or_a_json
|
|||
LiteLLMProxyRequestSetup.merge_caller_body_tags(request_data=data)
|
||||
assert set(data["litellm_metadata"]["tags"]) == {"key-tag", "client-tag"}
|
||||
assert data["metadata"] == caller_metadata
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_referencing_keys_resolved_secret_never_reaches_the_spend_log_row(monkeypatch):
|
||||
"""The request carries the resolved secret for the logger, but the spend log row built from it must not."""
|
||||
from litellm.proxy.litellm_pre_call_utils import add_litellm_data_to_request
|
||||
|
||||
monkeypatch.setattr(litellm, "callback_settings", _ARIZE_REFERENCE_SETTINGS)
|
||||
monkeypatch.setenv("LIT10061_ARIZE_API_KEY", "resolved-arize-secret-lit10061")
|
||||
monkeypatch.setenv("LIT10061_ARIZE_SPACE_ID", "space-a")
|
||||
|
||||
updated: Final = await add_litellm_data_to_request(
|
||||
data={"model": "gpt-5-mini", "messages": [{"role": "user", "content": "hello"}]},
|
||||
request=_make_request_mock("/v1/chat/completions", {"Content-Type": "application/json"}),
|
||||
user_api_key_dict=_key_auth_referencing("arize_space_a"),
|
||||
proxy_config=MagicMock(),
|
||||
general_settings={},
|
||||
version="test-version",
|
||||
)
|
||||
assert updated["arize_api_key"] == "resolved-arize-secret-lit10061"
|
||||
|
||||
row: Final = get_logging_payload(
|
||||
kwargs={
|
||||
"model": "gpt-5-mini",
|
||||
"custom_llm_provider": "openai",
|
||||
"litellm_params": {
|
||||
"metadata": updated["metadata"],
|
||||
"proxy_server_request": updated["proxy_server_request"],
|
||||
},
|
||||
"standard_callback_dynamic_params": {
|
||||
"arize_api_key": updated["arize_api_key"],
|
||||
"arize_space_id": updated["arize_space_id"],
|
||||
},
|
||||
},
|
||||
response_obj={},
|
||||
start_time=datetime.now(timezone.utc),
|
||||
end_time=datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
assert "resolved-arize-secret-lit10061" not in json.dumps(row, default=repr)
|
||||
assert "resolved-arize-secret-lit10061" not in json.dumps(updated["metadata"], default=repr)
|
||||
assert "resolved-arize-secret-lit10061" not in json.dumps(updated["proxy_server_request"], default=repr)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue