Repository navigation
fix(ui): Propagate agent 401 as HTTP 401 for transparent token refresh - #1447
Conversation
The streaming chat endpoint wrapped agent 401 responses inside SSE error events (HTTP 200), preventing the frontend's existing retry logic (PR rossoctl#1430) from triggering token refresh. This caused users to see "Agent error: 401" and need to manually re-login after deploying a new agent. Root cause: when a new agent is deployed, client-registration adds its audience scope to Keycloak. Tokens issued before that point lack the agent's SPIFFE ID in the aud claim. The frontend's forceRefreshToken() correctly obtains a new token with the updated audience, but it only fires on HTTP 401 — not on SSE error payloads. Fix: open the agent connection before committing to StreamingResponse. If the agent returns 401, raise HTTPException(401) so the frontend sees a real HTTP 401 and retries with a refreshed token transparently. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
|
|
||
| return StreamingResponse( | ||
| _stream_a2a_response(agent_url, request.message, session_id, authorization, user.username), | ||
| _stream_from_response(client, response, session_id, user.username), |
The test was failing in CI because: 1. Only load_incluster_config was mocked but load_kube_config also gets called as a fallback, causing ConfigException in CI (no kubeconfig) 2. ruff format --check failed due to import ordering and line length Signed-off-by: Hai Huang <[email protected]> Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
The k8s config mocks in the app fixture only cover import time. Since KubernetesService is instantiated per-request via the get_kubernetes_service dependency, override it with a MagicMock to avoid ConfigException in CI where no kubeconfig exists. Signed-off-by: Hai Huang <[email protected]> Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
|
/run-e2e |
E2E HyperShift Tests PassedCommit: The If you push new commits, the label will be removed and you'll need to comment |
| detail = response.text[:500] | ||
| except Exception: | ||
| detail = str(response.status_code) | ||
| logger.error(f"Agent error: {response.status_code}: {detail}") |
There was a problem hiding this comment.
Consider sanitizing detail, or not using f-strings, to avoid log injection.
Address CodeQL findings: - Replace f-string logger calls with %-formatting to prevent log injection - Strip newlines from agent response text before logging - Do not expose raw exception details in HTTPException (log server-side instead) Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
New Commits DetectedThe New HEAD: For maintainers: Please review the new changes and comment Security checklistBefore running E2E tests, verify the PR does NOT contain:
This is an automated security measure to prevent TOCTOU attacks |
Fixes ruff format --check failure and addresses CodeQL log injection finding (agent_url is already sanitized via sanitize_log but CodeQL cannot track through the call chain). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
| }, | ||
| } | ||
|
|
||
| logger.debug("Starting A2A stream to %s with session_id=%s", agent_url, session_id) |
|
/run-e2e |
E2E HyperShift Tests PassedCommit: The If you push new commits, the label will be removed and you'll need to comment |
Summary
_stream_a2a_responsefunction replaced by_stream_from_response(owns an already-opened connection)Problem
When a user deploys a new agent:
client-registrationcreates an audience scope in Keycloak with the agent's SPIFFE IDHTTPStatusError, yieldeddata: {"error": "Agent error: 401"}inside an HTTP 200 streamresponse.status === 401but sees 200 — never retriesFix
Open the agent connection in the endpoint function before committing to
StreamingResponse. If the agent returns 401, raiseHTTPException(401)directly. The frontend's existingforceRefreshToken()fires, gets a new token with the correct audience, and retries — all transparent to the user.Test plan
test_chat_stream_401.py(agent 401 → HTTP 401, connection error → HTTP 503)Assisted-By: Claude (Anthropic AI) [email protected]