diff --git a/litellm/llms/anthropic/wif.py b/litellm/llms/anthropic/wif.py index b096a11ace4..f5d257f7df0 100644 --- a/litellm/llms/anthropic/wif.py +++ b/litellm/llms/anthropic/wif.py @@ -64,6 +64,7 @@ _IDENTITY_SOURCE_PARAM: Final = "anthropic_identity_source" _IDENTITY_SOURCE_ENV: Final = "ANTHROPIC_IDENTITY_SOURCE" _IDENTITY_TOKEN_FILE_PARAM: Final = "anthropic_identity_token_file" _IDENTITY_TOKEN_PARAM: Final = "anthropic_identity_token" +_LEGACY_REF_PARAMS: Final = (_IDENTITY_TOKEN_FILE_PARAM, _IDENTITY_TOKEN_PARAM) # litellm_params key -> InternalIssuerSource/KeycloakSource field name. Every key here must # also be listed in ANTHROPIC_WIF_KWARGS_KEYS (types/workload_identity.py), which is what makes it @@ -138,7 +139,7 @@ def resolve_anthropic_wif_params(litellm_params: Mapping[str, object] | None) -> ) organization_id: Final = _config_value(litellm_params, "anthropic_organization_id", "ANTHROPIC_ORGANIZATION_ID") if federation_rule_id is None or organization_id is None: - _raise_if_identity_source_configured(litellm_params, federation_rule_id, organization_id) + _raise_if_federation_requested(litellm_params, federation_rule_id, organization_id) return None identity_source: Final = _resolve_identity_source(litellm_params) if identity_source is None: @@ -203,17 +204,15 @@ def _raise_unknown_source_kind(source_kind: str) -> NoReturn: ) -def _raise_if_identity_source_configured( +def _raise_if_federation_requested( litellm_params: Mapping[str, object] | None, federation_rule_id: str | None, organization_id: str | None ) -> None: - """A configured identity source is an explicit request to federate, so a missing rule or - organization id fails closed with the ids named, rather than silently skipping federation - and surfacing later as a missing API key.""" - source_kind: Final = _resolve_source_kind(litellm_params) - if source_kind is None: + """A configured identity source, or a token file or inline token set on the deployment, is an + explicit request to federate, so a missing rule or organization id fails closed with the ids + named, rather than silently skipping federation and surfacing later as a missing API key.""" + request: Final = _explicit_federation_request(litellm_params) + if request is None: return - if source_kind not in {kind.value for kind in AnthropicIdentitySourceKind}: - _raise_unknown_source_kind(source_kind) missing: Final = tuple( param for param, value in ( @@ -224,7 +223,7 @@ def _raise_if_identity_source_configured( ) raise litellm.AuthenticationError( message=( - f"{_IDENTITY_SOURCE_PARAM} is {source_kind!r}, but {' and '.join(missing)} " + f"{request}, but {' and '.join(missing)} " f"{'is' if len(missing) == 1 else 'are'} not set. {_MISSING_IDS_HINT}" ), llm_provider="anthropic", @@ -232,14 +231,25 @@ def _raise_if_identity_source_configured( ) +def _explicit_federation_request(litellm_params: Mapping[str, object] | None) -> str | None: + source_kind: Final = _resolve_source_kind(litellm_params) + if source_kind is None: + legacy_ref_param: Final = _legacy_ref_param(litellm_params) + return None if legacy_ref_param is None else f"{legacy_ref_param} is set" + if source_kind not in {kind.value for kind in AnthropicIdentitySourceKind}: + _raise_unknown_source_kind(source_kind) + return f"{_IDENTITY_SOURCE_PARAM} is {source_kind!r}" + + def _resolve_source_kind(litellm_params: Mapping[str, object] | None) -> str | None: param_kind: Final = _param_str(litellm_params, _IDENTITY_SOURCE_PARAM) if param_kind is not None: return param_kind - has_param_legacy_ref: Final = any( - _param_str(litellm_params, key) is not None for key in (_IDENTITY_TOKEN_FILE_PARAM, _IDENTITY_TOKEN_PARAM) - ) - return None if has_param_legacy_ref else _env_str(_IDENTITY_SOURCE_ENV) + return None if _legacy_ref_param(litellm_params) is not None else _env_str(_IDENTITY_SOURCE_ENV) + + +def _legacy_ref_param(litellm_params: Mapping[str, object] | None) -> str | None: + return next((key for key in _LEGACY_REF_PARAMS if _param_str(litellm_params, key) is not None), None) def _reject_foreign_variant_fields( diff --git a/tests/unit/llms/anthropic/test_anthropic_common_utils.py b/tests/unit/llms/anthropic/test_anthropic_common_utils.py index 67eb9f36bd4..63540e6dc23 100644 --- a/tests/unit/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/unit/llms/anthropic/test_anthropic_common_utils.py @@ -15,6 +15,7 @@ import os import sys from collections.abc import Mapping import threading +from pathlib import Path from types import SimpleNamespace from typing import Final from unittest.mock import patch @@ -2598,7 +2599,6 @@ class TestWifTierPrecedence: assert [record for record in caplog.records if "takes precedence" in record.getMessage()] == [] - class TestWifZeroBehaviorChange: def test_unconfigured_raises_same_authentication_error(self, clean_anthropic_env): """No WIF config and no keys: same AuthenticationError as today (message @@ -2616,6 +2616,29 @@ class TestWifZeroBehaviorChange: assert "ANTHROPIC_SERVICE_ACCOUNT_ID" in exc_info.value.message assert "ANTHROPIC_IDENTITY_TOKEN_FILE" in exc_info.value.message + def test_a_token_file_credential_missing_an_id_names_the_id_not_the_key(self, clean_anthropic_env: None, tmp_path: Path): + import litellm + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + with pytest.raises(litellm.AuthenticationError) as exc_info: + AnthropicModelInfo().validate_environment( + headers={}, + model="claude-haiku-5-5", + messages=[{"role": "user", "content": "Hello"}], + optional_params={}, + litellm_params={ + "anthropic_federation_rule_id": "fdrl_1", + "anthropic_identity_token_file": str(tmp_path / "token"), + }, + api_key=None, + api_base=None, + ) + + assert ( + "anthropic_identity_token_file is set, but anthropic_organization_id is not set" in exc_info.value.message + ) + assert "Missing Anthropic API Key" not in exc_info.value.message + class TestWifHeaderContract: def test_minted_token_headers(self, monkeypatch, wif_engine): diff --git a/tests/unit/llms/anthropic/test_anthropic_wif.py b/tests/unit/llms/anthropic/test_anthropic_wif.py index a054b4d130c..733e1472a8a 100644 --- a/tests/unit/llms/anthropic/test_anthropic_wif.py +++ b/tests/unit/llms/anthropic/test_anthropic_wif.py @@ -277,12 +277,18 @@ class TestExchangeHostTrust: def test_a_gateway_listed_with_its_port_is_trusted(self, monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("LITELLM_ANTHROPIC_WIF_ALLOWED_HOSTS", "gateway.internal:8443") - assert self._mint("https://gateway.internal:8443", monkeypatch) == "https://gateway.internal:8443/v1/oauth/token" + assert ( + self._mint("https://gateway.internal:8443", monkeypatch) == "https://gateway.internal:8443/v1/oauth/token" + ) def test_allowlist_matching_ignores_hostname_case(self, monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("LITELLM_ANTHROPIC_WIF_ALLOWED_HOSTS", "Gateway.Internal:8443") - assert self._mint("https://gateway.internal:8443", monkeypatch) == "https://gateway.internal:8443/v1/oauth/token" - assert self._mint("https://GATEWAY.internal:8443", monkeypatch) == "https://GATEWAY.internal:8443/v1/oauth/token" + assert ( + self._mint("https://gateway.internal:8443", monkeypatch) == "https://gateway.internal:8443/v1/oauth/token" + ) + assert ( + self._mint("https://GATEWAY.internal:8443", monkeypatch) == "https://GATEWAY.internal:8443/v1/oauth/token" + ) def test_a_gateway_listed_with_a_port_is_not_trusted_on_another_port(self, monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("LITELLM_ANTHROPIC_WIF_ALLOWED_HOSTS", "gateway.internal:8443") @@ -302,7 +308,9 @@ class TestExchangeHostTrust: def test_a_gateway_listed_without_a_port_is_trusted_on_every_port(self, monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("LITELLM_ANTHROPIC_WIF_ALLOWED_HOSTS", "gateway.internal") - assert self._mint("https://gateway.internal:9443", monkeypatch) == "https://gateway.internal:9443/v1/oauth/token" + assert ( + self._mint("https://gateway.internal:9443", monkeypatch) == "https://gateway.internal:9443/v1/oauth/token" + ) def test_an_entry_spelling_the_scheme_default_port_matches_a_base_that_omits_it( self, monkeypatch: pytest.MonkeyPatch @@ -311,7 +319,6 @@ class TestExchangeHostTrust: assert self._mint("https://gateway.internal", monkeypatch) == "https://gateway.internal/v1/oauth/token" - class TestBaseUrlDerivation: def _mint(self, api_base: str | None, monkeypatch: pytest.MonkeyPatch) -> str: monkeypatch.setenv("ANTHROPIC_IDENTITY_TOKEN", "inline-jwt") @@ -548,8 +555,6 @@ class TestResolutionMatrix: {"anthropic_federation_rule_id": "fdrl_1"}, {"anthropic_organization_id": "org-1"}, {"anthropic_federation_rule_id": "fdrl_1", "anthropic_organization_id": "org-1"}, - {"anthropic_organization_id": "org-1", "anthropic_identity_token": "oidc/env/TOK"}, - {"anthropic_federation_rule_id": "fdrl_1", "anthropic_identity_token": "oidc/env/TOK"}, ], ) def test_gate_unmet_returns_none(self, litellm_params: dict): @@ -834,7 +839,11 @@ class TestDenialHints: def test_only_console_pointer_when_both_set(self, monkeypatch: pytest.MonkeyPatch): message = self._raise( - {**self.BASE_PARAMS, "anthropic_federation_workspace_id": "wrkspc_1", "anthropic_service_account_id": "svac_1"}, + { + **self.BASE_PARAMS, + "anthropic_federation_workspace_id": "wrkspc_1", + "anthropic_service_account_id": "svac_1", + }, 401, monkeypatch, ) @@ -1064,7 +1073,6 @@ class TestKeycloakIdentitySourceDispatch: assert first.assertion_ref != second.assertion_ref - @pytest.mark.parametrize( "sparse_params", [TestInternalIssuerIdentitySourceDispatch.LITELLM_PARAMS, TestKeycloakIdentitySourceDispatch.LITELLM_PARAMS], @@ -1229,9 +1237,82 @@ class TestMissingIdsFailClosedWhenIdentitySourceConfigured: with pytest.raises(litellm.AuthenticationError, match="must be one of internal_issuer, keycloak"): resolve_anthropic_wif_params({"anthropic_identity_source": "bogus"}) - def test_legacy_token_params_without_ids_still_return_none(self, monkeypatch: pytest.MonkeyPatch): + +class TestLegacyRefsFailClosedWithoutIds: + """A token file or inline token on the deployment asks to federate as explicitly as a named + identity source does, so a missing rule or organization id is reported by name instead of + letting the request die later as a missing API key.""" + + def test_token_file_without_organization_id_names_the_file_param(self, tmp_path: Path): + with pytest.raises(litellm.AuthenticationError) as exc_info: + resolve_anthropic_wif_params( + {"anthropic_federation_rule_id": "fdrl_1", "anthropic_identity_token_file": str(tmp_path / "token")} + ) + + message: Final = exc_info.value.message + assert "anthropic_identity_token_file is set, but anthropic_organization_id is not set. Copy" in message + assert "Settings > Workload identity" in message + assert "ANTHROPIC_FEDERATION_RULE_ID" in message + assert not message.endswith(".") + + def test_inline_token_without_rule_id_names_the_token_param(self): + with pytest.raises(litellm.AuthenticationError) as exc_info: + resolve_anthropic_wif_params( + {"anthropic_organization_id": "org-1", "anthropic_identity_token": "oidc/env/TOK"} + ) + + assert ( + "anthropic_identity_token is set, but anthropic_federation_rule_id is not set. Copy" + in exc_info.value.message + ) + + def test_token_file_with_both_ids_missing_names_both(self, tmp_path: Path): + with pytest.raises( + litellm.AuthenticationError, match="anthropic_federation_rule_id and anthropic_organization_id are not set" + ): + resolve_anthropic_wif_params({"anthropic_identity_token_file": str(tmp_path / "token")}) + + def test_fleet_wide_env_source_does_not_relabel_a_legacy_param(self, monkeypatch: pytest.MonkeyPatch): monkeypatch.setenv("ANTHROPIC_IDENTITY_SOURCE", "internal_issuer") - assert resolve_anthropic_wif_params({"anthropic_identity_token": "oidc/env/TOK"}) is None + with pytest.raises(litellm.AuthenticationError) as exc_info: + resolve_anthropic_wif_params({"anthropic_identity_token": "oidc/env/TOK"}) + + assert "anthropic_identity_token is set, but" in exc_info.value.message + assert "internal_issuer" not in exc_info.value.message + + def test_env_token_file_without_ids_still_returns_none(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path): + monkeypatch.setenv("ANTHROPIC_IDENTITY_TOKEN_FILE", str(tmp_path / "token")) + assert resolve_anthropic_wif_params({"anthropic_federation_rule_id": "fdrl_1"}) is None + + def test_environment_ids_complete_a_token_file_param(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path): + monkeypatch.setenv("ANTHROPIC_FEDERATION_RULE_ID", "fdrl_env") + monkeypatch.setenv("ANTHROPIC_ORGANIZATION_ID", "org-env") + token_file: Final = tmp_path / "token" + + params: Final = resolve_anthropic_wif_params({"anthropic_identity_token_file": str(token_file)}) + + assert params is not None + assert (params.federation_rule_id, params.organization_id) == ("fdrl_env", "org-env") + assert params.assertion_ref == f"oidc/file/{token_file}" + + def test_disabling_federation_wins_over_the_gate(self, tmp_path: Path): + litellm_params: Final = { + "anthropic_disable_workload_identity_federation": True, + "anthropic_identity_token_file": str(tmp_path / "token"), + } + assert resolve_anthropic_wif_params(litellm_params) is None + + def test_facade_raises_without_an_engine_call(self): + poster: Final = ScriptedPoster([token_response()]) + engine: Final = make_engine(poster) + with pytest.raises(litellm.AuthenticationError, match="anthropic_identity_token is set, but"): + get_anthropic_wif_token( + {"anthropic_organization_id": "org-1", "anthropic_identity_token": "oidc/env/TOK"}, + None, + "claude-haiku-5-5", + engine, + ) + assert poster.requests == [] class TestConfigYamlShapedIdentitySources: