Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion litellm/proxy/client/cli/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ def prompt_team_selection_fallback(
return None


def _polling_error_message(response: requests.Response) -> str:
try:
body = response.json()
except ValueError:
body = None
detail = body.get("detail") if isinstance(body, dict) else None
if isinstance(detail, str) and detail:
return f"Polling error: HTTP {response.status_code}: {detail}"
return f"Polling error: HTTP {response.status_code}"


def _is_permanent_polling_error(status_code: int) -> bool:
return 400 <= status_code < 500 and status_code != 429


# Polling-based authentication - no local server needed
def _poll_for_ready_data(
url: str,
Expand Down Expand Up @@ -308,8 +323,11 @@ def _poll_for_ready_data(
click.echo(pending_message)
elif other_status_message and other_status_log_every > 0 and attempt % other_status_log_every == 0:
click.echo(other_status_message)
elif _is_permanent_polling_error(response.status_code):
click.echo(_polling_error_message(response))
return None
elif http_error_log_every > 0 and attempt % http_error_log_every == 0:
click.echo(f"Polling error: HTTP {response.status_code}")
click.echo(_polling_error_message(response))
except requests.RequestException as e:
if connection_error_log_every > 0 and attempt % connection_error_log_every == 0:
click.echo(f"Connection error (will retry): {e}")
Expand Down
8 changes: 8 additions & 0 deletions litellm/proxy/management_endpoints/ui_sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ def _check_cli_sso_start_rate_limit(


def _get_cli_sso_flow_or_raise(login_id: Optional[str], cache: DualCache) -> dict:
if isinstance(login_id, str) and login_id.startswith("sk-"):
raise HTTPException(
status_code=400,
detail=(
"Your litellm CLI is out of date and uses a login flow this proxy no longer supports. "
"Upgrade it with `pip install -U 'litellm[proxy]'` and run `litellm-proxy login` again."
),
)
if not _is_valid_cli_sso_login_id(login_id):
raise HTTPException(status_code=400, detail="Invalid CLI login session")

Expand Down
49 changes: 49 additions & 0 deletions tests/test_litellm/proxy/client/cli/test_auth_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,55 @@ def _mock_cli_sso_start_response(
return mock_response


class TestPollingErrorSurfacing:
def test_client_error_prints_server_detail_and_stops_polling(self, capsys):
from litellm.proxy.client.cli.commands.auth import _poll_for_ready_data

mock_response = Mock()
mock_response.status_code = 400
mock_response.json.return_value = {
"detail": "Your litellm CLI is out of date and uses a login flow this proxy no longer supports."
}

with patch("requests.get", return_value=mock_response) as mock_get, patch("time.sleep"):
result = _poll_for_ready_data("http://test/sso/cli/poll/sk-legacy")

assert result is None
assert mock_get.call_count == 1
assert (
"Polling error: HTTP 400: Your litellm CLI is out of date and uses a login flow "
"this proxy no longer supports." in capsys.readouterr().out
)

def test_server_error_without_json_body_retries_until_timeout(self, capsys):
from litellm.proxy.client.cli.commands.auth import _poll_for_ready_data

mock_response = Mock()
mock_response.status_code = 500
mock_response.json.side_effect = ValueError("no json")

with patch("requests.get", return_value=mock_response) as mock_get, patch("time.sleep"):
result = _poll_for_ready_data("http://test/sso/cli/poll/cli-abc", total_timeout=6, poll_interval=2)

assert result is None
assert mock_get.call_count == 3
assert "Polling error: HTTP 500" in capsys.readouterr().out

def test_rate_limit_is_retried_not_aborted(self, capsys):
from litellm.proxy.client.cli.commands.auth import _poll_for_ready_data

mock_response = Mock()
mock_response.status_code = 429
mock_response.json.return_value = {"detail": "Too many CLI login attempts. Try again later."}

with patch("requests.get", return_value=mock_response) as mock_get, patch("time.sleep"):
result = _poll_for_ready_data("http://test/sso/cli/poll/cli-abc", total_timeout=4, poll_interval=2)

assert result is None
assert mock_get.call_count == 2
assert "Polling error: HTTP 429: Too many CLI login attempts. Try again later." in capsys.readouterr().out


class TestTokenUtilities:
"""Test token file utility functions"""

Expand Down
29 changes: 29 additions & 0 deletions tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,35 @@ def test_cli_sso_login_id_validation_restricts_charset(self):
assert not _is_valid_cli_sso_login_id("cli-test\x001234567890")
assert not _is_valid_cli_sso_login_id("sk-test1234567890")

def test_cli_sso_flow_lookup_tells_legacy_clients_to_upgrade(self):
"""Legacy CLIs send self-generated sk-<uuid> login ids; the 400 must say the CLI is outdated"""
from litellm.proxy.management_endpoints.ui_sso import (
_get_cli_sso_flow_or_raise,
)

mock_cache = MagicMock()
mock_cache.get_cache.return_value = None

with pytest.raises(HTTPException) as legacy_exc:
_get_cli_sso_flow_or_raise(
login_id="sk-85c789af-fc21-474c-9dc9-b5d794fe07ec",
cache=mock_cache,
)
assert legacy_exc.value.status_code == 400
assert "out of date" in legacy_exc.value.detail
assert "pip install" in legacy_exc.value.detail
mock_cache.get_cache.assert_not_called()

with pytest.raises(HTTPException) as generic_exc:
_get_cli_sso_flow_or_raise(login_id="not-a-valid-id", cache=mock_cache)
assert generic_exc.value.status_code == 400
assert generic_exc.value.detail == "Invalid CLI login session"

with pytest.raises(HTTPException) as expired_exc:
_get_cli_sso_flow_or_raise(login_id="cli-test_1234567890", cache=mock_cache)
assert expired_exc.value.status_code == 400
assert expired_exc.value.detail == "Invalid CLI login session"

@pytest.mark.asyncio
async def test_cli_sso_start_creates_bound_flow(self):
"""Test CLI SSO start creates a polling secret bound flow"""
Expand Down
Loading