diff --git a/docs/ldap_sync.md b/docs/ldap_sync.md new file mode 100644 index 00000000000..25d553ed337 --- /dev/null +++ b/docs/ldap_sync.md @@ -0,0 +1,57 @@ +# LDAP team synchronization + +LDAP sync periodically reads a selected set of directory groups, creates missing LiteLLM teams, and reconciles their memberships using accounts that already exist in LiteLLM. Login continues to use your existing authentication setup. + +Install the proxy dependencies with `pip install 'litellm[proxy]'`. A database and shared Redis are required, including when running multiple workers in one proxy. Each worker uses the existing job lock so only one worker synchronizes at a time. + +```yaml +general_settings: + coordination_redis: + host: os.environ/REDIS_HOST + port: os.environ/REDIS_PORT + ldap_sync: + enabled: true + server_url: ldaps://ldap.example.org:636 + group_search_base: ou=groups,dc=example,dc=org + group_filter: "(cn=project-*)" + user_search_base: ou=people,dc=example,dc=org + group_id_attribute: cn + group_name_attribute: cn + member_attribute: member + user_match_attribute: mail + litellm_match_field: user_email + interval_minutes: 30 + dry_run: true + +litellm_settings: + default_team_params: + models: [your-approved-model] + max_budget: 100 + budget_duration: 30d +``` + +The example uses anonymous LDAP reads over TLS. For a service account, add `bind_dn: os.environ/LDAP_BIND_DN` and `bind_password: os.environ/LDAP_BIND_PASSWORD`. Every enabled sync requires TLS, including anonymous reads and dry runs. Use `ldaps://` or set `start_tls: true` with `ldap://`. LDAPS and StartTLS verify the server certificate, using the system trust store or the optional `ca_cert_path` CA bundle. + +Start with `dry_run: true`, restart the proxy, and review the planned team creation and member additions/removals in its normal logs. Set `dry_run: false` and restart to apply changes. The first run starts shortly after startup and later runs follow `interval_minutes`. Logs also report matched and unmatched users and skipped groups. There is no separate sync UI or run-history table. + +## Identity and membership + +`group_id_attribute` is matched exactly to LiteLLM's `team_id`. The default `cn` must be a unique, stable string across your selected groups. A group named `project-alpha` therefore creates or updates the team with ID `project-alpha`. A manually created team with that same ID is included in the sync. Matching only the display name does not adopt a different team. + +`group_name_attribute` supplies the new team's display name. Team creation inherits `default_team_params`, the same way SCIM-created teams do. Existing team names, model permissions, budgets and other settings remain unchanged. + +By default, `member` contains full user DNs. The sync reads each referenced user entry and compares its `mail` attribute to existing LiteLLM `user_email` values, ignoring email case. `litellm_match_field` may instead be `user_id` or `sso_user_id`, matched exactly. Set `user_match_attribute` to the LDAP attribute carrying the corresponding identity. Missing LiteLLM accounts are skipped and counted in the logs, then picked up by a later run after normal onboarding. Changing a user's matched LDAP attribute, such as their `mail`, without updating LiteLLM makes them unmatched, so the next run removes them from every synchronized team and deletes their keys for those teams. Update the matching LiteLLM field first, for example `user_email` through `/user/update`. Duplicate matches skip the affected group rather than choosing an account. + +For POSIX groups, set `member_attribute: memberUid`. These values are looked up under `user_search_base` using `member_lookup_attribute`, which defaults to `uid`. Other membership attributes such as `uniqueMember` or `roleOccupant` are treated as full DNs. Only direct users are supported. Binary group identifiers, nested groups and AD ranged membership responses are rejected. + +LDAP is authoritative for the membership of every matched team, including manually added members. Retained members keep their team roles. New members receive the regular member role. The shared membership operation updates the team roster, user membership list and member budget/spend association together. Removing a member also deletes their existing keys for that team, following the same behavior as SCIM/team-member removal. User accounts and memberships in other teams are retained. Global user roles and disabled states are unchanged. + +## Failure behavior + +A group no longer returned by LDAP leaves its team and memberships unchanged. A successfully read group with no members removes all memberships from its corresponding team. No team or user is ever deleted, and no user is provisioned by this sync. + +Failed or incomplete group searches, referrals, duplicate group IDs, invalid responses and LDAP operation exceptions abort the directory snapshot without applying it. A member DN whose entry no longer exists skips only its group, so other groups still synchronize and that team keeps its current members until the DN is removed or restored. Unreadable entries still abort the snapshot. Groups with missing, blank or multivalued IDs skip individually. Other member validation failures, such as ambiguous identities or nested groups, also skip their affected group. A membership-write failure for one valid group does not prevent other valid groups from synchronizing. A user disappearing during the membership write causes that group's transaction to make no changes. Repeated successful runs with the same roster do not rewrite it. + +The LDAP identity must have permission to read every selected group, its complete membership attributes, and the user identity attributes. LDAP can omit attributes hidden by access controls just as it omits empty attributes, so the sync cannot distinguish every permission restriction from an empty group. Verify the bind account and dry-run output before enabling writes. + +Runs have a timeout shorter than their shared Redis lease. A timeout cancels further writes and signals the LDAP reader to stop between entries and lookups. LDAP socket timeouts and server search time limits use `timeout_seconds` as an upper bound and are capped further for short sync intervals, and the next interval retries from current state. Cancellation does not undo groups already committed earlier in the run. Cache cleanup for a committed membership change finishes before cancellation is reported, so removed keys are not left cached by the job timeout. This cleanup can extend past the run deadline but performs no further membership writes. Stopping the sync (`enabled: false` or removing the block and restarting) leaves current teams and memberships in place. diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index d3ac264518e..5f6c2f473ab 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -87,6 +87,7 @@ from litellm.types.proxy.carried_budget_state import ( UserBudgetSnapshot as UserBudgetSnapshot, # noqa: PLC0414 # public re-export ) from litellm.types.proxy.control_plane_endpoints import WorkerRegistryEntry +from litellm.types.proxy.ldap_sync import LDAPSyncSettings from litellm.types.proxy.request_tag_mappings import RequestTagMapping from litellm.types.proxy.skill_scanning import SkillScannerConfig from litellm.types.proxy.spend_capture_rate import SpendCaptureRateCheckSettings @@ -2506,6 +2507,11 @@ class ConfigGeneralSettings(LiteLLMPydanticObjectBase): Documents all the fields supported by `general_settings` in config.yaml """ + ldap_sync: LDAPSyncSettings | None = Field( + None, + description="Periodically synchronize LDAP groups to teams and existing users' memberships. Requires Redis.", + ) + use_spend_logs_ledger: bool = Field( True, description="Apply request accounting from stored SpendLogs deliveries. Set to false to roll back to the previous in-memory and Redis spend queues.", diff --git a/litellm/proxy/common_utils/ldap_directory.py b/litellm/proxy/common_utils/ldap_directory.py new file mode 100644 index 00000000000..087029d2136 --- /dev/null +++ b/litellm/proxy/common_utils/ldap_directory.py @@ -0,0 +1,281 @@ +import importlib +import ssl +from collections.abc import Iterable, Mapping, Sequence +from dataclasses import dataclass +from threading import Event +from typing import Final, Protocol, TypeAlias, runtime_checkable +from urllib.parse import urlsplit + +from pydantic import BaseModel, ConfigDict, StrictStr, TypeAdapter + +from litellm.secret_managers.main import get_secret_str +from litellm.types.proxy.ldap_sync import LDAPSyncSettings + +_ITEMS: Final = TypeAdapter(Iterable[object]) +_STRINGS: Final = TypeAdapter(tuple[StrictStr, ...]) +_GROUP_CLASSES: Final = frozenset({"group", "groupofnames", "groupofuniquenames", "posixgroup"}) + + +@dataclass(frozen=True, slots=True) +class DirectoryGroup: + team_id: str + name: str + identities: tuple[str, ...] + + +@dataclass(frozen=True, slots=True) +class DirectoryError: + reason: str + group_dn: str | None = None + + +DirectoryResult: TypeAlias = tuple[DirectoryGroup | DirectoryError, ...] | DirectoryError + + +class _Entry(BaseModel): + model_config = ConfigDict(frozen=True) + + type: str + dn: str = "" + attributes: Mapping[str, object] = {} + + +_ENTRIES: Final = TypeAdapter(tuple[_Entry, ...]) + + +class _PagedSearch(Protocol): + def paged_search( + self, + *, + search_base: str, + search_filter: str, + search_scope: str, + attributes: Sequence[str], + paged_size: int, + time_limit: int, + generator: bool, + ) -> object: ... + + +class _Extensions(Protocol): + @property + def standard(self) -> _PagedSearch: ... + + +class LDAPConnection(Protocol): + @property + def result(self) -> Mapping[str, object]: ... + + @property + def response(self) -> object: ... + + @property + def extend(self) -> _Extensions: ... + + def open(self) -> object: ... + def start_tls(self) -> bool: ... + def bind(self) -> bool: ... + def unbind(self) -> bool: ... + def search( + self, + *, + search_base: str, + search_filter: str, + search_scope: str, + attributes: Sequence[str], + time_limit: int, + ) -> bool: ... + + +@runtime_checkable +class _LDAPLibrary(Protocol): + def Tls(self, *, validate: ssl.VerifyMode, ca_certs_file: str | None) -> object: ... + def Server(self, host: str, *, port: int, use_ssl: bool, tls: object, connect_timeout: int) -> object: ... + def Connection( + self, + server: object, + *, + user: str | None, + password: str | None, + authentication: str, + auto_referrals: bool, + auto_range: bool, + raise_exceptions: bool, + receive_timeout: int, + check_names: bool, + return_empty_attributes: bool, + ) -> LDAPConnection: ... + + +def _secret(value: str | None) -> str | None: + if value is None or not value.startswith("os.environ/"): + return value + resolved: Final = get_secret_str(value) + if resolved is None: + raise ValueError("An LDAP bind credential environment variable is missing") + return resolved + + +def open_ldap_connection(settings: LDAPSyncSettings) -> LDAPConnection: + library: Final = importlib.import_module("ldap3") + if not isinstance(library, _LDAPLibrary): + raise TypeError("ldap3 does not expose the required LDAP constructors") + parsed: Final = urlsplit(settings.server_url) + tls: Final = library.Tls(validate=ssl.CERT_REQUIRED, ca_certs_file=settings.ca_cert_path) + server: Final = library.Server( + parsed.hostname or "", + port=parsed.port or (636 if parsed.scheme == "ldaps" else 389), + use_ssl=parsed.scheme == "ldaps", + tls=tls, + connect_timeout=settings.timeout_seconds, + ) + return library.Connection( + server, + user=_secret(settings.bind_dn), + password=_secret(settings.bind_password.get_secret_value()) if settings.bind_password is not None else None, + authentication="SIMPLE" if settings.bind_dn else "ANONYMOUS", + auto_referrals=False, + auto_range=False, + raise_exceptions=True, + receive_timeout=settings.timeout_seconds, + check_names=False, + return_empty_attributes=True, + ) + + +def _values(entry: _Entry, attribute: str) -> tuple[str, ...]: + value: Final = next((value for key, value in entry.attributes.items() if key.lower() == attribute.lower()), ()) + return (value,) if isinstance(value, str) else _STRINGS.validate_python(value) + + +def _one(entry: _Entry, attribute: str) -> str | None: + values: Final = _values(entry, attribute) + return values[0] if len(values) == 1 and values[0].strip() else None + + +def _entries(raw: object) -> tuple[_Entry, ...] | DirectoryError: + entries: Final = _ENTRIES.validate_python(raw) + if any(entry.type != "searchResEntry" for entry in entries): + return DirectoryError("LDAP search returned a referral or incomplete entry") + return entries + + +def _escape_filter(value: str) -> str: + return "".join( + {"\\": r"\5c", "*": r"\2a", "(": r"\28", ")": r"\29", "\x00": r"\00"}.get(char, char) for char in value + ) + + +def _check_cancelled(stop: Event) -> None: + if stop.is_set(): + raise InterruptedError("LDAP sync cancelled") + + +def _checked_items(raw: object, stop: Event) -> Iterable[object]: + items: Final = iter(_ITEMS.validate_python(raw)) + while True: + _check_cancelled(stop) + try: + yield next(items) + except StopIteration: + return + + +def _resolve_member( + connection: LDAPConnection, settings: LDAPSyncSettings, value: str, stop: Event +) -> str | DirectoryError: + from ldap3.core.exceptions import LDAPNoSuchObjectResult + + _check_cancelled(stop) + usernames: Final = settings.member_attribute.lower() == "memberuid" + try: + _ = connection.search( + search_base=settings.user_search_base if usernames else value, + search_filter=f"({settings.member_lookup_attribute}={_escape_filter(value)})" + if usernames + else "(objectClass=*)", + search_scope="SUBTREE" if usernames else "BASE", + attributes=(settings.user_match_attribute, "objectClass"), + time_limit=settings.timeout_seconds, + ) + except LDAPNoSuchObjectResult: + if usernames: + raise + return DirectoryError("LDAP member entry does not exist") + if connection.result.get("result") != 0: + return DirectoryError("LDAP member lookup did not complete successfully") + entries: Final = _entries(connection.response) + if isinstance(entries, DirectoryError): + return entries + if len(entries) != 1: + return DirectoryError("LDAP member lookup was missing or ambiguous") + entry: Final = entries[0] + if _GROUP_CLASSES.intersection(value.lower() for value in _values(entry, "objectClass")): + return DirectoryError("Nested LDAP groups are not supported") + identity: Final = _one(entry, settings.user_match_attribute) + return identity if identity is not None else DirectoryError("LDAP member identity is missing or multivalued") + + +def _read_group( + connection: LDAPConnection, settings: LDAPSyncSettings, entry: _Entry, stop: Event +) -> DirectoryGroup | DirectoryError: + _check_cancelled(stop) + team_id: Final = _one(entry, settings.group_id_attribute) + name: Final = _one(entry, settings.group_name_attribute) + if team_id is None or name is None: + return DirectoryError("LDAP group ID or name is missing or multivalued", entry.dn) + if any(";range=" in key.lower() for key in entry.attributes): + return DirectoryError("LDAP ranged membership responses are not supported", entry.dn) + identities: Final = tuple( + _resolve_member(connection, settings, value, stop) for value in _values(entry, settings.member_attribute) + ) + error: Final = next((identity for identity in identities if isinstance(identity, DirectoryError)), None) + if error is not None: + return DirectoryError(error.reason, entry.dn) + return DirectoryGroup(team_id, name, tuple(identity for identity in identities if isinstance(identity, str))) + + +def read_directory( + settings: LDAPSyncSettings, connection: LDAPConnection | None = None, stop: Event | None = None +) -> DirectoryResult: + cancellation: Final = stop if stop is not None else Event() + try: + _check_cancelled(cancellation) + client: Final = connection if connection is not None else open_ldap_connection(settings) + try: + _ = client.open() + _check_cancelled(cancellation) + if settings.start_tls and urlsplit(settings.server_url).scheme == "ldap" and not client.start_tls(): + return DirectoryError("LDAP TLS negotiation failed") + _check_cancelled(cancellation) + if not client.bind(): + return DirectoryError("LDAP bind failed") + _check_cancelled(cancellation) + raw: Final = client.extend.standard.paged_search( + search_base=settings.group_search_base, + search_filter=settings.group_filter, + search_scope="SUBTREE", + attributes=(settings.group_id_attribute, settings.group_name_attribute, settings.member_attribute), + paged_size=500, + time_limit=settings.timeout_seconds, + generator=True, + ) + items: Final = tuple(_checked_items(raw, cancellation)) + _check_cancelled(cancellation) + if client.result.get("result") != 0: + return DirectoryError("LDAP group search did not complete successfully") + entries: Final = _entries(items) + if isinstance(entries, DirectoryError): + return entries + group_ids: Final = tuple( + group_id for entry in entries if (group_id := _one(entry, settings.group_id_attribute)) is not None + ) + if len(group_ids) != len(frozenset(group_ids)): + return DirectoryError("LDAP group IDs are not unique within the selected groups") + groups: Final = tuple(_read_group(client, settings, entry, cancellation) for entry in entries) + _check_cancelled(cancellation) + return groups + finally: + _ = client.unbind() + except Exception as exc: # noqa: BLE001 # SDK errors must abort safely without exposing bind credentials + return DirectoryError(f"LDAP read failed ({type(exc).__name__})") diff --git a/litellm/proxy/common_utils/ldap_sync.py b/litellm/proxy/common_utils/ldap_sync.py new file mode 100644 index 00000000000..61085ecdc22 --- /dev/null +++ b/litellm/proxy/common_utils/ldap_sync.py @@ -0,0 +1,226 @@ +import asyncio +import math +from collections.abc import Awaitable, Callable, Mapping +from datetime import datetime, timedelta, timezone +from threading import Event +from typing import Final, Protocol, runtime_checkable + +from fastapi import Request +from pydantic import TypeAdapter + +from litellm._logging import verbose_proxy_logger +from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth +from litellm.proxy.common_utils.ldap_directory import DirectoryError, DirectoryGroup, LDAPConnection, read_directory +from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache +from litellm.proxy.db.db_transaction_queue.pod_lock_manager import PodLockManager +from litellm.proxy.management_endpoints.team_endpoints import new_team +from litellm.proxy.management_helpers.team_roster_sync import ( + MembersMissing, + RosterSync, + RosterTarget, + new_team_request_with_defaults, + sync_team_roster, +) +from litellm.proxy.utils import PrismaClient, ProxyLogging +from litellm.repositories.team_repository import TeamRepository +from litellm.repositories.user_repository import UserRepository +from litellm.types.proxy.ldap_sync import LDAPSyncSettings + +LDAP_SYNC_JOB_ID: Final = "ldap_team_sync" +_SETTINGS: Final = TypeAdapter(Mapping[str, object]) + + +@runtime_checkable +class LDAPScheduler(Protocol): + def add_job( + self, + func: Callable[[], Awaitable[None]], + trigger: str, + *, + minutes: float, + next_run_time: datetime, + id: str, + replace_existing: bool, + max_instances: int, + coalesce: bool, + ) -> object: ... + + +async def resolve_user_ids( + users: UserRepository, group: DirectoryGroup, settings: LDAPSyncSettings +) -> frozenset[str] | DirectoryError: + rows: Final = ( + await users.find_by_emails(group.identities) + if settings.litellm_match_field == "user_email" + else await users.find_many_in(settings.litellm_match_field, group.identities) + ) + identities: Final = tuple( + row.user_email + if settings.litellm_match_field == "user_email" + else row.user_id + if settings.litellm_match_field == "user_id" + else row.sso_user_id + for row in rows + ) + normalized: Final = tuple( + identity.casefold() if settings.litellm_match_field == "user_email" else identity + for identity in identities + if identity is not None + ) + if len(normalized) != len(frozenset(normalized)): + return DirectoryError("Multiple LiteLLM users match one LDAP identity") + expected: Final = frozenset( + identity.casefold() if settings.litellm_match_field == "user_email" else identity + for identity in group.identities + ) + verbose_proxy_logger.info( + "LDAP sync team_id=%s matched_users=%d unmatched_users=%d", + group.team_id, + len(rows), + len(expected - frozenset(normalized)), + ) + return frozenset(row.user_id for row in rows) + + +class LDAPSync: + def __init__( + self, + settings: LDAPSyncSettings, + prisma_client: PrismaClient, + pod_lock_manager: PodLockManager, + user_api_key_cache: UserApiKeyCache, + proxy_logging_obj: ProxyLogging | None, + *, + directory_connection: LDAPConnection | None = None, + ) -> None: + self.directory_connection = directory_connection + self.settings = settings + self.lease_seconds = math.ceil(settings.interval_minutes * 60) - 1 + self.prisma_client = prisma_client + self.pod_lock_manager = pod_lock_manager + self.user_api_key_cache = user_api_key_cache + self.proxy_logging_obj = proxy_logging_obj + self.teams = TeamRepository(prisma_client, use_writer=True) + self.users = UserRepository(prisma_client, use_writer=True) + + async def sync_group(self, group: DirectoryGroup) -> None: + member_ids: Final = await resolve_user_ids(self.users, group, self.settings) + if isinstance(member_ids, DirectoryError): + verbose_proxy_logger.warning("LDAP sync skipped team_id=%s: %s", group.team_id, member_ids.reason) + return + team: Final = await self.teams.find_by_id(group.team_id, id_field="team_id") + if self.settings.dry_run: + existing: Final[frozenset[str]] = ( + frozenset(member.user_id for member in team.members_with_roles if member.user_id is not None) + if team is not None + else frozenset() + ) + verbose_proxy_logger.info( + "LDAP sync dry_run team_id=%s create_team=%s add=%s remove=%s", + group.team_id, + team is None, + sorted(member_ids - existing), + sorted(existing - member_ids), + ) + return + actor: Final = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN) + if team is None: + await new_team( + data=new_team_request_with_defaults(group.team_id, group.name, ()), + http_request=Request(scope={"type": "http", "path": "/team/new"}), + user_api_key_dict=actor, + ) + outcome: Final = await sync_team_roster( + self.prisma_client, + group.team_id, + RosterTarget(member_ids), + actor, + "ldap_sync", + self.user_api_key_cache, + self.proxy_logging_obj, + ) + if isinstance(outcome, RosterSync): + verbose_proxy_logger.info( + "LDAP sync team_id=%s created=%s added=%d removed=%d", + group.team_id, + team is None, + len(outcome.added), + len(outcome.removed), + ) + return + verbose_proxy_logger.warning( + "LDAP sync skipped team_id=%s: %s", + group.team_id, + "user disappeared before membership update" if isinstance(outcome, MembersMissing) else "team disappeared", + ) + + async def _sync(self) -> None: + cancellation: Final = Event() + directory_settings: Final = self.settings.model_copy( + update={"timeout_seconds": min(self.settings.timeout_seconds, max(1, self.lease_seconds // 4))} + ) + try: + snapshot: Final = await asyncio.to_thread( + read_directory, directory_settings, connection=self.directory_connection, stop=cancellation + ) + finally: + cancellation.set() + if isinstance(snapshot, DirectoryError): + verbose_proxy_logger.warning("LDAP sync aborted: %s", snapshot.reason) + return + for group in snapshot: + if isinstance(group, DirectoryError): + verbose_proxy_logger.warning("LDAP sync skipped group_dn=%s: %s", group.group_dn, group.reason) + continue + try: + await self.sync_group(group) + except Exception as exc: # noqa: BLE001 # isolate failed groups so later groups can still synchronize + verbose_proxy_logger.warning( + "LDAP sync failed team_id=%s (%s); retrying next interval", group.team_id, type(exc).__name__ + ) + + async def run(self) -> None: + if ( + await self.pod_lock_manager.acquire_lock( + cronjob_id=LDAP_SYNC_JOB_ID, + ttl=self.lease_seconds, + allow_reentrant=False, + ) + is not True + ): + return + try: + await asyncio.wait_for(self._sync(), timeout=min(600, self.lease_seconds / 2)) + except asyncio.TimeoutError: + verbose_proxy_logger.warning("LDAP sync timed out; retrying next interval") + + +def initialize_ldap_sync( + scheduler: object, + general_settings: Mapping[str, object], + prisma_client: PrismaClient | None, + pod_lock_manager: PodLockManager, + user_api_key_cache: UserApiKeyCache, + proxy_logging_obj: ProxyLogging, +) -> None: + raw: Final = general_settings.get("ldap_sync") + if raw is None or _SETTINGS.validate_python(raw).get("enabled") is not True: + return + settings: Final = LDAPSyncSettings.model_validate(raw) + if prisma_client is None: + raise ValueError("ldap_sync requires a database") + if pod_lock_manager.redis_cache is None: + raise ValueError("ldap_sync requires shared Redis for cross-worker coordination") + if not isinstance(scheduler, LDAPScheduler): + raise TypeError("ldap_sync requires a scheduler with add_job support") + job: Final = LDAPSync(settings, prisma_client, pod_lock_manager, user_api_key_cache, proxy_logging_obj) + scheduler.add_job( + job.run, + "interval", + minutes=settings.interval_minutes, + next_run_time=datetime.now(timezone.utc) + timedelta(seconds=1), + id=LDAP_SYNC_JOB_ID, + replace_existing=True, + max_instances=1, + coalesce=True, + ) diff --git a/litellm/proxy/management_endpoints/scim/scim_v2.py b/litellm/proxy/management_endpoints/scim/scim_v2.py index e48f68a2d0a..81fc486d736 100644 --- a/litellm/proxy/management_endpoints/scim/scim_v2.py +++ b/litellm/proxy/management_endpoints/scim/scim_v2.py @@ -6,7 +6,6 @@ This is an enterprise feature and requires a premium license. import re from collections.abc import Awaitable, Callable, Iterable, Iterator, Mapping, Sequence -from copy import deepcopy from dataclasses import dataclass from functools import partial, reduce from itertools import chain, groupby @@ -90,6 +89,7 @@ from litellm.proxy.management_helpers.team_roster_sync import ( RosterSyncOutcome, RosterTarget, TeamGone, + new_team_request_with_defaults, sync_team_roster, team_tx_db, user_tx_db, @@ -2945,27 +2945,8 @@ def _new_team_request_with_defaults( ) -> NewTeamRequest: """Build the SCIM group's team request, applying litellm.default_team_params (including models) the same way SSO auto-created teams do.""" - default_params: Final = litellm.default_team_params - defaults: Final[Mapping[str, object]] = ( - deepcopy(default_params) - if isinstance(default_params, dict) - else default_params.model_dump(exclude_none=True) - if default_params is not None - else {} - ) - default_metadata: Final = defaults.get("metadata") - metadata: Final = { - **(default_metadata if isinstance(default_metadata, dict) else {}), - SCIM_MANAGED_TEAM_METADATA_KEY: True, - } - return NewTeamRequest.model_validate( - { - **defaults, - "team_id": team_id, - "team_alias": team_alias, - "members_with_roles": members_with_roles, - "metadata": metadata, - } + return new_team_request_with_defaults( + team_id, team_alias, members_with_roles, metadata={SCIM_MANAGED_TEAM_METADATA_KEY: True} ) diff --git a/litellm/proxy/management_helpers/team_roster_sync.py b/litellm/proxy/management_helpers/team_roster_sync.py index 08be455a55d..a3231acd68e 100644 --- a/litellm/proxy/management_helpers/team_roster_sync.py +++ b/litellm/proxy/management_helpers/team_roster_sync.py @@ -1,6 +1,7 @@ import asyncio import json from collections.abc import Mapping, Sequence +from copy import deepcopy from dataclasses import dataclass from datetime import timedelta from types import MappingProxyType @@ -9,8 +10,9 @@ from typing import TYPE_CHECKING, Final from pydantic import TypeAdapter from typing_extensions import ReadOnly, TypedDict, assert_never +import litellm from litellm._uuid import uuid -from litellm.proxy._types import LiteLLM_TeamTable, Member, UserAPIKeyAuth +from litellm.proxy._types import LiteLLM_TeamTable, Member, NewTeamRequest, UserAPIKeyAuth from litellm.proxy.auth.auth_checks import ( await_inflight_team_membership_load, delete_cache_team_object, @@ -41,6 +43,7 @@ from litellm.proxy.management_helpers.access_group_team_sync import ( from litellm.proxy.utils import PrismaClient, ProxyLogging from litellm.repositories.chunked_in import delete_many_in, find_many_in, update_many_in from litellm.repositories.team_repository import TeamRepository +from litellm.types.proxy.management_endpoints.ui_sso import DefaultTeamSSOParams if TYPE_CHECKING: from prisma import Prisma @@ -50,6 +53,43 @@ if TYPE_CHECKING: ROSTER_TX_TIMEOUT: Final = timedelta(seconds=60) _JSON_OBJECT: Final = TypeAdapter(dict[str, object]) +_TEAM_DEFAULTS: Final[TypeAdapter[DefaultTeamSSOParams | Mapping[str, object] | None]] = TypeAdapter( + DefaultTeamSSOParams | dict[str, object] | None +) + + +def new_team_request_with_defaults( + team_id: str, + team_alias: str | None, + members_with_roles: Sequence[Member], + metadata: Mapping[str, object] | None = None, +) -> NewTeamRequest: + configured: Final = litellm.default_team_params + default_params: Final = ( + _JSON_OBJECT.validate_python(configured) + if isinstance(configured, dict) + else _TEAM_DEFAULTS.validate_python(configured) + ) + defaults: Final = _JSON_OBJECT.validate_python( + default_params.model_dump(exclude_none=True) + if isinstance(default_params, DefaultTeamSSOParams) + else deepcopy(default_params) + if default_params is not None + else {} + ) + default_metadata: Final = defaults.get("metadata") + return NewTeamRequest.model_validate( + { + **defaults, + "team_id": team_id, + "team_alias": team_alias, + "members_with_roles": members_with_roles, + "metadata": { + **(_JSON_OBJECT.validate_python(default_metadata) if isinstance(default_metadata, dict) else {}), + **(metadata or {}), + }, + } + ) @dataclass(frozen=True, slots=True) @@ -349,7 +389,22 @@ async def sync_team_roster( user_api_key_dict=user_api_key_dict, litellm_changed_by=None, ) - await _settle_caches(synced, sorted(to_add | to_remove), removal, user_api_key_cache, proxy_logging_obj) + cleanup: Final = asyncio.create_task( + _settle_caches(synced, sorted(to_add | to_remove), removal, user_api_key_cache, proxy_logging_obj) + ) + try: + await asyncio.shield(cleanup) + except asyncio.CancelledError as cancellation: + # The transaction committed. Finish revocation before propagating cancellation. + try: + while not cleanup.done(): + try: + await asyncio.shield(cleanup) + except asyncio.CancelledError: + continue + cleanup.result() + finally: + raise cancellation _emit_team_members_metric(synced) _schedule_team_membership_audit_log( team_id=team_id, diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index c1af5d6f980..e23238bd64b 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -1867,6 +1867,14 @@ async def _proxy_lifespan(app: FastAPI, resources: AsyncExitStack) -> AsyncGener redis_usage_cache=redis_usage_cache, ) + _ldap_sync_settings: Final = _GENERAL_SETTINGS_VIEW.validate_python(general_settings).get("ldap_sync") + if ( + prisma_client is None + and isinstance(_ldap_sync_settings, dict) + and _GENERAL_SETTINGS_VIEW.validate_python(_ldap_sync_settings).get("enabled") is True + ): + raise ValueError("ldap_sync requires a database") + ### START BATCH WRITING DB + CHECKING NEW MODELS### worker_heartbeat: Final = ( await ProxyStartupEvent.initialize_scheduled_background_jobs( @@ -11858,6 +11866,23 @@ class ProxyStartupEvent: await cls._initialize_spend_tracking_background_jobs(scheduler=scheduler) + ldap_general_settings: Final = _GENERAL_SETTINGS_VIEW.validate_python(general_settings) + ldap_settings: Final = ldap_general_settings.get("ldap_sync") + if ( + isinstance(ldap_settings, dict) + and _GENERAL_SETTINGS_VIEW.validate_python(ldap_settings).get("enabled") is True + ): + from litellm.proxy.common_utils.ldap_sync import initialize_ldap_sync + + initialize_ldap_sync( + scheduler=scheduler, + general_settings=ldap_general_settings, + prisma_client=prisma_client, + pod_lock_manager=proxy_logging_obj.db_spend_update_writer.pod_lock_manager, + user_api_key_cache=user_api_key_cache, + proxy_logging_obj=proxy_logging_obj, + ) + cls._initialize_spend_capture_rate_check_job( scheduler=scheduler, proxy_logging_obj=proxy_logging_obj, diff --git a/litellm/types/proxy/ldap_sync.py b/litellm/types/proxy/ldap_sync.py new file mode 100644 index 00000000000..73e2f0a168f --- /dev/null +++ b/litellm/types/proxy/ldap_sync.py @@ -0,0 +1,66 @@ +from typing import Final, Literal +from urllib.parse import urlsplit + +from pydantic import BaseModel, ConfigDict, Field, SecretStr, field_validator, model_validator +from typing_extensions import Self + + +class LDAPSyncSettings(BaseModel): + model_config = ConfigDict(frozen=True, extra="forbid", hide_input_in_errors=True) + + enabled: bool = False + server_url: str = "" + group_search_base: str = "" + group_filter: str = "" + user_search_base: str = "" + group_id_attribute: str = "cn" + group_name_attribute: str = "cn" + member_attribute: str = "member" + member_lookup_attribute: str = "uid" + user_match_attribute: str = "mail" + litellm_match_field: Literal["user_email", "user_id", "sso_user_id"] = "user_email" + interval_minutes: float = Field(default=30, ge=0.1, allow_inf_nan=False) + dry_run: bool = False + bind_dn: str | None = None + bind_password: SecretStr | None = None + ca_cert_path: str | None = None + start_tls: bool = False + timeout_seconds: int = Field(default=30, ge=1, le=120) + + @field_validator("server_url") + @classmethod + def validate_server_url(cls, value: str) -> str: + parsed: Final = urlsplit(value) + if parsed.scheme not in {"ldap", "ldaps"} or not parsed.hostname: + raise ValueError("server_url must be an ldap:// or ldaps:// server URL") + if parsed.username or parsed.password or parsed.query or parsed.fragment or parsed.path not in {"", "/"}: + raise ValueError("server_url must contain only the LDAP host and optional port") + _ = parsed.port + return value + + @field_validator( + "group_id_attribute", + "group_name_attribute", + "member_attribute", + "member_lookup_attribute", + "user_match_attribute", + ) + @classmethod + def validate_attribute(cls, value: str) -> str: + if not value or not value[0].isalpha() or not all(char.isalnum() or char == "-" for char in value): + raise ValueError("LDAP attributes must be plain attribute names") + return value + + @model_validator(mode="after") + def validate_bind(self) -> Self: + if self.enabled and not all( + (self.server_url, self.group_search_base, self.group_filter, self.user_search_base) + ): + raise ValueError( + "Enabled LDAP sync requires server_url, group_search_base, group_filter and user_search_base" + ) + if bool(self.bind_dn) != bool(self.bind_password): + raise ValueError("bind_dn and bind_password must be configured together") + if (self.enabled or self.bind_dn) and urlsplit(self.server_url).scheme == "ldap" and not self.start_tls: + raise ValueError("LDAP sync and service-account binds require TLS: use ldaps:// or start_tls: true") + return self diff --git a/pyproject.toml b/pyproject.toml index ee6cb6c1dcc..eb08a24490f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,6 +72,7 @@ proxy = [ "hiredis>=3.0.0,<4.0; python_version < '3.15'", "hiredis>=3.5.0,<4.0; python_version >= '3.15'", "apscheduler>=3.11.2,<4.0", + "ldap3>=2.9.1,<3.0", "fastapi-sso>=0.19.0,<1.0", "PyJWT>=2.13.0,<3.0", "python-multipart>=0.0.27,<1.0", diff --git a/tests/code_coverage_tests/liccheck.ini b/tests/code_coverage_tests/liccheck.ini index cd951c6cd28..58b379bb4e9 100644 --- a/tests/code_coverage_tests/liccheck.ini +++ b/tests/code_coverage_tests/liccheck.ini @@ -160,6 +160,7 @@ ruff: >=0.15.3 # MIT License types-requests: >=2.32.4.20260107 # Apache 2.0 license (typeshed) types-pyyaml: >=6.0.12.20250915 # Apache 2.0 license (typeshed) fakeredis: >=2.34.1 # BSD license +ldap3: >=2.9.1,<3.0 # LGPL-3.0-or-later verified in the 2.9.1 wheel LICENSE.txt, https://pypi.org/project/ldap3/2.9.1/ psycopg: >=3.2.13 # LGPL-3.0 license psycopg-binary: >=3.2.13 # LGPL-3.0 license psycopg2-binary: >=2.9.11 # LGPL with exceptions diff --git a/tests/unit/proxy/common_utils/test_ldap_directory.py b/tests/unit/proxy/common_utils/test_ldap_directory.py new file mode 100644 index 00000000000..3de67f00335 --- /dev/null +++ b/tests/unit/proxy/common_utils/test_ldap_directory.py @@ -0,0 +1,366 @@ +import ssl +from collections.abc import Iterable, Mapping, Sequence +from threading import Event +from typing import Final + +import pytest +from ldap3.core.exceptions import LDAPInsufficientAccessRightsResult, LDAPNoSuchObjectResult, LDAPSocketReceiveError +from pydantic import ValidationError + +from litellm.proxy.common_utils.ldap_directory import ( + DirectoryError, + DirectoryGroup, + open_ldap_connection, + read_directory, +) +from litellm.types.proxy.ldap_sync import LDAPSyncSettings + +SETTINGS: Final = LDAPSyncSettings( + enabled=True, + server_url="ldap://directory.example.org", + start_tls=True, + group_search_base="ou=groups,dc=example,dc=org", + group_filter="(cn=project-*)", + user_search_base="ou=people,dc=example,dc=org", +) +ALICE: Final = "uid=alice,ou=people,dc=example,dc=org" +BOB: Final = "uid=bob,ou=people,dc=example,dc=org" + + +def entry(dn: str, **attributes: object) -> Mapping[str, object]: + return {"type": "searchResEntry", "dn": dn, "attributes": attributes} + + +def group(name: str = "project-a", **attributes: object) -> Mapping[str, object]: + return entry(f"cn={name},ou=groups,dc=example,dc=org", cn=[name], **attributes) + + +def user(mail: str, **attributes: object) -> Mapping[str, object]: + return entry(ALICE, mail=[mail], objectClass=["person"], **attributes) + + +class Connection: + def __init__( + self, + groups: Iterable[Mapping[str, object]], + users: Mapping[str, Sequence[Mapping[str, object]]] | None = None, + result_code: int = 0, + tls_ok: bool = True, + stop_after_search: Event | None = None, + raising: Mapping[str, tuple[int | None, Exception]] | None = None, + ) -> None: + self.groups = groups + self.users = users or {} + self.raising = raising or {} + self.result_code = result_code + self.result: Mapping[str, object] = {"result": result_code} + self.response: object = () + self.extend = self + self.standard = self + self.searches: list[tuple[str, str, str]] = [] + self.closed = False + self.tls_ok = tls_ok + self.stop_after_search = stop_after_search + + def open(self) -> None: + pass + + def bind(self) -> bool: + return True + + def unbind(self) -> bool: + self.closed = True + return True + + def start_tls(self) -> bool: + return self.tls_ok + + def paged_search( + self, + *, + search_base: str, + search_filter: str, + search_scope: str, + attributes: Sequence[str], + paged_size: int, + time_limit: int, + generator: bool, + ) -> object: + return self.groups + + def search( + self, + *, + search_base: str, + search_filter: str, + search_scope: str, + attributes: Sequence[str], + time_limit: int, + ) -> bool: + self.searches.append((search_base, search_filter, search_scope)) + if search_base in self.raising: + result_code, exception = self.raising[search_base] + if result_code is not None: + self.result = {"result": result_code} + raise exception + self.response = self.users.get(search_base if search_scope == "BASE" else search_filter, ()) + self.result = {"result": self.result_code} + if self.stop_after_search is not None: + self.stop_after_search.set() + return self.result_code == 0 + + +def test_direct_members_resolve_to_user_identities_and_connection_is_closed(): + connection = Connection( + [group(member=[ALICE, BOB])], {ALICE: [user("alice@example.org")], BOB: [user("bob@example.org")]} + ) + result = read_directory(SETTINGS, connection) + assert result == (DirectoryGroup("project-a", "project-a", ("alice@example.org", "bob@example.org")),) + assert connection.closed + + +def test_deleted_member_entry_skips_only_its_group(): + connection = Connection( + [group("project-a", member=[ALICE, BOB]), group("project-b", member=[ALICE])], + {ALICE: [user("alice@example.org")]}, + raising={BOB: (32, LDAPNoSuchObjectResult(result=32, description="noSuchObject"))}, + ) + assert read_directory(SETTINGS, connection) == ( + DirectoryError("LDAP member entry does not exist", "cn=project-a,ou=groups,dc=example,dc=org"), + DirectoryGroup("project-b", "project-b", ("alice@example.org",)), + ) + assert connection.closed + + +def test_unreadable_member_entry_still_aborts_snapshot(): + connection = Connection( + [group("project-a", member=[ALICE, BOB]), group("project-b", member=[ALICE])], + {ALICE: [user("alice@example.org")]}, + raising={BOB: (50, LDAPInsufficientAccessRightsResult(result=50, description="insufficientAccessRights"))}, + ) + assert read_directory(SETTINGS, connection) == DirectoryError( + "LDAP read failed (LDAPInsufficientAccessRightsResult)" + ) + + +@pytest.mark.parametrize("membership", [[], ()]) +def test_successfully_read_empty_group_has_empty_roster(membership: Sequence[str]): + assert read_directory(SETTINGS, Connection([group(member=membership)])) == ( + DirectoryGroup("project-a", "project-a", ()), + ) + + +def test_omitted_membership_attribute_is_an_empty_roster_with_complete_read_permissions(): + assert read_directory(SETTINGS, Connection([group()])) == (DirectoryGroup("project-a", "project-a", ()),) + + +def test_ldap_attribute_names_are_case_insensitive(): + connection = Connection( + [entry("group", CN=["project-a"], MEMBER=[ALICE])], {ALICE: [entry(ALICE, MAIL=["a@example.org"])]} + ) + assert read_directory(SETTINGS, connection) == (DirectoryGroup("project-a", "project-a", ("a@example.org",)),) + + +@pytest.mark.parametrize( + "members", + [ + (), + (user("one@example.org"), user("two@example.org")), + (entry(ALICE, mail=["one@example.org"], objectClass=["groupOfNames"]),), + (entry(ALICE, mail=[], objectClass=["person"]),), + ], +) +def test_incomplete_or_nested_member_does_not_become_empty_group(members: Sequence[Mapping[str, object]]): + result = read_directory(SETTINGS, Connection([group(member=[ALICE])], {ALICE: members})) + assert isinstance(result, tuple) and isinstance(result[0], DirectoryError) + + +def test_duplicate_ids_are_rejected_even_when_one_group_cannot_resolve_members(): + result = read_directory(SETTINGS, Connection([group(member=[]), group(member=[ALICE])])) + assert result == DirectoryError("LDAP group IDs are not unique within the selected groups") + + +def test_ranged_membership_is_rejected_instead_of_removing_unread_members(): + result = read_directory(SETTINGS, Connection([group(**{"member;range=0-1499": [ALICE]})])) + assert isinstance(result, tuple) and isinstance(result[0], DirectoryError) + assert "ranged" in result[0].reason + + +@pytest.mark.parametrize("code", [4, 10, 50]) +def test_partial_directory_results_are_rejected(code: int): + result = read_directory(SETTINGS, Connection([group(member=[])], result_code=code)) + assert result == DirectoryError("LDAP group search did not complete successfully") + + +def test_referrals_are_not_treated_as_groups(): + result = read_directory(SETTINGS, Connection([{"type": "searchResRef", "uri": ["ldap://other.example.org"]}])) + assert isinstance(result, DirectoryError) + + +def test_missing_group_preserves_absence_in_snapshot(): + assert read_directory(SETTINGS, Connection([])) == () + + +def test_binary_group_identifiers_are_not_implicitly_decoded(): + result = read_directory(SETTINGS, Connection([entry("group", cn=[b"project-a"], member=[])])) + assert isinstance(result, DirectoryError) + + +def test_member_uid_lookup_escapes_filter_values(): + settings = SETTINGS.model_copy(update={"member_attribute": "memberUid"}) + expected_filter = r"(uid=alice\2a\29\28uid=\2a)" + connection = Connection([group(memberUid=["alice*)(uid=*"])], {expected_filter: [user("alice@example.org")]}) + assert read_directory(settings, connection) == (DirectoryGroup("project-a", "project-a", ("alice@example.org",)),) + assert connection.searches == [(SETTINGS.user_search_base, expected_filter, "SUBTREE")] + + +def test_start_tls_failure_prevents_search_and_closes_connection(): + connection = Connection([group(member=[])], tls_ok=False) + assert read_directory(SETTINGS.model_copy(update={"start_tls": True}), connection) == DirectoryError( + "LDAP TLS negotiation failed" + ) + assert connection.closed + + +def test_disabled_configuration_needs_no_connection_details(): + assert not LDAPSyncSettings(enabled=False).enabled + + +@pytest.mark.parametrize( + "updates", + [ + {"server_url": "https://example.org"}, + {"server_url": "ldap://user:password@example.org"}, + {"bind_dn": "cn=admin", "bind_password": "password", "start_tls": False}, + {"group_id_attribute": "cn)(uid=*"}, + {"litellm_match_field": "user_role"}, + {"interval_minutes": 0}, + ], +) +def test_invalid_configuration_is_rejected(updates: Mapping[str, object]): + with pytest.raises(ValidationError): + LDAPSyncSettings.model_validate({**SETTINGS.model_dump(), **updates}) + + +@pytest.mark.parametrize( + "updates,message", + [ + ({"bind_dn": "cn=admin", "start_tls": False}, "require TLS"), + ({}, "bind_dn and bind_password must be configured together"), + ], +) +def test_invalid_configuration_errors_do_not_expose_bind_password(updates: Mapping[str, object], message: str): + password: Final = "qa-secret-42" + with pytest.raises(ValidationError, match=message) as error: + LDAPSyncSettings.model_validate( + {**SETTINGS.model_dump(exclude_unset=True), **updates, "bind_password": password} + ) + assert password not in str(error.value) + + +def test_cancelled_reader_stops_before_opening_ldap(): + cancellation = Event() + cancellation.set() + connection = Connection([group(member=[])]) + result = read_directory(SETTINGS, connection, cancellation) + assert isinstance(result, DirectoryError) + assert not connection.closed + assert connection.searches == [] + + +def test_cancellation_after_member_read_stops_remaining_lookups_and_closes_connection(): + cancellation = Event() + connection = Connection( + [group(member=[ALICE, BOB])], + {ALICE: [user("alice@example.org")], BOB: [user("bob@example.org")]}, + stop_after_search=cancellation, + ) + result = read_directory(SETTINGS, connection, cancellation) + assert isinstance(result, DirectoryError) + assert len(connection.searches) == 1 + assert connection.closed + + +def test_cancellation_during_paged_snapshot_discards_results_before_membership_reads(): + cancellation = Event() + + def pages() -> Iterable[Mapping[str, object]]: + cancellation.set() + yield group(member=[ALICE]) + pytest.fail("LDAP snapshot should stop consuming pages after cancellation") + + connection = Connection(pages()) + result = read_directory(SETTINGS, connection, cancellation) + assert isinstance(result, DirectoryError) + assert connection.searches == [] + assert connection.closed + + +@pytest.mark.parametrize( + "url, ssl_enabled, port", + [("ldaps://directory.example.org", True, 636), ("ldap://directory.example.org:1389", False, 1389)], +) +def test_real_ldap_client_constructor_verifies_tls_and_disables_referrals(url: str, ssl_enabled: bool, port: int): + pytest.importorskip("ldap3") + client = open_ldap_connection(SETTINGS.model_copy(update={"server_url": url})) + assert client.server.tls.validate == ssl.CERT_REQUIRED + assert client.server.ssl is ssl_enabled + assert client.server.port == port + assert client.auto_referrals is False + + +@pytest.mark.parametrize("invalid_id", [(), ("",), ("one", "two")]) +def test_multiple_malformed_group_ids_do_not_block_healthy_groups(invalid_id: tuple[str, ...]): + connection: Final = Connection( + [ + entry("cn=broken-a", cn=invalid_id, member=[ALICE]), + entry("cn=broken-b", cn=invalid_id, member=[ALICE]), + group("healthy", member=[ALICE]), + ], + {ALICE: [user("alice@example.org")]}, + ) + result: Final = read_directory(SETTINGS, connection) + assert isinstance(result, tuple) + assert len(result) == 3 + assert all(isinstance(item, DirectoryError) for item in result[:2]) + assert result[2] == DirectoryGroup("healthy", "healthy", ("alice@example.org",)) + assert connection.closed + + +def test_connection_failure_after_deleted_member_aborts_snapshot_instead_of_reusing_old_result(): + connection: Final = Connection( + [group("deleted", member=[ALICE]), group("unreadable", member=[BOB]), group("healthy", member=[])], + raising={ + ALICE: (32, LDAPNoSuchObjectResult(result=32, description="noSuchObject")), + BOB: (None, LDAPSocketReceiveError("connection lost")), + }, + ) + assert read_directory(SETTINGS, connection) == DirectoryError("LDAP read failed (LDAPSocketReceiveError)") + assert connection.closed + + +@pytest.mark.parametrize("dry_run", [False, True]) +def test_anonymous_sync_rejects_unprotected_directory_responses(dry_run: bool): + with pytest.raises(ValidationError, match="TLS"): + LDAPSyncSettings.model_validate({**SETTINGS.model_dump(), "start_tls": False, "dry_run": dry_run}) + + +@pytest.mark.parametrize( + "url,start_tls", [("ldaps://directory.example.org", False), ("ldap://directory.example.org", True)] +) +def test_anonymous_sync_accepts_verified_encrypted_transport(url: str, start_tls: bool): + settings: Final = LDAPSyncSettings.model_validate( + {**SETTINGS.model_dump(), "server_url": url, "start_tls": start_tls} + ) + client: Final = open_ldap_connection(settings) + assert client.server.tls.validate == ssl.CERT_REQUIRED + assert client.server.ssl or settings.start_tls + + +def test_real_client_leaves_ranged_memberships_for_our_completeness_guard(): + client: Final = open_ldap_connection(SETTINGS) + assert client.auto_range is False + connection: Final = Connection([group(**{"member;range=0-1499": [ALICE]})], {ALICE: [user("alice@example.org")]}) + result: Final = read_directory(SETTINGS, connection) + assert isinstance(result, tuple) and isinstance(result[0], DirectoryError) + assert connection.searches == [] diff --git a/tests/unit/proxy/common_utils/test_ldap_sync.py b/tests/unit/proxy/common_utils/test_ldap_sync.py new file mode 100644 index 00000000000..72a7a7a2c31 --- /dev/null +++ b/tests/unit/proxy/common_utils/test_ldap_sync.py @@ -0,0 +1,263 @@ +import asyncio + +from collections.abc import Mapping, Sequence +from typing import Final + +import pytest + +from litellm.models.user import LiteLLM_UserTable +from litellm.proxy.common_utils.ldap_directory import DirectoryError, DirectoryGroup +from litellm.proxy.common_utils.ldap_sync import LDAPSync, resolve_user_ids +from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache +from litellm.proxy.db.db_transaction_queue.pod_lock_manager import PodLockManager +from tests.unit.proxy.common_utils.test_ldap_directory import ALICE, Connection, group, user +from tests.unit.proxy.management_helpers.test_team_roster_sync import ( + TEAM, + _PausedDeleteRedisCache, # pyright: ignore[reportPrivateUsage] # event-controlled Redis boundary + _FakePrisma, # pyright: ignore[reportPrivateUsage] # reuse the transactional database fake + _member, # pyright: ignore[reportPrivateUsage] # shared roster fixtures + _team, # pyright: ignore[reportPrivateUsage] # shared roster fixtures + _user, # pyright: ignore[reportPrivateUsage] # shared roster fixtures +) +from litellm.repositories.user_repository import UserRepository +from litellm.types.proxy.ldap_sync import LDAPSyncSettings + +SETTINGS: Final = LDAPSyncSettings( + enabled=True, + server_url="ldaps://directory.example.org", + group_search_base="ou=groups,dc=example,dc=org", + group_filter="(cn=project-*)", + user_search_base="ou=people,dc=example,dc=org", +) + + +class Users: + def __init__(self, rows: Sequence[LiteLLM_UserTable]) -> None: + self.rows = rows + self.db = self + self.writer_db = self + self.litellm_usertable = self + + async def find_many( + self, + *, + where: Mapping[str, object] | None = None, + take: int | None = None, + skip: int | None = None, + order: Mapping[str, str] | None = None, + ) -> Sequence[LiteLLM_UserTable]: + return self.rows + + +@pytest.mark.asyncio +async def test_existing_users_match_case_insensitively_without_creating_unknown_users(): + users = Users([LiteLLM_UserTable(user_id="alice", user_email="Alice@example.org")]) + result = await resolve_user_ids( + UserRepository(users), + DirectoryGroup("project-a", "project-a", ("alice@example.org", "unknown@example.org")), + SETTINGS, + ) + assert result == frozenset({"alice"}) + assert len(users.rows) == 1 + + +@pytest.mark.asyncio +async def test_duplicate_email_accounts_abort_group_matching(): + users = Users( + [ + LiteLLM_UserTable(user_id="alice", user_email="Alice@example.org"), + LiteLLM_UserTable(user_id="other", user_email="alice@example.org"), + ] + ) + result = await resolve_user_ids( + UserRepository(users), DirectoryGroup("project-a", "project-a", ("alice@example.org",)), SETTINGS + ) + assert isinstance(result, DirectoryError) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("field", ["user_id", "sso_user_id"]) +async def test_configured_identity_field_matches_existing_user(field: str): + users = Users([LiteLLM_UserTable(user_id="alice", sso_user_id="z1234")]) + identity = "alice" if field == "user_id" else "z1234" + result = await resolve_user_ids( + UserRepository(users), + DirectoryGroup("project-a", "project-a", (identity,)), + SETTINGS.model_copy(update={"litellm_match_field": field}), + ) + assert result == frozenset({"alice"}) + + +class LeaseRedis: + def __init__(self) -> None: + self.values: dict[str, object] = {} # mutable-ok: fake Redis state + + async def async_set_cache(self, key: str, value: object, *, nx: bool, ttl: int) -> bool: + assert nx and ttl > 0 + if key in self.values: + return False + self.values[key] = value + return True + + async def async_get_cache(self, key: str) -> object | None: + return self.values.get(key) + + +def job_for( + prisma: _FakePrisma, + connection: Connection, + settings: LDAPSyncSettings = SETTINGS, + redis: LeaseRedis | None = None, + cache: UserApiKeyCache | None = None, +) -> LDAPSync: + lock: Final = PodLockManager(redis or LeaseRedis()) # pyright: ignore[reportArgumentType] # in-process Redis boundary + return LDAPSync( + settings, + prisma, # pyright: ignore[reportArgumentType] # transactional in-process Prisma boundary + lock, + cache or UserApiKeyCache(), + None, + directory_connection=connection, + ) + + +@pytest.mark.asyncio +async def test_job_updates_existing_roster_and_revokes_only_removed_members_team_keys(): + prisma: Final = _FakePrisma( + users=[_user("alice"), _user("bob", TEAM)], + teams=[_team(_member("bob"))], + memberships=[(TEAM, "bob")], + tokens=[{"token": "bob-team-key", "user_id": "bob", "team_id": TEAM}], + ) + connection: Final = Connection([group(TEAM, member=[ALICE])], {ALICE: [user("alice")]}) + await job_for(prisma, connection, SETTINGS.model_copy(update={"litellm_match_field": "user_id"})).run() + assert [member.user_id for member in prisma.writer_db.litellm_teamtable.rows[TEAM].members_with_roles] == ["alice"] + assert prisma.writer_db.litellm_usertable.rows["bob"].teams == [] + assert prisma.writer_db.litellm_usertable.rows["alice"].teams == [TEAM] + assert prisma.writer_db.litellm_verificationtoken.rows == [] + assert set(prisma.writer_db.litellm_usertable.rows) == {"alice", "bob"} + assert connection.closed + + +@pytest.mark.asyncio +@pytest.mark.parametrize("team_exists", [False, True]) +async def test_dry_run_leaves_users_teams_memberships_and_keys_untouched(team_exists: bool): + prisma: Final = _FakePrisma( + users=[_user("bob", TEAM)], + teams=[_team(_member("bob"))] if team_exists else [], + memberships=[(TEAM, "bob")], + tokens=[{"token": "bob-team-key", "user_id": "bob", "team_id": TEAM}], + ) + connection: Final = Connection([group(TEAM, member=[])]) + await job_for(prisma, connection, SETTINGS.model_copy(update={"dry_run": True})).run() + assert prisma.transactions == 0 + assert set(prisma.writer_db.litellm_teamtable.rows) == ({TEAM} if team_exists else set()) + assert prisma.writer_db.litellm_usertable.rows["bob"].teams == [TEAM] + assert prisma.writer_db.litellm_teammembership.rows == [{"team_id": TEAM, "user_id": "bob", "budget_id": None}] + assert prisma.writer_db.litellm_verificationtoken.rows == [ + {"token": "bob-team-key", "user_id": "bob", "team_id": TEAM} + ] + + +@pytest.mark.asyncio +async def test_second_worker_cannot_sync_while_first_workers_lease_is_held(): + prisma: Final = _FakePrisma(teams=[_team()]) + redis: Final = LeaseRedis() + first: Final = Connection([group(TEAM, member=[])]) + second: Final = Connection([group(TEAM, member=[])]) + await job_for(prisma, first, redis=redis).run() + transactions: Final = prisma.transactions + await job_for(prisma, second, redis=redis).run() + assert first.closed + assert not second.closed + assert prisma.transactions == transactions + + +@pytest.mark.asyncio +async def test_bad_group_does_not_prevent_later_healthy_group_from_syncing(): + prisma: Final = _FakePrisma( + users=[_user("bob", TEAM)], + teams=[_team(_member("bob"))], + memberships=[(TEAM, "bob")], + ) + connection: Final = Connection([group("broken", member=[ALICE]), group(TEAM, member=[])]) + await job_for(prisma, connection).run() + assert set(prisma.writer_db.litellm_teamtable.rows) == {TEAM} + assert prisma.writer_db.litellm_teamtable.rows[TEAM].members_with_roles == [] + assert prisma.writer_db.litellm_teammembership.rows == [] + assert prisma.writer_db.litellm_usertable.rows["bob"].teams == [] + + +@pytest.mark.asyncio +async def test_failed_snapshot_preserves_every_team_roster(): + prisma: Final = _FakePrisma(users=[_user("bob", TEAM)], teams=[_team(_member("bob"))]) + connection: Final = Connection([group(TEAM, member=[])], result_code=50) + await job_for(prisma, connection).run() + assert prisma.transactions == 0 + assert [member.user_id for member in prisma.writer_db.litellm_teamtable.rows[TEAM].members_with_roles] == ["bob"] + assert connection.closed + + +@pytest.mark.asyncio +async def test_missing_team_is_created_with_defaults_and_only_existing_users(monkeypatch: pytest.MonkeyPatch): + import litellm + import litellm.proxy.proxy_server as proxy_server + + prisma: Final = _FakePrisma(users=[_user("alice")]) + monkeypatch.setattr(proxy_server, "prisma_client", prisma) + monkeypatch.setattr(litellm, "default_team_params", {"models": ["test-model"], "max_budget": 42}) + connection: Final = Connection([group(TEAM, member=[ALICE])], {ALICE: [user("alice")]}) + await job_for(prisma, connection, SETTINGS.model_copy(update={"litellm_match_field": "user_id"})).run() + assert set(prisma.writer_db.litellm_teamtable.rows) == {TEAM} + created: Final = prisma.writer_db.litellm_teamtable.rows[TEAM] + assert created.models == ["test-model"] + assert created.max_budget == 42 + assert [member.user_id for member in created.members_with_roles] == ["alice"] + assert set(prisma.writer_db.litellm_usertable.rows) == {"alice"} + assert prisma.writer_db.litellm_usertable.rows["alice"].teams == [TEAM] + + +@pytest.mark.asyncio +async def test_failed_team_creation_does_not_block_later_groups(monkeypatch: pytest.MonkeyPatch): + import litellm.proxy.proxy_server as proxy_server + from litellm.proxy._types import UI_TEAM_ID + + prisma: Final = _FakePrisma( + users=[_user("bob", TEAM)], + teams=[_team(_member("bob"))], + memberships=[(TEAM, "bob")], + ) + monkeypatch.setattr(proxy_server, "prisma_client", prisma) + connection: Final = Connection([group(UI_TEAM_ID, member=[]), group(TEAM, member=[])]) + await job_for(prisma, connection).run() + assert set(prisma.writer_db.litellm_teamtable.rows) == {TEAM} + assert prisma.writer_db.litellm_teamtable.rows[TEAM].members_with_roles == [] + assert prisma.writer_db.litellm_usertable.rows["bob"].teams == [] + assert prisma.writer_db.litellm_teammembership.rows == [] + + +@pytest.mark.asyncio +async def test_cancelled_job_finishes_committed_revocation_and_stops_before_next_group(monkeypatch: pytest.MonkeyPatch): + import litellm.proxy.common_utils.auth_cache_invalidation_pubsub as pubsub_module + + monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", None) + prisma: Final = _FakePrisma( + users=[_user("bob", TEAM)], + teams=[_team(_member("bob"))], + memberships=[(TEAM, "bob")], + tokens=[{"token": "bob-key", "user_id": "bob", "team_id": TEAM}], + ) + redis: Final = _PausedDeleteRedisCache({"bob-key": {"token": "bob-key"}}) + cache: Final = UserApiKeyCache() + cache.attach_redis_cache(redis) # pyright: ignore[reportArgumentType] # dict-backed Redis boundary + connection: Final = Connection([group(TEAM, member=[]), group("later", member=[])]) + job: Final = asyncio.create_task(job_for(prisma, connection, cache=cache).run()) + await redis.reached.wait() + assert prisma.writer_db.litellm_verificationtoken.rows == [] + _ = job.cancel() + asyncio.get_running_loop().call_soon(redis.release.set) + with pytest.raises(asyncio.CancelledError): + await job + assert "bob-key" not in redis.entries + assert set(prisma.writer_db.litellm_teamtable.rows) == {TEAM} + assert prisma.transactions == 1 diff --git a/tests/unit/proxy/management_helpers/test_team_roster_sync.py b/tests/unit/proxy/management_helpers/test_team_roster_sync.py index 74c3dee9d56..7722307084e 100644 --- a/tests/unit/proxy/management_helpers/test_team_roster_sync.py +++ b/tests/unit/proxy/management_helpers/test_team_roster_sync.py @@ -28,6 +28,7 @@ from litellm.proxy.management_helpers.team_roster_sync import ( RosterTarget, TeamGone, sync_team_roster, + new_team_request_with_defaults, ) ADMIN: Final = UserAPIKeyAuth(user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN, api_key="sk-admin") @@ -75,6 +76,10 @@ def _clause_matches(row: Mapping[str, object], field: str, clause: object) -> bo return row.get(field) == clause operator = _FILTER.validate_python(clause) if "in" in operator: + if operator.get("mode") == "insensitive": + return str(row.get(field)).casefold() in { + value.casefold() for value in _STRINGS.validate_python(operator["in"]) + } return row.get(field) in set(_STRINGS.validate_python(operator["in"])) if "has" in operator: return operator["has"] in _STRINGS.validate_python(row.get(field) or []) @@ -123,7 +128,14 @@ class _UserTable: self.ledger = ledger self.rows: dict[str, _UserRow] = {u.user_id: u for u in users} - async def find_many(self, where: Mapping[str, object]) -> list[_UserRow]: + async def find_many( + self, + where: Mapping[str, object], + *, + take: int | None = None, + skip: int | None = None, + order: Mapping[str, str] | None = None, + ) -> list[_UserRow]: self.ledger.statements.append("usertable.find_many") return [u for u in self.rows.values() if _matches(u.model_dump(), where)] @@ -150,13 +162,22 @@ class _TeamTable: self.ledger.statements.append("teamtable.find_unique") return self.rows.get(where["team_id"]) + async def count(self) -> int: + return len(self.rows) + + async def create(self, data: Mapping[str, object], include: object = None) -> LiteLLM_TeamTable: + self.ledger.statements.append("teamtable.create") + team: Final = LiteLLM_TeamTable.model_validate(data) + self.rows[team.team_id] = team + return team + async def update(self, where: Mapping[str, str], data: Mapping[str, str]) -> LiteLLM_TeamTable | None: self.ledger.statements.append("teamtable.update") if self.deleted_before_update: return None roster = _MEMBERS.validate_json(data["members_with_roles"]) updated = self.rows[where["team_id"]].model_copy( - update={"members_with_roles": roster, "updated_at": datetime.now(UTC)} + update={"members_with_roles": roster, "updated_at": datetime(2026, 1, 1, tzinfo=UTC)} ) self.rows[where["team_id"]] = updated return updated @@ -184,6 +205,15 @@ class _Db: self.litellm_budgettable = _Rows("budgettable", ledger, budgets) self.litellm_jwtkeymapping = _Rows("jwtkeymapping", ledger, jwt_mappings) + @asynccontextmanager + async def tx(self): + snapshot: Final = copy.deepcopy(self) + try: + yield _Tx(self, self.litellm_teamtable.ledger) + except BaseException: + self.__dict__.update(snapshot.__dict__) + raise + class _Tx: def __init__(self, db: _Db, ledger: _Ledger) -> None: @@ -197,6 +227,11 @@ class _Tx: async def query_raw(self, sql: str, *args: object) -> list[dict[str, object]]: self.ledger.statements.append("query_raw") + if "LiteLLM_AccessGroupTable" in sql or "SELECT access_group_ids" in sql: + return [] + if "SELECT members_with_roles" in sql: + team: Final = self.litellm_teamtable.rows.get(str(args[0])) + return [{"members_with_roles": [member.model_dump() for member in team.members_with_roles]}] if team else [] if "pg_advisory_xact_lock" in sql: assert args == (TEAM,) return [{"locked": False}] @@ -239,6 +274,13 @@ class _FakePrisma: self._fail_commit = fail_commit self.transactions = 0 + async def get_data(self, *, team_id: str, table_name: str, query_type: str) -> LiteLLM_TeamTable | None: + assert (table_name, query_type) == ("team", "find_unique") + return self.writer_db.litellm_teamtable.rows.get(team_id) + + def jsonify_team_object(self, db_data: Mapping[str, object]) -> Mapping[str, object]: + return db_data + @asynccontextmanager async def tx(self, *, timeout: object = None): self.transactions += 1 @@ -557,15 +599,13 @@ class _Pipeline: await self._send(tuple(self._messages)) -class _SlowPublishRedisClient: - def __init__(self, round_trip_seconds: float = 0.001) -> None: +class _RecordingPublishRedisClient: + def __init__(self) -> None: self.published: list[str] = [] self.round_trips = 0 - self._round_trip_seconds = round_trip_seconds async def _send(self, messages: Sequence[str]) -> None: self.round_trips += 1 - await asyncio.sleep(self._round_trip_seconds) self.published.extend(messages) async def publish(self, channel: str, message: str) -> int: @@ -579,10 +619,10 @@ class _SlowPublishRedisClient: class _PubSubRedisCache: namespace = None - def __init__(self, client: "_SlowPublishRedisClient | _WedgedPublishRedisClient") -> None: + def __init__(self, client: "_RecordingPublishRedisClient | _WedgedPublishRedisClient") -> None: self._client = client - def init_pubsub_client(self) -> "_SlowPublishRedisClient | _WedgedPublishRedisClient": + def init_pubsub_client(self) -> "_RecordingPublishRedisClient | _WedgedPublishRedisClient": return self._client @@ -590,6 +630,10 @@ async def _until_reached_or_done(signal: asyncio.Event, sync: "asyncio.Task[obje reached: Final = asyncio.create_task(signal.wait()) _ = await asyncio.wait((reached, sync), return_when=asyncio.FIRST_COMPLETED) _ = reached.cancel() + _ = await asyncio.gather(reached, return_exceptions=True) + if not signal.is_set(): + await sync + pytest.fail("Operation ended before reaching the expected boundary") async def _drain_publishes() -> None: @@ -602,7 +646,7 @@ async def test_removing_hundreds_of_members_at_once_broadcasts_every_key_and_mem ): monkeypatch.setattr(pubsub_module, "_in_flight_publishes", asyncio.Semaphore(16)) monkeypatch.setattr(pubsub_module, "_pending_publishes", set()) - client = _SlowPublishRedisClient() + client = _RecordingPublishRedisClient() removed = [f"u{index:03d}" for index in range(500)] prisma = _FakePrisma( users=[_user(u, TEAM) for u in removed], @@ -626,12 +670,14 @@ async def test_removing_hundreds_of_members_at_once_broadcasts_every_key_and_mem class _WedgedPublishRedisClient: def __init__(self) -> None: + self.published: list[str] = [] # mutable-ok: capture messages accepted by fake Redis self.release = asyncio.Event() self.reached = asyncio.Event() async def _send(self, messages: Sequence[str]) -> None: self.reached.set() await self.release.wait() + self.published.extend(messages) async def publish(self, channel: str, message: str) -> int: await self._send((message,)) @@ -647,7 +693,7 @@ async def test_removing_members_clears_this_worker_before_waiting_on_a_wedged_re ): monkeypatch.setattr(pubsub_module, "_in_flight_publishes", asyncio.Semaphore(16)) monkeypatch.setattr(pubsub_module, "_pending_publishes", set()) - monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0.5) + monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", None) client = _WedgedPublishRedisClient() removed = [f"u{index:03d}" for index in range(300)] token_of = {u: hashlib.sha256(f"key-{u}".encode()).hexdigest() for u in removed} @@ -726,7 +772,7 @@ async def test_removing_members_clears_the_redis_copies_before_waiting_on_a_wedg ): monkeypatch.setattr(pubsub_module, "_in_flight_publishes", asyncio.Semaphore(16)) monkeypatch.setattr(pubsub_module, "_pending_publishes", set()) - monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0.5) + monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", None) client = _WedgedPublishRedisClient() removed = [f"u{index:03d}" for index in range(300)] token_of = {u: hashlib.sha256(f"key-{u}".encode()).hexdigest() for u in removed} @@ -787,7 +833,7 @@ class _SlowToAcknowledgeRedisCache(_AuthRedisCache): async def test_a_removed_members_team_key_cannot_refill_from_redis_while_the_eviction_waits_on_redis( monkeypatch: pytest.MonkeyPatch, ): - monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0.5) + monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", None) token = hashlib.sha256(b"key-bob").hexdigest() redis = _SlowToAcknowledgeRedisCache({token: {"token": token}}) cache = UserApiKeyCache() @@ -838,8 +884,8 @@ async def test_removing_hundreds_of_members_through_a_slow_redis_broadcasts_ever ): monkeypatch.setattr(pubsub_module, "_in_flight_publishes", asyncio.Semaphore(16)) monkeypatch.setattr(pubsub_module, "_pending_publishes", set()) - monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0.05) - client = _SlowPublishRedisClient(round_trip_seconds=0.05) + monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0) + client = _WedgedPublishRedisClient() removed = [f"u{index:03d}" for index in range(600)] prisma, evicted = _removal_of(removed) @@ -848,6 +894,9 @@ async def test_removing_hundreds_of_members_through_a_slow_redis_broadcasts_ever return_value=_PubSubRedisCache(client), ): outcome = await _sync(prisma, RosterTarget(member_ids=frozenset())) + await client.reached.wait() + assert client.published == [] + client.release.set() await _drain_publishes() assert isinstance(outcome, RosterSync) and outcome.removed == frozenset(removed) @@ -866,7 +915,7 @@ async def test_the_redis_round_trips_do_not_grow_with_the_member_count(monkeypat redis = _AuthRedisCache({}) cache = UserApiKeyCache() cache.attach_redis_cache(redis) # pyright: ignore[reportArgumentType] # a dict-backed fake of the Redis layer - client = _SlowPublishRedisClient() + client = _RecordingPublishRedisClient() with patch( "litellm.proxy.common_utils.auth_cache_invalidation_pubsub.coordination_redis_cache", @@ -941,3 +990,69 @@ async def test_the_statement_count_does_not_grow_with_the_member_count( assert _tokens(prisma) == ({f"key-{u}" for u in old} if keeps_old else set()) counts[size] = len(prisma.ledger.statements) assert counts[500] == counts[5] + + +class _PausedDeleteRedisCache(_AuthRedisCache): + def __init__(self, entries: dict[str, object]) -> None: + super().__init__(entries) + self.reached = asyncio.Event() + self.release = asyncio.Event() + + async def delete_cache_keys(self, keys: Sequence[str]) -> None: + self.reached.set() + await self.release.wait() + await super().delete_cache_keys(keys) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("cancel_twice", [False, True]) +async def test_cancellation_after_commit_finishes_revoking_cached_team_keys( + monkeypatch: pytest.MonkeyPatch, + cancel_twice: bool, +): + monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", None) + token: Final = hashlib.sha256(b"key-bob").hexdigest() + redis: Final = _PausedDeleteRedisCache({token: {"token": token}}) + cache: Final = UserApiKeyCache() + cache.attach_redis_cache(redis) # pyright: ignore[reportArgumentType] # dict-backed Redis boundary + cache.in_memory_cache_for(token).set_cache(key=token, value={"token": token}) + prisma: Final = _FakePrisma( + users=[_user("bob", TEAM)], + teams=[_team(_member("bob"))], + memberships=[(TEAM, "bob")], + tokens=[{"token": token, "user_id": "bob", "team_id": TEAM}], + ) + sync: Final = asyncio.create_task(_sync(prisma, RosterTarget(member_ids=frozenset()), cache)) + await _until_reached_or_done(redis.reached, sync) + assert _tokens(prisma) == set() + assert _memberships(prisma) == [] + assert token in redis.entries + _ = sync.cancel() + if cancel_twice: + asyncio.get_running_loop().call_soon(sync.cancel) + asyncio.get_running_loop().call_soon(redis.release.set) + with pytest.raises(asyncio.CancelledError): + await sync + assert token not in redis.entries + assert await cache.async_get_cache(key=token) is None + + +def test_team_defaults_preserve_fields_outside_sso_model_without_mutating_configuration( + monkeypatch: pytest.MonkeyPatch, +): + import litellm + + defaults: Final = { + "models": ["allowed-model"], + "max_budget": 12.0, + "default_team_member_models": ["member-model"], + "metadata": {"environment": "production"}, + } + original: Final = copy.deepcopy(defaults) + monkeypatch.setattr(litellm, "default_team_params", defaults) + request: Final = new_team_request_with_defaults("new-team", "New team", (), {"managed_by": "scim"}) + assert request.models == ["allowed-model"] + assert request.max_budget == 12.0 + assert request.default_team_member_models == ["member-model"] + assert request.metadata == {"environment": "production", "managed_by": "scim"} + assert defaults == original diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index d14e490f606..2ab01f6e394 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -31023,6 +31023,8 @@ export interface components { infer_model_from_keys?: boolean | null; /** @description key manager to load keys from / decrypt keys with */ key_management_system?: components["schemas"]["KeyManagementSystem"] | null; + /** @description Periodically synchronize LDAP groups to teams and existing users' memberships. Requires Redis. */ + ldap_sync?: components["schemas"]["LDAPSyncSettings"] | null; /** * Master Key * @description require a key for all calls to proxy @@ -35123,6 +35125,91 @@ export interface components { /** Service Host */ service_host: string; }; + /** LDAPSyncSettings */ + LDAPSyncSettings: { + /** Bind Dn */ + bind_dn?: string | null; + /** Bind Password */ + bind_password?: string | null; + /** Ca Cert Path */ + ca_cert_path?: string | null; + /** + * Dry Run + * @default false + */ + dry_run: boolean; + /** + * Enabled + * @default false + */ + enabled: boolean; + /** + * Group Filter + * @default + */ + group_filter: string; + /** + * Group Id Attribute + * @default cn + */ + group_id_attribute: string; + /** + * Group Name Attribute + * @default cn + */ + group_name_attribute: string; + /** + * Group Search Base + * @default + */ + group_search_base: string; + /** + * Interval Minutes + * @default 30 + */ + interval_minutes: number; + /** + * Litellm Match Field + * @default user_email + * @enum {string} + */ + litellm_match_field: "user_email" | "user_id" | "sso_user_id"; + /** + * Member Attribute + * @default member + */ + member_attribute: string; + /** + * Member Lookup Attribute + * @default uid + */ + member_lookup_attribute: string; + /** + * Server Url + * @default + */ + server_url: string; + /** + * Start Tls + * @default false + */ + start_tls: boolean; + /** + * Timeout Seconds + * @default 30 + */ + timeout_seconds: number; + /** + * User Match Attribute + * @default mail + */ + user_match_attribute: string; + /** + * User Search Base + * @default + */ + user_search_base: string; + }; /** LLMV2Calibration */ LLMV2Calibration: { capable: components["schemas"]["LLMV2ProbabilityCalibration"]; diff --git a/uv.lock b/uv.lock index 954b9f90a75..d5b9b2c3be1 100644 --- a/uv.lock +++ b/uv.lock @@ -4820,6 +4820,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/03/70/0e0cc80a3b064c8d6c8d697c3125ed86e39d5a7393ec6dc8b07cb1cf13c4/langsmith-0.8.18-py3-none-any.whl", hash = "sha256:3940183349993faef48e6c7d08e4822ee9cefd906b362d0e3c2d650314d2f282", size = 508108, upload-time = "2026-06-19T13:12:15.348Z" }, ] +[[package]] +name = "ldap3" +version = "2.9.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pyasn1" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/43/ac/96bd5464e3edbc61595d0d69989f5d9969ae411866427b2500a8e5b812c0/ldap3-2.9.1.tar.gz", hash = "sha256:f3e7fc4718e3f09dda568b57100095e0ce58633bcabbed8667ce3f8fbaa4229f", size = 398830, upload-time = "2021-07-18T06:34:21.786Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4e/f6/71d6ec9f18da0b2201287ce9db6afb1a1f637dedb3f0703409558981c723/ldap3-2.9.1-py2.py3-none-any.whl", hash = "sha256:5869596fc4948797020d3f03b7939da938778a0f9e2009f7a072ccf92b8e8d70", size = 432192, upload-time = "2021-07-18T06:34:12.905Z" }, +] + + [[package]] name = "lazy-object-proxy" version = "1.12.0" @@ -5067,6 +5080,7 @@ mlflow = [ ] proxy = [ { name = "apscheduler" }, + { name = "ldap3" }, { name = "azure-identity" }, { name = "azure-storage-blob" }, { name = "backoff" }, @@ -5270,6 +5284,7 @@ requires-dist = [ { name = "anthropic", extras = ["vertex"], marker = "extra == 'proxy-runtime'", specifier = ">=0.84.0,<1.0" }, { name = "anyio", specifier = ">=4.14.0,<5.0" }, { name = "apscheduler", marker = "extra == 'proxy'", specifier = ">=3.11.2,<4.0" }, + { name = "ldap3", marker = "extra == 'proxy'", specifier = ">=2.9.1,<3.0" }, { name = "async-timeout", marker = "python_full_version < '3.11'", specifier = ">=4.0.3,<6.0" }, { name = "audioread", marker = "extra == 'stt-nvidia-riva'", specifier = ">=3.0.1" }, { name = "aurelio-sdk", marker = "python_full_version < '3.14' and extra == 'semantic-router'", specifier = ">=0.0.19,<1.0" },