mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
Merge 669614c032 into 34314a99f0
This commit is contained in:
commit
622375f673
16 changed files with 1761 additions and 27 deletions
|
|
@ -49,6 +49,22 @@ lite config unset hidden_commands # list everything again
|
|||
|
||||
Hidden commands drop out of both `lite --help` and the interactive shell's "Available commands" block, and stay runnable so existing scripts keep working
|
||||
|
||||
## Automatic Browser Sign-In for Agents
|
||||
|
||||
When a command that calls the proxy (`lite keys`, `lite models`, `lite model-groups`, `lite users`, `lite teams`, `lite credentials`, `lite debug claude`) gets a 401 and you did not pass a key with `--api-key` or `LITELLM_PROXY_API_KEY`, `lite` signs you in through your browser and then runs the same command again with the new key. That covers a proxy you never logged in to and a stored login the proxy no longer accepts. It needs no terminal, so an AI agent that runs `lite` through its shell tool finishes the command on its own once you approve the sign-in in the browser:
|
||||
|
||||
```bash
|
||||
lite --base-url https://your-proxy.example.com keys generate --models gpt-5.4-mini --key-alias experiment-1
|
||||
```
|
||||
|
||||
The sign-in is the browser flow of `lite login --pkce`, so you pick your team on the consent page, the credential is stored where `lite login` stores it, and its refresh token renews the key on its own from then on. Every line about the sign-in goes to stderr, so stdout carries only the command's own output, such as the JSON from `lite keys generate`. If another `lite` process is already signing in, the command waits for it to finish and then uses the login it stored, so parallel commands open one browser tab. When that sign-in ends without a login the proxy accepts, or is still waiting after 6 minutes, the command fails instead of opening a second tab. If another `lite` process stored a newer login for the same proxy in the meantime, that key is tried first and no browser opens. `lite` keeps one stored login, so when that login is for a different proxy, the command fails and tells you to run `lite --base-url <proxy> login --pkce` to switch, instead of replacing and revoking the other proxy's login on its own
|
||||
|
||||
Nothing signs in ahead of time. A proxy that accepts the request without a key never opens a browser, and neither does `--help`. The proxy also answers 401 when a valid key lacks the role a command needs, such as a non-admin key running `lite credentials list`, so before it signs in, `lite` checks a rejected stored login against `/key/info`. When the proxy accepts the login there, the command fails with an error that says the login likely lacks the role, and no browser opens. A key you passed yourself is never swapped out or checked again, and a 401 for it fails with an error that names `--api-key` and `LITELLM_PROXY_API_KEY`. When the browser sign-in cannot finish (the machine has no web browser or only a text-mode one such as `w3m` or `lynx`, the shell runs over SSH with no display and `BROWSER` is unset, the browser fails to open, the proxy does not serve `/.well-known/litellm-cli-auth`, you deny the request, or nobody approves it within 5 minutes), the command fails and tells you to run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, or set `LITELLM_PROXY_API_KEY`. `lite` prints that 5-minute wait on stderr when the sign-in starts, so an agent whose shell tool stops commands sooner knows to run the command again with a longer timeout
|
||||
|
||||
When the `CI` environment variable is set to anything but empty, `0`, or `false`, `lite` never opens a browser and the command fails with the same message. A rejected stored login still gets the `/key/info` check first, which can take up to 10 seconds, so a login that lacks the role gets the role error in CI too. Set `CI=1` for a script or cron job that should fail instead of waiting for a sign-in
|
||||
|
||||
`lite chat`, `lite encryption migrate`, `lite http request`, `lite keys import`, and `lite teams assign-key` do not sign in on their own. `lite chat` and `lite teams assign-key` are interactive, `lite encryption migrate` is a one-time admin migration you run with an admin key, `lite keys import` also talks to a second proxy, and `lite http request` cannot tell a 401 from the proxy apart from a provider's 401 that the proxy passes through
|
||||
|
||||
## Global Options
|
||||
|
||||
- `--version`, `-v`: Print the LiteLLM Proxy client and server version and exit.
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ from .claude_settings import (
|
|||
)
|
||||
from .config import pkce_default
|
||||
from .pkce_login import (
|
||||
LOGIN_TIMEOUT_SECONDS,
|
||||
Http,
|
||||
PkceFailure,
|
||||
RevocationUnavailable,
|
||||
|
|
@ -852,14 +853,14 @@ def _finish_login(base_url: str, api_key: str, config_claude: bool, stored: Secr
|
|||
show_commands()
|
||||
|
||||
|
||||
def _replace_stored_token(record: CliTokenData, http: Http, vault: SecretVault) -> SecretSave:
|
||||
def replace_stored_token(record: CliTokenData, http: Http, vault: SecretVault) -> SecretSave:
|
||||
previous: Final = load_token(vault=vault)
|
||||
stored: Final = save_token(record, vault=vault)
|
||||
if previous is None or isinstance(stored, CredentialNotSaved):
|
||||
return stored
|
||||
revocation: Final = revoke_stored_credential(previous, http)
|
||||
if revocation is not None:
|
||||
click.echo(
|
||||
_warn(
|
||||
f"Could not revoke the previous login's refresh token on the proxy ({revocation.reason}); "
|
||||
"it expires on its own."
|
||||
)
|
||||
|
|
@ -872,7 +873,7 @@ def _pkce_login(base_url: str, config_claude: bool, vault: SecretVault) -> None:
|
|||
if isinstance(credential, PkceFailure):
|
||||
click.echo(f"Authentication failed: {credential.reason}")
|
||||
return
|
||||
stored: Final = _replace_stored_token(pkce_token_record(base_url, credential), http, vault)
|
||||
stored: Final = replace_stored_token(pkce_token_record(base_url, credential), http, vault)
|
||||
_finish_login(base_url, credential.access_token, config_claude, stored)
|
||||
|
||||
|
||||
|
|
@ -950,7 +951,7 @@ def login(ctx: click.Context, config_claude: bool, pkce: bool | None) -> None:
|
|||
|
||||
# Save token data. base_url is stored so we can verify origin
|
||||
# before reusing the key on a subsequent CLI invocation.
|
||||
stored: Final = _replace_stored_token(
|
||||
stored: Final = replace_stored_token(
|
||||
{
|
||||
"base_url": base_url.rstrip("/"),
|
||||
"key": api_key,
|
||||
|
|
@ -1093,7 +1094,11 @@ def whoami(ctx: click.Context):
|
|||
token_data: Final = load_token(vault=vault)
|
||||
|
||||
if not token_data:
|
||||
click.echo("Not authenticated. Run 'lite login' to authenticate.")
|
||||
click.echo(
|
||||
"Not authenticated. Run the command you need, such as 'lite keys generate', and lite opens your browser "
|
||||
f"to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes for you to approve it. "
|
||||
"To sign in first from a terminal, run 'lite login --pkce', or 'lite login' where no browser can open."
|
||||
)
|
||||
return
|
||||
|
||||
key_readable: Final = token_data.get("key") is not None
|
||||
|
|
|
|||
398
litellm/proxy/client/cli/commands/auto_sign_in.py
Normal file
398
litellm/proxy/client/cli/commands/auto_sign_in.py
Normal file
|
|
@ -0,0 +1,398 @@
|
|||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import webbrowser
|
||||
from collections.abc import Callable, Generator, Mapping, Sequence
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from http import HTTPStatus
|
||||
from pathlib import Path
|
||||
from typing import Final, Literal, Protocol
|
||||
|
||||
import click
|
||||
import requests
|
||||
from filelock import FileLock, Timeout
|
||||
from typing_extensions import NotRequired, ReadOnly, TypedDict
|
||||
|
||||
from litellm.litellm_core_utils.cli_token_utils import get_cli_token_file_path, load_cli_token
|
||||
from litellm.litellm_core_utils.secret_redaction import redact_string
|
||||
|
||||
from ...exceptions import UnauthorizedError
|
||||
from .auth import context_secret_vault, replace_stored_token, storage_notice
|
||||
from .pkce_login import LOGIN_TIMEOUT_SECONDS, PkceCredential, PkceFailure, pkce_token_record, run_pkce_login
|
||||
|
||||
|
||||
class BrowserSignIn(Protocol):
|
||||
def unavailable_because(self) -> PkceFailure | None: ...
|
||||
|
||||
def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure: ...
|
||||
|
||||
|
||||
class SignInLock(Protocol):
|
||||
def acquire(self, timeout: float) -> object: ...
|
||||
|
||||
def release(self) -> object: ...
|
||||
|
||||
|
||||
_TEXT_MODE_BROWSERS: Final = frozenset({"www-browser", "links", "elinks", "lynx", "w3m"})
|
||||
_SSH_SESSION_VARIABLES: Final = ("SSH_CONNECTION", "SSH_TTY")
|
||||
_DISPLAY_VARIABLES: Final = ("DISPLAY", "WAYLAND_DISPLAY")
|
||||
_SIGNED_IN_LOGIN_WITHOUT_KEY_ROW: Final = HTTPStatus.NOT_FOUND
|
||||
_WAIT_FOR_ANOTHER_SIGN_IN_SECONDS: Final = LOGIN_TIMEOUT_SECONDS + 60
|
||||
_PROXY_REASON_CHARS: Final = 500
|
||||
_NO_PROXY_REASON: Final = "no printable reason"
|
||||
_UNPRINTABLE_CHARACTERS: Final = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f\ufffd]")
|
||||
|
||||
|
||||
def _runs_in_ci(environ: Mapping[str, str]) -> bool:
|
||||
return environ.get("CI", "").strip().lower() not in {"", "0", "false"}
|
||||
|
||||
|
||||
def _ssh_session_without_a_display(environ: Mapping[str, str]) -> bool:
|
||||
over_ssh: Final = any(environ.get(name) for name in _SSH_SESSION_VARIABLES)
|
||||
return over_ssh and not any(environ.get(name) for name in _DISPLAY_VARIABLES)
|
||||
|
||||
|
||||
_OPEN_URL_IN_BROWSER: Final = (
|
||||
"import sys, webbrowser; "
|
||||
"sys.exit(0 if any(webbrowser.get(name).open(sys.argv[1]) for name in sys.argv[2:] or [None]) else 1)"
|
||||
)
|
||||
|
||||
|
||||
def open_browser_detached(url: str, browsers: Sequence[str]) -> PkceFailure | None:
|
||||
launcher: Final = subprocess.run(
|
||||
(sys.executable, "-I", "-c", _OPEN_URL_IN_BROWSER, url, *browsers),
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=False,
|
||||
)
|
||||
return None if launcher.returncode == 0 else PkceFailure("the web browser did not open")
|
||||
|
||||
|
||||
def _live_environ() -> Mapping[str, str]:
|
||||
return os.environ
|
||||
|
||||
|
||||
def _listed_browsers(environ: Mapping[str, str]) -> tuple[str, ...]:
|
||||
return tuple(name for name in environ.get("BROWSER", "").split(os.pathsep) if name)
|
||||
|
||||
|
||||
def _locate_browser(
|
||||
find_browser: Callable[[str | None], webbrowser.BaseBrowser], name: str | None
|
||||
) -> webbrowser.BaseBrowser | None:
|
||||
try:
|
||||
return find_browser(name)
|
||||
except webbrowser.Error:
|
||||
return None
|
||||
|
||||
|
||||
def _shows_a_window(browser: webbrowser.BaseBrowser) -> bool:
|
||||
return Path(getattr(browser, "name", "")).name not in _TEXT_MODE_BROWSERS
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PkceBrowserSignIn:
|
||||
find_browser: Callable[[str | None], webbrowser.BaseBrowser] = webbrowser.get
|
||||
environ: Mapping[str, str] = field(default_factory=_live_environ)
|
||||
open_browser: Callable[[str, Sequence[str]], object] = open_browser_detached
|
||||
|
||||
def _browsers_to_open(self) -> tuple[str, ...] | PkceFailure:
|
||||
names: Final = _listed_browsers(self.environ) or (None,)
|
||||
located: Final = tuple(
|
||||
(name, browser) for name in names if (browser := _locate_browser(self.find_browser, name)) is not None
|
||||
)
|
||||
if not located:
|
||||
return PkceFailure("no web browser is available on this machine")
|
||||
windowed: Final = tuple(name for name, browser in located if _shows_a_window(browser))
|
||||
if not windowed:
|
||||
return PkceFailure("the web browser is a text-mode one, which lite cannot show while a command waits")
|
||||
return tuple(name for name in windowed if name is not None)
|
||||
|
||||
def unavailable_because(self) -> PkceFailure | None:
|
||||
if _runs_in_ci(self.environ):
|
||||
return PkceFailure("the CI environment variable is set, so lite does not open a browser")
|
||||
if "BROWSER" not in self.environ and _ssh_session_without_a_display(self.environ):
|
||||
return PkceFailure(
|
||||
"this is an SSH session without a display, so a browser would open where you cannot see it"
|
||||
)
|
||||
browsers: Final = self._browsers_to_open()
|
||||
return browsers if isinstance(browsers, PkceFailure) else None
|
||||
|
||||
def __call__(self, base_url: str, echo: Callable[[str], None]) -> PkceCredential | PkceFailure:
|
||||
browsers: Final = self._browsers_to_open()
|
||||
if isinstance(browsers, PkceFailure):
|
||||
return browsers
|
||||
echo(
|
||||
f"lite is opening your browser to sign in and waits up to {LOGIN_TIMEOUT_SECONDS // 60} minutes "
|
||||
"for you to approve it. "
|
||||
"A shell or agent that stops commands sooner should run this one with a longer timeout."
|
||||
)
|
||||
return run_pkce_login(
|
||||
base_url, requests.Session(), open_browser=lambda url: self.open_browser(url, browsers), echo=echo
|
||||
)
|
||||
|
||||
|
||||
class _SignInContext(TypedDict):
|
||||
base_url: ReadOnly[str]
|
||||
api_key: ReadOnly[NotRequired[str | None]]
|
||||
api_key_from_token_file: ReadOnly[NotRequired[bool]]
|
||||
browser_sign_in: ReadOnly[NotRequired[BrowserSignIn]]
|
||||
sign_in_lock: ReadOnly[NotRequired[Callable[[], SignInLock]]]
|
||||
|
||||
|
||||
class _SignInContextView(Protocol):
|
||||
@property
|
||||
def obj(self) -> _SignInContext | None: ...
|
||||
|
||||
|
||||
class _ApiKeySlot(Protocol):
|
||||
def __setitem__(self, key: Literal["api_key"], value: str, /) -> None: ...
|
||||
|
||||
|
||||
class _ApiKeySlotView(Protocol):
|
||||
@property
|
||||
def obj(self) -> _ApiKeySlot: ...
|
||||
|
||||
|
||||
def _sign_in_context(view: _SignInContextView) -> _SignInContext | None:
|
||||
return view.obj
|
||||
|
||||
|
||||
def _api_key_slot(view: _ApiKeySlotView) -> _ApiKeySlot:
|
||||
return view.obj
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _Completed:
|
||||
value: object
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _Unauthorized:
|
||||
error: UnauthorizedError
|
||||
|
||||
|
||||
def _attempt(run: Callable[[click.Context], object], ctx: click.Context) -> _Completed | _Unauthorized:
|
||||
try:
|
||||
return _Completed(run(ctx))
|
||||
except UnauthorizedError as error:
|
||||
return _Unauthorized(error)
|
||||
|
||||
|
||||
def _attempt_with_key(
|
||||
run: Callable[[click.Context], object], ctx: click.Context, api_key: str
|
||||
) -> _Completed | _Unauthorized:
|
||||
slot: Final = _api_key_slot(ctx)
|
||||
slot["api_key"] = api_key
|
||||
return _attempt(run, ctx)
|
||||
|
||||
|
||||
def _echo_to_stderr(message: str) -> None:
|
||||
click.echo(message, err=True)
|
||||
|
||||
|
||||
def _newer_stored_key(ctx: click.Context, base_url: str, rejected_key: str | None) -> str | None:
|
||||
login: Final = load_cli_token(vault=context_secret_vault(ctx))
|
||||
if login is None or login.base_url != base_url.rstrip("/") or not login.key or login.key == rejected_key:
|
||||
return None
|
||||
expired: Final = login.expires_at is not None and time.time() >= login.expires_at
|
||||
return None if expired else login.key
|
||||
|
||||
|
||||
def _attempt_with_newer_stored_key(
|
||||
run: Callable[[click.Context], object], ctx: click.Context, base_url: str, rejected_key: str | None
|
||||
) -> _Completed | _Unauthorized | None:
|
||||
stored_key: Final = _newer_stored_key(ctx, base_url, rejected_key)
|
||||
if stored_key is None:
|
||||
return None
|
||||
return _attempt_with_key(run, ctx, stored_key)
|
||||
|
||||
|
||||
def _displayable(text: str) -> str:
|
||||
normalized: Final = text.replace("\r\n", "\n").strip()
|
||||
return "" if _UNPRINTABLE_CHARACTERS.search(normalized) else redact_string(normalized)
|
||||
|
||||
|
||||
def _proxy_reason(error: UnauthorizedError) -> str:
|
||||
original: Final = error.orig_exception
|
||||
response: Final = original.response if isinstance(original, requests.HTTPError) else None
|
||||
body: Final = response.content.decode("utf-8", errors="replace") if response is not None else ""
|
||||
reason: Final = _displayable(body) or _displayable(str(error)) or _NO_PROXY_REASON
|
||||
return reason if len(reason) <= _PROXY_REASON_CHARS else f"{reason[:_PROXY_REASON_CHARS]}..."
|
||||
|
||||
|
||||
def _sign_in_failure(base_url: str, failure: PkceFailure) -> str:
|
||||
if failure.proxy_predates_pkce:
|
||||
return (
|
||||
f"The proxy at {base_url} has no browser sign-in for lite, so it is likely older than v1.99.0. "
|
||||
"Upgrade it, run `lite login` if it uses SSO, or set LITELLM_PROXY_API_KEY."
|
||||
)
|
||||
return (
|
||||
f"Could not sign in to {base_url} through the browser: {failure.reason}. "
|
||||
"Run `lite login --pkce` from a terminal on a machine with a browser, `lite login` on one without, "
|
||||
"or set LITELLM_PROXY_API_KEY."
|
||||
)
|
||||
|
||||
|
||||
def _sign_in(ctx: click.Context, base_url: str, sign_in: BrowserSignIn) -> str:
|
||||
_echo_to_stderr(f"The proxy at {base_url} needs a sign-in.")
|
||||
credential: Final = sign_in(base_url, _echo_to_stderr)
|
||||
if isinstance(credential, PkceFailure):
|
||||
raise click.ClickException(_sign_in_failure(base_url, credential))
|
||||
other_proxy: Final = _login_for_another_proxy(ctx, base_url)
|
||||
if other_proxy is not None:
|
||||
_echo_to_stderr(
|
||||
f"A login for {other_proxy} was stored while this sign-in waited, so lite kept it "
|
||||
f"and used this sign-in for `{ctx.command_path}` only."
|
||||
)
|
||||
return credential.access_token
|
||||
stored: Final = replace_stored_token(
|
||||
pkce_token_record(base_url, credential), requests.Session(), context_secret_vault(ctx)
|
||||
)
|
||||
_echo_to_stderr(storage_notice(stored))
|
||||
_echo_to_stderr(f"Signed in. Continuing `{ctx.command_path}`.")
|
||||
return credential.access_token
|
||||
|
||||
|
||||
def _proxy_accepts(base_url: str, api_key: str) -> bool:
|
||||
try:
|
||||
response: Final = requests.get(
|
||||
f"{base_url.rstrip('/')}/key/info", headers={"Authorization": f"Bearer {api_key}"}, timeout=10
|
||||
)
|
||||
except requests.RequestException:
|
||||
return False
|
||||
return response.ok or response.status_code == _SIGNED_IN_LOGIN_WITHOUT_KEY_ROW
|
||||
|
||||
|
||||
def _sign_in_lock() -> SignInLock:
|
||||
return FileLock(Path(get_cli_token_file_path()).with_name("sign-in.lock"))
|
||||
|
||||
|
||||
def _try_to_lock(lock: SignInLock, timeout: float) -> Literal["held", "busy", "unlockable"]:
|
||||
try:
|
||||
lock.acquire(timeout=timeout)
|
||||
except Timeout:
|
||||
return "busy"
|
||||
except OSError:
|
||||
return "unlockable"
|
||||
return "held"
|
||||
|
||||
|
||||
def _lock_sign_in(lock: SignInLock) -> Literal["held", "waited", "unlockable"]:
|
||||
first_try: Final = _try_to_lock(lock, timeout=0)
|
||||
if first_try != "busy":
|
||||
return first_try
|
||||
_echo_to_stderr(
|
||||
"Another lite command is waiting for a browser sign-in, so this one waits for it instead of opening a second one."
|
||||
)
|
||||
match _try_to_lock(lock, timeout=_WAIT_FOR_ANOTHER_SIGN_IN_SECONDS):
|
||||
case "held":
|
||||
return "waited"
|
||||
case "unlockable":
|
||||
return "unlockable"
|
||||
case "busy":
|
||||
raise click.ClickException(
|
||||
"Another lite command has been waiting for a browser sign-in for over "
|
||||
f"{_WAIT_FOR_ANOTHER_SIGN_IN_SECONDS // 60} minutes, so this one stopped waiting. "
|
||||
"Finish or stop that sign-in, then run this command again."
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _sign_in_lock_held(lock: SignInLock) -> Generator[bool]:
|
||||
outcome: Final = _lock_sign_in(lock)
|
||||
if outcome == "unlockable":
|
||||
yield False
|
||||
return
|
||||
try:
|
||||
yield outcome == "waited"
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
|
||||
def _login_for_another_proxy(ctx: click.Context, base_url: str) -> str | None:
|
||||
login: Final = load_cli_token(vault=context_secret_vault(ctx))
|
||||
if login is None or not login.base_url or login.base_url == base_url.rstrip("/"):
|
||||
return None
|
||||
return login.base_url
|
||||
|
||||
|
||||
def _rerun_after_sign_in(
|
||||
run: Callable[[click.Context], object], ctx: click.Context, context: _SignInContext, error: UnauthorizedError
|
||||
) -> object:
|
||||
base_url: Final = context["base_url"]
|
||||
if not context.get("api_key_from_token_file", False):
|
||||
raise click.ClickException(
|
||||
f"The proxy at {base_url} refused `{ctx.command_path}` with HTTP 401 for the API key from --api-key "
|
||||
"or LITELLM_PROXY_API_KEY. Check that the key is valid and has the role this command needs, "
|
||||
f"or unset both to sign in through your browser instead. The proxy said: {_proxy_reason(error)}"
|
||||
)
|
||||
rejected_key: Final = context.get("api_key")
|
||||
if rejected_key is not None and _proxy_accepts(base_url, rejected_key):
|
||||
raise click.ClickException(
|
||||
f"The proxy at {base_url} accepts your stored login but refused `{ctx.command_path}` with HTTP 401, "
|
||||
"so it likely lacks the role this command needs. Set LITELLM_PROXY_API_KEY to a key that has it. "
|
||||
f"The proxy said: {_proxy_reason(error)}"
|
||||
)
|
||||
sign_in: Final = context.get("browser_sign_in") or PkceBrowserSignIn()
|
||||
unavailable: Final = sign_in.unavailable_because()
|
||||
if unavailable is not None:
|
||||
_echo_to_stderr(f"The proxy at {base_url} needs a sign-in.")
|
||||
raise click.ClickException(_sign_in_failure(base_url, unavailable))
|
||||
with _sign_in_lock_held(context.get("sign_in_lock", _sign_in_lock)()) as waited_for_another_sign_in:
|
||||
return _rerun_once_signed_in(run, ctx, base_url, sign_in, rejected_key, waited_for_another_sign_in)
|
||||
|
||||
|
||||
def _rerun_once_signed_in(
|
||||
run: Callable[[click.Context], object],
|
||||
ctx: click.Context,
|
||||
base_url: str,
|
||||
sign_in: BrowserSignIn,
|
||||
rejected_key: str | None,
|
||||
waited_for_another_sign_in: bool,
|
||||
) -> object:
|
||||
retried: Final = _attempt_with_newer_stored_key(run, ctx, base_url, rejected_key)
|
||||
if isinstance(retried, _Completed):
|
||||
return retried.value
|
||||
other_proxy: Final = _login_for_another_proxy(ctx, base_url)
|
||||
if other_proxy is not None:
|
||||
raise click.ClickException(
|
||||
f"The proxy at {base_url} needs a sign-in, but lite keeps one stored login and it is for {other_proxy}, "
|
||||
f"so lite did not replace it on its own. Run `lite --base-url {base_url} login --pkce` to switch proxies, "
|
||||
"or set LITELLM_PROXY_API_KEY."
|
||||
)
|
||||
if waited_for_another_sign_in:
|
||||
raise click.ClickException(
|
||||
"The browser sign-in another lite command started ended while this one waited, "
|
||||
f"and the proxy at {base_url} still refuses `{ctx.command_path}`, so lite did not open a second one. "
|
||||
"Run the command again to sign in."
|
||||
)
|
||||
match _attempt_with_key(run, ctx, _sign_in(ctx, base_url, sign_in)):
|
||||
case _Completed(value=value):
|
||||
return value
|
||||
case _Unauthorized(error=error):
|
||||
raise click.ClickException(
|
||||
f"The proxy at {base_url} refused `{ctx.command_path}` with HTTP 401 again after the browser sign-in, "
|
||||
"so the signed-in user likely lacks the role this command needs. "
|
||||
f"Set LITELLM_PROXY_API_KEY to a key that has it. The proxy said: {_proxy_reason(error)}"
|
||||
)
|
||||
|
||||
|
||||
class SignInOnUnauthorizedCommand(click.Command):
|
||||
def invoke(self, ctx: click.Context) -> object:
|
||||
run: Final = super().invoke
|
||||
match _attempt(run, ctx):
|
||||
case _Completed(value=value):
|
||||
return value
|
||||
case _Unauthorized(error=error):
|
||||
context: Final = _sign_in_context(ctx)
|
||||
if context is None:
|
||||
raise error
|
||||
return _rerun_after_sign_in(run, ctx, context, error)
|
||||
|
||||
|
||||
class SignInOnUnauthorizedGroup(click.Group):
|
||||
command_class = SignInOnUnauthorizedCommand
|
||||
|
|
@ -10,6 +10,7 @@ from typing_extensions import NotRequired, ReadOnly, TypedDict
|
|||
|
||||
from ...credentials import CredentialsManagementClient
|
||||
from ._cli_context import cli_context_values
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
|
||||
class _CredentialInfo(TypedDict):
|
||||
|
|
@ -39,7 +40,7 @@ def _print_json(data: object) -> None:
|
|||
rich.print_json(data=data)
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(cls=SignInOnUnauthorizedGroup)
|
||||
def credentials():
|
||||
"""Manage credentials for the LiteLLM proxy server"""
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import re
|
|||
from collections.abc import Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from http import HTTPStatus
|
||||
from pathlib import Path
|
||||
from types import MappingProxyType
|
||||
from typing import Final
|
||||
|
|
@ -23,8 +24,10 @@ from pydantic import ConfigDict, Field, JsonValue, TypeAdapter, ValidationError,
|
|||
|
||||
from litellm.types.llms.base_model import LiteLLMBaseModel
|
||||
|
||||
from ...exceptions import UnauthorizedError
|
||||
from ...http_client import HTTPClient
|
||||
from ._cli_context import cli_context_values
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
CLAUDE_DIR: Final = Path.home() / ".claude"
|
||||
REPORT_DIR: Final = Path.home() / ".litellm" / "debug"
|
||||
|
|
@ -45,6 +48,7 @@ report was saved to so I can hand it off. If nothing failed, say so.
|
|||
@dataclass(frozen=True, slots=True)
|
||||
class DebugFailure:
|
||||
message: str
|
||||
unauthorized: requests.HTTPError | None = None
|
||||
|
||||
|
||||
class ErrorInformation(LiteLLMBaseModel):
|
||||
|
|
@ -141,7 +145,13 @@ def detect_claude_session_id(env: Mapping[str, str], claude_dir: Path) -> str |
|
|||
def _transport_failure(uri: str, error: requests.exceptions.RequestException) -> DebugFailure:
|
||||
body: Final = error.response.text[:_TRANSPORT_BODY_CHARS] if error.response is not None else ""
|
||||
detail: Final = f"\n{body}" if body else ""
|
||||
return DebugFailure(f"GET {uri} failed: {error}{detail}")
|
||||
return DebugFailure(f"GET {uri} failed: {error}{detail}", _unauthorized(error))
|
||||
|
||||
|
||||
def _unauthorized(error: requests.exceptions.RequestException) -> requests.HTTPError | None:
|
||||
if not isinstance(error, requests.HTTPError) or error.response is None:
|
||||
return None
|
||||
return error if error.response.status_code == HTTPStatus.UNAUTHORIZED else None
|
||||
|
||||
|
||||
class SpendLogsFetcher:
|
||||
|
|
@ -311,7 +321,7 @@ def install_slash_command(claude_dir: Path) -> Path:
|
|||
return path
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(cls=SignInOnUnauthorizedGroup)
|
||||
def debug() -> None:
|
||||
"""Pull debug reports (spend, request, response, error) for coding-agent sessions"""
|
||||
|
||||
|
|
@ -360,6 +370,8 @@ def debug_claude(
|
|||
recent_bodies=recent_bodies,
|
||||
max_chars=max_body_chars,
|
||||
)
|
||||
if isinstance(outcome, DebugFailure) and outcome.unauthorized is not None:
|
||||
raise UnauthorizedError(outcome.unauthorized)
|
||||
if isinstance(outcome, DebugFailure):
|
||||
raise click.ClickException(outcome.message)
|
||||
click.echo(outcome)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ from rich.table import Table
|
|||
from typing_extensions import ReadOnly, TypedDict
|
||||
|
||||
from ...keys import KeysManagementClient
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
|
||||
class _CliContext(TypedDict):
|
||||
|
|
@ -37,7 +38,7 @@ def _cli_context(view: _CliContextView) -> _CliContext:
|
|||
return view.obj
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(cls=SignInOnUnauthorizedGroup)
|
||||
def keys():
|
||||
"""Manage API keys for the LiteLLM proxy server"""
|
||||
|
||||
|
|
@ -352,7 +353,7 @@ def _import_keys_to_destination(
|
|||
return imported_count, failed_count
|
||||
|
||||
|
||||
@keys.command(name="import")
|
||||
@keys.command(name="import", cls=click.Command)
|
||||
@click.option(
|
||||
"--source-base-url",
|
||||
required=True,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import rich.table
|
|||
|
||||
from ... import Client
|
||||
from ._cli_context import cli_context_values
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
|
||||
def create_client(ctx: click.Context) -> Client:
|
||||
|
|
@ -19,7 +20,7 @@ def _rendered_field(group: Mapping[str, object], key: str, default: str) -> str:
|
|||
return str(group.get(key, default))
|
||||
|
||||
|
||||
@click.group(name="model-groups")
|
||||
@click.group(name="model-groups", cls=SignInOnUnauthorizedGroup)
|
||||
def model_groups() -> None:
|
||||
"""Inspect model groups your key can access on the proxy"""
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
# stdlib imports
|
||||
import re
|
||||
from collections import defaultdict
|
||||
from collections.abc import Callable, Mapping
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Any, Final, Literal
|
||||
|
|
@ -14,7 +14,9 @@ from typing_extensions import NotRequired, ReadOnly, TypedDict
|
|||
|
||||
# local imports
|
||||
from ... import Client
|
||||
from ...exceptions import UnauthorizedError
|
||||
from ._cli_context import cli_context_values
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from rich.console import JustifyMethod
|
||||
|
|
@ -43,7 +45,7 @@ class ModelYamlInfo:
|
|||
return ", ".join(self.access_groups) if self.access_groups else ""
|
||||
|
||||
|
||||
def _get_model_info_obj_from_yaml(model: dict[str, Any]) -> ModelYamlInfo:
|
||||
def _get_model_info_obj_from_yaml(model: Mapping[str, Any]) -> ModelYamlInfo:
|
||||
"""Extract model info from a model dict and return as ModelYamlInfo dataclass."""
|
||||
model_name: Final[str] = model["model_name"]
|
||||
model_params: Final[dict[str, Any]] = model["litellm_params"]
|
||||
|
|
@ -107,7 +109,7 @@ def create_client(ctx: click.Context) -> Client:
|
|||
return Client(base_url=context["base_url"], api_key=context["api_key"])
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(cls=SignInOnUnauthorizedGroup)
|
||||
def models() -> None:
|
||||
"""Manage models on your LiteLLM proxy server"""
|
||||
|
||||
|
|
@ -394,6 +396,12 @@ def get_model_list_from_yaml_file(yaml_file: str) -> list[dict[str, object]]:
|
|||
return model_list
|
||||
|
||||
|
||||
def _model_list_from_yaml_argument(
|
||||
_ctx: click.Context, _param: click.Parameter, yaml_file: str
|
||||
) -> Sequence[Mapping[str, object]]:
|
||||
return get_model_list_from_yaml_file(yaml_file)
|
||||
|
||||
|
||||
def _get_filtered_model_list(model_list, only_models_matching_regex, only_access_groups_matching_regex):
|
||||
"""Return a list of models that pass the filter criteria."""
|
||||
model_regex: Final = re.compile(only_models_matching_regex) if only_models_matching_regex else None
|
||||
|
|
@ -409,7 +417,12 @@ def _import_models_get_table_title(dry_run: bool) -> str:
|
|||
|
||||
|
||||
@models.command("import")
|
||||
@click.argument("yaml_file", type=click.Path(exists=True, dir_okay=False, readable=True))
|
||||
@click.argument(
|
||||
"model_list",
|
||||
metavar="YAML_FILE",
|
||||
type=click.Path(exists=True, dir_okay=False, readable=True),
|
||||
callback=_model_list_from_yaml_argument,
|
||||
)
|
||||
@click.option(
|
||||
"--dry-run",
|
||||
is_flag=True,
|
||||
|
|
@ -428,7 +441,7 @@ def _import_models_get_table_title(dry_run: bool) -> str:
|
|||
@click.pass_context
|
||||
def import_models(
|
||||
ctx: click.Context,
|
||||
yaml_file: str,
|
||||
model_list: Sequence[Mapping[str, object]],
|
||||
dry_run: bool,
|
||||
only_models_matching_regex: str | None,
|
||||
only_access_groups_matching_regex: str | None,
|
||||
|
|
@ -436,7 +449,6 @@ def import_models(
|
|||
"""Import models from a YAML file and add them to the proxy."""
|
||||
provider_counts: Final[dict[str, int]] = defaultdict(int)
|
||||
added_models: Final[list[ModelYamlInfo]] = []
|
||||
model_list: Final = get_model_list_from_yaml_file(yaml_file)
|
||||
filtered_model_list: Final = _get_filtered_model_list(
|
||||
model_list, only_models_matching_regex, only_access_groups_matching_regex
|
||||
)
|
||||
|
|
@ -453,6 +465,13 @@ def import_models(
|
|||
model_params=model_info_obj.model_params,
|
||||
model_info=model_info_obj.model_info,
|
||||
)
|
||||
except UnauthorizedError as error:
|
||||
if added_models:
|
||||
raise click.ClickException(
|
||||
f"The proxy rejected the API key after {len(added_models)} of {len(filtered_model_list)} "
|
||||
"models were sent, so the import stopped there."
|
||||
) from error
|
||||
raise
|
||||
except Exception:
|
||||
pass # For summary, ignore errors
|
||||
added_models.append(model_info_obj)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import webbrowser
|
|||
from base64 import urlsafe_b64encode
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
from http import HTTPStatus
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Final, Literal, Protocol
|
||||
|
|
@ -68,6 +69,7 @@ class _TokenResponse(LiteLLMBaseModel):
|
|||
@dataclass(frozen=True, slots=True)
|
||||
class PkceFailure:
|
||||
reason: str
|
||||
proxy_predates_pkce: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
|
|
@ -128,6 +130,7 @@ class LoopbackServer(HTTPServer):
|
|||
self.expected_state: Final = expected_state
|
||||
self.connection_timeout_seconds: Final = connection_timeout_seconds
|
||||
self.outcome: CallbackOutcome | None = None
|
||||
self.launch_failure: PkceFailure | None = None
|
||||
self.timeout = 1
|
||||
|
||||
@property
|
||||
|
|
@ -139,11 +142,18 @@ class LoopbackServer(HTTPServer):
|
|||
accepted[0].settimeout(self.connection_timeout_seconds)
|
||||
return accepted
|
||||
|
||||
def launch_browser(self, open_browser: Callable[[str], object], url: str) -> None:
|
||||
opened: Final = open_browser(url)
|
||||
if isinstance(opened, PkceFailure):
|
||||
self.launch_failure = opened
|
||||
|
||||
def wait(
|
||||
self, timeout_seconds: float, clock: Callable[[], float] = time.monotonic
|
||||
) -> CallbackOutcome | PkceFailure:
|
||||
deadline: Final = clock() + timeout_seconds
|
||||
while self.outcome is None:
|
||||
if self.launch_failure is not None:
|
||||
return self.launch_failure
|
||||
if clock() >= deadline:
|
||||
return PkceFailure("timed out waiting for the browser sign-in to finish")
|
||||
self.handle_request()
|
||||
|
|
@ -200,7 +210,8 @@ def discover_cli_auth(base_url: str, http: Http) -> CliAuthContract | PkceFailur
|
|||
return PkceFailure(f"could not reach {url}: {exc}")
|
||||
if response.status_code != 200:
|
||||
return PkceFailure(
|
||||
f"{url} answered {response.status_code}; this proxy version does not support `lite login --pkce`"
|
||||
f"{url} answered {response.status_code}; this proxy version does not support `lite login --pkce`",
|
||||
proxy_predates_pkce=response.status_code == HTTPStatus.NOT_FOUND,
|
||||
)
|
||||
try:
|
||||
contract: Final = CliAuthContract.model_validate(response.json())
|
||||
|
|
@ -454,7 +465,9 @@ def run_pkce_login(
|
|||
url: Final = authorize_url(contract, client_id, server.redirect_uri, state, challenge)
|
||||
echo(f"Opening browser to: {url}")
|
||||
echo("Approve the sign-in in your browser. Waiting...")
|
||||
threading.Thread(target=open_browser, args=(url,), name="lite-login-browser", daemon=True).start()
|
||||
threading.Thread(
|
||||
target=server.launch_browser, args=(open_browser, url), name="lite-login-browser", daemon=True
|
||||
).start()
|
||||
outcome: Final = server.wait(timeout_seconds)
|
||||
match outcome:
|
||||
case PkceFailure():
|
||||
|
|
|
|||
|
|
@ -10,8 +10,10 @@ from rich.table import Table
|
|||
from typing_extensions import ReadOnly, TypedDict
|
||||
|
||||
from litellm.proxy.client import Client
|
||||
from litellm.proxy.client.exceptions import UnauthorizedError
|
||||
|
||||
from ._cli_context import cli_context_values
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
|
||||
class _TeamRow(TypedDict):
|
||||
|
|
@ -29,7 +31,7 @@ class _ErrorBodyView(TypedDict):
|
|||
body: ReadOnly[Mapping[str, object]]
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(cls=SignInOnUnauthorizedGroup)
|
||||
def teams():
|
||||
"""Manage teams and team assignments"""
|
||||
|
||||
|
|
@ -94,6 +96,8 @@ def list(ctx: click.Context):
|
|||
# Use list() for simpler response structure (returns array directly)
|
||||
teams: Final = client.teams.list()
|
||||
display_teams_table(teams)
|
||||
except UnauthorizedError:
|
||||
raise
|
||||
except requests.exceptions.HTTPError as e:
|
||||
click.echo(f"Error: HTTP {e.response.status_code}", err=True)
|
||||
error_body: Final[_ErrorBodyView] = {"body": e.response.json()}
|
||||
|
|
@ -119,6 +123,8 @@ def available(ctx: click.Context):
|
|||
display_teams_table(teams)
|
||||
else:
|
||||
click.echo("No available teams to join.")
|
||||
except UnauthorizedError:
|
||||
raise
|
||||
except requests.exceptions.HTTPError as e:
|
||||
click.echo(f"Error: HTTP {e.response.status_code}", err=True)
|
||||
error_body: Final[_ErrorBodyView] = {"body": e.response.json()}
|
||||
|
|
@ -128,7 +134,7 @@ def available(ctx: click.Context):
|
|||
raise click.Abort()
|
||||
|
||||
|
||||
@teams.command()
|
||||
@teams.command(cls=click.Command)
|
||||
@click.option("--team-id", type=str, help="Team ID to assign the key to")
|
||||
@click.pass_context
|
||||
def assign_key(ctx: click.Context, team_id: str | None):
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ import rich
|
|||
|
||||
from ... import UsersManagementClient
|
||||
from ._cli_context import cli_context_values
|
||||
from .auto_sign_in import SignInOnUnauthorizedGroup
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(cls=SignInOnUnauthorizedGroup)
|
||||
def users():
|
||||
"""Manage users on your LiteLLM proxy server"""
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,9 @@ class TeamsManagementClient:
|
|||
response: Final = requests.get(url, headers=self._get_headers(), params=params, timeout=self._timeout)
|
||||
|
||||
if response.status_code == 401:
|
||||
raise UnauthorizedError("Authentication failed. Check your API key.")
|
||||
raise UnauthorizedError(
|
||||
requests.exceptions.HTTPError("Authentication failed. Check your API key.", response=response)
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
|
@ -122,7 +124,9 @@ class TeamsManagementClient:
|
|||
response: Final = requests.get(url, headers=self._get_headers(), params=params, timeout=self._timeout)
|
||||
|
||||
if response.status_code == 401:
|
||||
raise UnauthorizedError("Authentication failed. Check your API key.")
|
||||
raise UnauthorizedError(
|
||||
requests.exceptions.HTTPError("Authentication failed. Check your API key.", response=response)
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
|
@ -143,7 +147,9 @@ class TeamsManagementClient:
|
|||
response: Final = requests.get(url, headers=self._get_headers(), timeout=self._timeout)
|
||||
|
||||
if response.status_code == 401:
|
||||
raise UnauthorizedError("Authentication failed. Check your API key.")
|
||||
raise UnauthorizedError(
|
||||
requests.exceptions.HTTPError("Authentication failed. Check your API key.", response=response)
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ button {{ flex: 1; padding: 10px; border-radius: 6px; font-size: 15px; cursor: p
|
|||
<div class="container">
|
||||
<h1>Authorize CLI access</h1>
|
||||
<p>A command-line client at <code>{escape(client_origin)}</code> wants to call LiteLLM as <strong>{escape(user_id)}</strong>.</p>
|
||||
<p>Approving issues it a personal credential that expires within {CLI_JWT_EXPIRATION_HOURS} hours. <code>lite logout</code> stops it from being renewed. Only approve if you started this sign-in yourself.</p>
|
||||
<p>Approving issues it a personal credential that expires within {CLI_JWT_EXPIRATION_HOURS} hours. <code>lite logout</code> stops it from being renewed. Only approve if you started this sign-in, yourself or through an agent you are running.</p>
|
||||
<form method="post" action="{escape(complete_url)}">
|
||||
<input type="hidden" name="flow" value="{escape(flow_handle)}">
|
||||
{team_select_field(teams)}
|
||||
|
|
|
|||
|
|
@ -710,7 +710,9 @@ class TestWhoamiCommand:
|
|||
|
||||
assert result.exit_code == 0
|
||||
assert "Not authenticated" in result.output
|
||||
assert "Run 'lite login'" in result.output
|
||||
assert "lite opens your browser to sign in and waits up to 5 minutes" in result.output
|
||||
assert "run 'lite login --pkce'" in result.output
|
||||
assert "or 'lite login' where no browser can open" in result.output
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("proxy_status", "expected_last_line_start"),
|
||||
|
|
|
|||
1237
tests/unit/proxy/client/cli/test_auto_sign_in.py
Normal file
1237
tests/unit/proxy/client/cli/test_auto_sign_in.py
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -549,6 +549,22 @@ def test_run_pkce_login_stops_before_the_browser_when_registration_fails():
|
|||
assert opened == []
|
||||
|
||||
|
||||
def test_run_pkce_login_stops_waiting_when_the_browser_launcher_reports_it_did_not_open():
|
||||
http = _FakeHttp(
|
||||
{
|
||||
("GET", DISCOVERY_URL): _FakeResponse(200, CONTRACT_DOC),
|
||||
("POST", f"{BASE}/register"): _FakeResponse(201, {"client_id": "llm_dcrc_abc"}),
|
||||
}
|
||||
)
|
||||
launch_failure = PkceFailure("the web browser did not open")
|
||||
|
||||
result = run_pkce_login(
|
||||
BASE, http, open_browser=lambda _url: launch_failure, echo=lambda _: None, timeout_seconds=30
|
||||
)
|
||||
|
||||
assert result == launch_failure
|
||||
|
||||
|
||||
def test_pkce_token_record_keeps_every_refresh_input_next_to_the_key():
|
||||
credential = PkceCredential(
|
||||
access_token="sk-cli-new",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue