Skip to content
Draft
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: 5 additions & 0 deletions agent-service/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ LOG_PRETTY=true
LLM_API_KEY=dummy
LLM_ENDPOINT=http://localhost:9096

# The secret the dashboard service signs login tokens with; every request's
# token is verified with it. Unset, it is auth.conf's default, as it is for
# the backend. Set it here only together with the backend's AUTH_JWT_SECRET.
# AUTH_JWT_SECRET=

# Texera backend services
TEXERA_DASHBOARD_SERVICE_ENDPOINT=http://localhost:8080
WORKFLOW_COMPILING_SERVICE_ENDPOINT=http://localhost:9090
Expand Down
12 changes: 10 additions & 2 deletions agent-service/src/agent/texera-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function makeAgent(): TexeraAgent {
modelType: "test-model",
agentId: "agent-1",
userToken: "user-token",
ownerUid: 1,
});
}

Expand Down Expand Up @@ -110,8 +111,14 @@ describe("TexeraAgent", () => {
test("falls back to a name derived from the id", () => {
expect(makeAgent().agentName).toBe("Agent-agent-1");
expect(
new TexeraAgent({ model: {} as any, modelType: "m", agentId: "a", agentName: "Named", userToken: "t" })
.agentName
new TexeraAgent({
model: {} as any,
modelType: "m",
agentId: "a",
agentName: "Named",
userToken: "t",
ownerUid: 1,
}).agentName
).toBe("Named");
});
});
Expand Down Expand Up @@ -403,6 +410,7 @@ function makeAgentWith(model: any): TexeraAgent {
agentId: "agent-1",
systemPrompt: "SYS-XYZ",
userToken: "user-token",
ownerUid: 1,
});
liveAgents.push(agent);
return agent;
Expand Down
4 changes: 4 additions & 0 deletions agent-service/src/agent/texera-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export interface TexeraAgentConfig {
systemPrompt?: string;
/** JWT of the user the agent acts for, sent on backend calls that are not tied to a workflow. */
userToken?: string;
/** uid of the user who created the agent, the only one it is listed for and answers to. */
ownerUid: number;
}

