litellm/tests/unit/secret_managers/test_secret_managers_main.py
Mateo Wang 6c4c134f23
fix(secret_managers)!: resolve oidc/file and oidc/env refs in admin-configured api_key per request (#47341)
* fix(secret_managers): resolve oidc/file and oidc/env refs in admin-configured api_key per request

An api_key set to oidc/file/<path> or oidc/env/<VAR> in a credential_list entry or a deployment's
litellm_params was sent upstream as literal text, so every call answered 401. The SDK client wrapper
now reads the referenced file or env var on each call and never writes the token back, so a rotated
token is used on the next call. The existing server-owned federation gates treat an oidc/ api_key as
server-owned by value, so a request body, a non-admin credential or model write, user_config, fallback
targets and routing tier params can never carry or name one. Other oidc/ kinds answer 400

* fix(secret_managers): strip only the leading oidc/ from a secret reference

A token file under a directory named oidc resolved to the wrong path, because every oidc/ in the reference was removed

* fix(auth): refuse oidc/ api_key refs in advisor tools and scope the route gates to them

The advisor tool's own api_key now answers a 400 when it holds an oidc/
reference, since its sub-call runs through the client wrapper that resolves
one. Stored fallback targets, auto-router tiers and user_config refuse only an
oidc/ api_key, inline or through the credential they name, so their workload
identity federation handling stays as it was. Provider model discovery for a
wildcard deployment resolves the reference before calling the provider.

* fix(auth): allow admin oidc api_key on deployment writes, resolve refs on discovery, batch probe and realtime health, refuse client api_base on oidc deployments

* fix(utils): resolve oidc api_key refs only from admin-configured sources

Mark an oidc/ api_key read from a proxy-level deployment or a stored
credential, and resolve only that marked value. Team deployments, prompt
templates, nested configs and a direct SDK api_key send the text as
written. An unsupported reference kind raises a 401 so the router fails
over to the group's next deployment.

* fix(utils): send a credential's oidc token only to its own api_base host

A stored credential's oidc/ api_key now resolves only for the scheme, host and port of the
credential's own api_base or base_url, so a team model or vector store that names the
credential with another api_base gets a 401 and that host gets nothing. Saving a team model
with an inline oidc/ api_key answers 400, since team rows never resolve one.

* fix(proxy): send an admin oidc api_key only to the hosts configured beside it

Every admin oidc/ reference now carries the origins of the host params set beside it, checked for api_base, base_url and the Bedrock, S3, SageMaker and deployment_url params. Token counting reads the reference only when it calls the provider, answering 401 when it cannot, and pass-through 401s redact internal paths.

* fix(proxy): redact the token path from token count auth errors

* test(security): cover token count, Bedrock endpoint, api_base update and pass-through path redaction for oidc api_key refs

* fix(proxy): redact the token path from secret reference errors where they are raised

* fix(proxy): keep the secret reference's path out of the error a caller sees

The 401 for an unreadable oidc api_key or pass-through credential now names only
the reference kind and a fixed reason, so a token path under /run/secrets or a
custom credential dir never reaches the caller, while the server log keeps the
full reference for the operator

* fix(secret_managers): strip line breaks from the logged secret reference

A reference holding a newline can no longer forge a server log line

* fix(secret_managers): label an unknown oidc reference kind instead of echoing it

An api_key such as oidc/env_MY_VAR (a typo for oidc/env/MY_VAR) came back in
full in the caller's 401 and in the spend log, since the kind was whatever
text followed oidc/. A kind get_secret does not know now reads
oidc/<unknown kind>. The relative-path hint uses oidc/file//<absolute path>,
which the client-message redaction on chat routes leaves whole.

* test(health): run the real model auth check in the oidc test connection test

* test(secret_managers): read an oidc/file token stored under a directory named oidc

* fix(secret_managers): count a URL in the model name as an upstream host for oidc api_key references

A huggingface/* wildcard call could name its own endpoint URL as the model and
receive the resolved token. The origin check now treats a URL in the model as
a host the reference must allow. The integration contract also reads the
proxy's own database instead of the pytest worker's.

* fix(secret_managers): treat a default port as explicit when matching an oidc api_key origin

A credential on https://api.mistral.ai refused a call to https://api.mistral.ai:443,
though both reach the same server. The origin check now compares scheme, host
and effective port, as assert_same_origin does, and an unparseable api_base is
refused instead of raising. The advisor test sets the real proxy flag instead of
replacing our own helpers.

* test: type the oidc request-body helpers and drive the tier test through mock_response

* fix(secret_managers): refuse an oidc api_key reference whose call drops the configured host
2026-10-20 01:43:59 +00:00

501 lines
18 KiB
Python

import base64
import json
import logging
import os
import time
from typing import Final
from unittest.mock import Mock, patch
import pytest
import litellm
from litellm._logging import redact_internal_details_from_client_message
from litellm.integrations.custom_secret_manager import CustomSecretManager
from litellm.secret_managers.main import (
get_secret,
normalize_nonempty_secret_str,
resolve_local_oidc_reference,
secret_manager_would_be_consulted,
)
from litellm.types.secret_managers.main import KeyManagementSettings, KeyManagementSystem
# Set up logging for debugging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Mock HTTPHandler and oidc_cache
class MockHTTPHandler:
def __init__(self, timeout):
self.timeout = timeout
self.status_code = 200
self.text = "mocked_token"
self.json_data = {"value": "mocked_token"}
def get(self, url, params=None, headers=None):
# Store params for audience verification
self.last_params = params
logger.debug(
f"MockHTTPHandler.get called with url={url}, params={params}, headers={headers}"
)
mock_response = Mock()
mock_response.status_code = self.status_code
mock_response.text = self.text
mock_response.json.return_value = self.json_data
return mock_response
@pytest.fixture
def mock_oidc_cache():
cache = Mock()
cache.get_cache.return_value = None
cache.set_cache = Mock()
return cache
@pytest.fixture
def mock_env():
with patch.dict(os.environ, {}, clear=True):
yield os.environ
def test_oidc_google_success():
"""Test Google OIDC token fetch with mocked handler (no real network calls)."""
secret_name = "oidc/google/[invalid url, do not cite]"
mock_handler = MockHTTPHandler(timeout=600.0)
mock_get_http_handler = Mock(return_value=mock_handler)
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = None
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
with patch(
"litellm.secret_managers.main.HTTPHandler",
side_effect=lambda timeout=None: mock_handler,
):
result = get_secret(secret_name)
assert result == "mocked_token"
assert mock_handler.last_params == {"audience": "[invalid url, do not cite]"}
mock_oidc_cache.set_cache.assert_called_once_with(
key=secret_name, value="mocked_token", ttl=3540
)
def test_oidc_google_cached():
"""Test Google OIDC uses cache and does not call HTTP (no real network calls)."""
secret_name = "oidc/google/[invalid url, do not cite]"
mock_get_http_handler = Mock()
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = "cached_token"
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
with patch(
"litellm.secret_managers.main.HTTPHandler",
Mock(side_effect=AssertionError("HTTPHandler should not be used")),
):
result = get_secret(secret_name)
assert result == "cached_token", f"Expected cached token, got {result}"
mock_oidc_cache.get_cache.assert_called_with(key=secret_name)
mock_get_http_handler.assert_not_called()
def _jwt_with_exp(exp: int) -> str:
header = base64.urlsafe_b64encode(json.dumps({"alg": "RS256"}).encode()).rstrip(b"=").decode()
payload = base64.urlsafe_b64encode(json.dumps({"exp": exp}).encode()).rstrip(b"=").decode()
return f"{header}.{payload}.signature"
def test_oidc_google_cache_ttl_capped_by_token_exp():
"""A token the metadata server returns near its expiry must not be cached past
its exp claim; the cached-entry TTL is exp - now - 60s, not the 59m default."""
secret_name = "oidc/google/https://example.com/api"
mock_handler = MockHTTPHandler(timeout=600.0)
mock_handler.text = _jwt_with_exp(int(time.time()) + 300)
mock_get_http_handler = Mock(return_value=mock_handler)
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = None
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
result = get_secret(secret_name)
assert result == mock_handler.text
mock_oidc_cache.set_cache.assert_called_once()
ttl = mock_oidc_cache.set_cache.call_args.kwargs["ttl"]
assert 0 < ttl <= 240
def test_oidc_google_expired_token_not_cached():
"""An already-expired token is returned (STS gives the authoritative error) but
never cached, so the next call fetches a fresh token instead of replaying it."""
secret_name = "oidc/google/https://example.com/api"
mock_handler = MockHTTPHandler(timeout=600.0)
mock_handler.text = _jwt_with_exp(int(time.time()) - 10)
mock_get_http_handler = Mock(return_value=mock_handler)
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = None
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
result = get_secret(secret_name)
assert result == mock_handler.text
mock_oidc_cache.set_cache.assert_not_called()
def test_oidc_google_long_lived_token_still_capped_at_default_ttl():
"""A token expiring far in the future must not extend the cache past the
59m policy ceiling; exp only ever shortens the TTL."""
secret_name = "oidc/google/https://example.com/api"
mock_handler = MockHTTPHandler(timeout=600.0)
mock_handler.text = _jwt_with_exp(int(time.time()) + 7200)
mock_get_http_handler = Mock(return_value=mock_handler)
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = None
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
result = get_secret(secret_name)
assert result == mock_handler.text
mock_oidc_cache.set_cache.assert_called_once_with(
key=secret_name, value=mock_handler.text, ttl=3540
)
def test_oidc_google_non_jwt_token_keeps_default_ttl():
"""A token without a readable exp claim falls back to the 59m default TTL."""
secret_name = "oidc/google/https://example.com/api"
mock_handler = MockHTTPHandler(timeout=600.0)
mock_get_http_handler = Mock(return_value=mock_handler)
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = None
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
result = get_secret(secret_name)
assert result == "mocked_token"
mock_oidc_cache.set_cache.assert_called_once_with(
key=secret_name, value="mocked_token", ttl=3540
)
def test_oidc_google_failure():
"""Test Google OIDC raises when provider returns error (no real network calls)."""
secret_name = "oidc/google/https://example.com/api"
mock_handler = MockHTTPHandler(timeout=600.0)
mock_handler.status_code = 400
mock_get_http_handler = Mock(return_value=mock_handler)
mock_oidc_cache = Mock()
mock_oidc_cache.get_cache.return_value = None
with patch("litellm.secret_managers.main.oidc_cache", mock_oidc_cache):
with patch(
"litellm.secret_managers.main._get_oidc_http_handler",
mock_get_http_handler,
):
with patch(
"litellm.secret_managers.main.HTTPHandler",
side_effect=lambda timeout=None: mock_handler,
):
with pytest.raises(ValueError, match="Google OIDC provider failed"):
get_secret(secret_name)
def test_oidc_circleci_success(monkeypatch):
monkeypatch.setenv("CIRCLE_OIDC_TOKEN", "circleci_token")
secret_name = "oidc/circleci/test-audience"
result = get_secret(secret_name)
assert result == "circleci_token"
def test_oidc_circleci_v2_returns_the_environment_token(monkeypatch: pytest.MonkeyPatch) -> None:
token: Final = "circleci-v2-token"
monkeypatch.setenv("CIRCLE_OIDC_TOKEN_V2", token)
assert get_secret("oidc/circleci_v2/test-audience") == token
def test_oidc_circleci_failure(monkeypatch):
monkeypatch.delenv("CIRCLE_OIDC_TOKEN", raising=False)
secret_name = "oidc/circleci/test-audience"
with pytest.raises(ValueError, match="CIRCLE_OIDC_TOKEN not found in environment"):
get_secret(secret_name)
@patch("litellm.secret_managers.main.oidc_cache")
@patch("litellm.secret_managers.main._get_oidc_http_handler")
def test_oidc_github_success(mock_get_http_handler, mock_oidc_cache, mock_env):
mock_env["ACTIONS_ID_TOKEN_REQUEST_URL"] = "https://github.com/token"
mock_env["ACTIONS_ID_TOKEN_REQUEST_TOKEN"] = "github_token"
mock_oidc_cache.get_cache.return_value = None
mock_handler = MockHTTPHandler(timeout=600.0)
mock_get_http_handler.return_value = mock_handler
secret_name = "oidc/github/github-audience"
result = get_secret(secret_name)
assert result == "mocked_token", f"Expected token 'mocked_token', got {result}"
assert mock_handler.last_params == {"audience": "github-audience"}
logger.debug(f"set_cache call args: {mock_oidc_cache.set_cache.call_args}")
mock_oidc_cache.set_cache.assert_called_once()
mock_oidc_cache.set_cache.assert_called_with(
key=secret_name, value="mocked_token", ttl=295
)
def test_oidc_github_missing_env():
secret_name = "oidc/github/github-audience"
with pytest.raises(
ValueError,
match="ACTIONS_ID_TOKEN_REQUEST_URL or ACTIONS_ID_TOKEN_REQUEST_TOKEN not found in environment",
):
get_secret(secret_name)
def test_oidc_azure_file_success(mock_env, tmp_path):
token_file = tmp_path / "token.txt"
token_file.write_text("azure_token")
mock_env["AZURE_FEDERATED_TOKEN_FILE"] = str(token_file)
secret_name = "oidc/azure/azure-audience"
result = get_secret(secret_name)
assert result == "azure_token"
@patch("litellm.secret_managers.main.get_azure_ad_token_provider")
def test_oidc_azure_ad_token_success(mock_get_azure_ad_token_provider, monkeypatch):
# Force-unset so we always hit the Azure AD token provider path (CI may set AZURE_FEDERATED_TOKEN_FILE)
monkeypatch.delenv("AZURE_FEDERATED_TOKEN_FILE", raising=False)
# Mock the token provider function that gets returned and called
mock_token_provider = Mock(return_value="azure_ad_token")
mock_get_azure_ad_token_provider.return_value = mock_token_provider
# Also mock the Azure Identity SDK to prevent any real Azure calls
with patch("azure.identity.get_bearer_token_provider") as mock_bearer:
mock_bearer.return_value = mock_token_provider
secret_name = "oidc/azure/api://azure-audience"
result = get_secret(secret_name)
assert result == "azure_ad_token"
mock_get_azure_ad_token_provider.assert_called_once_with(
azure_scope="api://azure-audience"
)
mock_token_provider.assert_called_once_with()
def test_oidc_file_success(tmp_path, monkeypatch):
token_file = tmp_path / "token.txt"
token_file.write_text("file_token")
monkeypatch.setenv("LITELLM_OIDC_ALLOWED_CREDENTIAL_DIRS", str(tmp_path))
secret_name = f"oidc/file/{token_file}"
result = get_secret(secret_name)
assert result == "file_token"
def test_oidc_file_reads_a_token_under_a_directory_named_oidc(tmp_path, monkeypatch):
token_dir: Final = tmp_path / "oidc"
token_dir.mkdir()
(token_dir / "token").write_text("nested_token")
monkeypatch.setenv("LITELLM_OIDC_ALLOWED_CREDENTIAL_DIRS", str(tmp_path))
assert get_secret(f"oidc/file/{token_dir}/token") == "nested_token"
def test_oidc_file_rejects_path_outside_allowlist(tmp_path, monkeypatch):
outside_file = tmp_path / "outside.txt"
outside_file.write_text("should_not_read")
# Allowlist a different directory.
allowed_dir = tmp_path / "allowed"
allowed_dir.mkdir()
monkeypatch.setenv("LITELLM_OIDC_ALLOWED_CREDENTIAL_DIRS", str(allowed_dir))
with pytest.raises(ValueError, match="outside the allowed credential directories"):
get_secret(f"oidc/file/{outside_file}")
def test_oidc_file_rejects_relative_path_with_a_hint_client_redaction_leaves_whole(tmp_path, monkeypatch):
monkeypatch.setenv("LITELLM_OIDC_ALLOWED_CREDENTIAL_DIRS", str(tmp_path))
with pytest.raises(ValueError, match="must be absolute") as excinfo:
get_secret("oidc/file/relative/path/token")
hint: Final = str(excinfo.value)
assert redact_internal_details_from_client_message(hint) == hint
def test_oidc_env_success(mock_env):
mock_env["CUSTOM_TOKEN"] = "env_token"
secret_name = "oidc/env/CUSTOM_TOKEN"
result = get_secret(secret_name)
assert result == "env_token"
def test_oidc_env_path_success(mock_env, tmp_path):
token_file = tmp_path / "token.txt"
token_file.write_text("env_path_token")
mock_env["TOKEN_PATH"] = str(token_file)
secret_name = "oidc/env_path/TOKEN_PATH"
result = get_secret(secret_name)
assert result == "env_path_token"
def test_unsupported_oidc_provider():
secret_name = "oidc/unsupported/unsupported-audience"
with pytest.raises(ValueError, match="Unsupported OIDC provider"):
get_secret(secret_name)
@pytest.mark.parametrize(
("raw", "expected"),
[
(None, None),
("", None),
(" \t\n", None),
("abc", "abc"),
(" xyz ", "xyz"),
],
)
def test_normalize_nonempty_secret_str(raw, expected):
assert normalize_nonempty_secret_str(raw) == expected
class _SpySecretManager(CustomSecretManager):
"""Records every name the manager is actually asked for."""
def __init__(self, asked):
self.asked = asked
def sync_read_secret(self, secret_name, optional_params=None, timeout=None, **kwargs):
self.asked.append(secret_name)
return "a-value"
async def async_read_secret(self, secret_name, optional_params=None, timeout=None, **kwargs):
self.asked.append(secret_name)
return "a-value"
@pytest.mark.parametrize(
("access_mode", "hosted_keys", "secret_name", "expected"),
[
("read_only", None, "ANY_NAME", True),
("read_only", ["ALLOWED"], "ALLOWED", True),
("read_only", ["ALLOWED"], "NOT_ALLOWED", False),
("read_and_write", ["ALLOWED"], "ALLOWED", True),
("write_only", None, "ANY_NAME", False),
("write_only", ["ALLOWED"], "ALLOWED", False),
],
)
def test_secret_manager_would_be_consulted_matches_get_secret(
monkeypatch, access_mode, hosted_keys, secret_name, expected
):
"""The predicate must agree with what get_secret actually does, not with a reading of it.
Callers use it to tell "the manager does not have this key" apart from "the manager was
never asked", so a predicate that drifts from get_secret's gating makes them state a
lookup that never happened.
"""
asked = []
monkeypatch.setattr(litellm, "secret_manager_client", _SpySecretManager(asked))
monkeypatch.setattr(litellm, "_key_management_system", KeyManagementSystem.CUSTOM)
monkeypatch.setattr(
litellm,
"_key_management_settings",
KeyManagementSettings(access_mode=access_mode, hosted_keys=hosted_keys),
)
monkeypatch.delenv(secret_name, raising=False)
predicted = secret_manager_would_be_consulted(f"os.environ/{secret_name}")
get_secret(f"os.environ/{secret_name}")
assert {"predicted": predicted, "actually_consulted": bool(asked)} == {
"predicted": expected,
"actually_consulted": expected,
}
def test_secret_manager_would_be_consulted_is_false_without_a_client(monkeypatch):
monkeypatch.setattr(litellm, "secret_manager_client", None)
assert secret_manager_would_be_consulted("os.environ/ANY_NAME") is False
class _FixedValueSecretManager(CustomSecretManager):
def __init__(self, value):
self.value = value
def sync_read_secret(self, secret_name, optional_params=None, timeout=None):
return self.value
async def async_read_secret(self, secret_name, optional_params=None, timeout=None):
return self.value
@pytest.mark.parametrize(
("stored", "expected"),
[
("True", True),
("False", False),
("1", "1"),
("[True]", "[True]"),
("'True'", "'True'"),
("sk-not-a-literal", "sk-not-a-literal"),
("", ""),
],
)
def test_get_secret_turns_only_boolean_literals_from_the_secret_manager_into_bools(monkeypatch, stored, expected):
monkeypatch.setattr(litellm, "secret_manager_client", _FixedValueSecretManager(stored))
monkeypatch.setattr(litellm, "_key_management_system", KeyManagementSystem.CUSTOM)
monkeypatch.setattr(litellm, "_key_management_settings", KeyManagementSettings(access_mode="read_only"))
monkeypatch.delenv("STORED_FLAG", raising=False)
secret = get_secret("os.environ/STORED_FLAG")
assert secret == expected
assert type(secret) is type(expected)
def test_an_unresolvable_reference_cannot_forge_a_server_log_line(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
monkeypatch.delenv("LIT_UNSET_SA_TOKEN", raising=False)
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
resolve_local_oidc_reference("oidc/env/LIT_UNSET_SA_TOKEN\nFORGED ENTRY")
assert "LIT_UNSET_SA_TOKEN" in caplog.text
assert "\nFORGED" not in caplog.text