fix(cli): expire a CLI token with no recorded proxy and stop treating 404 as rejected

A token file without base_url cannot be checked, so it keeps the client
window's verdict instead of printing forever. A 404 also comes from a proxy
that does not serve /v1/models, so only 401 and 403 count as a rejected key,
matching the CLI's existing key check. The timeout test now runs in process.
This commit is contained in:
mateo-berri 2026-10-20 21:04:12 -07:00
parent 44eb40d855
commit 4310791550
2 changed files with 25 additions and 10 deletions

View file

@ -143,7 +143,7 @@ _TeamMapping: Final = TypeVar("_TeamMapping", bound=Mapping[str, object])
_JSON_OBJECT: Final = TypeAdapter(Mapping[str, object], config=ConfigDict(hide_input_in_errors=True))
_PROXY_KEY_CHECK_TIMEOUT_SECONDS: Final = 3
_PROXY_REJECTED_KEY_STATUSES: Final = frozenset({401, 403, 404})
_PROXY_REJECTED_KEY_STATUSES: Final = frozenset({401, 403})
KEYRING_INSTALL_HINT: Final = "pip install 'litellm[cli]'"
@ -1130,8 +1130,8 @@ def token_expired(token_data: Mapping[str, object]) -> bool:
def _proxy_rejects_key(token_data: Mapping[str, object]) -> bool:
key: Final = token_data.get("key")
base_url: Final = token_data.get("base_url")
if not isinstance(key, str) or not isinstance(base_url, str):
return False
if not isinstance(key, str) or not isinstance(base_url, str) or not base_url:
return True
try:
response: Final = requests.get(
f"{base_url}/v1/models",

View file

@ -1,6 +1,5 @@
import json
import os
import socket
import stat
import time
from pathlib import Path
@ -1077,7 +1076,7 @@ class TestPrintTokenCommand:
(200, 0, "sk-stale-key\n", ""),
(401, 1, "", "Token expired. Run 'lite login' again.\n"),
(403, 1, "", "Token expired. Run 'lite login' again.\n"),
(404, 1, "", "Token expired. Run 'lite login' again.\n"),
(404, 0, "sk-stale-key\n", ""),
(500, 0, "sk-stale-key\n", ""),
(requests.ConnectionError("proxy unreachable"), 0, "sk-stale-key\n", ""),
],
@ -1107,22 +1106,38 @@ class TestPrintTokenCommand:
assert result.stderr == expected_stderr
assert len(responses.calls) == 1
@responses.activate
def test_proxy_check_waits_only_a_few_seconds_for_a_silent_proxy(self):
with socket.create_server(("127.0.0.1", 0)) as silent_proxy, patch(
_register_models_answer("sk-stale-key", requests.ReadTimeout("proxy sent nothing"))
with patch(
"litellm.proxy.client.cli.commands.auth.load_cli_token",
return_value=CliTokenRecord(
base_url=f"http://127.0.0.1:{silent_proxy.getsockname()[1]}",
base_url=STALE_TOKEN_BASE_URL,
key="sk-stale-key",
timestamp=STALE_TOKEN_TIMESTAMP,
),
):
started: Final = time.monotonic()
result: Final = self.runner.invoke(print_token, obj={})
elapsed: Final = time.monotonic() - started
timeout: Final = responses.calls[0].request.req_kwargs["timeout"]
assert result.exit_code == 0
assert result.stdout == "sk-stale-key\n"
assert elapsed < 5
assert isinstance(timeout, int | float)
assert 0 < timeout <= 5
@responses.activate
def test_key_without_a_recorded_proxy_still_expires_after_the_client_window(self):
with patch(
"litellm.proxy.client.cli.commands.auth.load_cli_token",
return_value=CliTokenRecord(key="sk-old-format", timestamp=STALE_TOKEN_TIMESTAMP),
):
result: Final = self.runner.invoke(print_token, obj={})
assert result.exit_code == 1
assert result.stdout == ""
assert result.stderr == "Token expired. Run 'lite login' again.\n"
assert len(responses.calls) == 0
class TestPrintTokenWithConfigFile: