diff --git a/litellm/proxy/management_helpers/auto_router_permissions.py b/litellm/proxy/management_helpers/auto_router_permissions.py index e6a79229ba8..b7ff534f468 100644 --- a/litellm/proxy/management_helpers/auto_router_permissions.py +++ b/litellm/proxy/management_helpers/auto_router_permissions.py @@ -28,7 +28,7 @@ from litellm.proxy.auth.auth_checks import ( # noqa: F401 # legacy module expo check_team_member_model_access, # pyright: ignore[reportPrivateUsage] # shared membership authorization owner ) from litellm.proxy.auth.team_grants import team_model_aliases -from litellm.proxy.common_utils.encrypt_decrypt_utils import decrypt_value_helper +from litellm.proxy.common_utils.encrypt_decrypt_utils import decrypt_json_strings, decrypt_value_helper, json_value from litellm.repositories.organization_repository import OrganizationRepository from litellm.repositories.prisma_protocols import DatabaseClient from litellm.repositories.project_repository import ProjectRepository @@ -130,6 +130,14 @@ class _RouterConfigSource(LiteLLMBaseModel): complexity_router_config: Mapping[str, object] | None = None +_ROUTER_CONFIG: Final[TypeAdapter[Mapping[str, object] | None]] = TypeAdapter(Mapping[str, object] | None) + + +def _decrypted_router_config(existing: Deployment) -> Mapping[str, object] | None: + stored: Final = _RouterConfigSource.model_validate(existing.litellm_params.model_dump()).complexity_router_config + return _ROUTER_CONFIG.validate_python(decrypt_json_strings(json_value(stored))) + + class _MembershipKey(TypedDict): user_id: ReadOnly[str] team_id: ReadOnly[str] @@ -380,11 +388,7 @@ async def authorize_member_auto_router_write( if existing is not None and incoming.model_name not in (None, public_name, existing.model_name): raise HTTPException(status_code=403, detail="Team members cannot rename an auto router.") supplied_config: Final = _RouterConfigSource.model_validate(params.model_dump()).complexity_router_config - stored_config: Final = ( - _RouterConfigSource.model_validate(existing.litellm_params.model_dump()).complexity_router_config - if existing is not None - else None - ) + stored_config: Final = _decrypted_router_config(existing) if existing is not None else None resolved_config: Final = resolve_complexity_router_config_write(supplied_config, stored_config) if resolved_config.supplied_connection_fields: raise HTTPException(status_code=403, detail="Team members cannot change classifier connections.") diff --git a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py index 7fb88dfa1ac..487ce5d7427 100644 --- a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py @@ -9418,6 +9418,37 @@ class TestTeamMemberAutoRouterWrites: } assert "retained-laya-secret" in row.model_dump_json() + @pytest.mark.asyncio + @pytest.mark.parametrize("endpoint", ["patch", "legacy"]) + async def test_member_save_over_an_encrypted_stored_connection_keeps_it(self, endpoint: str) -> None: + from litellm.repositories.model_repository import encrypt_model_litellm_params + + config: Final = { + "classifier_type": "jev", "tiers": {"SIMPLE": "allowed"}, + "jev_classifier_config": {"provider": "laya", "model": "english"}, + } + connection: Final = {"api_key": "retained-laya-secret", "api_base": "https://laya.test"} + stored_params: Final = encrypt_model_litellm_params({ + "model": "auto_router/complexity_router", + "complexity_router_config": {**config, "jev_classifier_config": {**config["jev_classifier_config"], **connection}}, + }) + row: Final = self._row().model_copy(update={"litellm_params": dict(stored_params)}) + team: Final = self._team().model_copy(update={"models": ["allowed", "laya/english"]}) + database: Final = self._database(team, row) + database.transaction.litellm_proxymodeltable.update.return_value = row + request: Final = updateDeployment( + litellm_params=updateLiteLLMParams.model_validate({"complexity_router_config": config}), + model_info=ModelInfo(id=row.model_id, team_id="member-team"), + ) + actor: Final = UserAPIKeyAuth( + user_id="owner", user_role=LitellmUserRoles.INTERNAL_USER, models=["allowed", "laya/english"], config={"timeout": 60}, + ) + with self._environment(database, row): + await (patch_model(row.model_id, request, actor) if endpoint == "patch" else update_model(request, actor)) + written: Final = database.transaction.litellm_proxymodeltable.update.await_args.kwargs["data"] + saved: Final = decrypt_json_strings(json_value(json.loads(written["litellm_params"])["complexity_router_config"])) + assert saved["opensource_classifier_config"] == {**config["jev_classifier_config"], **connection} + @pytest.mark.asyncio @pytest.mark.parametrize("endpoint", ["patch", "legacy"]) @pytest.mark.parametrize("access", ["owner", "peer", "limited-key"])