Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion litellm/proxy/middleware/prometheus_auth_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ async def receive_for_auth() -> MutableMapping[str, Any]:
# Send 401 response directly via ASGI protocol
error_message = getattr(e, "message", str(e))
body = json.dumps(
f"Unauthorized access to metrics endpoint: {error_message}"
f"Unauthorized access to metrics endpoint: {error_message} "
f"To allow unauthenticated access, set "
f"`litellm_settings.require_auth_for_metrics_endpoint: false` "
f"in your proxy_config.yaml."
).encode("utf-8")
await send(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ def test_invalid_auth_metrics(app_with_middleware, monkeypatch):
assert "Unauthorized access to metrics endpoint" in response.text


def test_invalid_auth_metrics_includes_optout_hint(app_with_middleware, monkeypatch):
"""
The 401 body must tell operators how to restore the previous unauthenticated
behavior, otherwise a Prometheus scraper that worked pre-upgrade just sees
"Malformed API Key" with no actionable migration path.
"""
monkeypatch.setattr(litellm, "require_auth_for_metrics_endpoint", True)
monkeypatch.setattr(
"litellm.proxy.middleware.prometheus_auth_middleware.user_api_key_auth",
fake_invalid_auth,
)

client = TestClient(app_with_middleware)
response = client.get("/metrics")

assert response.status_code == 401, response.text
assert "require_auth_for_metrics_endpoint" in response.text
assert "false" in response.text
Comment on lines +140 to +141

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The assert "false" in response.text assertion is overly broad — it would also pass if the inner error message or any other part of the JSON body happened to contain the word "false". Checking for the full key-value substring is more precise and will catch regressions if the hint wording changes.

Suggested change
assert "require_auth_for_metrics_endpoint" in response.text
assert "false" in response.text
assert "require_auth_for_metrics_endpoint" in response.text
assert "require_auth_for_metrics_endpoint: false" in response.text



def test_metrics_auth_uses_real_auth_when_route_is_public(
app_with_middleware, monkeypatch
):
Expand Down
Loading