@app-vitals/agent-admin 1.115.2 → 1.117.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-boot.ts +21 -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/admin-ui.ts +5 -0
- package/src/agent-cron-run-stats.smoke.test.ts +6 -4
- package/src/agent-envs.ts +16 -0
- package/src/agent-template-items.ts +1 -0
- 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/agent-tools-token.integration.test.ts +127 -0
- package/src/agent-tools-token.ts +80 -0
- package/src/agent-tools-token.unit.test.ts +95 -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 +24 -2
- package/src/agents.ts +18 -0
- 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
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-tokens.unit.test.ts
|
|
3
|
+
* Unit tests for AgentTokenService kind handling (runtime | tools) against a
|
|
4
|
+
* minimal in-memory Prisma stand-in.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
9
|
+
import {
|
|
10
|
+
type AgentTokenKind,
|
|
11
|
+
AgentTokenService,
|
|
12
|
+
agentTokenKindOf,
|
|
13
|
+
isAgentTokenKind,
|
|
14
|
+
} from "./agent-tokens.ts";
|
|
15
|
+
|
|
16
|
+
interface Row {
|
|
17
|
+
id: string;
|
|
18
|
+
agentId: string;
|
|
19
|
+
token: string;
|
|
20
|
+
label: string | null;
|
|
21
|
+
kind: string;
|
|
22
|
+
createdAt: Date;
|
|
23
|
+
revokedAt: Date | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function makeService() {
|
|
27
|
+
const rows: Row[] = [];
|
|
28
|
+
const prisma = {
|
|
29
|
+
agent: { findUnique: async () => ({ id: "agent-1" }) },
|
|
30
|
+
agentToken: {
|
|
31
|
+
create: async ({
|
|
32
|
+
data,
|
|
33
|
+
}: {
|
|
34
|
+
data: {
|
|
35
|
+
agentId: string;
|
|
36
|
+
token: string;
|
|
37
|
+
label?: string;
|
|
38
|
+
kind?: string;
|
|
39
|
+
};
|
|
40
|
+
}) => {
|
|
41
|
+
const row: Row = {
|
|
42
|
+
id: `t${rows.length + 1}`,
|
|
43
|
+
agentId: data.agentId,
|
|
44
|
+
token: data.token,
|
|
45
|
+
label: data.label ?? null,
|
|
46
|
+
// Mirrors the column default when kind is omitted.
|
|
47
|
+
kind: data.kind ?? "runtime",
|
|
48
|
+
createdAt: new Date(),
|
|
49
|
+
revokedAt: null,
|
|
50
|
+
};
|
|
51
|
+
rows.push(row);
|
|
52
|
+
return row;
|
|
53
|
+
},
|
|
54
|
+
findUnique: async ({ where }: { where: { token: string } }) =>
|
|
55
|
+
rows.find((r) => r.token === where.token) ?? null,
|
|
56
|
+
},
|
|
57
|
+
} as unknown as PrismaClient;
|
|
58
|
+
return { service: new AgentTokenService(prisma), rows };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("AgentTokenService kind", () => {
|
|
62
|
+
it("create() defaults to the runtime kind", async () => {
|
|
63
|
+
const { service } = makeService();
|
|
64
|
+
const { token } = await service.create("agent-1", "label");
|
|
65
|
+
expect(token.kind).toBe("runtime");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("create() stores an explicit tools kind", async () => {
|
|
69
|
+
const { service } = makeService();
|
|
70
|
+
const { token } = await service.create("agent-1", undefined, "tools");
|
|
71
|
+
expect(token.kind).toBe("tools");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("validate() returns the runtime kind for a default token", async () => {
|
|
75
|
+
const { service } = makeService();
|
|
76
|
+
const { rawToken } = await service.create("agent-1");
|
|
77
|
+
expect(await service.validate(rawToken)).toEqual({
|
|
78
|
+
agentId: "agent-1",
|
|
79
|
+
kind: "runtime",
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("validate() returns the tools kind for a tools token", async () => {
|
|
84
|
+
const { service } = makeService();
|
|
85
|
+
const { rawToken } = await service.create("agent-1", "t", "tools");
|
|
86
|
+
expect(await service.validate(rawToken)).toEqual({
|
|
87
|
+
agentId: "agent-1",
|
|
88
|
+
kind: "tools",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("validate() returns null for an unknown token", async () => {
|
|
93
|
+
const { service } = makeService();
|
|
94
|
+
expect(await service.validate("nope")).toBeNull();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("create() rejects an unknown kind before writing a row", async () => {
|
|
98
|
+
const { service, rows } = makeService();
|
|
99
|
+
await expect(
|
|
100
|
+
service.create("agent-1", "x", "admin" as AgentTokenKind),
|
|
101
|
+
).rejects.toThrow("kind must be runtime or tools");
|
|
102
|
+
expect(rows).toHaveLength(0);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("validate() fails closed on a stored kind it does not recognize", async () => {
|
|
106
|
+
const { service, rows } = makeService();
|
|
107
|
+
const { rawToken } = await service.create("agent-1");
|
|
108
|
+
(rows[0] as Row).kind = "admin";
|
|
109
|
+
expect(await service.validate(rawToken)).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("AgentTokenKind helpers", () => {
|
|
114
|
+
it("isAgentTokenKind() accepts only runtime and tools", () => {
|
|
115
|
+
expect(isAgentTokenKind("runtime")).toBe(true);
|
|
116
|
+
expect(isAgentTokenKind("tools")).toBe(true);
|
|
117
|
+
expect(isAgentTokenKind("admin")).toBe(false);
|
|
118
|
+
expect(isAgentTokenKind("")).toBe(false);
|
|
119
|
+
expect(isAgentTokenKind(undefined)).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("agentTokenKindOf() keeps known kinds and reports unknown as runtime", () => {
|
|
123
|
+
expect(agentTokenKindOf("tools")).toBe("tools");
|
|
124
|
+
expect(agentTokenKindOf("runtime")).toBe("runtime");
|
|
125
|
+
expect(agentTokenKindOf("bogus")).toBe("runtime");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for AgentToolsTokenService against a real PostgreSQL DB.
|
|
3
|
+
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
4
|
+
*/
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
6
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
7
|
+
import { AgentCronJobService } from "./agent-cron-jobs.ts";
|
|
8
|
+
import { AgentEnvService } from "./agent-envs.ts";
|
|
9
|
+
import { AgentMemberService } from "./agent-members.ts";
|
|
10
|
+
import { AgentPluginService } from "./agent-plugins.ts";
|
|
11
|
+
import { AgentToolService } from "./agent-tools.ts";
|
|
12
|
+
import { AgentService, createAgent } from "./agents.ts";
|
|
13
|
+
import { SpecialtyTypes } from "./specialty-types.ts";
|
|
14
|
+
import { AgentTokenService } from "./agent-tokens.ts";
|
|
15
|
+
import {
|
|
16
|
+
AgentToolsTokenService,
|
|
17
|
+
TOOLS_TOKEN_ENV_KEY,
|
|
18
|
+
} from "./agent-tools-token.ts";
|
|
19
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
20
|
+
import { identityCrypto } from "./token-crypto.ts";
|
|
21
|
+
|
|
22
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
23
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
24
|
+
|
|
25
|
+
describeOrSkip("AgentToolsTokenService (integration)", () => {
|
|
26
|
+
let prisma: PrismaClient;
|
|
27
|
+
let tokens: AgentTokenService;
|
|
28
|
+
let envs: AgentEnvService;
|
|
29
|
+
let svc: AgentToolsTokenService;
|
|
30
|
+
let errors: string[];
|
|
31
|
+
|
|
32
|
+
beforeEach(async () => {
|
|
33
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
34
|
+
await prisma.agentToken.deleteMany();
|
|
35
|
+
await prisma.agentCronJob.deleteMany();
|
|
36
|
+
await prisma.agentTool.deleteMany();
|
|
37
|
+
await prisma.agentEnv.deleteMany();
|
|
38
|
+
await prisma.agent.deleteMany();
|
|
39
|
+
tokens = new AgentTokenService(prisma);
|
|
40
|
+
envs = new AgentEnvService(prisma, identityCrypto);
|
|
41
|
+
errors = [];
|
|
42
|
+
svc = new AgentToolsTokenService(tokens, envs, {
|
|
43
|
+
error: (m: string) => void errors.push(m),
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(async () => {
|
|
48
|
+
await prisma.$disconnect();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
async function newAgent(name: string): Promise<string> {
|
|
52
|
+
return (await prisma.agent.create({ data: { name } })).id;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
it("creates a token and secret env row that validates as tools-kind, idempotently", async () => {
|
|
56
|
+
const id = await newAgent("agent-a");
|
|
57
|
+
expect(await svc.ensureToolsToken(id)).toBe("created");
|
|
58
|
+
const row = await prisma.agentEnv.findUnique({
|
|
59
|
+
where: { agentId_key: { agentId: id, key: TOOLS_TOKEN_ENV_KEY } },
|
|
60
|
+
});
|
|
61
|
+
expect(row?.secret).toBe(true);
|
|
62
|
+
const raw = await envs.getValue(id, TOOLS_TOKEN_ENV_KEY);
|
|
63
|
+
expect(await tokens.validate(raw as string)).toEqual({
|
|
64
|
+
agentId: id,
|
|
65
|
+
kind: "tools",
|
|
66
|
+
});
|
|
67
|
+
expect(await svc.ensureToolsToken(id)).toBe("unchanged");
|
|
68
|
+
expect(await prisma.agentToken.count({ where: { agentId: id } })).toBe(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("repairs a revoked token", async () => {
|
|
72
|
+
const id = await newAgent("agent-a");
|
|
73
|
+
await svc.ensureToolsToken(id);
|
|
74
|
+
const [tok] = await tokens.listForAgent(id);
|
|
75
|
+
await tokens.revoke((tok as { id: string }).id);
|
|
76
|
+
expect(await svc.ensureToolsToken(id)).toBe("created");
|
|
77
|
+
const raw = await envs.getValue(id, TOOLS_TOKEN_ENV_KEY);
|
|
78
|
+
expect((await tokens.validate(raw as string))?.kind).toBe("tools");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("backfill leaves the others covered when one agent fails", async () => {
|
|
82
|
+
const a = await newAgent("agent-a");
|
|
83
|
+
const b = await newAgent("agent-b");
|
|
84
|
+
const result = await svc.ensureAll([
|
|
85
|
+
{ id: a },
|
|
86
|
+
{ id: "missing-agent" },
|
|
87
|
+
{ id: b },
|
|
88
|
+
]);
|
|
89
|
+
expect(result.created.sort()).toEqual([a, b].sort());
|
|
90
|
+
expect(result.failed.map((f) => f.agentId)).toEqual(["missing-agent"]);
|
|
91
|
+
for (const id of [a, b]) {
|
|
92
|
+
expect(await envs.getValue(id, TOOLS_TOKEN_ENV_KEY)).not.toBeNull();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("createAgent provisioning mints the token and env row", async () => {
|
|
97
|
+
const result = await createAgent(
|
|
98
|
+
{
|
|
99
|
+
agentService: new AgentService(prisma),
|
|
100
|
+
agentToolService: new AgentToolService(prisma),
|
|
101
|
+
agentPluginService: new AgentPluginService(prisma),
|
|
102
|
+
agentMemberService: new AgentMemberService(prisma),
|
|
103
|
+
agentEnvService: envs,
|
|
104
|
+
agentCronJobService: new AgentCronJobService(prisma),
|
|
105
|
+
provisioner: { canProvision: false, provision: async () => {} },
|
|
106
|
+
specialtyRegistry: new SpecialtyTypes(),
|
|
107
|
+
toolsTokenService: svc,
|
|
108
|
+
} as never,
|
|
109
|
+
{
|
|
110
|
+
name: "agent-created",
|
|
111
|
+
specialty: "specialist",
|
|
112
|
+
runtime: undefined,
|
|
113
|
+
reposRaw: undefined,
|
|
114
|
+
authorAllowlistRaw: undefined,
|
|
115
|
+
patchAuthorAllowlistRaw: undefined,
|
|
116
|
+
memberEmailsRaw: undefined,
|
|
117
|
+
restrictSlackToMembersRaw: undefined,
|
|
118
|
+
claudeCodeOauthToken: undefined,
|
|
119
|
+
anthropicApiKey: undefined,
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
expect(result.ok).toBe(true);
|
|
123
|
+
const id = (result as { agent: { id: string } }).agent.id;
|
|
124
|
+
const raw = await envs.getValue(id, TOOLS_TOKEN_ENV_KEY);
|
|
125
|
+
expect((await tokens.validate(raw as string))?.kind).toBe("tools");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-tools-token.ts
|
|
3
|
+
* Mints a tools-kind AgentToken per agent and stores the raw value, encrypted,
|
|
4
|
+
* in the agent's env bundle as SHIPWRIGHT_AGENT_TOOLS_API_KEY (secret).
|
|
5
|
+
*
|
|
6
|
+
* Idempotent: a stored value that still validates as a tools-kind token for
|
|
7
|
+
* the agent means nothing to do. A missing env row, a revoked token, or a
|
|
8
|
+
* value that is not this agent's tools token is repaired by minting a new one.
|
|
9
|
+
* The raw token is never logged.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { SECRET_ENV_VARS } from "@app-vitals/agent-lib/secret-env-vars";
|
|
13
|
+
import type { AgentEnvService } from "./agent-envs.ts";
|
|
14
|
+
import type { AgentTokenService } from "./agent-tokens.ts";
|
|
15
|
+
|
|
16
|
+
export const TOOLS_TOKEN_ENV_KEY = "SHIPWRIGHT_AGENT_TOOLS_API_KEY";
|
|
17
|
+
|
|
18
|
+
export type EnsureToolsTokenResult = "created" | "unchanged";
|
|
19
|
+
|
|
20
|
+
export interface EnsureAllToolsTokensResult {
|
|
21
|
+
created: string[];
|
|
22
|
+
unchanged: string[];
|
|
23
|
+
failed: Array<{ agentId: string; error: string }>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ToolsTokenEnsurer {
|
|
27
|
+
ensureToolsToken(agentId: string): Promise<EnsureToolsTokenResult>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class AgentToolsTokenService implements ToolsTokenEnsurer {
|
|
31
|
+
constructor(
|
|
32
|
+
private tokens: Pick<AgentTokenService, "create" | "validate">,
|
|
33
|
+
private envs: Pick<AgentEnvService, "patch" | "getValue">,
|
|
34
|
+
private logger: Pick<Console, "error"> = console,
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
async ensureToolsToken(agentId: string): Promise<EnsureToolsTokenResult> {
|
|
38
|
+
const stored = await this.envs.getValue(agentId, TOOLS_TOKEN_ENV_KEY);
|
|
39
|
+
if (stored) {
|
|
40
|
+
const valid = await this.tokens.validate(stored);
|
|
41
|
+
if (valid && valid.agentId === agentId && valid.kind === "tools") {
|
|
42
|
+
return "unchanged";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const { rawToken } = await this.tokens.create(
|
|
46
|
+
agentId,
|
|
47
|
+
"tools (auto)",
|
|
48
|
+
"tools",
|
|
49
|
+
);
|
|
50
|
+
await this.envs.patch(
|
|
51
|
+
agentId,
|
|
52
|
+
{ [TOOLS_TOKEN_ENV_KEY]: rawToken },
|
|
53
|
+
new Set(SECRET_ENV_VARS),
|
|
54
|
+
);
|
|
55
|
+
return "created";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Ensure every agent; one agent's failure is logged and never stops the rest. */
|
|
59
|
+
async ensureAll(
|
|
60
|
+
agents: ReadonlyArray<{ id: string }>,
|
|
61
|
+
): Promise<EnsureAllToolsTokensResult> {
|
|
62
|
+
const result: EnsureAllToolsTokensResult = {
|
|
63
|
+
created: [],
|
|
64
|
+
unchanged: [],
|
|
65
|
+
failed: [],
|
|
66
|
+
};
|
|
67
|
+
for (const { id } of agents) {
|
|
68
|
+
try {
|
|
69
|
+
result[await this.ensureToolsToken(id)].push(id);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
72
|
+
this.logger.error(
|
|
73
|
+
`[agent-tools-token] agent ${id}: ensure failed: ${error}`,
|
|
74
|
+
);
|
|
75
|
+
result.failed.push({ agentId: id, error });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import type { AgentTokenKind } from "./agent-tokens.ts";
|
|
3
|
+
import {
|
|
4
|
+
AgentToolsTokenService,
|
|
5
|
+
TOOLS_TOKEN_ENV_KEY,
|
|
6
|
+
} from "./agent-tools-token.ts";
|
|
7
|
+
|
|
8
|
+
function setup() {
|
|
9
|
+
const active = new Map<string, { agentId: string; kind: AgentTokenKind }>();
|
|
10
|
+
const env = new Map<string, string>();
|
|
11
|
+
const created: Array<{ agentId: string; kind: AgentTokenKind }> = [];
|
|
12
|
+
const patches: Array<{ key: string; secret: boolean }> = [];
|
|
13
|
+
const errors: string[] = [];
|
|
14
|
+
let n = 0;
|
|
15
|
+
const failFor = new Set<string>();
|
|
16
|
+
const svc = new AgentToolsTokenService(
|
|
17
|
+
{
|
|
18
|
+
create: async (agentId: string, _l?: string, kind = "runtime") => {
|
|
19
|
+
if (failFor.has(agentId)) throw new Error("boom");
|
|
20
|
+
const raw = `raw-${++n}`;
|
|
21
|
+
active.set(raw, { agentId, kind: kind as AgentTokenKind });
|
|
22
|
+
created.push({ agentId, kind: kind as AgentTokenKind });
|
|
23
|
+
return { token: {} as never, rawToken: raw };
|
|
24
|
+
},
|
|
25
|
+
validate: async (raw: string) => active.get(raw) ?? null,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
getValue: async (agentId: string) => env.get(agentId) ?? null,
|
|
29
|
+
patch: async (
|
|
30
|
+
agentId: string,
|
|
31
|
+
e: Record<string, string>,
|
|
32
|
+
secrets?: Set<string>,
|
|
33
|
+
) => {
|
|
34
|
+
for (const [k, v] of Object.entries(e)) {
|
|
35
|
+
env.set(agentId, v);
|
|
36
|
+
patches.push({ key: k, secret: secrets?.has(k) ?? false });
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{ error: (m: string) => void errors.push(m) },
|
|
41
|
+
);
|
|
42
|
+
return { svc, active, env, created, patches, errors, failFor };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("AgentToolsTokenService.ensureToolsToken", () => {
|
|
46
|
+
it("mints a tools token and stores it as a secret", async () => {
|
|
47
|
+
const t = setup();
|
|
48
|
+
expect(await t.svc.ensureToolsToken("a1")).toBe("created");
|
|
49
|
+
expect(t.created).toEqual([{ agentId: "a1", kind: "tools" }]);
|
|
50
|
+
expect(t.patches).toEqual([{ key: TOOLS_TOKEN_ENV_KEY, secret: true }]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("is idempotent", async () => {
|
|
54
|
+
const t = setup();
|
|
55
|
+
await t.svc.ensureToolsToken("a1");
|
|
56
|
+
expect(await t.svc.ensureToolsToken("a1")).toBe("unchanged");
|
|
57
|
+
expect(t.created).toHaveLength(1);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("repairs a missing env row", async () => {
|
|
61
|
+
const t = setup();
|
|
62
|
+
await t.svc.ensureToolsToken("a1");
|
|
63
|
+
t.env.delete("a1");
|
|
64
|
+
expect(await t.svc.ensureToolsToken("a1")).toBe("created");
|
|
65
|
+
expect(t.created).toHaveLength(2);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("repairs a revoked token", async () => {
|
|
69
|
+
const t = setup();
|
|
70
|
+
await t.svc.ensureToolsToken("a1");
|
|
71
|
+
t.active.clear();
|
|
72
|
+
expect(await t.svc.ensureToolsToken("a1")).toBe("created");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("replaces a stored value that is not this agent's tools token", async () => {
|
|
76
|
+
const t = setup();
|
|
77
|
+
t.active.set("rt", { agentId: "a1", kind: "runtime" });
|
|
78
|
+
t.env.set("a1", "rt");
|
|
79
|
+
expect(await t.svc.ensureToolsToken("a1")).toBe("created");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("ensureAll logs a failure without stopping the rest or leaking tokens", async () => {
|
|
83
|
+
const t = setup();
|
|
84
|
+
t.failFor.add("bad");
|
|
85
|
+
const r = await t.svc.ensureAll([
|
|
86
|
+
{ id: "a1" },
|
|
87
|
+
{ id: "bad" },
|
|
88
|
+
{ id: "a2" },
|
|
89
|
+
]);
|
|
90
|
+
expect(r.created).toEqual(["a1", "a2"]);
|
|
91
|
+
expect(r.failed.map((f) => f.agentId)).toEqual(["bad"]);
|
|
92
|
+
expect(t.errors).toHaveLength(1);
|
|
93
|
+
expect(t.errors[0]).not.toContain("raw-");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -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,
|