refactor(proxy): keep lint and type gates at merge-base ceilings after main merge

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yucheng 2026-10-14 05:56:40 +00:00
parent 23640c98b0
commit 00440cf102
4 changed files with 57 additions and 44 deletions

View file

@ -9,7 +9,7 @@ import asyncio
from collections.abc import Mapping
from itertools import chain
from types import MappingProxyType
from typing import Final
from typing import Final, cast # noqa: TID251 # narrows untyped credential values at the decrypt boundary
import litellm
from litellm.proxy.common_utils.encrypt_decrypt_utils import decrypt_value_helper, legacy_unreadable
@ -71,8 +71,11 @@ def decrypted_or_stored(key: str, value: str) -> str | None:
return None if legacy_unreadable(value) else value
def decrypted_values(values: Mapping[str, str]) -> Mapping[str, str]:
resolved: Final = {key: decrypted_or_stored(key, value) for key, value in values.items()}
def decrypted_values(values: Mapping[str, object]) -> Mapping[str, str]:
resolved: Final = {
key: decrypted_or_stored(key, cast("str", value)) # cast-ok: stored values are str
for key, value in values.items()
}
return MappingProxyType({key: value for key, value in resolved.items() if value is not None})

View file

@ -32,10 +32,12 @@ config rows, and the SSO config table.
"""
import json
from collections.abc import Iterator
from collections.abc import Iterator, Mapping
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Final, Literal, cast
from pydantic import TypeAdapter, ValidationError
from litellm._logging import verbose_proxy_logger
from litellm.proxy.db.db_span import db_span
@ -48,9 +50,9 @@ from litellm.proxy.common_utils.encrypt_decrypt_utils import ( # noqa: F401 #
_V2_GCM_PREFIX, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export
ALGO_AES_GCM,
ENCRYPTION_ALGORITHM_SETTING,
V2_GCM_PREFIX,
V2_GCM_PREFIX, # pyright: ignore[reportUnusedImport] # backwards-compatible package export
SecretMapDecodeError,
_get_encryption_algorithm,
_get_encryption_algorithm, # pyright: ignore[reportPrivateUsage] # algorithm selection has no public accessor
_get_salt_key, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export
decode_secret_map,
decrypt_value_helper,
@ -527,6 +529,22 @@ _COVERED_TABLE_SPECS: Final = [
]
_MCP_CREDENTIAL_TABLES: Final = frozenset({"litellm_mcpservertable", "litellm_mcpserveroauthclient"})
_PLAINTEXT_LITELLM_PARAMS: Final = frozenset({"complexity_router_config"})
_OBJECT_MAP: Final = TypeAdapter(dict[str, object])
_OBJECT_LIST: Final = TypeAdapter(list[object])
def _object_map(raw: object) -> Mapping[str, object] | None:
try:
return _OBJECT_MAP.validate_python(raw)
except ValidationError:
return None
def _object_list(raw: object) -> tuple[object, ...]:
try:
return tuple(_OBJECT_LIST.validate_python(raw))
except ValidationError:
return ()
def _iter_encrypted_strings(obj: object):
@ -548,29 +566,28 @@ def _iter_encrypted_strings(obj: object):
def _mcp_encrypted_leaves(col: str, raw: object) -> Iterator[str]:
"""Only the strings an MCP column encrypts at rest: a credentials blob keeps auth_type, scopes and urls in
plaintext and env_vars keeps every name and every per user placeholder, so without PyNaCl those must not be
"""MCP ``credentials`` and ``env_vars`` columns hold plaintext field names and
``scope="user"`` placeholders next to encrypted values, so only the secret
leaves are yielded; a plaintext leaf would otherwise be
mistaken for legacy ciphertext and refuse the scan"""
from litellm.proxy._experimental.mcp_server.db import MCP_CREDENTIAL_SECRET_FIELDS, is_global_env_var_scope
if col == "credentials":
if not isinstance(raw, dict):
return iter(())
return (v for k, v in raw.items() if k in MCP_CREDENTIAL_SECRET_FIELDS and isinstance(v, str))
if not isinstance(raw, list):
return iter(())
credentials: Final = _object_map(raw) or {}
return (v for k, v in credentials.items() if k in MCP_CREDENTIAL_SECRET_FIELDS and isinstance(v, str))
entries: Final = tuple(entry for e in _object_list(raw) if (entry := _object_map(e)) is not None)
return (
e["value"]
for e in raw
if isinstance(e, dict) and is_global_env_var_scope(e.get("scope")) and isinstance(e.get("value"), str)
value
for entry in entries
if is_global_env_var_scope(entry.get("scope")) and isinstance(value := entry.get("value"), str)
)
def _encrypted_leaves(db_attr: str, col: str, raw: object) -> Iterator[str]:
if db_attr in _MCP_CREDENTIAL_TABLES and col in ("credentials", "env_vars"):
return _mcp_encrypted_leaves(col, raw)
if db_attr == "litellm_proxymodeltable" and isinstance(raw, dict):
return _iter_encrypted_strings({k: v for k, v in raw.items() if k not in _PLAINTEXT_LITELLM_PARAMS})
if db_attr == "litellm_proxymodeltable" and (params := _object_map(raw)) is not None:
return _iter_encrypted_strings({k: v for k, v in params.items() if k not in _PLAINTEXT_LITELLM_PARAMS})
return _iter_encrypted_strings(raw)
@ -678,11 +695,11 @@ async def _scan_config_env_vars(prisma_client: object) -> LocationReport:
async def scan_covered_tables(prisma_client: object) -> list[LocationReport]:
"""Read-only classification of every rotation-covered table. No writes."""
reports: Final[list[LocationReport]] = []
for location, db_attr, json_cols, scalar_cols in _COVERED_TABLE_SPECS:
reports.append(await _scan_one_table(prisma_client, location, db_attr, json_cols, scalar_cols))
reports.append(await _scan_config_env_vars(prisma_client))
return reports
table_reports: Final = [
await _scan_one_table(prisma_client, location, db_attr, json_cols, scalar_cols)
for location, db_attr, json_cols, scalar_cols in _COVERED_TABLE_SPECS
]
return [*table_reports, await _scan_config_env_vars(prisma_client)]
# ---------------------------------------------------------------------------