export interface AgentMessageResult {
Expand All @@ -89,6 +91,7 @@ export class TexeraAgent {
readonly agentName: string;
readonly modelType: string;
readonly createdAt: Date;
readonly ownerUid: number;

private state: AgentStateEnum = AgentStateEnum.AVAILABLE;
private workflowState: WorkflowState;
Expand Down Expand Up @@ -135,6 +138,7 @@ export class TexeraAgent {
this.agentName = config.agentName || `Agent-${config.agentId}`;
this.modelType = config.modelType;
this.createdAt = new Date();
this.ownerUid = config.ownerUid;
this.model = config.model;
this.userToken = config.userToken;
this.systemPrompt = config.systemPrompt || "";
Expand Down
48 changes: 42 additions & 6 deletions agent-service/src/api/auth-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@
*/

import { describe, expect, test } from "bun:test";
import { createHmac } from "node:crypto";
import { readFileSync } from "node:fs";
import { createAuthHeaders, extractBearerToken, extractUserFromToken, validateToken } from "./auth-api";
import { env, EnvSchema } from "../config/env";

function segment(value: Record<string, unknown>): string {
return Buffer.from(JSON.stringify(value), "utf-8").toString("base64url");
}

/**
* Builds a well-formed three-segment token carrying the given payload. All three segments are
* required — `decodeJWT` rejects anything that is not exactly three — but only the payload one is
* decoded, so the header and signature just have to be present.
* Builds a token carrying the given payload, signed the way the dashboard service signs one:
* HS256 over the first two segments, with the shared secret unless a test names another.
*/
function tokenWith(payload: Record<string, unknown>): string {
const encoded = Buffer.from(JSON.stringify(payload), "utf-8").toString("base64");
return `header.${encoded}.signature`;
function tokenWith(payload: Record<string, unknown>, secret: string = env.AUTH_JWT_SECRET): string {
const signed = `${segment({ alg: "HS256" })}.${segment(payload)}`;
return `${signed}.${createHmac("sha256", secret).update(signed).digest("base64url")}`;
}

/** `exp` is a UNIX second count, so expiries are built in seconds throughout. */
Expand Down Expand Up @@ -101,6 +107,36 @@ describe("validateToken", () => {
// and reject it. A far-future second count must still validate.
expect(validateToken(tokenWith({ exp: nowInSeconds() + 3600 }))).toBe(true);
});

test("rejects a token signed with another secret, whatever it claims", () => {
// Anyone can write these claims; only the dashboard service's secret can sign them. The
// agent service gives a token's user their agents, so an unsigned claim must count for nothing.
const forged = tokenWith({ userId: 1, sub: "texera", role: "ADMIN", exp: nowInSeconds() + 60 }, "not-the-secret");

expect(validateToken(forged)).toBe(false);
});

test("rejects a token whose claims were edited after it was signed", () => {
const [header, , signature] = tokenWith({ userId: 2, sub: "bob", exp: nowInSeconds() + 60 }).split(".");
const edited = segment({ userId: 1, sub: "bob", exp: nowInSeconds() + 60 });

expect(validateToken(`${header}.${edited}.${signature}`)).toBe(false);
});

test("rejects an unsigned token that declares no algorithm", () => {
// The check never reads the header's `alg`, so a token cannot ask for a weaker one.
const unsigned = `${segment({ alg: "none" })}.${segment({ userId: 1, sub: "texera", exp: nowInSeconds() + 60 })}.`;

expect(validateToken(unsigned)).toBe(false);
});

test("defaults to the secret auth.conf gives the dashboard service", () => {
// env.ts repeats auth.conf's default. If the two drift, a stack that sets neither (local
// development, single-node) would reject every login token.
const conf = readFileSync(new URL("../../../common/config/src/main/resources/auth.conf", import.meta.url), "utf-8");

expect(EnvSchema.parse({}).AUTH_JWT_SECRET).toBe(/256-bit-secret = "([^"]+)"/.exec(conf)![1]);
});
});

describe("extractBearerToken", () => {
Expand Down
19 changes: 18 additions & 1 deletion agent-service/src/api/auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

import { createHmac, timingSafeEqual } from "node:crypto";
import { env } from "../config/env";
import type { UserInfo } from "../types/agent";

export type { UserInfo } from "../types/agent";
Expand Down Expand Up @@ -53,8 +55,23 @@ function isTokenExpired(token: string): boolean {
}
}

// Whether the dashboard service issued this token: its signature is the HMAC-SHA256
// of the first two segments under the shared secret, as JwtAuth.scala writes it. The
// header's own `alg` is never consulted, so a token cannot choose a weaker check.
function isSignedByTexera(token: string): boolean {
const parts = token.split(".");
if (parts.length !== 3) {
return false;
}
const expected = createHmac("sha256", env.AUTH_JWT_SECRET).update(`${parts[0]}.${parts[1]}`).digest();
const actual = Buffer.from(parts[2], "base64url");
return actual.length === expected.length && timingSafeEqual(actual, expected);
}

// A token's claims can be trusted only when this holds: anyone can write the claims,
// only the dashboard service can sign them.
export function validateToken(token: string): boolean {
return !isTokenExpired(token);
return isSignedByTexera(token) && !isTokenExpired(token);
}

export function extractBearerToken(header: string | undefined): string | undefined {
Expand Down
8 changes: 7 additions & 1 deletion agent-service/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { z } from "zod";

const EnvSchema = z.object({
export const EnvSchema = z.object({
PORT: z.coerce.number().default(3001),
API_PREFIX: z.string().default("/api"),
TEXERA_SERVICE_LOG_LEVEL: z
Expand All @@ -33,6 +33,12 @@ const EnvSchema = z.object({
WORKFLOW_COMPILING_SERVICE_ENDPOINT: z.string().url().default("http://localhost:9090"),
WORKFLOW_EXECUTION_SERVICE_ENDPOINT: z.string().url().default("http://localhost:8085"),
EXECUTION_ENDPOINT_TEMPLATE: z.string().optional(),

// The secret the dashboard service signs login tokens with (HS256). The same
// variable overrides it there, and the default here is the one in
// common/config/src/main/resources/auth.conf, so the two agree unless a
// deployment sets it for one service and not the other.
AUTH_JWT_SECRET: z.string().min(1).default("8a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d"),
});

export const env = EnvSchema.parse(process.env);
109 changes: 98 additions & 11 deletions agent-service/src/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { beforeEach, describe, expect, spyOn, test } from "bun:test";
import { createHmac } from "node:crypto";
import { buildApp, start, _resetAgentStoreForTests, _getAgentForTests } from "./server";
import { WorkflowSystemMetadata } from "./agent/util/workflow-system-metadata";
import { env } from "./config/env";
Expand All @@ -26,27 +27,42 @@ import { ConsoleMessageType, OperatorState } from "./types/execution";
const API = env.API_PREFIX;
const app = buildApp();

function mintTestToken(): string {
// Signed as the dashboard service signs a login token, unless a test names another secret.
function mintTestToken(userId = 1, secret: string = env.AUTH_JWT_SECRET): string {
const header = Buffer.from(JSON.stringify({ alg: "HS256", typ: "JWT" })).toString("base64url");
const payload = Buffer.from(
JSON.stringify({
sub: "tester",
userId: 1,
userId,
email: "tester@example.com",
role: "REGULAR",
exp: Math.floor(Date.now() / 1000) + 3600,
})
).toString("base64url");
return `${header}.${payload}.test-signature`;
const signature = createHmac("sha256", secret).update(`${header}.${payload}`).digest("base64url");
return `${header}.${payload}.${signature}`;
}

const TOKEN = mintTestToken();
const OTHER_USER_TOKEN = mintTestToken(2);
// TOKEN's claims under a signature the dashboard service did not make.
const FORGED_TOKEN = mintTestToken(1, "not-the-secret");

function url(path: string): string {
return `http://localhost${path}`;
}

async function postJson(path: string, body: unknown, headers: Record<string, string> = {}): Promise<Response> {
// Every agent route needs a user, so the helpers below act as TOKEN's user
// unless a test names another token, or null for none.
function bearer(token: string | null): Record<string, string> {
return token ? { Authorization: `Bearer ${token}` } : {};
}

async function postJson(
path: string,
body: unknown,
headers: Record<string, string> = bearer(TOKEN)
): Promise<Response> {
return app.handle(
new Request(url(path), {
method: "POST",
Expand All @@ -57,25 +73,25 @@ async function postJson(path: string, body: unknown, headers: Record<string, str
}

async function createAgent(body: Record<string, unknown> = {}, token: string | null = TOKEN): Promise<Response> {
return postJson(`${API}/agents`, { modelType: "m", ...body }, token ? { Authorization: `Bearer ${token}` } : {});
return postJson(`${API}/agents`, { modelType: "m", ...body }, bearer(token));
}

async function patchJson(path: string, body: unknown): Promise<Response> {
async function patchJson(path: string, body: unknown, token: string | null = TOKEN): Promise<Response> {
return app.handle(
new Request(url(path), {
method: "PATCH",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", ...bearer(token) },
body: JSON.stringify(body),
})
);
}

async function getJson(path: string): Promise<Response> {
return app.handle(new Request(url(path)));
async function getJson(path: string, token: string | null = TOKEN): Promise<Response> {
return app.handle(new Request(url(path), { headers: bearer(token) }));
}

async function del(path: string): Promise<Response> {
return app.handle(new Request(url(path), { method: "DELETE" }));
async function del(path: string, token: string | null = TOKEN): Promise<Response> {
return app.handle(new Request(url(path), { method: "DELETE", headers: bearer(token) }));
}

async function readJson<T = unknown>(res: Response): Promise<T> {
Expand Down Expand Up @@ -167,6 +183,77 @@ describe(`GET ${API}/agents`, () => {
expect(body.agents).toHaveLength(2);
expect(body.agents.map(a => a.name).sort()).toEqual(["one", "two"]);
});

test("lists each user only their own agents", async () => {
await createAgent({ name: "mine" });
await createAgent({ name: "theirs" }, OTHER_USER_TOKEN);

const names = async (token: string) =>
(await readJson<{ agents: { name: string }[] }>(await getJson(`${API}/agents`, token))).agents.map(a => a.name);
expect(await names(TOKEN)).toEqual(["mine"]);
expect(await names(OTHER_USER_TOKEN)).toEqual(["theirs"]);
});

test("rejects a request without a token", async () => {
const res = await getJson(`${API}/agents`, null);
expect(res.status).toBe(401);
});

test("rejects a token the dashboard service did not sign, and lists it nothing", async () => {
await createAgent({ name: "mine" });

const res = await getJson(`${API}/agents`, FORGED_TOKEN);
expect(res.status).toBe(401);
expect(await readJson<unknown>(res)).toEqual({ error: "Invalid or expired token" });
});
});

// An agent answers only to the user who created it. For anyone else every route
// that names it gives the answer for an agent that does not exist.
describe("an agent created by another user", () => {
const ROUTES: [method: string, path: string, body?: unknown][] = [
["GET", ""],
["DELETE", ""],
["GET", "/react-steps"],
["GET", "/operator-results"],
["POST", "/steps-by-operators", { operatorIds: [] }],
["GET", "/system-info"],
["POST", "/stop", {}],
["POST", "/clear", {}],
["GET", "/operator-types"],
["GET", "/settings"],
["PATCH", "/settings", { maxSteps: 3 }],
];

function request(method: string, path: string, body: unknown, token: string | null): Promise<Response> {
return app.handle(
new Request(url(path), {
method,
headers: { ...(body === undefined ? {} : { "Content-Type": "application/json" }), ...bearer(token) },
body: body === undefined ? undefined : JSON.stringify(body),
})
);
}

test.each(ROUTES)("%s /:id%s is not found, and needs a signed token", async (method, path, body) => {
const id = (await readJson<{ id: string }>(await createAgent())).id;
const settingsBefore = _getAgentForTests(id)!.getSettings().maxSteps;

const asOther = await request(method, `${API}/agents/${id}${path}`, body, OTHER_USER_TOKEN);
expect(asOther.status).toBe(404);
expect((await readJson<{ error: string }>(asOther)).error).toBe("Agent not found");

const withoutToken = await request(method, `${API}/agents/${id}${path}`, body, null);
expect(withoutToken.status).toBe(401);

// The owner's own claims, forged: the user id in a token counts only once it is signed.
const forged = await request(method, `${API}/agents/${id}${path}`, body, FORGED_TOKEN);
expect(forged.status).toBe(401);

// None of the calls reached the agent: it is still there for its owner, unchanged.
expect((await getJson(`${API}/agents/${id}`)).status).toBe(200);
expect(_getAgentForTests(id)!.getSettings().maxSteps).toBe(settingsBefore);
});
});

describe(`GET ${API}/agents/:id`, () => {
Expand Down
Loading
Loading