The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(cli): stop a file-held secret from vouching for an unreadable keychain
mateo-berrif86aeba1e7b1
diff --git a/litellm/litellm_core_utils/cli_token_utils.py b/litellm/litellm_core_utils/cli_token_utils.pyindex 4e01dd723ce..78b52f33e2d 100644--- a/litellm/litellm_core_utils/cli_token_utils.py+++ b/litellm/litellm_core_utils/cli_token_utils.py@@ -185,22 +185,19 @@ def _nothing_left_behind(outcome: SecretErase, record: CliTokenRecord | None) ->A machine with no token file has no stored login to end, and `clear_cli_token` keeps one behindwhenever the keychain is left unconfirmed, so a missing file is real evidence rather than the- absence of it. Past that, a keychain that exists but is out of reach right now is- never trusted, whatever the file looks like: the login that stored a secret there and the- logout that cannot remove it are separate runs, free to differ in whether the keychain was- usable at the time. The exception is a missing `keyring` package, which had to be missing when- the credential was stored too, so a file still holding its own secret proves no keychain was- ever involved. `SecretStranded` is the keychain answering for itself and outranks the file.+ absence of it. Past that, a keychain that could not be reached is never trusted, whatever the+ file looks like. Even a file holding its own secret says only that the login which wrote it had+ no keychain to write to, and the login before it may well have had one: the entry that login+ left outlives both the uninstalled package and the file that replaced it. `SecretStranded` is+ the keychain answering for itself and outranks the file."""match outcome:case SecretErased():return Truecase SecretStranded():return False- case KeyringDisabled() | KeyringUnreachable():+ case KeyringDisabled() | KeyringNotInstalled() | KeyringUnreachable():return record is None- case KeyringNotInstalled():- return record is None or record.key is not Nonedef get_litellm_gateway_api_key(diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.pyindex eb956cd536c..5fcc53e69eb 100644--- a/litellm/proxy/client/cli/commands/auth.py+++ b/litellm/proxy/client/cli/commands/auth.py@@ -803,7 +803,7 @@ def logout(ctx: click.Context):click.echo(STRANDED_CREDENTIAL_MESSAGE)click.echo("Unlock your keychain and run 'lite logout' again to clear it.")case KeyringNotInstalled():- click.echo(STRANDED_CREDENTIAL_MESSAGE)+ click.echo(UNCHECKED_KEYCHAIN_MESSAGE)click.echo(f"Install the keyring package with: {KEYRING_INSTALL_HINT}, then run 'lite logout' again.")case KeyringDisabled():click.echo(UNCHECKED_KEYCHAIN_MESSAGE)diff --git a/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py b/tests/test_litellm/litellm_core_utils/test_cli_token_utils.pyindex 162e5dd4b67..143c9e738a9 100644--- a/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py+++ b/tests/test_litellm/litellm_core_utils/test_cli_token_utils.py@@ -582,15 +582,22 @@ class TestClearCliToken:assert clear_cli_token(vault=vault) == SecretErased()- def test_a_file_backed_login_logs_out_quietly_without_keyring(self, isolated_home, secret_vault_factory):- """The complement, and the one inference the file does support: nothing here can reach a- keychain without the package, so an install that lacks it and a file that still holds its- own secret between them account for the whole credential."""- _write_legacy_file(isolated_home)- vault = secret_vault_factory(available=False, failure=KeyringNotInstalled())+ def test_a_file_backed_login_cannot_vouch_for_a_keychain_no_package_can_reach(+ self, isolated_home, secret_vault_factory+ ):+ """Sign in with the keyring package installed, lose the package, then sign in again so the+ second secret lands in the file. The first login's entry outlives both, and the file that+ replaced it holds a secret of its own, which is the shape a logout must not read as proof+ that no keychain was ever involved."""+ vault = secret_vault_factory()+ save_cli_token(CliTokenRecord(base_url=SERVER, key="sk-keychain"), vault=vault)+ vault.available = False+ vault.failure = KeyringNotInstalled()+ save_cli_token(CliTokenRecord(base_url=SERVER, key="sk-in-file"), vault=vault)- assert clear_cli_token(vault=vault) == SecretErased()- assert not _token_file(isolated_home).exists()+ assert clear_cli_token(vault=vault) == KeyringNotInstalled()+ assert vault.blob is not None+ assert "sk-in-file" not in _token_file(isolated_home).read_text()def test_is_safe_when_nothing_was_ever_stored(self, isolated_home, secret_vault_factory):assert clear_cli_token(vault=secret_vault_factory()) == SecretErased()diff --git a/tests/test_litellm/proxy/client/cli/test_auth_commands.py b/tests/test_litellm/proxy/client/cli/test_auth_commands.pyindex 8e1551c0720..9e4ede88337 100644--- a/tests/test_litellm/proxy/client/cli/test_auth_commands.py+++ b/tests/test_litellm/proxy/client/cli/test_auth_commands.py@@ -459,7 +459,7 @@ class TestLogoutCommand:assert result.exit_code == 0assert "Logged out successfully" not in result.output- assert "still in the OS keychain" in result.output+ assert "could not be checked" in result.outputassert "pip install 'litellm[cli]'" in result.outputdef test_logout_does_not_call_an_unusable_keychain_clean(self, isolated_home, secret_vault_factory):@@ -492,9 +492,12 @@ class TestLogoutCommand:assert "still in the OS keychain" in result.outputassert "Unlock your keychain" in result.output- def test_logout_from_a_file_only_login_stays_quiet(self, isolated_home, secret_vault_factory):- """The credential never went to a keychain, so removing the file is the whole logout and- warning about a keychain entry would send the user chasing one that cannot exist."""+ def test_logout_without_the_keyring_package_still_warns_about_a_file_held_secret(+ self, isolated_home, secret_vault_factory+ ):+ """A file holding its own secret only says the login that wrote it had no keychain to write+ to. An earlier login on this machine may have had one, and no install without the package+ can look, so the honest answer is that the keychain went unchecked."""_write_token_file(isolated_home, key="sk-in-file")result = self.runner.invoke(@@ -502,8 +505,9 @@ class TestLogoutCommand:)assert result.exit_code == 0- assert "Logged out successfully" in result.output- assert "still in the OS keychain" not in result.output+ assert "Logged out successfully" not in result.output+ assert "could not be checked" in result.output+ assert "pip install 'litellm[cli]'" in result.outputclass TestWhoamiCommand: