@app-vitals/agent-admin 1.115.2 → 1.116.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/openapi.platform.json +22 -0
- package/package.json +2 -2
- package/prisma/client/internal/class.ts +3 -3
- package/prisma/client/internal/prismaNamespace.ts +1 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -0
- package/prisma/client/models/AgentToken.ts +36 -1
- package/prisma/migrations/20261202000000_add_agent_token_kind/migration.sql +4 -0
- package/prisma/schema/schema.prisma +5 -0
- package/src/admin-local-agent.smoke.test.ts +1 -0
- package/src/admin-ui-pages.ts +8 -1
- package/src/admin-ui-pages.unit.test.ts +10 -0
- package/src/admin-ui.fixtures.ts +1 -0
- package/src/agent-cron-run-stats.smoke.test.ts +6 -4
- package/src/agent-tokens.integration.test.ts +23 -0
- package/src/agent-tokens.ts +39 -5
- package/src/agent-tokens.unit.test.ts +127 -0
- package/src/agents-api.sentry.smoke.test.ts +3 -1
- package/src/agents-api.smoke.test.ts +111 -10
- package/src/agents-api.ts +4 -1
- package/src/api-auth.ts +11 -2
- package/src/api-auth.unit.test.ts +82 -12
- package/src/api.smoke.test.ts +2 -0
- package/src/chat.smoke.test.ts +1 -0
- package/src/openapi-schemas.ts +11 -0
- package/src/openapi-schemas.unit.test.ts +1 -0
|
@@ -232,7 +232,9 @@ function makeBaseDeps(
|
|
|
232
232
|
listForAgent: async () => [],
|
|
233
233
|
revoke: async () => ({}) as never,
|
|
234
234
|
validate: async (raw: string) =>
|
|
235
|
-
raw === VALID_BEARER_TOKEN
|
|
235
|
+
raw === VALID_BEARER_TOKEN
|
|
236
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
237
|
+
: null,
|
|
236
238
|
},
|
|
237
239
|
agentPluginService: {
|
|
238
240
|
list: async () => [],
|
|
@@ -517,6 +517,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
517
517
|
id: TOKEN_ID,
|
|
518
518
|
agentId: AGENT_ID,
|
|
519
519
|
token: "sha256hash",
|
|
520
|
+
kind: "runtime" as const,
|
|
520
521
|
label: "test token",
|
|
521
522
|
createdAt: new Date("2024-01-01"),
|
|
522
523
|
revokedAt: null,
|
|
@@ -529,6 +530,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
529
530
|
id: TOKEN_ID,
|
|
530
531
|
agentId: AGENT_ID,
|
|
531
532
|
token: "sha256hashvalue",
|
|
533
|
+
kind: "runtime" as const,
|
|
532
534
|
label: "test token",
|
|
533
535
|
createdAt: new Date("2024-01-01"),
|
|
534
536
|
revokedAt: null,
|
|
@@ -538,11 +540,12 @@ function makeMockDeps(): AdminDeps {
|
|
|
538
540
|
id: TOKEN_ID,
|
|
539
541
|
agentId: AGENT_ID,
|
|
540
542
|
token: "sha256hashvalue",
|
|
543
|
+
kind: "runtime" as const,
|
|
541
544
|
label: "test token",
|
|
542
545
|
createdAt: new Date("2024-01-01"),
|
|
543
546
|
revokedAt: new Date("2024-01-02"),
|
|
544
547
|
}),
|
|
545
|
-
validate: async () => ({ agentId: AGENT_ID }),
|
|
548
|
+
validate: async () => ({ agentId: AGENT_ID, kind: "runtime" as const }),
|
|
546
549
|
},
|
|
547
550
|
agentPluginService: {
|
|
548
551
|
list: async () => [
|
|
@@ -1392,7 +1395,10 @@ describe("admin API — GitHub installations snapshot", () => {
|
|
|
1392
1395
|
|
|
1393
1396
|
it("the agent's own bearer token is accepted (200)", async () => {
|
|
1394
1397
|
const app = createAdminApp(
|
|
1395
|
-
makeDepsWithTokenValidation(async () => ({
|
|
1398
|
+
makeDepsWithTokenValidation(async () => ({
|
|
1399
|
+
agentId: AGENT_ID,
|
|
1400
|
+
kind: "runtime" as const,
|
|
1401
|
+
})),
|
|
1396
1402
|
);
|
|
1397
1403
|
const res = await put(app, BODY, {
|
|
1398
1404
|
Cookie: "",
|
|
@@ -1403,7 +1409,10 @@ describe("admin API — GitHub installations snapshot", () => {
|
|
|
1403
1409
|
|
|
1404
1410
|
it("a bearer token owned by a different agent is rejected (403)", async () => {
|
|
1405
1411
|
const app = createAdminApp(
|
|
1406
|
-
makeDepsWithTokenValidation(async () => ({
|
|
1412
|
+
makeDepsWithTokenValidation(async () => ({
|
|
1413
|
+
agentId: "other-agent",
|
|
1414
|
+
kind: "runtime" as const,
|
|
1415
|
+
})),
|
|
1407
1416
|
);
|
|
1408
1417
|
const putRes = await put(app, BODY, {
|
|
1409
1418
|
Cookie: "",
|
|
@@ -1512,6 +1521,78 @@ describe("admin API — tokens", () => {
|
|
|
1512
1521
|
expect(body.token).toBeDefined();
|
|
1513
1522
|
});
|
|
1514
1523
|
|
|
1524
|
+
it("POST /agents/:id/tokens with kind tools: list shows the kind", async () => {
|
|
1525
|
+
type Row = {
|
|
1526
|
+
id: string;
|
|
1527
|
+
agentId: string;
|
|
1528
|
+
token: string;
|
|
1529
|
+
label: string | null;
|
|
1530
|
+
kind: "runtime" | "tools";
|
|
1531
|
+
createdAt: Date;
|
|
1532
|
+
revokedAt: Date | null;
|
|
1533
|
+
};
|
|
1534
|
+
const rows: Row[] = [];
|
|
1535
|
+
const base = makeMockDeps();
|
|
1536
|
+
const deps: AdminDeps = {
|
|
1537
|
+
...base,
|
|
1538
|
+
agentTokenService: {
|
|
1539
|
+
...base.agentTokenService,
|
|
1540
|
+
create: async (agentId, label, kind = "runtime") => {
|
|
1541
|
+
const token: Row = {
|
|
1542
|
+
id: `tok-${rows.length + 1}`,
|
|
1543
|
+
agentId,
|
|
1544
|
+
token: "hash",
|
|
1545
|
+
label: label ?? null,
|
|
1546
|
+
kind,
|
|
1547
|
+
createdAt: new Date("2024-01-01"),
|
|
1548
|
+
revokedAt: null,
|
|
1549
|
+
};
|
|
1550
|
+
rows.push(token);
|
|
1551
|
+
return { token, rawToken: "raw" };
|
|
1552
|
+
},
|
|
1553
|
+
listForAgent: async () => rows,
|
|
1554
|
+
},
|
|
1555
|
+
};
|
|
1556
|
+
const app = createAdminApp(deps);
|
|
1557
|
+
const headers = {
|
|
1558
|
+
"Content-Type": "application/json",
|
|
1559
|
+
Cookie: `admin_session=${cookie}`,
|
|
1560
|
+
};
|
|
1561
|
+
const created = await app.request(`/agents/${AGENT_ID}/tokens`, {
|
|
1562
|
+
method: "POST",
|
|
1563
|
+
body: JSON.stringify({ label: "tools", kind: "tools" }),
|
|
1564
|
+
headers,
|
|
1565
|
+
});
|
|
1566
|
+
expect(created.status).toBe(201);
|
|
1567
|
+
expect((await created.json()).token.kind).toBe("tools");
|
|
1568
|
+
// Omitted kind defaults to runtime.
|
|
1569
|
+
await app.request(`/agents/${AGENT_ID}/tokens`, {
|
|
1570
|
+
method: "POST",
|
|
1571
|
+
body: JSON.stringify({ label: "default" }),
|
|
1572
|
+
headers,
|
|
1573
|
+
});
|
|
1574
|
+
const list = await app.request(`/agents/${AGENT_ID}/tokens`, { headers });
|
|
1575
|
+
expect(list.status).toBe(200);
|
|
1576
|
+
const { tokens } = await list.json();
|
|
1577
|
+
expect(tokens.map((t: { kind: string }) => t.kind)).toEqual([
|
|
1578
|
+
"tools",
|
|
1579
|
+
"runtime",
|
|
1580
|
+
]);
|
|
1581
|
+
});
|
|
1582
|
+
|
|
1583
|
+
it("POST /agents/:id/tokens rejects an invalid kind with 400", async () => {
|
|
1584
|
+
const app = createAdminApp(makeMockDeps());
|
|
1585
|
+
const res = await app.request(`/agents/${AGENT_ID}/tokens`, {
|
|
1586
|
+
method: "POST",
|
|
1587
|
+
body: JSON.stringify({ kind: "admin" }),
|
|
1588
|
+
headers: {
|
|
1589
|
+
"Content-Type": "application/json",
|
|
1590
|
+
Cookie: `admin_session=${cookie}`,
|
|
1591
|
+
},
|
|
1592
|
+
});
|
|
1593
|
+
expect(res.status).toBe(400);
|
|
1594
|
+
});
|
|
1595
|
+
|
|
1515
1596
|
it("GET /agents/:id/tokens returns list WITHOUT rawToken", async () => {
|
|
1516
1597
|
const app = createAdminApp(makeMockDeps());
|
|
1517
1598
|
const res = await app.request(`/agents/${AGENT_ID}/tokens`, {
|
|
@@ -1862,7 +1943,7 @@ describe("admin API — POST /agents", () => {
|
|
|
1862
1943
|
...makeMockDeps(),
|
|
1863
1944
|
agentTokenService: {
|
|
1864
1945
|
...makeMockDeps().agentTokenService,
|
|
1865
|
-
validate: async () => ({ agentId: AGENT_ID }),
|
|
1946
|
+
validate: async () => ({ agentId: AGENT_ID, kind: "runtime" as const }),
|
|
1866
1947
|
},
|
|
1867
1948
|
};
|
|
1868
1949
|
const app = createAdminApp(deps);
|
|
@@ -2292,7 +2373,10 @@ describe("admin API — delete agent", () => {
|
|
|
2292
2373
|
it("DELETE /agents/:id with per-agent bearer → 403", async () => {
|
|
2293
2374
|
const provisioner = new RecordingProvisioner();
|
|
2294
2375
|
const deps: AdminDeps = {
|
|
2295
|
-
...makeDepsWithTokenValidation(async () => ({
|
|
2376
|
+
...makeDepsWithTokenValidation(async () => ({
|
|
2377
|
+
agentId: AGENT_ID,
|
|
2378
|
+
kind: "runtime" as const,
|
|
2379
|
+
})),
|
|
2296
2380
|
provisioner,
|
|
2297
2381
|
};
|
|
2298
2382
|
const app = createAdminApp(deps);
|
|
@@ -2317,6 +2401,7 @@ describe("admin API — POST /agents/reconcile", () => {
|
|
|
2317
2401
|
it("POST /agents/reconcile with per-agent bearer → 403", async () => {
|
|
2318
2402
|
const deps = makeDepsWithTokenValidation(async () => ({
|
|
2319
2403
|
agentId: AGENT_ID,
|
|
2404
|
+
kind: "runtime" as const,
|
|
2320
2405
|
}));
|
|
2321
2406
|
const app = createAdminApp(deps);
|
|
2322
2407
|
const res = await app.request("/agents/reconcile", {
|
|
@@ -2548,7 +2633,10 @@ describe("admin API — POST /agents/federation/strip-credentials", () => {
|
|
|
2548
2633
|
...deps,
|
|
2549
2634
|
agentTokenService: {
|
|
2550
2635
|
...deps.agentTokenService,
|
|
2551
|
-
validate: async () => ({
|
|
2636
|
+
validate: async () => ({
|
|
2637
|
+
agentId: "managed-a",
|
|
2638
|
+
kind: "runtime" as const,
|
|
2639
|
+
}),
|
|
2552
2640
|
},
|
|
2553
2641
|
});
|
|
2554
2642
|
const res = await app.request(
|
|
@@ -2570,7 +2658,10 @@ describe("admin API — POST /agents/federation/strip-credentials", () => {
|
|
|
2570
2658
|
...deps,
|
|
2571
2659
|
agentTokenService: {
|
|
2572
2660
|
...deps.agentTokenService,
|
|
2573
|
-
validate: async () => ({
|
|
2661
|
+
validate: async () => ({
|
|
2662
|
+
agentId: "federation",
|
|
2663
|
+
kind: "runtime" as const,
|
|
2664
|
+
}),
|
|
2574
2665
|
},
|
|
2575
2666
|
});
|
|
2576
2667
|
const res = await app.request(
|
|
@@ -2658,6 +2749,7 @@ describe("admin API — bearer token auth", () => {
|
|
|
2658
2749
|
it("GET /agents/:id/envs accepts a valid bearer token (200)", async () => {
|
|
2659
2750
|
const deps = makeDepsWithTokenValidation(async () => ({
|
|
2660
2751
|
agentId: AGENT_ID,
|
|
2752
|
+
kind: "runtime" as const,
|
|
2661
2753
|
}));
|
|
2662
2754
|
const app = createAdminApp(deps);
|
|
2663
2755
|
const res = await app.request(`/agents/${AGENT_ID}/envs`, {
|
|
@@ -2669,6 +2761,7 @@ describe("admin API — bearer token auth", () => {
|
|
|
2669
2761
|
it("GET /agents/:id/tools accepts a valid bearer token (200)", async () => {
|
|
2670
2762
|
const deps = makeDepsWithTokenValidation(async () => ({
|
|
2671
2763
|
agentId: AGENT_ID,
|
|
2764
|
+
kind: "runtime" as const,
|
|
2672
2765
|
}));
|
|
2673
2766
|
const app = createAdminApp(deps);
|
|
2674
2767
|
const res = await app.request(`/agents/${AGENT_ID}/tools`, {
|
|
@@ -2747,7 +2840,10 @@ describe("admin API — provision agent", () => {
|
|
|
2747
2840
|
it("POST /agents/:id/provision with per-agent bearer → 403 (no provision call)", async () => {
|
|
2748
2841
|
const provisioner = new RecordingProvisioner();
|
|
2749
2842
|
const deps: AdminDeps = {
|
|
2750
|
-
...makeDepsWithTokenValidation(async () => ({
|
|
2843
|
+
...makeDepsWithTokenValidation(async () => ({
|
|
2844
|
+
agentId: AGENT_ID,
|
|
2845
|
+
kind: "runtime" as const,
|
|
2846
|
+
})),
|
|
2751
2847
|
provisioner,
|
|
2752
2848
|
};
|
|
2753
2849
|
const app = createAdminApp(deps);
|
|
@@ -7028,6 +7124,7 @@ describe("admin API — baked marketplaces report", () => {
|
|
|
7028
7124
|
let baked: string[] = [];
|
|
7029
7125
|
const base = makeDepsWithTokenValidation(async () => ({
|
|
7030
7126
|
agentId: tokenAgentId,
|
|
7127
|
+
kind: "runtime" as const,
|
|
7031
7128
|
}));
|
|
7032
7129
|
const deps: AdminDeps = {
|
|
7033
7130
|
...base,
|
|
@@ -7144,7 +7241,9 @@ describe("admin API — PATCH /agents/:id githubUsername (GHE-2.1)", () => {
|
|
|
7144
7241
|
agentTokenService: {
|
|
7145
7242
|
...base.agentTokenService,
|
|
7146
7243
|
validate: async (t: string) =>
|
|
7147
|
-
t === AGENT_KEY
|
|
7244
|
+
t === AGENT_KEY
|
|
7245
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
7246
|
+
: null,
|
|
7148
7247
|
},
|
|
7149
7248
|
agentService: {
|
|
7150
7249
|
...base.agentService,
|
|
@@ -7535,7 +7634,9 @@ describe("admin API — agent App installation repos and GitHub Sync (IRM-1.2, I
|
|
|
7535
7634
|
agentTokenService: {
|
|
7536
7635
|
...base.agentTokenService,
|
|
7537
7636
|
validate: async (t: string) =>
|
|
7538
|
-
t === AGENT_KEY
|
|
7637
|
+
t === AGENT_KEY
|
|
7638
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
7639
|
+
: null,
|
|
7539
7640
|
},
|
|
7540
7641
|
githubInstallationRepos,
|
|
7541
7642
|
githubSync,
|
package/src/agents-api.ts
CHANGED
|
@@ -70,7 +70,7 @@ import {
|
|
|
70
70
|
} from "./agent-provisioner.ts";
|
|
71
71
|
import { AGENT_TEMPLATE_ITEM_KINDS } from "./agent-template-items.ts";
|
|
72
72
|
import type { AgentTemplateReconcileService } from "./agent-template-reconcile.ts";
|
|
73
|
-
import type
|
|
73
|
+
import { type AgentTokenService, agentTokenKindOf } from "./agent-tokens.ts";
|
|
74
74
|
import type { AgentToolService } from "./agent-tools.ts";
|
|
75
75
|
import type { AgentService } from "./agents.ts";
|
|
76
76
|
import {
|
|
@@ -3096,6 +3096,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3096
3096
|
const { token, rawToken } = await agentTokenService.create(
|
|
3097
3097
|
agentId,
|
|
3098
3098
|
body?.label,
|
|
3099
|
+
body?.kind,
|
|
3099
3100
|
);
|
|
3100
3101
|
// Return the token record (without the hashed token value) + rawToken
|
|
3101
3102
|
const { token: _hash, ...tokenMeta } = token;
|
|
@@ -3103,6 +3104,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3103
3104
|
{
|
|
3104
3105
|
token: {
|
|
3105
3106
|
...tokenMeta,
|
|
3107
|
+
kind: agentTokenKindOf(tokenMeta.kind),
|
|
3106
3108
|
createdAt: tokenMeta.createdAt.toISOString(),
|
|
3107
3109
|
revokedAt: tokenMeta.revokedAt
|
|
3108
3110
|
? tokenMeta.revokedAt.toISOString()
|
|
@@ -3121,6 +3123,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3121
3123
|
// Never expose the stored hash — return only metadata
|
|
3122
3124
|
const tokens = records.map(({ token: _hash, ...meta }) => ({
|
|
3123
3125
|
...meta,
|
|
3126
|
+
kind: agentTokenKindOf(meta.kind),
|
|
3124
3127
|
createdAt: meta.createdAt.toISOString(),
|
|
3125
3128
|
revokedAt: meta.revokedAt ? meta.revokedAt.toISOString() : null,
|
|
3126
3129
|
}));
|
package/src/api-auth.ts
CHANGED
|
@@ -13,7 +13,7 @@ import type { MiddlewareHandler } from "hono";
|
|
|
13
13
|
import { getCookie } from "hono/cookie";
|
|
14
14
|
import { verify } from "hono/jwt";
|
|
15
15
|
import type { AgentMemberService } from "./agent-members.ts";
|
|
16
|
-
import type { AgentTokenService } from "./agent-tokens.ts";
|
|
16
|
+
import type { AgentTokenKind, AgentTokenService } from "./agent-tokens.ts";
|
|
17
17
|
import {
|
|
18
18
|
type CallerScopeResolver,
|
|
19
19
|
memberOnlyCallerScopeResolver,
|
|
@@ -39,6 +39,11 @@ export type AdminScopeKind =
|
|
|
39
39
|
export interface AdminScope {
|
|
40
40
|
kind: AdminScopeKind;
|
|
41
41
|
agentId?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Kind of the DB agent token that authenticated the request. Optional and
|
|
44
|
+
* additive: unset for env-key and session callers, which are never tools-kind.
|
|
45
|
+
*/
|
|
46
|
+
tokenKind?: AgentTokenKind;
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
export type AdminAuthEnv = {
|
|
@@ -371,7 +376,11 @@ export function createAdminAuthMiddleware(deps: {
|
|
|
371
376
|
// DB token path — per-agent bearer, not an admin.
|
|
372
377
|
c.set("isAdmin", false);
|
|
373
378
|
c.set("caller", { name: result.agentId, scope: result.agentId });
|
|
374
|
-
c.set("scope", {
|
|
379
|
+
c.set("scope", {
|
|
380
|
+
kind: "agent",
|
|
381
|
+
agentId: result.agentId,
|
|
382
|
+
tokenKind: result.kind,
|
|
383
|
+
});
|
|
375
384
|
return next();
|
|
376
385
|
}
|
|
377
386
|
|
|
@@ -11,7 +11,7 @@ import type { Caller } from "@app-vitals/agent-lib/request-context";
|
|
|
11
11
|
import { Hono } from "hono";
|
|
12
12
|
import { sign } from "hono/jwt";
|
|
13
13
|
import type { AgentTokenService, AgentTokenValidated } from "./agent-tokens.ts";
|
|
14
|
-
import type { AdminAuthEnv } from "./api-auth.ts";
|
|
14
|
+
import type { AdminAuthEnv, AdminScope } from "./api-auth.ts";
|
|
15
15
|
import {
|
|
16
16
|
AGENT_CROSS_ROUTES,
|
|
17
17
|
createAdminAuthMiddleware,
|
|
@@ -133,7 +133,9 @@ describe("createAdminAuthMiddleware — session cookie", () => {
|
|
|
133
133
|
describe("createAdminAuthMiddleware — bearer token", () => {
|
|
134
134
|
it("passes when a valid bearer token is provided", async () => {
|
|
135
135
|
const app = buildApp(async (raw) =>
|
|
136
|
-
raw === VALID_RAW_TOKEN
|
|
136
|
+
raw === VALID_RAW_TOKEN
|
|
137
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
138
|
+
: null,
|
|
137
139
|
);
|
|
138
140
|
const res = await app.request("/test", {
|
|
139
141
|
headers: { Authorization: `Bearer ${VALID_RAW_TOKEN}` },
|
|
@@ -143,6 +145,58 @@ describe("createAdminAuthMiddleware — bearer token", () => {
|
|
|
143
145
|
expect(body.ok).toBe(true);
|
|
144
146
|
});
|
|
145
147
|
|
|
148
|
+
it("exposes the token kind on the caller scope", async () => {
|
|
149
|
+
for (const kind of ["runtime", "tools"] as const) {
|
|
150
|
+
let seen: AdminScope | undefined;
|
|
151
|
+
const mock: Pick<AgentTokenService, "validate"> = {
|
|
152
|
+
validate: async () => ({ agentId: AGENT_ID, kind }),
|
|
153
|
+
};
|
|
154
|
+
const app = new Hono<AdminAuthEnv>();
|
|
155
|
+
app.use(
|
|
156
|
+
"*",
|
|
157
|
+
createAdminAuthMiddleware({
|
|
158
|
+
sessionSecret: SESSION_SECRET,
|
|
159
|
+
agentTokenService: mock,
|
|
160
|
+
}),
|
|
161
|
+
);
|
|
162
|
+
app.get("/test", (c) => {
|
|
163
|
+
seen = c.get("scope");
|
|
164
|
+
return c.json({ ok: true });
|
|
165
|
+
});
|
|
166
|
+
const res = await app.request("/test", {
|
|
167
|
+
headers: { Authorization: `Bearer ${VALID_RAW_TOKEN}` },
|
|
168
|
+
});
|
|
169
|
+
expect(res.status).toBe(200);
|
|
170
|
+
expect(seen).toEqual({
|
|
171
|
+
kind: "agent",
|
|
172
|
+
agentId: AGENT_ID,
|
|
173
|
+
tokenKind: kind,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("does not set tokenKind for env-key callers", async () => {
|
|
179
|
+
let seen: AdminScope | undefined;
|
|
180
|
+
const app = new Hono<AdminAuthEnv>();
|
|
181
|
+
app.use(
|
|
182
|
+
"*",
|
|
183
|
+
createAdminAuthMiddleware({
|
|
184
|
+
sessionSecret: SESSION_SECRET,
|
|
185
|
+
agentTokenService: { validate: async () => null },
|
|
186
|
+
adminApiKeys: new Map([["env-key", { name: "env", scope: AGENT_ID }]]),
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
app.get("/test", (c) => {
|
|
190
|
+
seen = c.get("scope");
|
|
191
|
+
return c.json({ ok: true });
|
|
192
|
+
});
|
|
193
|
+
const res = await app.request("/test", {
|
|
194
|
+
headers: { Authorization: "Bearer env-key" },
|
|
195
|
+
});
|
|
196
|
+
expect(res.status).toBe(200);
|
|
197
|
+
expect(seen?.tokenKind).toBeUndefined();
|
|
198
|
+
});
|
|
199
|
+
|
|
146
200
|
it("returns 401 when bearer token is invalid (validate returns null)", async () => {
|
|
147
201
|
const app = buildApp(async () => null);
|
|
148
202
|
const res = await app.request("/test", {
|
|
@@ -209,7 +263,9 @@ describe("createAdminAuthMiddleware — bearer token", () => {
|
|
|
209
263
|
describe("createAdminAuthMiddleware — bearer token scope enforcement", () => {
|
|
210
264
|
it("passes when token agentId matches the :id route param", async () => {
|
|
211
265
|
const app = buildApp(async (raw) =>
|
|
212
|
-
raw === VALID_RAW_TOKEN
|
|
266
|
+
raw === VALID_RAW_TOKEN
|
|
267
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
268
|
+
: null,
|
|
213
269
|
);
|
|
214
270
|
const res = await app.request(`/agents/${AGENT_ID}/envs`, {
|
|
215
271
|
headers: { Authorization: `Bearer ${VALID_RAW_TOKEN}` },
|
|
@@ -221,7 +277,9 @@ describe("createAdminAuthMiddleware — bearer token scope enforcement", () => {
|
|
|
221
277
|
|
|
222
278
|
it("returns 403 when token agentId does not match the :id route param", async () => {
|
|
223
279
|
const app = buildApp(async (raw) =>
|
|
224
|
-
raw === VALID_RAW_TOKEN
|
|
280
|
+
raw === VALID_RAW_TOKEN
|
|
281
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
282
|
+
: null,
|
|
225
283
|
);
|
|
226
284
|
const res = await app.request("/agents/agent-different-id/envs", {
|
|
227
285
|
headers: { Authorization: `Bearer ${VALID_RAW_TOKEN}` },
|
|
@@ -234,7 +292,9 @@ describe("createAdminAuthMiddleware — bearer token scope enforcement", () => {
|
|
|
234
292
|
it("passes for routes without an :id param (no scope to enforce)", async () => {
|
|
235
293
|
// Unscoped route — token is valid, no :id to compare against
|
|
236
294
|
const app = buildApp(async (raw) =>
|
|
237
|
-
raw === VALID_RAW_TOKEN
|
|
295
|
+
raw === VALID_RAW_TOKEN
|
|
296
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
297
|
+
: null,
|
|
238
298
|
);
|
|
239
299
|
const res = await app.request("/test", {
|
|
240
300
|
headers: { Authorization: `Bearer ${VALID_RAW_TOKEN}` },
|
|
@@ -372,7 +432,9 @@ describe("createAdminAuthMiddleware — admin API keys", () => {
|
|
|
372
432
|
let validateCalled = false;
|
|
373
433
|
const app = buildApp(async (raw) => {
|
|
374
434
|
validateCalled = true;
|
|
375
|
-
return raw === VALID_RAW_TOKEN
|
|
435
|
+
return raw === VALID_RAW_TOKEN
|
|
436
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
437
|
+
: null;
|
|
376
438
|
});
|
|
377
439
|
|
|
378
440
|
const res = await app.request("/test", {
|
|
@@ -416,7 +478,9 @@ describe("createAdminAuthMiddleware — shared Caller", () => {
|
|
|
416
478
|
|
|
417
479
|
it("sets caller = {name: agentId, scope: agentId} for a DB agent token", async () => {
|
|
418
480
|
const app = buildApp(async (raw) =>
|
|
419
|
-
raw === VALID_RAW_TOKEN
|
|
481
|
+
raw === VALID_RAW_TOKEN
|
|
482
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
483
|
+
: null,
|
|
420
484
|
);
|
|
421
485
|
|
|
422
486
|
const res = await app.request("/test", {
|
|
@@ -893,7 +957,9 @@ describe("createAdminAuthMiddleware — agent cross routes", () => {
|
|
|
893
957
|
sessionSecret: SESSION_SECRET,
|
|
894
958
|
agentTokenService: {
|
|
895
959
|
validate: async (raw) =>
|
|
896
|
-
raw === VALID_RAW_TOKEN
|
|
960
|
+
raw === VALID_RAW_TOKEN
|
|
961
|
+
? { agentId: AGENT_ID, kind: "runtime" as const }
|
|
962
|
+
: null,
|
|
897
963
|
},
|
|
898
964
|
adminApiKeys: new Map([
|
|
899
965
|
[SCOPED_TOKEN, { name: "svc", scope: AGENT_ID }],
|
|
@@ -911,9 +977,13 @@ describe("createAdminAuthMiddleware — agent cross routes", () => {
|
|
|
911
977
|
]);
|
|
912
978
|
});
|
|
913
979
|
|
|
914
|
-
for (const [label, token] of [
|
|
915
|
-
["env-scoped key", SCOPED_TOKEN],
|
|
916
|
-
[
|
|
980
|
+
for (const [label, token, expectedScope] of [
|
|
981
|
+
["env-scoped key", SCOPED_TOKEN, { kind: "agent", agentId: AGENT_ID }],
|
|
982
|
+
[
|
|
983
|
+
"DB token",
|
|
984
|
+
VALID_RAW_TOKEN,
|
|
985
|
+
{ kind: "agent", agentId: AGENT_ID, tokenKind: "runtime" },
|
|
986
|
+
],
|
|
917
987
|
] as const) {
|
|
918
988
|
it(`${label} may POST another agent's /ask (handler binds from)`, async () => {
|
|
919
989
|
const res = await crossApp().request("/agents/other-agent/ask", {
|
|
@@ -922,7 +992,7 @@ describe("createAdminAuthMiddleware — agent cross routes", () => {
|
|
|
922
992
|
});
|
|
923
993
|
expect(res.status).toBe(200);
|
|
924
994
|
expect(await res.json()).toEqual({
|
|
925
|
-
scope:
|
|
995
|
+
scope: expectedScope,
|
|
926
996
|
});
|
|
927
997
|
});
|
|
928
998
|
|
package/src/api.smoke.test.ts
CHANGED
|
@@ -1036,6 +1036,7 @@ function buildCombinedApp() {
|
|
|
1036
1036
|
id: "tok1",
|
|
1037
1037
|
agentId: COMBINED_AGENT_ID,
|
|
1038
1038
|
token: "hash",
|
|
1039
|
+
kind: "runtime" as const,
|
|
1039
1040
|
label: null,
|
|
1040
1041
|
createdAt: new Date(),
|
|
1041
1042
|
revokedAt: null,
|
|
@@ -1047,6 +1048,7 @@ function buildCombinedApp() {
|
|
|
1047
1048
|
id: "tok1",
|
|
1048
1049
|
agentId: COMBINED_AGENT_ID,
|
|
1049
1050
|
token: "hash",
|
|
1051
|
+
kind: "runtime" as const,
|
|
1050
1052
|
label: null,
|
|
1051
1053
|
createdAt: new Date(),
|
|
1052
1054
|
revokedAt: new Date(),
|
package/src/chat.smoke.test.ts
CHANGED
package/src/openapi-schemas.ts
CHANGED
|
@@ -686,11 +686,19 @@ export const PatchAgentToolBodySchema = z
|
|
|
686
686
|
/**
|
|
687
687
|
* Token metadata returned from list/create (never the hash).
|
|
688
688
|
*/
|
|
689
|
+
export const AgentTokenKindSchema = z
|
|
690
|
+
.enum(["runtime", "tools"])
|
|
691
|
+
.openapi("AgentTokenKind", {
|
|
692
|
+
description:
|
|
693
|
+
"What the token is for: `runtime` (the agent process) or `tools` (the model's own process).",
|
|
694
|
+
});
|
|
695
|
+
|
|
689
696
|
export const AgentTokenSchema = z
|
|
690
697
|
.object({
|
|
691
698
|
id: z.string().openapi({ example: "clx1234567890" }),
|
|
692
699
|
agentId: z.string().openapi({ example: "clx1234567890" }),
|
|
693
700
|
label: z.string().nullable().optional().openapi({ example: "ci-runner" }),
|
|
701
|
+
kind: AgentTokenKindSchema,
|
|
694
702
|
createdAt: z
|
|
695
703
|
.string()
|
|
696
704
|
.datetime()
|
|
@@ -721,6 +729,9 @@ export const CreateAgentTokenResponseSchema = z
|
|
|
721
729
|
export const CreateAgentTokenBodySchema = z
|
|
722
730
|
.object({
|
|
723
731
|
label: z.string().optional().openapi({ example: "ci-runner" }),
|
|
732
|
+
kind: AgentTokenKindSchema.optional().openapi({
|
|
733
|
+
description: "Token kind; defaults to `runtime`.",
|
|
734
|
+
}),
|
|
724
735
|
})
|
|
725
736
|
.openapi("CreateAgentTokenBody");
|
|
726
737
|
|