Skip to content

fix(agent-service): authenticate to the LLM proxy as the delegating user - #5605

Merged
bobbai00 merged 7 commits into
apache:mainfrom
bobbai00:fix/agent-service-llm-auth
Jun 11, 2026
Merged

bobbai00 merged 7 commits into
apache:mainfrom
bobbai00:fix/agent-service-llm-auth

Conversation

@bobbai00

@bobbai00 bobbai00 commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

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).
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
Loading

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)

@bobbai00

Copy link
Copy Markdown
Contributor Author

@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.
@bobbai00
bobbai00 force-pushed the fix/agent-service-llm-auth branch from 205cefa to 6750b40 Compare June 10, 2026 09:31
@github-actions github-actions Bot added the infra label Jun 10, 2026
@codecov-commenter

codecov-commenter commented Jun 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.19%. Comparing base (539a685) to head (cf5070c).

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     
Flag Coverage Δ *Carryforward flag
access-control-service 44.70% <ø> (-19.91%) ⬇️ Carriedforward from ce91924
agent-service 34.36% <100.00%> (+0.59%) ⬆️
amber 52.84% <ø> (-0.51%) ⬇️ Carriedforward from ce91924
computing-unit-managing-service 1.65% <ø> (ø) Carriedforward from ce91924
config-service 55.38% <ø> (-1.34%) ⬇️ Carriedforward from ce91924
file-service 38.32% <ø> (+0.10%) ⬆️ Carriedforward from ce91924
frontend 47.18% <100.00%> (+0.14%) ⬆️
pyamber 90.61% <ø> (-0.11%) ⬇️ Carriedforward from ce91924
python 90.75% <ø> (ø) Carriedforward from ce91924
workflow-compiling-service 58.69% <ø> (ø) Carriedforward from ce91924

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xuang7 xuang7 left a comment

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.

Backport LGTM!

@xuang7 xuang7 added the release/v1.2 back porting to release/v1.2 label Jun 10, 2026
@aglinxinyuan
aglinxinyuan requested a review from Copilot June 10, 2026 21:18

Copilot AI left a comment

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.

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 userToken when creating agents and use it as the OpenAI client credential when calling the LLM gateway.
  • Remove LLM_API_KEY configuration 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 userToken requirements.

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 .onError handler unconditionally sets set.status = 500 for any unrecognized error. In Elysia, request body validation errors (e.g., missing required userToken) can be routed through .onError, and this handler will then turn a client-side 4xx into a 500 response. Handle validation errors explicitly (via the code field) 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.

Comment thread agent-service/src/server.test.ts Resolved
bobbai00 and others added 2 commits June 10, 2026 14:32
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.
@bobbai00
bobbai00 requested a review from xuang7 June 10, 2026 22:14

@xuang7 xuang7 left a comment

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.

LGTM!

@Yicong-Huang

Copy link
Copy Markdown
Contributor

Please allow me some time to check on this tonight

Comment thread agent-service/src/types/agent.ts Resolved
…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.
@github-actions github-actions Bot added the frontend Changes related to the frontend GUI label Jun 11, 2026
@bobbai00
bobbai00 requested a review from Yicong-Huang June 11, 2026 03:17

@Yicong-Huang Yicong-Huang left a comment

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.

LGTM

@Yicong-Huang

Copy link
Copy Markdown
Contributor

@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.
@Yicong-Huang

Copy link
Copy Markdown
Contributor

@bobbai00 you may want to check your git configuration. this commit is not made with your GitHub account 565c688

@bobbai00

Copy link
Copy Markdown
Contributor Author

@bobbai00 you may want to check your git configuration. this commit is not made with your GitHub account 565c688

OK. I fixed it locally. Does that block this PR ? If not I will merge it.

@Yicong-Huang

Copy link
Copy Markdown
Contributor

it's nit. please go ahead to merge

@bobbai00
bobbai00 enabled auto-merge June 11, 2026 06:24
@bobbai00
bobbai00 added this pull request to the merge queue Jun 11, 2026
Merged via the queue into apache:main with commit ae3128d Jun 11, 2026
26 checks passed
@bobbai00
bobbai00 deleted the fix/agent-service-llm-auth branch June 11, 2026 06:43
@github-actions

Copy link
Copy Markdown
Contributor

Backport to release/v1.2 succeeded as bad1ae8. Run

Yicong-Huang pushed a commit that referenced this pull request Jun 11, 2026
…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]>
ELin2025 pushed a commit to ELin2025/texera that referenced this pull request Jun 16, 2026
…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]>
yangzhang75 pushed a commit to yangzhang75/texera that referenced this pull request Jun 22, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-service fix frontend Changes related to the frontend GUI infra release/v1.2 back porting to release/v1.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agent-service cannot authenticate to the LiteLLM proxy after #5421 — every agent LLM call returns 401

5 participants