View file

@ -5549,28 +5549,22 @@ async def rotate_master_key(
# Side effect preserved: decrypted values are loaded into os.environ
# (read compat), exactly as before. Keys that do NOT decrypt
# (wrong-key ciphertext, corrupt blobs, stored plaintext) are absent
# from the decrypted dict — they must be written back byte-identical,
# from the decrypted dict so they must be written back byte-identical,
# never silently dropped from the stored row.
decrypted_env_vars: Final = proxy_config.decrypt_and_set_db_env_variables(
decrypted_env_vars: Final[dict[str, str]] = proxy_config.decrypt_and_set_db_env_variables(
environment_variables=dict[str, str](environment_variables_dict)
)
encrypted_env_vars: dict[str, str] = {}
preserved_env_var_keys: list[str] = []
for env_key, stored_value in environment_variables_dict.items():
decrypted_value = decrypted_env_vars.get(env_key)
if decrypted_value is None:
# Undecryptable or plaintext: preserve the stored bytes as-is
# so the entry stays readable via read-compat or a manual
# rotation by the admin. Never delete.
encrypted_env_vars[env_key] = stored_value
preserved_env_var_keys.append(env_key)
else:
# The empty string decrypts to itself (it round-trips), so it
# lands here too and is re-encrypted like any other value.
encrypted_env_vars[env_key] = encrypt_value_helper(
value=decrypted_value,
new_encryption_key=new_master_key,
)
preserved_env_var_keys: Final = tuple(
env_key for env_key in environment_variables_dict if decrypted_env_vars.get(env_key) is None
)
encrypted_env_vars: Final = {
env_key: (
stored_value
if env_key in preserved_env_var_keys
else encrypt_value_helper(value=decrypted_env_vars[env_key], new_encryption_key=new_master_key)
)
for env_key, stored_value in environment_variables_dict.items()
}
if preserved_env_var_keys:
verbose_proxy_logger.warning(
"Master-key rotation preserved %d environment_variables entries "

View file

@ -10,7 +10,6 @@ import base64
import hashlib
import json
import os
import signal
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
@ -248,7 +247,7 @@ def test_proxy_restart_mid_burst_reloads_every_encryption_format_from_the_databa
for index, marker in enumerate(markers)
)
eventually(lambda: sum(future.done() for future in futures), lambda done: done >= 6, seconds=60)
os.killpg(first.process.pid, signal.SIGTERM)
first.begin_shutdown()
outcomes: Final = tuple(future.result(timeout=120) for future in futures)
served: Final = frozenset(marker for marker, status in outcomes if status == 200)
assert served, outcomes