@app-vitals/agent-admin 1.39.0 → 1.41.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 +584 -0
- package/package.json +2 -2
- package/prisma/client/browser.ts +10 -0
- package/prisma/client/client.ts +10 -0
- package/prisma/client/internal/class.ts +24 -4
- package/prisma/client/internal/prismaNamespace.ts +184 -2
- package/prisma/client/internal/prismaNamespaceBrowser.ts +33 -1
- package/prisma/client/models/Account.ts +148 -0
- package/prisma/client/models/Agent.ts +80 -1
- package/prisma/client/models/AgentCronJob.ts +54 -1
- package/prisma/client/models/AgentEnv.ts +36 -1
- package/prisma/client/models/AgentPlugin.ts +36 -1
- package/prisma/client/models/AgentTemplate.ts +1501 -0
- package/prisma/client/models/AgentTemplateItem.ts +1416 -0
- package/prisma/client/models/AgentTool.ts +71 -1
- package/prisma/client/models.ts +2 -0
- package/prisma/migrations/20261113000000_add_agent_templates/migration.sql +71 -0
- package/prisma/schema/schema.prisma +61 -4
- package/src/admin-boot.ts +18 -0
- package/src/admin-local-agent.smoke.test.ts +2 -0
- package/src/admin-spec.ts +18 -0
- package/src/admin-ui.fixtures.ts +3 -0
- package/src/admin-ui.smoke.test.ts +2 -0
- package/src/agent-cron-run-stats.smoke.test.ts +10 -0
- package/src/agent-plugins.unit.test.ts +1 -0
- package/src/agent-template-items.ts +82 -0
- package/src/agent-template-items.unit.test.ts +70 -0
- package/src/agent-templates-api.integration.test.ts +428 -0
- package/src/agent-templates-api.smoke.test.ts +135 -0
- package/src/agent-templates-api.ts +293 -0
- package/src/agent-templates.integration.test.ts +200 -0
- package/src/agent-templates.ts +432 -0
- package/src/agents-api.smoke.test.ts +17 -0
- package/src/api-auth.ts +3 -0
- package/src/api.smoke.test.ts +13 -0
- package/src/chat.smoke.test.ts +2 -0
- package/src/openapi-schemas.ts +84 -0
|
@@ -265,6 +265,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
265
265
|
create: async () => ({
|
|
266
266
|
id: "cron-id",
|
|
267
267
|
agentId: AGENT_ID,
|
|
268
|
+
source: "custom",
|
|
268
269
|
schedule: "0 9 * * *",
|
|
269
270
|
prompt: "test",
|
|
270
271
|
channel: null,
|
|
@@ -284,6 +285,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
284
285
|
update: async () => ({
|
|
285
286
|
id: "cron-id",
|
|
286
287
|
agentId: AGENT_ID,
|
|
288
|
+
source: "custom",
|
|
287
289
|
schedule: "0 9 * * *",
|
|
288
290
|
prompt: "test",
|
|
289
291
|
channel: null,
|
|
@@ -304,6 +306,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
304
306
|
get: async () => ({
|
|
305
307
|
id: "cron-id",
|
|
306
308
|
agentId: AGENT_ID,
|
|
309
|
+
source: "custom",
|
|
307
310
|
schedule: "0 9 * * *",
|
|
308
311
|
prompt: "test",
|
|
309
312
|
channel: null,
|
|
@@ -323,6 +326,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
323
326
|
setEnabled: async (_agentId, _cronId, enabled) => ({
|
|
324
327
|
id: "cron-id",
|
|
325
328
|
agentId: AGENT_ID,
|
|
329
|
+
source: "custom",
|
|
326
330
|
schedule: "0 9 * * *",
|
|
327
331
|
prompt: "test",
|
|
328
332
|
channel: null,
|
|
@@ -342,6 +346,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
342
346
|
updatePreCheck: async (_agentId, _cronId, preCheck) => ({
|
|
343
347
|
id: "cron-id",
|
|
344
348
|
agentId: AGENT_ID,
|
|
349
|
+
source: "custom",
|
|
345
350
|
schedule: "0 9 * * *",
|
|
346
351
|
prompt: "test",
|
|
347
352
|
channel: null,
|
|
@@ -369,6 +374,8 @@ function makeMockDeps(): AdminDeps {
|
|
|
369
374
|
add: async () => ({
|
|
370
375
|
id: "tool-id",
|
|
371
376
|
agentId: AGENT_ID,
|
|
377
|
+
source: "custom",
|
|
378
|
+
updatedAt: new Date(),
|
|
372
379
|
pattern: "Read",
|
|
373
380
|
enabled: true,
|
|
374
381
|
createdAt: new Date(),
|
|
@@ -376,6 +383,8 @@ function makeMockDeps(): AdminDeps {
|
|
|
376
383
|
toggle: async () => ({
|
|
377
384
|
id: "tool-id",
|
|
378
385
|
agentId: AGENT_ID,
|
|
386
|
+
source: "custom",
|
|
387
|
+
updatedAt: new Date(),
|
|
379
388
|
pattern: "Read",
|
|
380
389
|
enabled: false,
|
|
381
390
|
createdAt: new Date(),
|
|
@@ -411,6 +420,7 @@ function makeMockDeps(): AdminDeps {
|
|
|
411
420
|
add: async () => ({
|
|
412
421
|
id: "plugin-id",
|
|
413
422
|
agentId: AGENT_ID,
|
|
423
|
+
source: "custom",
|
|
414
424
|
name: "test",
|
|
415
425
|
version: "1.0.0",
|
|
416
426
|
enabled: true,
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-template-items.ts
|
|
3
|
+
* ATP-1.1: AgentTemplateItem kinds, row source markers, and the at-rest
|
|
4
|
+
* encryption of template item payloads.
|
|
5
|
+
*
|
|
6
|
+
* An env item's payload is `{ value, secret? }`; `value` is encrypted with the
|
|
7
|
+
* same TokenCrypto (AES-256-GCM, SHIPWRIGHT_ENCRYPTION_KEY) that AgentEnvService
|
|
8
|
+
* uses for AgentEnv.value, so copying a template env onto an agent is a
|
|
9
|
+
* decrypt-then-encrypt with one key. Other kinds hold no secrets and pass
|
|
10
|
+
* through unchanged.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Prisma } from "../prisma/client/client.ts";
|
|
14
|
+
import { ANTHROPIC_CREDENTIAL_KEYS } from "./anthropic-credential-guard.ts";
|
|
15
|
+
import { UnprocessableEntityError } from "./errors.ts";
|
|
16
|
+
import type { TokenCrypto } from "./token-crypto.ts";
|
|
17
|
+
|
|
18
|
+
export const AGENT_TEMPLATE_ITEM_KINDS = [
|
|
19
|
+
"env",
|
|
20
|
+
"tool",
|
|
21
|
+
"plugin",
|
|
22
|
+
"cron",
|
|
23
|
+
] as const;
|
|
24
|
+
export type AgentTemplateItemKind = (typeof AGENT_TEMPLATE_ITEM_KINDS)[number];
|
|
25
|
+
|
|
26
|
+
/** `source` on AgentEnv / AgentTool / AgentPlugin / AgentCronJob. */
|
|
27
|
+
export const ROW_SOURCES = ["template", "custom"] as const;
|
|
28
|
+
export type RowSource = (typeof ROW_SOURCES)[number];
|
|
29
|
+
|
|
30
|
+
/** AgentTemplateItem.payload: a JSON object whose fields depend on `kind`. */
|
|
31
|
+
export type TemplateItemPayload = Prisma.InputJsonObject;
|
|
32
|
+
|
|
33
|
+
/** Returns the payload to store: an env item's `value` encrypted. */
|
|
34
|
+
export function sealTemplateItemPayload(
|
|
35
|
+
kind: AgentTemplateItemKind,
|
|
36
|
+
payload: TemplateItemPayload,
|
|
37
|
+
crypto: TokenCrypto,
|
|
38
|
+
): TemplateItemPayload {
|
|
39
|
+
if (kind !== "env") return payload;
|
|
40
|
+
if (typeof payload.value !== "string") {
|
|
41
|
+
throw new UnprocessableEntityError(
|
|
42
|
+
"env template item payload needs a string value",
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return { ...payload, value: crypto.encrypt(payload.value) };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Reverses sealTemplateItemPayload(): an env item's `value` decrypted. */
|
|
49
|
+
export function openTemplateItemPayload(
|
|
50
|
+
kind: AgentTemplateItemKind,
|
|
51
|
+
payload: TemplateItemPayload,
|
|
52
|
+
crypto: TokenCrypto,
|
|
53
|
+
): TemplateItemPayload {
|
|
54
|
+
if (kind !== "env" || typeof payload.value !== "string") return payload;
|
|
55
|
+
return { ...payload, value: crypto.decrypt(payload.value) };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Env keys a template may never carry (ATP-1.3): per-agent credentials that
|
|
60
|
+
* are minted for, or provisioned onto, one specific agent. Copying them from a
|
|
61
|
+
* template would hand every agent the same identity.
|
|
62
|
+
*/
|
|
63
|
+
export const TEMPLATE_ENV_DENYLIST: ReadonlySet<string> = new Set([
|
|
64
|
+
"SLACK_BOT_TOKEN",
|
|
65
|
+
"SLACK_APP_TOKEN",
|
|
66
|
+
"SLACK_APP_ID",
|
|
67
|
+
"SLACK_SIGNING_SECRET",
|
|
68
|
+
"GH_TOKEN",
|
|
69
|
+
"SHIPWRIGHT_AGENT_API_KEY",
|
|
70
|
+
"SHIPWRIGHT_TASK_STORE_URL",
|
|
71
|
+
"SHIPWRIGHT_TASK_STORE_TOKEN",
|
|
72
|
+
...ANTHROPIC_CREDENTIAL_KEYS,
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
/** Throws UnprocessableEntityError (422) for a denylisted env key (case-insensitive). */
|
|
76
|
+
export function assertTemplateEnvKeyAllowed(key: string): void {
|
|
77
|
+
if (TEMPLATE_ENV_DENYLIST.has(key.toUpperCase())) {
|
|
78
|
+
throw new UnprocessableEntityError(
|
|
79
|
+
`env key ${key} is a per-agent credential and cannot be set in a template`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-template-items.unit.test.ts
|
|
3
|
+
* ATP-1.1: template item payload sealing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "bun:test";
|
|
7
|
+
import {
|
|
8
|
+
assertTemplateEnvKeyAllowed,
|
|
9
|
+
openTemplateItemPayload,
|
|
10
|
+
sealTemplateItemPayload,
|
|
11
|
+
} from "./agent-template-items.ts";
|
|
12
|
+
import { UnprocessableEntityError } from "./errors.ts";
|
|
13
|
+
import { makeStrictTokenCrypto } from "./token-crypto.ts";
|
|
14
|
+
|
|
15
|
+
const crypto = makeStrictTokenCrypto("ab".repeat(32));
|
|
16
|
+
if (!crypto) throw new Error("test key rejected");
|
|
17
|
+
|
|
18
|
+
describe("sealTemplateItemPayload", () => {
|
|
19
|
+
it("encrypts an env item's value and keeps other fields", () => {
|
|
20
|
+
const sealed = sealTemplateItemPayload(
|
|
21
|
+
"env",
|
|
22
|
+
{ value: "s3cret", secret: true },
|
|
23
|
+
crypto,
|
|
24
|
+
);
|
|
25
|
+
expect(sealed.secret).toBe(true);
|
|
26
|
+
expect(sealed.value).not.toBe("s3cret");
|
|
27
|
+
expect(String(sealed.value).split(":")).toHaveLength(3);
|
|
28
|
+
expect(openTemplateItemPayload("env", sealed, crypto)).toEqual({
|
|
29
|
+
value: "s3cret",
|
|
30
|
+
secret: true,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("does not mutate the input payload", () => {
|
|
35
|
+
const payload = { value: "v" };
|
|
36
|
+
sealTemplateItemPayload("env", payload, crypto);
|
|
37
|
+
expect(payload).toEqual({ value: "v" });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("rejects an env payload without a string value", () => {
|
|
41
|
+
expect(() => sealTemplateItemPayload("env", {}, crypto)).toThrow(
|
|
42
|
+
UnprocessableEntityError,
|
|
43
|
+
);
|
|
44
|
+
expect(() => sealTemplateItemPayload("env", { value: 1 }, crypto)).toThrow(
|
|
45
|
+
UnprocessableEntityError,
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("passes non-env kinds through unchanged", () => {
|
|
50
|
+
const payload = { schedule: "0 9 * * *", prompt: "hi", value: "x" };
|
|
51
|
+
for (const kind of ["tool", "plugin", "cron"] as const) {
|
|
52
|
+
expect(sealTemplateItemPayload(kind, payload, crypto)).toBe(payload);
|
|
53
|
+
expect(openTemplateItemPayload(kind, payload, crypto)).toBe(payload);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("assertTemplateEnvKeyAllowed", () => {
|
|
59
|
+
it("rejects per-agent credential keys, case-insensitively", () => {
|
|
60
|
+
for (const key of ["SLACK_BOT_TOKEN", "gh_token", "ANTHROPIC_API_KEY"]) {
|
|
61
|
+
expect(() => assertTemplateEnvKeyAllowed(key)).toThrow(
|
|
62
|
+
UnprocessableEntityError,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("allows ordinary keys", () => {
|
|
68
|
+
expect(() => assertTemplateEnvKeyAllowed("LINEAR_API_KEY")).not.toThrow();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-templates-api.integration.test.ts
|
|
3
|
+
* ATP-1.3: template CRUD authz, validation and env crypto against real
|
|
4
|
+
* Postgres, driven through the /templates app.
|
|
5
|
+
*
|
|
6
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
10
|
+
import { sign } from "hono/jwt";
|
|
11
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
12
|
+
import { AgentTemplateService } from "./agent-templates.ts";
|
|
13
|
+
import { createAgentTemplatesApp } from "./agent-templates-api.ts";
|
|
14
|
+
import { parseAdminApiKeys } from "./api-auth.ts";
|
|
15
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
16
|
+
import { SpecialtyTypes } from "./specialty-types.ts";
|
|
17
|
+
import { makeStrictTokenCrypto } from "./token-crypto.ts";
|
|
18
|
+
|
|
19
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
20
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
21
|
+
|
|
22
|
+
const KEY = "ab".repeat(32);
|
|
23
|
+
const SECRET = "test-session-secret-32-chars-long!!";
|
|
24
|
+
|
|
25
|
+
describeOrSkip("/templates (integration)", () => {
|
|
26
|
+
let prisma: PrismaClient;
|
|
27
|
+
let app: ReturnType<typeof createAgentTemplatesApp>;
|
|
28
|
+
let accountA: string;
|
|
29
|
+
let accountB: string;
|
|
30
|
+
const crypto = makeStrictTokenCrypto(KEY);
|
|
31
|
+
if (!crypto) throw new Error("test crypto");
|
|
32
|
+
|
|
33
|
+
const admin = { Authorization: "Bearer tok-admin" };
|
|
34
|
+
|
|
35
|
+
async function cookie(email: string, isAdmin = false) {
|
|
36
|
+
const token = await sign(
|
|
37
|
+
{
|
|
38
|
+
userId: email,
|
|
39
|
+
email,
|
|
40
|
+
isAdmin,
|
|
41
|
+
exp: Math.floor(Date.now() / 1000) + 600,
|
|
42
|
+
},
|
|
43
|
+
SECRET,
|
|
44
|
+
"HS256",
|
|
45
|
+
);
|
|
46
|
+
return { Cookie: `admin_session=${token}` };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function call(
|
|
50
|
+
method: string,
|
|
51
|
+
path: string,
|
|
52
|
+
headers: Record<string, string>,
|
|
53
|
+
body?: unknown,
|
|
54
|
+
) {
|
|
55
|
+
return app.request(path, {
|
|
56
|
+
method,
|
|
57
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
58
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
beforeEach(async () => {
|
|
63
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
64
|
+
await prisma.agentTemplateItem.deleteMany();
|
|
65
|
+
await prisma.agentTemplate.deleteMany();
|
|
66
|
+
await prisma.agent.deleteMany();
|
|
67
|
+
await prisma.accountInvite.deleteMany();
|
|
68
|
+
await prisma.accountMember.deleteMany();
|
|
69
|
+
await prisma.account.deleteMany();
|
|
70
|
+
accountA = (
|
|
71
|
+
await prisma.account.create({ data: { name: "A", maxAgents: 5 } })
|
|
72
|
+
).id;
|
|
73
|
+
accountB = (
|
|
74
|
+
await prisma.account.create({ data: { name: "B", maxAgents: 5 } })
|
|
75
|
+
).id;
|
|
76
|
+
const accountByEmail: Record<string, string> = {
|
|
77
|
+
"a@example.com": accountA,
|
|
78
|
+
"b@example.com": accountB,
|
|
79
|
+
};
|
|
80
|
+
app = createAgentTemplatesApp({
|
|
81
|
+
templateService: new AgentTemplateService(
|
|
82
|
+
prisma,
|
|
83
|
+
crypto,
|
|
84
|
+
new SpecialtyTypes({ defaultTypeName: "specialist" }),
|
|
85
|
+
),
|
|
86
|
+
callerScopeResolver: async (email, isAdmin) =>
|
|
87
|
+
isAdmin
|
|
88
|
+
? { kind: "all" }
|
|
89
|
+
: {
|
|
90
|
+
kind: "scoped",
|
|
91
|
+
accountId: accountByEmail[email] ?? null,
|
|
92
|
+
agentIds: [],
|
|
93
|
+
},
|
|
94
|
+
sessionSecret: SECRET,
|
|
95
|
+
adminApiKeys: parseAdminApiKeys("root:tok-admin:*,bot:tok-agent:agent-1"),
|
|
96
|
+
agentTokenService: { validate: async () => null },
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
afterEach(async () => {
|
|
101
|
+
await prisma.$disconnect();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("validation", () => {
|
|
105
|
+
it("422 for an unknown specialty", async () => {
|
|
106
|
+
const res = await call("PUT", "/templates", admin, {
|
|
107
|
+
specialty: "nope",
|
|
108
|
+
items: [],
|
|
109
|
+
});
|
|
110
|
+
expect(res.status).toBe(422);
|
|
111
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it.each([
|
|
115
|
+
"SLACK_BOT_TOKEN",
|
|
116
|
+
"SLACK_APP_TOKEN",
|
|
117
|
+
"GH_TOKEN",
|
|
118
|
+
"ANTHROPIC_API_KEY",
|
|
119
|
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
120
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
121
|
+
"SHIPWRIGHT_TASK_STORE_URL",
|
|
122
|
+
"SHIPWRIGHT_TASK_STORE_TOKEN",
|
|
123
|
+
"gh_token",
|
|
124
|
+
])("422 for denylisted env key %s", async (key) => {
|
|
125
|
+
const res = await call("PUT", "/templates", admin, {
|
|
126
|
+
items: [{ kind: "env", key, payload: { value: "x" } }],
|
|
127
|
+
});
|
|
128
|
+
expect(res.status).toBe(422);
|
|
129
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("422 for a cron item without a name key", async () => {
|
|
133
|
+
const res = await call("PUT", "/templates", admin, {
|
|
134
|
+
items: [
|
|
135
|
+
{
|
|
136
|
+
kind: "cron",
|
|
137
|
+
key: " ",
|
|
138
|
+
payload: { schedule: "0 9 * * *", prompt: "brief" },
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
expect(res.status).toBe(422);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("422 for a cron item missing schedule or prompt", async () => {
|
|
146
|
+
const res = await call("PUT", "/templates", admin, {
|
|
147
|
+
items: [{ kind: "cron", key: "brief", payload: { prompt: "x" } }],
|
|
148
|
+
});
|
|
149
|
+
expect(res.status).toBe(422);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("422 for duplicate keys within a template, accepted across kinds", async () => {
|
|
153
|
+
const cron = {
|
|
154
|
+
kind: "cron",
|
|
155
|
+
key: "brief",
|
|
156
|
+
payload: { schedule: "0 9 * * *", prompt: "brief" },
|
|
157
|
+
};
|
|
158
|
+
const dup = await call("PUT", "/templates", admin, {
|
|
159
|
+
items: [cron, cron],
|
|
160
|
+
});
|
|
161
|
+
expect(dup.status).toBe(422);
|
|
162
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
163
|
+
|
|
164
|
+
const ok = await call("PUT", "/templates", admin, {
|
|
165
|
+
items: [cron, { kind: "tool", key: "brief" }],
|
|
166
|
+
});
|
|
167
|
+
expect(ok.status).toBe(201);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("400 for an unknown item kind", async () => {
|
|
171
|
+
const res = await call("PUT", "/templates", admin, {
|
|
172
|
+
items: [{ kind: "bogus", key: "x" }],
|
|
173
|
+
});
|
|
174
|
+
expect(res.status).toBe(400);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("crypto and masking", () => {
|
|
179
|
+
it("encrypts env values at rest and masks secrets on GET", async () => {
|
|
180
|
+
const put = await call("PUT", "/templates", admin, {
|
|
181
|
+
specialty: "specialist",
|
|
182
|
+
items: [
|
|
183
|
+
{
|
|
184
|
+
kind: "env",
|
|
185
|
+
key: "LINEAR_API_KEY",
|
|
186
|
+
payload: { value: "lin_secret", secret: true },
|
|
187
|
+
},
|
|
188
|
+
{ kind: "env", key: "REGION", payload: { value: "us-east-1" } },
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
expect(put.status).toBe(201);
|
|
192
|
+
const { id } = (await put.json()) as { id: string };
|
|
193
|
+
|
|
194
|
+
const rows = await prisma.agentTemplateItem.findMany({
|
|
195
|
+
where: { templateId: id },
|
|
196
|
+
orderBy: { key: "asc" },
|
|
197
|
+
});
|
|
198
|
+
const stored = Object.fromEntries(
|
|
199
|
+
rows.map((r) => [r.key, r.payload as Record<string, unknown>]),
|
|
200
|
+
);
|
|
201
|
+
expect(stored.LINEAR_API_KEY?.value).not.toBe("lin_secret");
|
|
202
|
+
expect(crypto.decrypt(stored.LINEAR_API_KEY?.value as string)).toBe(
|
|
203
|
+
"lin_secret",
|
|
204
|
+
);
|
|
205
|
+
expect(stored.REGION?.value).not.toBe("us-east-1");
|
|
206
|
+
expect(JSON.stringify(rows)).not.toContain("lin_secret");
|
|
207
|
+
|
|
208
|
+
const get = await call("GET", `/templates/${id}`, admin);
|
|
209
|
+
const body = (await get.json()) as {
|
|
210
|
+
items: { key: string; payload: Record<string, unknown> }[];
|
|
211
|
+
};
|
|
212
|
+
const byKey = Object.fromEntries(body.items.map((i) => [i.key, i]));
|
|
213
|
+
expect(byKey.LINEAR_API_KEY?.payload).toEqual({
|
|
214
|
+
value: "***",
|
|
215
|
+
secret: true,
|
|
216
|
+
});
|
|
217
|
+
expect(byKey.REGION?.payload.value).toBe("us-east-1");
|
|
218
|
+
|
|
219
|
+
const list = await call("GET", "/templates", admin);
|
|
220
|
+
expect(JSON.stringify(await list.json())).not.toContain("lin_secret");
|
|
221
|
+
const put2 = await call("PUT", "/templates", admin, {
|
|
222
|
+
specialty: "specialist",
|
|
223
|
+
items: [
|
|
224
|
+
{
|
|
225
|
+
kind: "env",
|
|
226
|
+
key: "LINEAR_API_KEY",
|
|
227
|
+
payload: { value: "lin_secret_2", secret: true },
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
});
|
|
231
|
+
expect(JSON.stringify(await put2.json())).not.toContain("lin_secret_2");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("keeps the stored secret when the mask is sent back", async () => {
|
|
235
|
+
const put = await call("PUT", "/templates", admin, {
|
|
236
|
+
items: [
|
|
237
|
+
{
|
|
238
|
+
kind: "env",
|
|
239
|
+
key: "K",
|
|
240
|
+
payload: { value: "keep-me", secret: true },
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
});
|
|
244
|
+
const { id } = (await put.json()) as { id: string };
|
|
245
|
+
const again = await call("PUT", "/templates", admin, {
|
|
246
|
+
items: [
|
|
247
|
+
{ kind: "env", key: "K", payload: { value: "***", secret: true } },
|
|
248
|
+
],
|
|
249
|
+
});
|
|
250
|
+
expect(again.status).toBe(200);
|
|
251
|
+
const row = await prisma.agentTemplateItem.findFirstOrThrow({
|
|
252
|
+
where: { templateId: id, key: "K" },
|
|
253
|
+
});
|
|
254
|
+
expect(crypto.decrypt((row.payload as { value: string }).value)).toBe(
|
|
255
|
+
"keep-me",
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const bad = await call("PUT", "/templates", admin, {
|
|
259
|
+
items: [
|
|
260
|
+
{ kind: "env", key: "NEW", payload: { value: "***", secret: true } },
|
|
261
|
+
],
|
|
262
|
+
});
|
|
263
|
+
expect(bad.status).toBe(422);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("CRUD", () => {
|
|
268
|
+
it("create then replace one scope, list with filters, item ops, delete", async () => {
|
|
269
|
+
const first = await call("PUT", "/templates", admin, {
|
|
270
|
+
accountId: accountA,
|
|
271
|
+
specialty: "specialist",
|
|
272
|
+
items: [{ kind: "tool", key: "Bash" }],
|
|
273
|
+
});
|
|
274
|
+
expect(first.status).toBe(201);
|
|
275
|
+
const { id } = (await first.json()) as { id: string };
|
|
276
|
+
|
|
277
|
+
const second = await call("PUT", "/templates", admin, {
|
|
278
|
+
accountId: accountA,
|
|
279
|
+
specialty: "specialist",
|
|
280
|
+
items: [
|
|
281
|
+
{ kind: "plugin", key: "shipwright@app-vitals/shipwright" },
|
|
282
|
+
{
|
|
283
|
+
kind: "cron",
|
|
284
|
+
key: "brief",
|
|
285
|
+
payload: { schedule: "0 9 * * *", prompt: "go" },
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
});
|
|
289
|
+
expect(second.status).toBe(200);
|
|
290
|
+
const replaced = (await second.json()) as {
|
|
291
|
+
id: string;
|
|
292
|
+
items: { kind: string; key: string }[];
|
|
293
|
+
};
|
|
294
|
+
expect(replaced.id).toBe(id);
|
|
295
|
+
expect(replaced.items.map((i) => i.kind)).toEqual(["cron", "plugin"]);
|
|
296
|
+
expect(await prisma.agentTemplate.count()).toBe(1);
|
|
297
|
+
|
|
298
|
+
await call("PUT", "/templates", admin, { items: [] }); // deployment-wide
|
|
299
|
+
const all = (await (await call("GET", "/templates", admin)).json()) as {
|
|
300
|
+
templates: unknown[];
|
|
301
|
+
};
|
|
302
|
+
expect(all.templates).toHaveLength(2);
|
|
303
|
+
const filtered = (await (
|
|
304
|
+
await call("GET", `/templates?accountId=${accountA}`, admin)
|
|
305
|
+
).json()) as { templates: unknown[] };
|
|
306
|
+
expect(filtered.templates).toHaveLength(1);
|
|
307
|
+
|
|
308
|
+
const added = await call("PUT", `/templates/${id}/items`, admin, {
|
|
309
|
+
kind: "env",
|
|
310
|
+
key: "REGION",
|
|
311
|
+
payload: { value: "eu" },
|
|
312
|
+
});
|
|
313
|
+
expect(added.status).toBe(200);
|
|
314
|
+
expect(
|
|
315
|
+
await prisma.agentTemplateItem.count({ where: { templateId: id } }),
|
|
316
|
+
).toBe(3);
|
|
317
|
+
|
|
318
|
+
const del = await call(
|
|
319
|
+
"DELETE",
|
|
320
|
+
`/templates/${id}/items?kind=plugin&key=${encodeURIComponent("shipwright@app-vitals/shipwright")}`,
|
|
321
|
+
admin,
|
|
322
|
+
);
|
|
323
|
+
expect(del.status).toBe(204);
|
|
324
|
+
const missing = await call(
|
|
325
|
+
"DELETE",
|
|
326
|
+
`/templates/${id}/items?kind=plugin&key=nope`,
|
|
327
|
+
admin,
|
|
328
|
+
);
|
|
329
|
+
expect(missing.status).toBe(404);
|
|
330
|
+
|
|
331
|
+
expect((await call("DELETE", `/templates/${id}`, admin)).status).toBe(
|
|
332
|
+
204,
|
|
333
|
+
);
|
|
334
|
+
expect(
|
|
335
|
+
await prisma.agentTemplateItem.count({ where: { templateId: id } }),
|
|
336
|
+
).toBe(0);
|
|
337
|
+
expect((await call("GET", `/templates/${id}`, admin)).status).toBe(404);
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe("authz", () => {
|
|
342
|
+
it("account caller defaults to and manages only its own account", async () => {
|
|
343
|
+
const h = await cookie("a@example.com");
|
|
344
|
+
const put = await call("PUT", "/templates", h, {
|
|
345
|
+
items: [{ kind: "tool", key: "Bash" }],
|
|
346
|
+
});
|
|
347
|
+
expect(put.status).toBe(201);
|
|
348
|
+
const t = (await put.json()) as { id: string; accountId: string };
|
|
349
|
+
expect(t.accountId).toBe(accountA);
|
|
350
|
+
|
|
351
|
+
expect((await call("GET", `/templates/${t.id}`, h)).status).toBe(200);
|
|
352
|
+
const list = (await (await call("GET", "/templates", h)).json()) as {
|
|
353
|
+
templates: unknown[];
|
|
354
|
+
};
|
|
355
|
+
expect(list.templates).toHaveLength(1);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("cross-account and deployment-wide access is refused", async () => {
|
|
359
|
+
const aTemplate = (await (
|
|
360
|
+
await call("PUT", "/templates", admin, {
|
|
361
|
+
accountId: accountA,
|
|
362
|
+
items: [],
|
|
363
|
+
})
|
|
364
|
+
).json()) as { id: string };
|
|
365
|
+
const global = (await (
|
|
366
|
+
await call("PUT", "/templates", admin, { items: [] })
|
|
367
|
+
).json()) as { id: string };
|
|
368
|
+
const b = await cookie("b@example.com");
|
|
369
|
+
|
|
370
|
+
// Reading/changing another account's or a global template: 404 (no leak).
|
|
371
|
+
for (const id of [aTemplate.id, global.id]) {
|
|
372
|
+
expect((await call("GET", `/templates/${id}`, b)).status).toBe(404);
|
|
373
|
+
expect((await call("DELETE", `/templates/${id}`, b)).status).toBe(404);
|
|
374
|
+
expect(
|
|
375
|
+
(
|
|
376
|
+
await call("PUT", `/templates/${id}/items`, b, {
|
|
377
|
+
kind: "tool",
|
|
378
|
+
key: "Bash",
|
|
379
|
+
})
|
|
380
|
+
).status,
|
|
381
|
+
).toBe(404);
|
|
382
|
+
expect(
|
|
383
|
+
(await call("DELETE", `/templates/${id}/items?kind=tool&key=x`, b))
|
|
384
|
+
.status,
|
|
385
|
+
).toBe(404);
|
|
386
|
+
}
|
|
387
|
+
expect(await prisma.agentTemplate.count()).toBe(2);
|
|
388
|
+
|
|
389
|
+
// Naming another account or the deployment scope: 403.
|
|
390
|
+
expect(
|
|
391
|
+
(await call("PUT", "/templates", b, { accountId: accountA, items: [] }))
|
|
392
|
+
.status,
|
|
393
|
+
).toBe(403);
|
|
394
|
+
expect(
|
|
395
|
+
(await call("PUT", "/templates", b, { accountId: null, items: [] }))
|
|
396
|
+
.status,
|
|
397
|
+
).toBe(403);
|
|
398
|
+
expect(
|
|
399
|
+
(await call("GET", `/templates?accountId=${accountA}`, b)).status,
|
|
400
|
+
).toBe(403);
|
|
401
|
+
|
|
402
|
+
// The list never includes others' templates.
|
|
403
|
+
const list = (await (await call("GET", "/templates", b)).json()) as {
|
|
404
|
+
templates: unknown[];
|
|
405
|
+
};
|
|
406
|
+
expect(list.templates).toEqual([]);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("403 for callers without an account, agent keys and non-scoped sessions", async () => {
|
|
410
|
+
expect(
|
|
411
|
+
(await call("GET", "/templates", await cookie("nobody@example.com")))
|
|
412
|
+
.status,
|
|
413
|
+
).toBe(403);
|
|
414
|
+
expect(
|
|
415
|
+
(await call("GET", "/templates", { Authorization: "Bearer tok-agent" }))
|
|
416
|
+
.status,
|
|
417
|
+
).toBe(403);
|
|
418
|
+
expect((await call("GET", "/templates", {})).status).toBe(401);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("an admin session manages deployment-wide templates", async () => {
|
|
422
|
+
const h = await cookie("root@example.com", true);
|
|
423
|
+
const res = await call("PUT", "/templates", h, { items: [] });
|
|
424
|
+
expect(res.status).toBe(201);
|
|
425
|
+
expect(((await res.json()) as { accountId: null }).accountId).toBeNull();
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
});
|