litellm/tests/e2e/management/conftest.py
mubashir1osmani 6c003562f3 test(e2e): add JWT auth journey suite against a real Keycloak IdP
Authenticate to the gateway with real RS256 JWTs from Keycloak (the IdP in the
e2e stack), the way an enterprise fronts the proxy with its identity provider.
The suite idempotently provisions a realm with an admin client (token carries the
litellm_proxy_admin scope), a team client (token carries a hardcoded team_id
claim), and a short-lived client, then asserts: an admin token is allowed on a
management route; a team token can call its team's model and is denied a model
outside the team's allow-list; a token signed by a key outside the IdP's JWKS is
rejected; and a short-lived token flips from accepted to 401 once it expires.

Adds a urllib-based Keycloak driver (pydantic-validated boundaries, no requests),
a JWTAuthClient, keycloak_env/jwt_client fixtures, KEYCLOAK_* config, and two new
other.auth.jwt team-model cells (the three jwt reject/allow cells already existed).

Requires a licensed proxy started with enable_jwt_auth + litellm_jwtauth.issuers
trusting the Keycloak realm (config block documented in the suite module docstring).

Part of LIT-4639.
2026-07-20 21:11:48 -07:00

57 lines
1.9 KiB
Python

"""Management suite's `client` fixture.
Lifecycle/liveness gate/marker live in the parent conftest. ManagementClient
holds the shared ProxyClient so `resources` / `scoped_key` clean up keys, teams,
users, and orgs this suite creates.
"""
import pytest
from e2e_config import KEYCLOAK_ADMIN_PASSWORD, KEYCLOAK_ADMIN_USER, KEYCLOAK_REALM, KEYCLOAK_URL
from jwt_auth_client import JWTAuthClient, build_jwt_client
from keycloak import KeycloakAdmin, KeycloakEnv
from management_client import ManagementClient, build_client
from proxy_client import ProxyClient
from scim_provisioning_client import SCIMProvisioningClient, build_scim_client
from sso_management_client import SSOManagementClient, build_sso_client
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line(
"markers",
"covers: registry cell a test covers, e.g. mgmt.key.generate.persists",
)
@pytest.fixture(scope="session")
def client(proxy: ProxyClient) -> ManagementClient:
return build_client(proxy)
@pytest.fixture(scope="session")
def sso_client(proxy: ProxyClient) -> SSOManagementClient:
return build_sso_client(proxy)
@pytest.fixture(scope="session")
def scim_client(proxy: ProxyClient) -> SCIMProvisioningClient:
return build_scim_client(proxy)
@pytest.fixture(scope="session")
def jwt_client(proxy: ProxyClient) -> JWTAuthClient:
return build_jwt_client(proxy)
@pytest.fixture(scope="session")
def keycloak_env() -> KeycloakEnv:
"""Provision (idempotently) the realm/clients the JWT-auth suite needs and
return a handle that mints real tokens. Hard-fails if Keycloak is unreachable -
a live e2e never skips for missing infrastructure."""
admin = KeycloakAdmin(
base_url=KEYCLOAK_URL,
realm=KEYCLOAK_REALM,
admin_user=KEYCLOAK_ADMIN_USER,
admin_password=KEYCLOAK_ADMIN_PASSWORD,
)
return admin.provision()