litellm/tests/unit/proxy/test_credential_slot_registry.py
giladk1221 9d827093c5
feat(github_copilot): support GitHub App authentication with PEM upload (#51427)
* feat(github_copilot): support GitHub App installation authentication

* Add GitHub App authentication to the Copilot credential form

* Explain secure private-key setup beside Copilot App fields

* Store uploaded Copilot App keys through encrypted named credentials

* fix(github_copilot): handle App validation errors and credential replacement

* fix(ui): make GitHub App field help accessible

* fix(github_copilot): preserve saved App key references on edit

* fix(github_copilot): trust litellm's CA settings when minting App tokens and hide App keys from /health

* fix(github_copilot): keep device-login headers, mint App tokens off the shared thread pool, cache App responses

Device-login requests keep the merge base header behavior, so caller headers still replace the Copilot defaults. Only App requests merge and pin their identity headers.

App token refreshes are single-flight per cache key with a shared future. Only the refreshing caller uses a thread, so waiting callers no longer fill the default executor that every provider call shares.

A cached App token is served before the key secret is read. Uploaded keys still rotate the token when their contents change.

App deployments are deployment-owned, so they share the response cache like shared device-login deployments.

App credentials are validated even when credential_info names no provider.

* fix(github_copilot): finish an App token refresh even when the caller that started it is cancelled

The caller that owns a refresh used to await asyncio.to_thread, so cancelling it while the job was still queued cancelled the job, left the in-flight future unfinished, and every later request for that App waited on it. The refresh now runs in the executor without being tied to the caller, and every caller, the owner included, awaits the shared future

* fix(github_copilot): refuse deployments naming an unloaded credential, keep App key references, accept numeric App IDs

A Copilot deployment whose litellm_credential_name is not loaded now answers 400 instead of falling back to the shared device login. The router registry keeps github_copilot_app_private_key_ref values as references, a YAML App ID can be an integer, the server-owned field error names Copilot auth for Copilot fields, and the credential edit modal shows the stored provider again

* fix(github_copilot): report a deployment naming an unloaded credential as unhealthy

The /health check now answers unhealthy with the same 400 the request path returns for a Copilot deployment whose litellm_credential_name is not loaded, instead of running it through the shared device login

* fix(github_copilot): keep the base fallback for an unloaded credential and report the App host as api_base

A Copilot deployment that names an unloaded credential uses the shared device login again, as on base.
A deployment that sets github_copilot_auth_type: github_app still refuses before any GitHub call.
get_api_base now returns the Copilot host an App or per-user session calls instead of an admin api_base it ignores.

* fix(github_copilot): count a GitHub rejection of the App installation token as a Prometheus deployment failure

* fix(github_copilot): keep per-user session api_base reporting as on base and name Copilot in the non-admin 403

Only an App deployment reports its App host as api_base before a configured api_base.
Per-user OAuth sessions report the same api_base they did on base.
The 403 for a non-admin attaching App credentials now names GitHub Copilot authentication.
The token-dir fixture in the App api_base test is applied with usefixtures.

* fix(github_copilot): report the configured api_base instead of raising when GitHub App settings are incomplete

* test(github_copilot): type the incomplete App settings test parameter

---------

Co-authored-by: Gilad Katz <29776916+giladk1221@users.noreply.github.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-14 10:33:20 -05:00

233 lines
9.9 KiB
Python

"""Every credential-bearing param is classified for the credential canary suite.
These tests fail until a param is classified below as one of:
- ``Secret(<slot id>)``: an integration test in ``tests/integration/security`` plants a canary in
exactly this param under that slot id.
- ``Unplanted()``: the param can carry a credential, but no integration test plants a canary
in it yet. This is a classification only.
- ``NotSecret(<reason>)``: the param cannot carry a credential.
``CANARY_SLOTS`` mirrors ``SLOTS`` in ``tests/integration/security/_canary.py``, limited to the ids
whose test plants a canary in one of these params.
"""
import re
from collections.abc import Iterable, Mapping
from dataclasses import dataclass
from pathlib import Path
from types import MappingProxyType
from typing import Final
from litellm.proxy.auth.auth_utils import is_request_body_safe
from litellm.types.router import LiteLLM_Params, LiteLLMParamsTypedDict
from litellm.types.utils import CustomPricingLiteLLMParams, StandardCallbackDynamicParams
CANARY_SLOTS: Final[Mapping[str, str]] = MappingProxyType(
{
"B1": "deployment api_key in config.yaml",
"B4": "deployment aws_secret_access_key added through /model/new",
"B4v": "deployment vertex_credentials added through /model/new",
"C1": "team callback langfuse_secret / langfuse_secret_key",
"C3": "team callback dd_api_key for the Datadog sink",
"D1": "client-side api_key in the request body",
}
)
THIS_FILE: Final = "tests/unit/proxy/test_credential_slot_registry.py"
HARNESS_FILE: Final = Path(__file__).resolve().parents[2] / "integration" / "security" / "_canary.py"
CREDENTIAL_NAME: Final = re.compile(r"(?:^|_)(?:key|secret|token|password|credential)")
"""Matches a name segment that starts with a credential word. Anchoring on a segment start keeps
``valkey_host`` and the other ``valkey_*`` settings out, and still matches ``aws_access_key_id``."""
PRICING_FIELDS: Final = frozenset(CustomPricingLiteLLMParams.model_fields)
"""Excluded from the name match: in ``input_cost_per_token`` and friends, token is a billing unit."""
@dataclass(frozen=True)
class Secret:
slot: str
def __post_init__(self) -> None:
if self.slot not in CANARY_SLOTS:
raise ValueError(f"Secret({self.slot!r}) names no slot in CANARY_SLOTS")
@dataclass(frozen=True)
class Unplanted:
pass
@dataclass(frozen=True)
class NotSecret:
reason: str
Classification = Secret | Unplanted | NotSecret
CALLBACK_PARAM_CLASSIFICATION: Final[Mapping[str, Classification]] = MappingProxyType(
{
"langfuse_public_key": NotSecret("public half of the Langfuse key pair, an identifier"),
"langfuse_secret": Secret("C1"),
"langfuse_secret_key": Secret("C1"),
"langfuse_host": NotSecret("sink endpoint URL"),
"langfuse_environment": NotSecret("environment label"),
"langfuse_span_scope": NotSecret("span scope setting"),
"langfuse_prompt_version": NotSecret("prompt version number"),
"gcs_bucket_name": NotSecret("bucket name"),
"gcs_path_service_account": Unplanted(),
"langsmith_api_key": Unplanted(),
"langsmith_project": NotSecret("project name"),
"langsmith_base_url": NotSecret("sink endpoint URL"),
"langsmith_sampling_rate": NotSecret("sampling rate"),
"langsmith_tenant_id": NotSecret("tenant identifier"),
"humanloop_api_key": Unplanted(),
"arize_api_key": Unplanted(),
"arize_space_key": Unplanted(),
"arize_space_id": NotSecret("space identifier"),
"arize_success_sampling_rate": NotSecret("sampling rate"),
"arize_error_sampling_rate": NotSecret("sampling rate"),
"arize_otlp_protocol": NotSecret("transport protocol enum"),
"posthog_api_key": Unplanted(),
"posthog_api_url": NotSecret("sink endpoint URL"),
"wandb_api_key": Unplanted(),
"weave_project_id": NotSecret("project identifier"),
"dd_api_key": Secret("C3"),
"dd_site": NotSecret("sink site name"),
"dd_agent_host": NotSecret("agent host name"),
"dd_agent_port": NotSecret("agent port"),
"newrelic_api_key": Unplanted(),
"newrelic_region": NotSecret("region name"),
"signoz_ingestion_key": Unplanted(),
"signoz_ingestion_endpoint": NotSecret("sink endpoint URL"),
"turn_off_message_logging": NotSecret("boolean logging switch"),
"litellm_disabled_callbacks": NotSecret("list of callback names"),
}
)
DEPLOYMENT_PARAM_CLASSIFICATION: Final[Mapping[str, Classification]] = MappingProxyType(
{
"api_key": Secret("B1"),
"azure_ad_token": Unplanted(),
"client_secret": Unplanted(),
"azure_password": Unplanted(),
"token_exchange_endpoint": NotSecret("OAuth token endpoint URL"),
"token_exchange_profile": NotSecret("OAuth grant profile"),
"token_exchange_scope": NotSecret("OAuth scope string"),
"token_exchange_audience": NotSecret("OAuth audience identifier"),
"vertex_credentials": Secret("B4v"),
"aws_access_key_id": Unplanted(),
"aws_secret_access_key": Secret("B4"),
"aws_session_token": Unplanted(),
"aws_web_identity_token": Unplanted(),
"s3_access_key_id": Unplanted(),
"s3_secret_access_key": Unplanted(),
"s3_encryption_key_id": NotSecret("KMS key identifier, not key material"),
"litellm_credential_name": NotSecret("name of a credentials table entry, not a credential"),
"default_api_key_tpm_limit": NotSecret("rate limit number"),
"default_api_key_rpm_limit": NotSecret("rate limit number"),
"valkey_password": Unplanted(),
"anthropic_identity_token": Unplanted(),
"anthropic_identity_token_file": Unplanted(),
"anthropic_issuer_signing_key_ref": Unplanted(),
"anthropic_keycloak_token_url": NotSecret("Keycloak token endpoint URL"),
"anthropic_keycloak_client_id": NotSecret("Keycloak client identifier"),
"anthropic_keycloak_auth_method": NotSecret("name of the client authentication method"),
"anthropic_keycloak_client_secret_ref": Unplanted(),
"anthropic_keycloak_scope": NotSecret("OAuth scope string"),
"openai_identity_token_file": Unplanted(),
"github_copilot_app_private_key": Unplanted(),
"github_copilot_app_private_key_ref": Unplanted(),
}
)
REQUEST_BODY_PARAM_CLASSIFICATION: Final[Mapping[str, Classification]] = MappingProxyType(
{
"api_key": Secret("D1"),
"aws_access_key_id": Unplanted(),
"aws_secret_access_key": Unplanted(),
"aws_session_token": Unplanted(),
"s3_access_key_id": Unplanted(),
"s3_secret_access_key": Unplanted(),
"valkey_password": Unplanted(),
"s3_encryption_key_id": NotSecret("KMS key identifier, not key material"),
"litellm_credential_name": NotSecret("name of a credentials table entry, not a credential"),
"default_api_key_tpm_limit": NotSecret("rate limit number"),
"default_api_key_rpm_limit": NotSecret("rate limit number"),
}
)
def _credential_named(names: Iterable[str]) -> frozenset[str]:
return frozenset(name for name in names if CREDENTIAL_NAME.search(name)) - PRICING_FIELDS
def _deployment_param_names() -> frozenset[str]:
return (
frozenset(LiteLLM_Params.model_fields)
| LiteLLMParamsTypedDict.__required_keys__
| LiteLLMParamsTypedDict.__optional_keys__
)
def _callback_param_names() -> frozenset[str]:
return StandardCallbackDynamicParams.__required_keys__ | StandardCallbackDynamicParams.__optional_keys__
def _accepted_in_request_body(param: str) -> bool:
try:
return is_request_body_safe({"model": "m", param: "v"}, general_settings={}, llm_router=None, model="m")
except ValueError:
return False
def _assert_classified(
source: str, names: frozenset[str], mapping: Mapping[str, Classification], mapping_name: str
) -> None:
unclassified: Final = sorted(names - mapping.keys())
stale: Final = sorted(mapping.keys() - names)
assert not unclassified, (
f"{source} has params with no credential classification: {unclassified}. "
f"Add each to {mapping_name} in {THIS_FILE} as Secret('<slot id>') if it can hold a credential "
"and an integration test plants it under a slot in CANARY_SLOTS, as Unplanted() if it can hold a credential "
"but no integration test plants it yet, "
"or as NotSecret('<one-line reason>') if it cannot."
)
assert not stale, f"{mapping_name} in {THIS_FILE} classifies params {source} no longer has: {stale}. Remove them."
def test_every_callback_dynamic_param_is_classified():
_assert_classified(
"StandardCallbackDynamicParams",
_callback_param_names(),
CALLBACK_PARAM_CLASSIFICATION,
"CALLBACK_PARAM_CLASSIFICATION",
)
def test_every_credential_named_deployment_param_is_classified():
_assert_classified(
"LiteLLM_Params / LiteLLMParamsTypedDict",
_credential_named(_deployment_param_names()),
DEPLOYMENT_PARAM_CLASSIFICATION,
"DEPLOYMENT_PARAM_CLASSIFICATION",
)
def test_every_credential_named_param_a_client_may_send_is_classified():
candidates: Final = _credential_named(_deployment_param_names() | _callback_param_names())
_assert_classified(
"is_request_body_safe with default settings",
frozenset(name for name in candidates if _accepted_in_request_body(name)),
REQUEST_BODY_PARAM_CLASSIFICATION,
"REQUEST_BODY_PARAM_CLASSIFICATION",
)
def test_every_canary_slot_exists_in_the_harness():
harness_slots: Final = frozenset(re.findall(r'^\s+"(\w+)": Slot\(', HARNESS_FILE.read_text(), re.MULTILINE))
assert harness_slots, f"found no Slot(...) entries in {HARNESS_FILE}"
missing: Final = sorted(CANARY_SLOTS.keys() - harness_slots)
assert not missing, f"CANARY_SLOTS in {THIS_FILE} names slots {HARNESS_FILE.name} does not define: {missing}"