Skip to content

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]

fix(cli): pick the credential by the sign-in it came from

mateo-berri71a583390a4e
diff --git a/litellm/litellm_core_utils/cli_token_utils.py b/litellm/litellm_core_utils/cli_token_utils.py
index 69a77a36882..134e7e2331c 100644
--- a/litellm/litellm_core_utils/cli_token_utils.py
+++ b/litellm/litellm_core_utils/cli_token_utils.py
@@ -105,7 +105,8 @@ class CliTokenSecret(BaseModel):
`base_url` is duplicated from the metadata file purely as a pairing tag: a
secret minted for one server is never handed to another, even if the
- metadata file is edited underneath us.
+ metadata file is edited underneath us. `timestamp` is the sign-in this
+ secret came from, which is what decides it against a secret still on disk.
"""
model_config = ConfigDict(frozen=True)
@@ -113,6 +114,7 @@ class CliTokenSecret(BaseModel):
base_url: str
key: str
jwt_token: str = ""
+ timestamp: float = 0.0
def get_cli_token_file_path() -> str:
@@ -145,11 +147,7 @@ def save_cli_token(record: CliTokenRecord, *, vault: SecretVault = SYSTEM_KEYRIN
staged: Final = _stage_token_file(_without_secret(record))
if isinstance(staged, CredentialNotSaved):
return staged
- outcome: Final = (
- SecretStored()
- if record.key is None
- else vault.write(_encode_secret(record.base_url, record.key, record.jwt_token))
- )
+ outcome: Final = SecretStored() if record.key is None else vault.write(_encode_secret(record, record.key))
if isinstance(outcome, SecretStored):
return outcome if _commit_token_file(staged) else CredentialNotRecorded()
discard_staged_json(staged)
@@ -330,19 +328,24 @@ def _resolve_secret(record: CliTokenRecord, vault: SecretVault) -> CliTokenRecor
def _apply_vault_secret(record: CliTokenRecord, blob: str, vault: SecretVault) -> CliTokenRecord | None:
"""Resolve the credential when both stores hold one.
- A secret still on disk is the fresher of the two, because it is only left there when the
- keychain write that should have removed it failed, so it outranks the vault entry.
+ The sign-in each secret came from decides it, because either store can be the stale one. A
+ secret is usually left on disk by a keychain that would not take it, which makes the file the
+ fresher of the two. It is the older one when a login the keychain did take could not replace
+ the file afterwards, and serving that one would put a superseded credential back in use.
"""
- if record.key is not None:
- return _migrate_file_secret(record, vault)
- try:
- secret: Final = CliTokenSecret.model_validate_json(blob)
- except ValidationError:
- return _migrate_file_secret(record, vault)
- if secret.base_url != record.base_url:
+ secret: Final = _decode_secret(blob, record.base_url)
+ if secret is None or (record.key is not None and secret.timestamp <= record.timestamp):
return _migrate_file_secret(record, vault)
_scrub_file_secret(record)
- return record.model_copy(update=MappingProxyType({"key": secret.key, "jwt_token": secret.jwt_token}))
+ return record.model_copy(
+ update=MappingProxyType(
+ {
+ "key": secret.key,
+ "jwt_token": secret.jwt_token,
+ "timestamp": max(secret.timestamp, record.timestamp),
+ }
+ )
+ )
def _migrate_file_secret(record: CliTokenRecord, vault: SecretVault) -> CliTokenRecord | None:
@@ -363,7 +366,7 @@ def _migrate_file_secret(record: CliTokenRecord, vault: SecretVault) -> CliToken
staged: Final = _stage_scrubbed_file(record)
if staged is None:
return record
- if not isinstance(vault.write(_encode_secret(record.base_url, record.key, record.jwt_token)), SecretStored):
+ if not isinstance(vault.write(_encode_secret(record, record.key)), SecretStored):
discard_staged_json(staged)
return record
if not _commit_token_file(staged) and not _overwrite_file_secret(record):
@@ -422,8 +425,19 @@ def _without_secret(record: CliTokenRecord) -> CliTokenRecord:
return record.model_copy(update=MappingProxyType({"key": None, "jwt_token": ""}))
-def _encode_secret(base_url: str, key: str, jwt_token: str) -> str:
- return CliTokenSecret(base_url=base_url, key=key, jwt_token=jwt_token).model_dump_json()
+def _encode_secret(record: CliTokenRecord, key: str) -> str:
+ return CliTokenSecret(
+ base_url=record.base_url, key=key, jwt_token=record.jwt_token, timestamp=record.timestamp
+ ).model_dump_json()
+
+
+def _decode_secret(blob: str, base_url: str) -> CliTokenSecret | None:
+ """The keychain entry, when it is one this metadata file may be paired with"""
+ try:
+ secret: Final = CliTokenSecret.model_validate_json(blob)
+ except ValidationError:
+ return None
+ return secret if secret.base_url == base_url else None
def _write_token_file(record: CliTokenRecord) -> None:
diff --git a/litellm/proxy/client/cli/commands/auth.py b/litellm/proxy/client/cli/commands/auth.py
index ac330ad6892..e5d90b9b640 100644
--- a/litellm/proxy/client/cli/commands/auth.py
+++ b/litellm/proxy/client/cli/commands/auth.py
@@ -131,8 +131,9 @@ def storage_notice(outcome: SecretSave) -> str:
case CredentialNotRecorded():
return (
f"Signed in, and the credential is in your OS keychain, but {path} could not be "
- "replaced, so this machine may still be using your previous login. Run 'lite login' "
- "again once that path is writable, or 'lite logout' to clear both."
+ "replaced, so it still describes your previous login and may still hold its "
+ "credential. Run 'lite login' again once that path is writable, or 'lite logout' "
+ "to clear both."
)
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.py
index c6fee6d0c25..4fc835d2db1 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
@@ -80,8 +80,8 @@ def _write_metadata_only_file(home):
return path
-def _blob(base_url=SERVER, key="sk-vault", jwt_token=""):
- return json.dumps({"base_url": base_url, "key": key, "jwt_token": jwt_token})
+def _blob(base_url=SERVER, key="sk-vault", jwt_token="", timestamp=0.0):
+ return json.dumps({"base_url": base_url, "key": key, "jwt_token": jwt_token, "timestamp": timestamp})
_REAL_MKSTEMP = tempfile.mkstemp
@@ -210,6 +210,37 @@ class TestLoadCliToken:
assert json.loads(vault.blob)["key"] == "sk-fresh"
assert "key" not in json.loads(path.read_text())
+ def test_a_login_the_file_could_not_record_is_the_one_that_gets_used(
+ self, isolated_home, secret_vault_factory
+ ):
+ """A login the keychain took and the file could not be pointed at afterwards leaves the
+ superseded secret sitting on disk in front of the fresh one. Serving the file's copy would
+ put a credential the user just replaced, and may well have just revoked, back into every
+ request, and would overwrite the keychain with it on the way past."""
+ path = _write_legacy_file(isolated_home, key="sk-superseded", timestamp=1000.0)
+ vault = secret_vault_factory(blob=_blob(key="sk-fresh", timestamp=2000.0))
+
+ record = load_cli_token(vault=vault)
+
+ assert record.key == "sk-fresh"
+ assert record.timestamp == 2000.0
+ assert json.loads(vault.blob)["key"] == "sk-fresh"
+ assert "key" not in json.loads(path.read_text())
+
+ def test_a_secret_written_to_disk_after_the_keychain_entry_still_wins(
+ self, isolated_home, secret_vault_factory
+ ):
+ """The other direction of the same rule, which is the common one: a login that fell back to
+ the file because the keychain refused it is newer than whatever the keychain kept."""
+ path = _write_legacy_file(isolated_home, key="sk-fresh", timestamp=2000.0)
+ vault = secret_vault_factory(blob=_blob(key="sk-stale", timestamp=1000.0))
+
+ record = load_cli_token(vault=vault)
+
+ assert record.key == "sk-fresh"
+ assert json.loads(vault.blob)["key"] == "sk-fresh"
+ assert "key" not in json.loads(path.read_text())
+
def test_a_disk_secret_survives_when_the_stale_vault_refuses_the_rewrite(
self, isolated_home, secret_vault_factory
):