Repository navigation
fix(agent-service): authenticate to the LLM proxy as the delegating user - #5605
Conversation
|
@xuang7 I think this should be included in the v1.2 release. Please have a pass. |
The LLM gateway (access-control-service) requires a REGULAR/ADMIN-role JWT (apache#5421) and injects the LiteLLM master key downstream, but the agent-service still sent the static LLM_API_KEY ("dummy"), so every generation call returned 401 Unauthorized. The delegating user's JWT is now the only credential the agent-service sends: userToken is required when creating an agent (the request is rejected otherwise), and LLM_API_KEY is removed from the service, the helm chart, and the single-node env.
205cefa to
6750b40
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5605 +/- ##
============================================
- Coverage 52.45% 52.19% -0.26%
+ Complexity 2485 2467 -18
============================================
Files 1071 1070 -1
Lines 41366 41443 +77
Branches 4442 4476 +34
============================================
- Hits 21698 21633 -65
- Misses 18399 18540 +141
- Partials 1269 1270 +1
*This pull request uses carry forward flags. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Updates agent-service authentication to the LLM gateway (access-control-service / LiteLLM proxy) to use the delegating user’s JWT instead of a static service API key, aligning with the tightened gateway auth requirements introduced in #5421.
Changes:
- Require
userTokenwhen creating agents and use it as the OpenAI client credential when calling the LLM gateway. - Remove
LLM_API_KEYconfiguration from agent-service runtime config, single-node env, and Helm chart wiring. - Update agent-service tests to mint a JWT-like token and validate new
userTokenrequirements.
Reviewed changes
Copilot reviewed 4 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| bin/single-node/.env | Removes LLM_API_KEY from single-node env. |
| bin/k8s/values.yaml | Removes agentService.env.llmApiKey values entry. |
| bin/k8s/values-development.yaml | Removes agentService.env.llmApiKey values entry for dev. |
| bin/k8s/templates/agent-service-secret.yaml | Removes llm-api-key from secret; scopes secret creation to LiteLLM enablement. |
| bin/k8s/templates/agent-service-deployment.yaml | Stops injecting LLM_API_KEY env var into agent-service pod. |
| agent-service/src/types/agent.ts | Makes CreateAgentRequest.userToken required. |
| agent-service/src/server.ts | Uses delegating JWT as OpenAI apiKey; rejects missing/empty userToken. |
| agent-service/src/server.test.ts | Updates tests to include minted JWT and adds userToken validation cases. |
| agent-service/src/config/env.ts | Removes LLM_API_KEY from env schema. |
Comments suppressed due to low confidence (2)
agent-service/src/server.ts:168
- The global
.onErrorhandler unconditionally setsset.status = 500for any unrecognized error. In Elysia, request body validation errors (e.g., missing requireduserToken) can be routed through.onError, and this handler will then turn a client-side 4xx into a 500 response. Handle validation errors explicitly (via thecodefield) so malformed requests consistently return a 4xx.
.onError(({ error, set }) => {
log.error({ err: error }, "request error");
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage === "Agent not found") {
set.status = 404;
return { error: "Agent not found" };
}
if (errorMessage === "Invalid or expired token") {
set.status = 401;
return { error: "Invalid or expired token" };
}
if (errorMessage === "userToken is required") {
set.status = 401;
return { error: "userToken is required" };
}
if (errorMessage === "modelType is required") {
set.status = 400;
return { error: "modelType is required" };
}
set.status = 500;
return { error: errorMessage || "Internal server error" };
agent-service/src/server.test.ts:151
- This test currently allows a 5xx response for a client-side body schema violation (missing
modelType). A 5xx here would indicate a server bug; assert the status is a 4xx to ensure request validation doesn’t get surfaced as an internal error.
// Body schema violation; the exact status depends on the Elysia version but
// it is always a 4xx or 5xx, never a successful 2xx.
expect(res.status).toBeGreaterThanOrEqual(400);
});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Copilot review on apache#5605 flagged that the schema-violation tests accepted any status >= 400; tightening them exposed that Elysia VALIDATION/PARSE errors fell through the onError chain to the generic 500. Map them to 400 and assert the exact status in the tests.
|
Please allow me some time to check on this tonight |
…ization header Credentials belong in the Authorization header, not the JSON payload: it follows the bearer-token convention and keeps JWTs out of body logs. The frontend's JWT interceptor already attaches the header to every API request, so agent.service.ts simply stops duplicating the token in the create-agent body.
|
@bobbai00 I think we can improve on the test coverage. |
… lookup Codecov flagged the new createAgent body construction as uncovered, so add an AgentService spec asserting the payload carries workflowId and computingUnitId but never the user token. Replacing the repetitive per-message onError branches with an ERROR_STATUS lookup also clears the uncoverable dangling-brace line in the old chain.
|
it's nit. please go ahead to merge |
|
Backport to |
…ser (#5605) ### What changes were proposed in this PR? Since #5421, the access-control-service LLM gateway requires a REGULAR/ADMIN-role user JWT and injects `LITELLM_MASTER_KEY` into the downstream request itself, but the agent-service still authenticated with the static `LLM_API_KEY` default (`"dummy"`), so every agent generation call returned 401 Unauthorized. This PR makes the delegating user's JWT the only credential the agent-service ever sends: - Creating an agent (`POST /agents`) now requires the user's JWT in the `Authorization: Bearer` header (the standard place for credentials, instead of a `userToken` field in the JSON payload); requests without it are rejected with 401, since an agent without a user could never call the gateway anyway. The frontend's JWT interceptor already attaches this header, so `agent.service.ts` simply stops duplicating the token in the request body. - The OpenAI client authenticates with the delegating user's JWT; the `LLM_API_KEY` env var is removed from the service (`env.ts`), the helm chart (deployment env, `llm-api-key` secret entry, `llmApiKey` values), and the single-node `.env`. The master key now lives only in the access-control-service and LiteLLM. - Elysia `VALIDATION`/`PARSE` errors are mapped to 400 instead of falling through to a generic 500 (surfaced by Copilot's review comment on the schema-violation tests). ```mermaid sequenceDiagram participant FE as Frontend (user JWT) participant AS as agent-service participant ACS as access-control-service (LLM gateway) participant LLM as LiteLLM FE->>AS: POST /agents<br/>Authorization: Bearer (user JWT) — required, else 401 FE->>AS: send message rect rgb(255, 235, 235) Note over AS,ACS: before: Authorization: Bearer dummy → 401 end AS->>ACS: POST /api/chat/completions<br/>Authorization: Bearer (user JWT) ✅ ACS->>LLM: forward with Authorization: Bearer (LITELLM_MASTER_KEY) LLM-->>AS: completion AS-->>FE: agent response ``` ### Any related issues, documentation, discussions? Closes #5604 ### How was this PR tested? Updated `server.test.ts`: agent creation sends a minted JWT in the `Authorization` header; added cases for a missing header, a non-Bearer scheme, and an invalid token (93 tests pass via `bun test`). Also verified end-to-end on a local Kubernetes deployment (this branch's chart + agent-service image, everything else from `ghcr.io/apache` nightlies): creation without the header → 401, with the header → 200, and a websocket message produced a real `gpt-5-mini` completion through access-control-service → LiteLLM. `typecheck` and `format:check` pass; the frontend change typechecks via `tsc --noEmit`. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Fable 5 (1M context) --------- (backported from commit ae3128d) Co-authored-by: Bob Bai <[email protected]>
…ser (apache#5605) ### What changes were proposed in this PR? Since apache#5421, the access-control-service LLM gateway requires a REGULAR/ADMIN-role user JWT and injects `LITELLM_MASTER_KEY` into the downstream request itself, but the agent-service still authenticated with the static `LLM_API_KEY` default (`"dummy"`), so every agent generation call returned 401 Unauthorized. This PR makes the delegating user's JWT the only credential the agent-service ever sends: - Creating an agent (`POST /agents`) now requires the user's JWT in the `Authorization: Bearer` header (the standard place for credentials, instead of a `userToken` field in the JSON payload); requests without it are rejected with 401, since an agent without a user could never call the gateway anyway. The frontend's JWT interceptor already attaches this header, so `agent.service.ts` simply stops duplicating the token in the request body. - The OpenAI client authenticates with the delegating user's JWT; the `LLM_API_KEY` env var is removed from the service (`env.ts`), the helm chart (deployment env, `llm-api-key` secret entry, `llmApiKey` values), and the single-node `.env`. The master key now lives only in the access-control-service and LiteLLM. - Elysia `VALIDATION`/`PARSE` errors are mapped to 400 instead of falling through to a generic 500 (surfaced by Copilot's review comment on the schema-violation tests). ```mermaid sequenceDiagram participant FE as Frontend (user JWT) participant AS as agent-service participant ACS as access-control-service (LLM gateway) participant LLM as LiteLLM FE->>AS: POST /agents<br/>Authorization: Bearer (user JWT) — required, else 401 FE->>AS: send message rect rgb(255, 235, 235) Note over AS,ACS: before: Authorization: Bearer dummy → 401 end AS->>ACS: POST /api/chat/completions<br/>Authorization: Bearer (user JWT) ✅ ACS->>LLM: forward with Authorization: Bearer (LITELLM_MASTER_KEY) LLM-->>AS: completion AS-->>FE: agent response ``` ### Any related issues, documentation, discussions? Closes apache#5604 ### How was this PR tested? Updated `server.test.ts`: agent creation sends a minted JWT in the `Authorization` header; added cases for a missing header, a non-Bearer scheme, and an invalid token (93 tests pass via `bun test`). Also verified end-to-end on a local Kubernetes deployment (this branch's chart + agent-service image, everything else from `ghcr.io/apache` nightlies): creation without the header → 401, with the header → 200, and a websocket message produced a real `gpt-5-mini` completion through access-control-service → LiteLLM. `typecheck` and `format:check` pass; the frontend change typechecks via `tsc --noEmit`. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Fable 5 (1M context) --------- Co-authored-by: Bob Bai <[email protected]>
…ser (apache#5605) ### What changes were proposed in this PR? Since apache#5421, the access-control-service LLM gateway requires a REGULAR/ADMIN-role user JWT and injects `LITELLM_MASTER_KEY` into the downstream request itself, but the agent-service still authenticated with the static `LLM_API_KEY` default (`"dummy"`), so every agent generation call returned 401 Unauthorized. This PR makes the delegating user's JWT the only credential the agent-service ever sends: - Creating an agent (`POST /agents`) now requires the user's JWT in the `Authorization: Bearer` header (the standard place for credentials, instead of a `userToken` field in the JSON payload); requests without it are rejected with 401, since an agent without a user could never call the gateway anyway. The frontend's JWT interceptor already attaches this header, so `agent.service.ts` simply stops duplicating the token in the request body. - The OpenAI client authenticates with the delegating user's JWT; the `LLM_API_KEY` env var is removed from the service (`env.ts`), the helm chart (deployment env, `llm-api-key` secret entry, `llmApiKey` values), and the single-node `.env`. The master key now lives only in the access-control-service and LiteLLM. - Elysia `VALIDATION`/`PARSE` errors are mapped to 400 instead of falling through to a generic 500 (surfaced by Copilot's review comment on the schema-violation tests). ```mermaid sequenceDiagram participant FE as Frontend (user JWT) participant AS as agent-service participant ACS as access-control-service (LLM gateway) participant LLM as LiteLLM FE->>AS: POST /agents<br/>Authorization: Bearer (user JWT) — required, else 401 FE->>AS: send message rect rgb(255, 235, 235) Note over AS,ACS: before: Authorization: Bearer dummy → 401 end AS->>ACS: POST /api/chat/completions<br/>Authorization: Bearer (user JWT) ✅ ACS->>LLM: forward with Authorization: Bearer (LITELLM_MASTER_KEY) LLM-->>AS: completion AS-->>FE: agent response ``` ### Any related issues, documentation, discussions? Closes apache#5604 ### How was this PR tested? Updated `server.test.ts`: agent creation sends a minted JWT in the `Authorization` header; added cases for a missing header, a non-Bearer scheme, and an invalid token (93 tests pass via `bun test`). Also verified end-to-end on a local Kubernetes deployment (this branch's chart + agent-service image, everything else from `ghcr.io/apache` nightlies): creation without the header → 401, with the header → 200, and a websocket message produced a real `gpt-5-mini` completion through access-control-service → LiteLLM. `typecheck` and `format:check` pass; the frontend change typechecks via `tsc --noEmit`. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Fable 5 (1M context) --------- Co-authored-by: Bob Bai <[email protected]>
What changes were proposed in this PR?
Since #5421, the access-control-service LLM gateway requires a REGULAR/ADMIN-role user JWT and injects
LITELLM_MASTER_KEYinto the downstream request itself, but the agent-service still authenticated with the staticLLM_API_KEYdefault ("dummy"), so every agent generation call returned 401 Unauthorized. This PR makes the delegating user's JWT the only credential the agent-service ever sends:POST /agents) now requires the user's JWT in theAuthorization: Bearerheader (the standard place for credentials, instead of auserTokenfield in the JSON payload); requests without it are rejected with 401, since an agent without a user could never call the gateway anyway. The frontend's JWT interceptor already attaches this header, soagent.service.tssimply stops duplicating the token in the request body.LLM_API_KEYenv var is removed from the service (env.ts), the helm chart (deployment env,llm-api-keysecret entry,llmApiKeyvalues), and the single-node.env. The master key now lives only in the access-control-service and LiteLLM.VALIDATION/PARSEerrors are mapped to 400 instead of falling through to a generic 500 (surfaced by Copilot's review comment on the schema-violation tests).sequenceDiagram participant FE as Frontend (user JWT) participant AS as agent-service participant ACS as access-control-service (LLM gateway) participant LLM as LiteLLM FE->>AS: POST /agents<br/>Authorization: Bearer (user JWT) — required, else 401 FE->>AS: send message rect rgb(255, 235, 235) Note over AS,ACS: before: Authorization: Bearer dummy → 401 end AS->>ACS: POST /api/chat/completions<br/>Authorization: Bearer (user JWT) ✅ ACS->>LLM: forward with Authorization: Bearer (LITELLM_MASTER_KEY) LLM-->>AS: completion AS-->>FE: agent responseAny related issues, documentation, discussions?
Closes #5604
How was this PR tested?
Updated
server.test.ts: agent creation sends a minted JWT in theAuthorizationheader; added cases for a missing header, a non-Bearer scheme, and an invalid token (93 tests pass viabun test). Also verified end-to-end on a local Kubernetes deployment (this branch's chart + agent-service image, everything else fromghcr.io/apachenightlies): creation without the header → 401, with the header → 200, and a websocket message produced a realgpt-5-minicompletion through access-control-service → LiteLLM.typecheckandformat:checkpass; the frontend change typechecks viatsc --noEmit.Was this PR authored or co-authored using generative AI tooling?
Generated-by: Claude Fable 5 (1M context)