@app-vitals/agent-admin 1.127.0 → 1.128.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 +494 -0
- package/package.json +2 -2
- package/src/admin-boot.ts +7 -1
- package/src/agent-github-apps-api.smoke.test.ts +330 -0
- package/src/agent-github-apps.ts +65 -0
- package/src/agent-github-apps.unit.test.ts +79 -0
- package/src/agents-api.ts +146 -0
- package/src/api.ts +40 -7
- package/src/openapi-schemas.ts +99 -0
- package/src/tools-token-policy.ts +38 -0
- package/src/tools-token-policy.unit.test.ts +19 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smoke tests for the agent GitHub Apps admin CRUD routes and the `apps`
|
|
3
|
+
* member of the config bundle (multi-github-apps 1.2). Uses app.request() with
|
|
4
|
+
* the real AgentGitHubAppService over an in-memory Prisma double — no DB.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import {
|
|
9
|
+
AgentGitHubAppService,
|
|
10
|
+
type AgentGitHubAppView,
|
|
11
|
+
} from "./agent-github-apps.ts";
|
|
12
|
+
import { createAdminApp, parseAdminApiKeys } from "./agents-api.ts";
|
|
13
|
+
import { createAgentRuntimeApp } from "./api.ts";
|
|
14
|
+
import { identityCrypto } from "./token-crypto.ts";
|
|
15
|
+
|
|
16
|
+
const SESSION_SECRET = "test-session-secret-32bytes!!!!!";
|
|
17
|
+
const ADMIN_KEY = "gh-apps-admin-key";
|
|
18
|
+
const AGENT_ID = "agent-1";
|
|
19
|
+
const PEM =
|
|
20
|
+
"-----BEGIN RSA PRIVATE KEY-----\nMIIfake\n-----END RSA PRIVATE KEY-----";
|
|
21
|
+
|
|
22
|
+
interface Row {
|
|
23
|
+
id: string;
|
|
24
|
+
agentId: string;
|
|
25
|
+
appId: string;
|
|
26
|
+
slug: string | null;
|
|
27
|
+
clientId: string | null;
|
|
28
|
+
privateKeyEncrypted: string;
|
|
29
|
+
clientSecretEncrypted: string | null;
|
|
30
|
+
installationId: string | null;
|
|
31
|
+
priority: number;
|
|
32
|
+
createdAt: Date;
|
|
33
|
+
updatedAt: Date;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Minimal in-memory stand-in for the slice of PrismaClient the service uses. */
|
|
37
|
+
function fakePrisma(agentIds: string[]) {
|
|
38
|
+
const rows: Row[] = [];
|
|
39
|
+
let seq = 0;
|
|
40
|
+
const match = (r: Row, where: { id?: string; agentId?: string }) =>
|
|
41
|
+
(where.id === undefined || r.id === where.id) &&
|
|
42
|
+
(where.agentId === undefined || r.agentId === where.agentId);
|
|
43
|
+
return {
|
|
44
|
+
agent: {
|
|
45
|
+
findUnique: async ({ where }: { where: { id: string } }) =>
|
|
46
|
+
agentIds.includes(where.id) ? { id: where.id } : null,
|
|
47
|
+
},
|
|
48
|
+
agentGitHubApp: {
|
|
49
|
+
aggregate: async ({ where }: { where: { agentId: string } }) => {
|
|
50
|
+
const ps = rows
|
|
51
|
+
.filter((r) => r.agentId === where.agentId)
|
|
52
|
+
.map((r) => r.priority);
|
|
53
|
+
return { _max: { priority: ps.length ? Math.max(...ps) : null } };
|
|
54
|
+
},
|
|
55
|
+
create: async ({ data }: { data: Omit<Row, "id"> }) => {
|
|
56
|
+
if (
|
|
57
|
+
rows.some((r) => r.agentId === data.agentId && r.appId === data.appId)
|
|
58
|
+
) {
|
|
59
|
+
throw Object.assign(new Error("unique"), { code: "P2002" });
|
|
60
|
+
}
|
|
61
|
+
const row = { id: `row${++seq}`, ...data };
|
|
62
|
+
rows.push(row);
|
|
63
|
+
return row;
|
|
64
|
+
},
|
|
65
|
+
findMany: async ({ where }: { where: { agentId: string } }) =>
|
|
66
|
+
rows.filter((r) => match(r, where)),
|
|
67
|
+
findFirst: async ({
|
|
68
|
+
where,
|
|
69
|
+
}: {
|
|
70
|
+
where: { id: string; agentId: string };
|
|
71
|
+
}) => rows.find((r) => match(r, where)) ?? null,
|
|
72
|
+
updateMany: async ({
|
|
73
|
+
where,
|
|
74
|
+
data,
|
|
75
|
+
}: {
|
|
76
|
+
where: { id: string; agentId: string };
|
|
77
|
+
data: Partial<Row>;
|
|
78
|
+
}) => {
|
|
79
|
+
const hits = rows.filter((r) => match(r, where));
|
|
80
|
+
for (const h of hits) Object.assign(h, data);
|
|
81
|
+
return { count: hits.length };
|
|
82
|
+
},
|
|
83
|
+
deleteMany: async ({
|
|
84
|
+
where,
|
|
85
|
+
}: {
|
|
86
|
+
where: { id: string; agentId: string };
|
|
87
|
+
}) => {
|
|
88
|
+
const before = rows.length;
|
|
89
|
+
for (let i = rows.length - 1; i >= 0; i--) {
|
|
90
|
+
if (match(rows[i] as Row, where)) rows.splice(i, 1);
|
|
91
|
+
}
|
|
92
|
+
return { count: before - rows.length };
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function makeService() {
|
|
99
|
+
return new AgentGitHubAppService(
|
|
100
|
+
fakePrisma([AGENT_ID]) as never,
|
|
101
|
+
identityCrypto,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const adminKeys = parseAdminApiKeys(`admin:${ADMIN_KEY}:*`);
|
|
106
|
+
|
|
107
|
+
function makeAdminApp(service?: AgentGitHubAppService) {
|
|
108
|
+
return createAdminApp({
|
|
109
|
+
...(service ? { agentGitHubAppService: service } : {}),
|
|
110
|
+
sessionSecret: SESSION_SECRET,
|
|
111
|
+
adminApiKeys: adminKeys,
|
|
112
|
+
agentTokenService: { validate: async () => null },
|
|
113
|
+
agentMemberService: {
|
|
114
|
+
exists: async () => false,
|
|
115
|
+
listByEmail: async () => [],
|
|
116
|
+
},
|
|
117
|
+
agentService: { exists: async () => true },
|
|
118
|
+
} as never);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const call = (
|
|
122
|
+
app: {
|
|
123
|
+
request: (p: string, i?: RequestInit) => Response | Promise<Response>;
|
|
124
|
+
},
|
|
125
|
+
method: string,
|
|
126
|
+
path: string,
|
|
127
|
+
body?: unknown,
|
|
128
|
+
) =>
|
|
129
|
+
app.request(path, {
|
|
130
|
+
method,
|
|
131
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
132
|
+
headers: {
|
|
133
|
+
"Content-Type": "application/json",
|
|
134
|
+
Authorization: `Bearer ${ADMIN_KEY}`,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const base = `/agents/${AGENT_ID}/github-apps`;
|
|
139
|
+
|
|
140
|
+
describe("agent GitHub Apps admin API", () => {
|
|
141
|
+
it("creates an app and never echoes the keys", async () => {
|
|
142
|
+
const app = makeAdminApp(makeService());
|
|
143
|
+
const res = await call(app, "POST", base, {
|
|
144
|
+
appId: "12345",
|
|
145
|
+
slug: "my-app",
|
|
146
|
+
clientId: "Iv1.abcdef0123456789",
|
|
147
|
+
privateKey: PEM,
|
|
148
|
+
clientSecret: "s3cret",
|
|
149
|
+
});
|
|
150
|
+
expect(res.status).toBe(201);
|
|
151
|
+
const text = await res.text();
|
|
152
|
+
expect(text).not.toContain("MIIfake");
|
|
153
|
+
expect(text).not.toContain("s3cret");
|
|
154
|
+
const { app: created } = JSON.parse(text) as { app: AgentGitHubAppView };
|
|
155
|
+
expect(created.appId).toBe("12345");
|
|
156
|
+
expect(created.hasPrivateKey).toBe(true);
|
|
157
|
+
expect(created.hasClientSecret).toBe(true);
|
|
158
|
+
expect(created.priority).toBe(0);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("lists apps in priority order with masked keys", async () => {
|
|
162
|
+
const app = makeAdminApp(makeService());
|
|
163
|
+
await call(app, "POST", base, { appId: "1", privateKey: PEM, priority: 5 });
|
|
164
|
+
await call(app, "POST", base, { appId: "2", privateKey: PEM, priority: 1 });
|
|
165
|
+
const res = await call(app, "GET", base);
|
|
166
|
+
expect(res.status).toBe(200);
|
|
167
|
+
const text = await res.text();
|
|
168
|
+
expect(text).not.toContain("MIIfake");
|
|
169
|
+
const body = JSON.parse(text) as { apps: AgentGitHubAppView[] };
|
|
170
|
+
expect(body.apps.map((a) => a.appId)).toEqual(["2", "1"]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("updates metadata, rotates the key write-only, and 404s an unknown app", async () => {
|
|
174
|
+
const app = makeAdminApp(makeService());
|
|
175
|
+
const created = (await (
|
|
176
|
+
await call(app, "POST", base, { appId: "1", privateKey: PEM })
|
|
177
|
+
).json()) as { app: AgentGitHubAppView };
|
|
178
|
+
const res = await call(app, "PATCH", `${base}/${created.app.id}`, {
|
|
179
|
+
slug: "renamed",
|
|
180
|
+
privateKey: "NEWKEY",
|
|
181
|
+
priority: 3,
|
|
182
|
+
});
|
|
183
|
+
expect(res.status).toBe(200);
|
|
184
|
+
const text = await res.text();
|
|
185
|
+
expect(text).not.toContain("NEWKEY");
|
|
186
|
+
const updated = (JSON.parse(text) as { app: AgentGitHubAppView }).app;
|
|
187
|
+
expect(updated.slug).toBe("renamed");
|
|
188
|
+
expect(updated.priority).toBe(3);
|
|
189
|
+
expect(
|
|
190
|
+
(await call(app, "PATCH", `${base}/nope`, { slug: "x" })).status,
|
|
191
|
+
).toBe(404);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("answers 409 for a duplicate appId and 422 for invalid input", async () => {
|
|
195
|
+
const app = makeAdminApp(makeService());
|
|
196
|
+
await call(app, "POST", base, { appId: "1", privateKey: PEM });
|
|
197
|
+
expect(
|
|
198
|
+
(await call(app, "POST", base, { appId: "1", privateKey: PEM })).status,
|
|
199
|
+
).toBe(409);
|
|
200
|
+
expect(
|
|
201
|
+
(await call(app, "POST", base, { appId: "abc", privateKey: PEM })).status,
|
|
202
|
+
).toBe(422);
|
|
203
|
+
expect((await call(app, "POST", base, { appId: "9" })).status).toBe(400);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("deletes an app (204) and 404s a second delete", async () => {
|
|
207
|
+
const app = makeAdminApp(makeService());
|
|
208
|
+
const created = (await (
|
|
209
|
+
await call(app, "POST", base, { appId: "1", privateKey: PEM })
|
|
210
|
+
).json()) as { app: AgentGitHubAppView };
|
|
211
|
+
expect(
|
|
212
|
+
(await call(app, "DELETE", `${base}/${created.app.id}`)).status,
|
|
213
|
+
).toBe(204);
|
|
214
|
+
expect(
|
|
215
|
+
(await call(app, "DELETE", `${base}/${created.app.id}`)).status,
|
|
216
|
+
).toBe(404);
|
|
217
|
+
expect(
|
|
218
|
+
((await (await call(app, "GET", base)).json()) as { apps: unknown[] })
|
|
219
|
+
.apps,
|
|
220
|
+
).toEqual([]);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("answers 503 when the service is not wired", async () => {
|
|
224
|
+
expect((await call(makeAdminApp(), "GET", base)).status).toBe(503);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("requires auth", async () => {
|
|
228
|
+
const res = await makeAdminApp(makeService()).request(base);
|
|
229
|
+
expect(res.status).toBe(401);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("config bundle apps", () => {
|
|
234
|
+
const bundleEnv = { GH_APP_ID: "12345", GH_APP_PRIVATE_KEY: PEM };
|
|
235
|
+
|
|
236
|
+
function makeRuntimeApp(opts: {
|
|
237
|
+
withApps: boolean;
|
|
238
|
+
service?: AgentGitHubAppService;
|
|
239
|
+
}) {
|
|
240
|
+
return createAgentRuntimeApp({
|
|
241
|
+
agentEnvService: {
|
|
242
|
+
getConfigBundle: async () => ({ env: bundleEnv, allowedTools: [] }),
|
|
243
|
+
},
|
|
244
|
+
agentCronJobService: { list: async () => [] },
|
|
245
|
+
agentService: {
|
|
246
|
+
getById: async () => ({
|
|
247
|
+
id: AGENT_ID,
|
|
248
|
+
repos: [],
|
|
249
|
+
reviewAuthorAllowlist: [],
|
|
250
|
+
patchAuthorAllowlist: [],
|
|
251
|
+
restrictSlackToMembers: false,
|
|
252
|
+
memberEmails: [],
|
|
253
|
+
specialty: "coding",
|
|
254
|
+
autoPostReviews: true,
|
|
255
|
+
allowSelfReview: false,
|
|
256
|
+
minConfidence: 75,
|
|
257
|
+
maxFindings: 5,
|
|
258
|
+
cleanupMergedWorktrees: true,
|
|
259
|
+
cleanupAfterDays: 14,
|
|
260
|
+
}),
|
|
261
|
+
},
|
|
262
|
+
agentPluginService: { listEnabled: async () => [] },
|
|
263
|
+
...(opts.withApps && opts.service
|
|
264
|
+
? { agentGitHubAppService: opts.service }
|
|
265
|
+
: {}),
|
|
266
|
+
sessionSecret: SESSION_SECRET,
|
|
267
|
+
adminApiKeys: adminKeys,
|
|
268
|
+
agentTokenService: { validate: async () => null },
|
|
269
|
+
} as never);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
it("carries apps with keys and still emits the legacy GH_APP_* env vars", async () => {
|
|
273
|
+
const service = makeService();
|
|
274
|
+
await service.create(AGENT_ID, {
|
|
275
|
+
appId: "12345",
|
|
276
|
+
privateKey: PEM,
|
|
277
|
+
clientSecret: "s3cret",
|
|
278
|
+
slug: "my-app",
|
|
279
|
+
});
|
|
280
|
+
await service.create(AGENT_ID, { appId: "67890", privateKey: "K2" });
|
|
281
|
+
const res = await call(
|
|
282
|
+
makeRuntimeApp({ withApps: true, service }),
|
|
283
|
+
"GET",
|
|
284
|
+
`/${AGENT_ID}/config`,
|
|
285
|
+
);
|
|
286
|
+
expect(res.status).toBe(200);
|
|
287
|
+
const body = (await res.json()) as {
|
|
288
|
+
env: Record<string, string>;
|
|
289
|
+
apps: Array<Record<string, unknown>>;
|
|
290
|
+
};
|
|
291
|
+
expect(body.env.GH_APP_ID).toBe("12345");
|
|
292
|
+
expect(body.env.GH_APP_PRIVATE_KEY).toBe(PEM);
|
|
293
|
+
expect(body.apps.map((a) => a.appId)).toEqual(["12345", "67890"]);
|
|
294
|
+
expect(body.apps[0]).toMatchObject({
|
|
295
|
+
privateKey: PEM,
|
|
296
|
+
clientSecret: "s3cret",
|
|
297
|
+
slug: "my-app",
|
|
298
|
+
priority: 0,
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("omits apps when the service is not wired (additive)", async () => {
|
|
303
|
+
const res = await call(
|
|
304
|
+
makeRuntimeApp({ withApps: false }),
|
|
305
|
+
"GET",
|
|
306
|
+
`/${AGENT_ID}/config`,
|
|
307
|
+
);
|
|
308
|
+
const body = (await res.json()) as Record<string, unknown>;
|
|
309
|
+
expect(res.status).toBe(200);
|
|
310
|
+
expect("apps" in body).toBe(false);
|
|
311
|
+
expect((body.env as Record<string, string>).GH_APP_ID).toBe("12345");
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("returns apps: [] for an agent without apps", async () => {
|
|
315
|
+
const res = await call(
|
|
316
|
+
makeRuntimeApp({ withApps: true, service: makeService() }),
|
|
317
|
+
"GET",
|
|
318
|
+
`/${AGENT_ID}/config`,
|
|
319
|
+
);
|
|
320
|
+
expect(((await res.json()) as { apps: unknown[] }).apps).toEqual([]);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("never exposes keys on the admin read route", async () => {
|
|
324
|
+
const service = makeService();
|
|
325
|
+
await service.create(AGENT_ID, { appId: "12345", privateKey: PEM });
|
|
326
|
+
const text = await (await call(makeAdminApp(service), "GET", base)).text();
|
|
327
|
+
expect(text).not.toContain("MIIfake");
|
|
328
|
+
expect(text).not.toContain('privateKey"');
|
|
329
|
+
});
|
|
330
|
+
});
|
package/src/agent-github-apps.ts
CHANGED
|
@@ -150,6 +150,71 @@ export function toAgentGitHubAppView(
|
|
|
150
150
|
};
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
/** Wire shape of a read: the view with ISO dates. Key fields are flags only. */
|
|
154
|
+
export interface AgentGitHubAppWire {
|
|
155
|
+
id: string;
|
|
156
|
+
agentId: string;
|
|
157
|
+
appId: string;
|
|
158
|
+
slug: string | null;
|
|
159
|
+
clientId: string | null;
|
|
160
|
+
installationId: string | null;
|
|
161
|
+
priority: number;
|
|
162
|
+
hasPrivateKey: boolean;
|
|
163
|
+
hasClientSecret: boolean;
|
|
164
|
+
createdAt: string;
|
|
165
|
+
updatedAt: string;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Serializer for the admin read routes. Picks the declared fields explicitly,
|
|
170
|
+
* so nothing else on the input (key material included) can reach a response.
|
|
171
|
+
*/
|
|
172
|
+
export function serializeAgentGitHubApp(
|
|
173
|
+
view: AgentGitHubAppView,
|
|
174
|
+
): AgentGitHubAppWire {
|
|
175
|
+
return {
|
|
176
|
+
id: view.id,
|
|
177
|
+
agentId: view.agentId,
|
|
178
|
+
appId: view.appId,
|
|
179
|
+
slug: view.slug,
|
|
180
|
+
clientId: view.clientId,
|
|
181
|
+
installationId: view.installationId,
|
|
182
|
+
priority: view.priority,
|
|
183
|
+
hasPrivateKey: view.hasPrivateKey,
|
|
184
|
+
hasClientSecret: view.hasClientSecret,
|
|
185
|
+
createdAt: view.createdAt.toISOString(),
|
|
186
|
+
updatedAt: view.updatedAt.toISOString(),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** One app in the agent config bundle (keys included; agent-authenticated route only). */
|
|
191
|
+
export interface AgentConfigBundleApp {
|
|
192
|
+
id: string;
|
|
193
|
+
appId: string;
|
|
194
|
+
slug: string | null;
|
|
195
|
+
clientId: string | null;
|
|
196
|
+
installationId: string | null;
|
|
197
|
+
priority: number;
|
|
198
|
+
privateKey: string;
|
|
199
|
+
clientSecret: string | null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Maps decrypted credentials (already in priority order) to the bundle shape. */
|
|
203
|
+
export function toConfigBundleApps(
|
|
204
|
+
credentials: readonly AgentGitHubAppCredentials[],
|
|
205
|
+
): AgentConfigBundleApp[] {
|
|
206
|
+
return credentials.map((c) => ({
|
|
207
|
+
id: c.id,
|
|
208
|
+
appId: c.appId,
|
|
209
|
+
slug: c.slug,
|
|
210
|
+
clientId: c.clientId,
|
|
211
|
+
installationId: c.installationId,
|
|
212
|
+
priority: c.priority,
|
|
213
|
+
privateKey: c.privateKey,
|
|
214
|
+
clientSecret: c.clientSecret,
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
|
|
153
218
|
/** The legacy env keys the backfill reads. */
|
|
154
219
|
export const GH_APP_ENV_KEYS = [
|
|
155
220
|
"GH_APP_ID",
|
|
@@ -2,7 +2,9 @@ import { describe, expect, it } from "bun:test";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import {
|
|
4
4
|
planGitHubAppBackfill,
|
|
5
|
+
serializeAgentGitHubApp,
|
|
5
6
|
sortAgentGitHubApps,
|
|
7
|
+
toConfigBundleApps,
|
|
6
8
|
} from "./agent-github-apps.ts";
|
|
7
9
|
import type { TokenCrypto } from "./token-crypto.ts";
|
|
8
10
|
|
|
@@ -264,3 +266,80 @@ describe("20261204000000_add_agent_github_app migration", () => {
|
|
|
264
266
|
}
|
|
265
267
|
});
|
|
266
268
|
});
|
|
269
|
+
|
|
270
|
+
describe("serializeAgentGitHubApp (masking)", () => {
|
|
271
|
+
const view = {
|
|
272
|
+
id: "row1",
|
|
273
|
+
agentId: "agent1",
|
|
274
|
+
appId: "12345",
|
|
275
|
+
slug: "my-app",
|
|
276
|
+
clientId: "Iv1.abcdef0123456789",
|
|
277
|
+
installationId: null,
|
|
278
|
+
priority: 0,
|
|
279
|
+
hasPrivateKey: true,
|
|
280
|
+
hasClientSecret: false,
|
|
281
|
+
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
282
|
+
updatedAt: new Date("2026-01-02T00:00:00.000Z"),
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
it("emits ISO dates and the key flags", () => {
|
|
286
|
+
expect(serializeAgentGitHubApp(view)).toEqual({
|
|
287
|
+
...view,
|
|
288
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
289
|
+
updatedAt: "2026-01-02T00:00:00.000Z",
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("drops anything that is not a declared read field (no key leakage)", () => {
|
|
294
|
+
const leaky = {
|
|
295
|
+
...view,
|
|
296
|
+
privateKey: PEM,
|
|
297
|
+
privateKeyEncrypted: "cipher",
|
|
298
|
+
clientSecret: "s3cret",
|
|
299
|
+
clientSecretEncrypted: "cipher2",
|
|
300
|
+
};
|
|
301
|
+
const json = JSON.stringify(serializeAgentGitHubApp(leaky));
|
|
302
|
+
for (const needle of [PEM, "cipher", "s3cret", "privateKey"]) {
|
|
303
|
+
expect(json).not.toContain(needle);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
describe("toConfigBundleApps", () => {
|
|
309
|
+
it("carries the decrypted keys in priority order for the runtime", () => {
|
|
310
|
+
const apps = toConfigBundleApps([
|
|
311
|
+
{
|
|
312
|
+
id: "row1",
|
|
313
|
+
appId: "12345",
|
|
314
|
+
slug: "my-app",
|
|
315
|
+
clientId: null,
|
|
316
|
+
installationId: "99",
|
|
317
|
+
priority: 0,
|
|
318
|
+
privateKey: PEM,
|
|
319
|
+
clientSecret: "s3cret",
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
id: "row2",
|
|
323
|
+
appId: "67890",
|
|
324
|
+
slug: null,
|
|
325
|
+
clientId: null,
|
|
326
|
+
installationId: null,
|
|
327
|
+
priority: 1,
|
|
328
|
+
privateKey: PEM,
|
|
329
|
+
clientSecret: null,
|
|
330
|
+
},
|
|
331
|
+
]);
|
|
332
|
+
expect(apps.map((a) => a.appId)).toEqual(["12345", "67890"]);
|
|
333
|
+
expect(apps[0]).toEqual({
|
|
334
|
+
id: "row1",
|
|
335
|
+
appId: "12345",
|
|
336
|
+
slug: "my-app",
|
|
337
|
+
clientId: null,
|
|
338
|
+
installationId: "99",
|
|
339
|
+
priority: 0,
|
|
340
|
+
privateKey: PEM,
|
|
341
|
+
clientSecret: "s3cret",
|
|
342
|
+
});
|
|
343
|
+
expect(apps[1]?.clientSecret).toBeNull();
|
|
344
|
+
});
|
|
345
|
+
});
|
package/src/agents-api.ts
CHANGED
|
@@ -60,6 +60,10 @@ import type { AgentCronRunService } from "./agent-cron-runs.ts";
|
|
|
60
60
|
import type { DeleteAgentFullyDeps } from "./agent-deletion.ts";
|
|
61
61
|
import { deleteAgentFully } from "./agent-deletion.ts";
|
|
62
62
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
63
|
+
import {
|
|
64
|
+
type AgentGitHubAppService,
|
|
65
|
+
serializeAgentGitHubApp,
|
|
66
|
+
} from "./agent-github-apps.ts";
|
|
63
67
|
import type { AgentGitHubInstallationsService } from "./agent-github-installations.ts";
|
|
64
68
|
import { AgentMarketplaceService } from "./agent-marketplaces.ts";
|
|
65
69
|
import type { AgentMemberService } from "./agent-members.ts";
|
|
@@ -126,6 +130,8 @@ import {
|
|
|
126
130
|
AgentEnvBodySchema,
|
|
127
131
|
AgentEnvPatchBodySchema,
|
|
128
132
|
AgentEnvResponseSchema,
|
|
133
|
+
AgentGitHubAppIdParamSchema,
|
|
134
|
+
AgentGitHubAppSchema,
|
|
129
135
|
AgentGitHubInstallationsSnapshotSchema,
|
|
130
136
|
AgentIdParamSchema,
|
|
131
137
|
AgentMarketplaceSchema,
|
|
@@ -140,6 +146,7 @@ import {
|
|
|
140
146
|
CreateAgentBodySchema,
|
|
141
147
|
CreateAgentCronJobBodySchema,
|
|
142
148
|
CreateAgentCronRunBodySchema,
|
|
149
|
+
CreateAgentGitHubAppBodySchema,
|
|
143
150
|
CreateAgentPluginBodySchema,
|
|
144
151
|
CreateAgentTokenBodySchema,
|
|
145
152
|
CreateAgentTokenResponseSchema,
|
|
@@ -170,6 +177,7 @@ import {
|
|
|
170
177
|
PutGitHubInstallationsBodySchema,
|
|
171
178
|
TokenIdParamSchema,
|
|
172
179
|
ToolIdParamSchema,
|
|
180
|
+
UpdateAgentGitHubAppBodySchema,
|
|
173
181
|
UpsertAgentMarketplaceBodySchema,
|
|
174
182
|
UpsertChatTokenDailyBodySchema,
|
|
175
183
|
} from "./openapi-schemas.ts";
|
|
@@ -249,6 +257,14 @@ export interface AdminDeps {
|
|
|
249
257
|
AgentMarketplaceService,
|
|
250
258
|
"list" | "upsert" | "remove"
|
|
251
259
|
>;
|
|
260
|
+
/**
|
|
261
|
+
* multi-github-apps 1.2: admin CRUD for the agent's GitHub Apps. Optional
|
|
262
|
+
* (additive); absent means the /agents/{id}/github-apps routes answer 503.
|
|
263
|
+
*/
|
|
264
|
+
agentGitHubAppService?: Pick<
|
|
265
|
+
AgentGitHubAppService,
|
|
266
|
+
"list" | "create" | "update" | "delete"
|
|
267
|
+
>;
|
|
252
268
|
agentMemberService: Pick<
|
|
253
269
|
AgentMemberService,
|
|
254
270
|
"add" | "listByAgentId" | "exists" | "listByEmail"
|
|
@@ -1120,6 +1136,96 @@ const deleteEnvKeyRoute = createRoute({
|
|
|
1120
1136
|
},
|
|
1121
1137
|
});
|
|
1122
1138
|
|
|
1139
|
+
const GitHubAppWrapperSchema = z
|
|
1140
|
+
.object({ app: AgentGitHubAppSchema })
|
|
1141
|
+
.openapi("AgentGitHubAppWrapper");
|
|
1142
|
+
|
|
1143
|
+
const GitHubAppsWrapperSchema = z
|
|
1144
|
+
.object({ apps: z.array(AgentGitHubAppSchema) })
|
|
1145
|
+
.openapi("AgentGitHubAppsWrapper");
|
|
1146
|
+
|
|
1147
|
+
const listGitHubAppsRoute = createRoute({
|
|
1148
|
+
method: "get",
|
|
1149
|
+
path: "/agents/{id}/github-apps",
|
|
1150
|
+
summary: "List GitHub Apps",
|
|
1151
|
+
description:
|
|
1152
|
+
"Returns the agent's GitHub Apps in priority order (lowest wins). Private keys and client secrets are write-only: only `hasPrivateKey` / `hasClientSecret` flags are returned.",
|
|
1153
|
+
request: { params: AgentIdParamSchema },
|
|
1154
|
+
responses: {
|
|
1155
|
+
200: {
|
|
1156
|
+
description: "List of GitHub Apps",
|
|
1157
|
+
content: { "application/json": { schema: GitHubAppsWrapperSchema } },
|
|
1158
|
+
},
|
|
1159
|
+
503: { description: "GitHub Apps not configured", ...jsonError },
|
|
1160
|
+
},
|
|
1161
|
+
});
|
|
1162
|
+
|
|
1163
|
+
const createGitHubAppRoute = createRoute({
|
|
1164
|
+
method: "post",
|
|
1165
|
+
path: "/agents/{id}/github-apps",
|
|
1166
|
+
summary: "Add a GitHub App",
|
|
1167
|
+
description:
|
|
1168
|
+
"Adds a GitHub App to the agent. The private key and client secret are encrypted and never returned. 409 when the agent already has this `appId`; 422 on an invalid field.",
|
|
1169
|
+
request: {
|
|
1170
|
+
params: AgentIdParamSchema,
|
|
1171
|
+
body: {
|
|
1172
|
+
content: {
|
|
1173
|
+
"application/json": { schema: CreateAgentGitHubAppBodySchema },
|
|
1174
|
+
},
|
|
1175
|
+
},
|
|
1176
|
+
},
|
|
1177
|
+
responses: {
|
|
1178
|
+
201: {
|
|
1179
|
+
description: "GitHub App added",
|
|
1180
|
+
content: { "application/json": { schema: GitHubAppWrapperSchema } },
|
|
1181
|
+
},
|
|
1182
|
+
400: { description: "Bad request", ...jsonError },
|
|
1183
|
+
409: { description: "App already added", ...jsonError },
|
|
1184
|
+
422: { description: "Invalid field", ...jsonError },
|
|
1185
|
+
503: { description: "GitHub Apps not configured", ...jsonError },
|
|
1186
|
+
},
|
|
1187
|
+
});
|
|
1188
|
+
|
|
1189
|
+
const updateGitHubAppRoute = createRoute({
|
|
1190
|
+
method: "patch",
|
|
1191
|
+
path: "/agents/{id}/github-apps/{appRowId}",
|
|
1192
|
+
summary: "Update a GitHub App",
|
|
1193
|
+
description:
|
|
1194
|
+
"Partial update: omitted fields are unchanged, null clears a nullable field. `privateKey` / `clientSecret` replace the stored secret and are never returned.",
|
|
1195
|
+
request: {
|
|
1196
|
+
params: AgentGitHubAppIdParamSchema,
|
|
1197
|
+
body: {
|
|
1198
|
+
content: {
|
|
1199
|
+
"application/json": { schema: UpdateAgentGitHubAppBodySchema },
|
|
1200
|
+
},
|
|
1201
|
+
},
|
|
1202
|
+
},
|
|
1203
|
+
responses: {
|
|
1204
|
+
200: {
|
|
1205
|
+
description: "GitHub App updated",
|
|
1206
|
+
content: { "application/json": { schema: GitHubAppWrapperSchema } },
|
|
1207
|
+
},
|
|
1208
|
+
400: { description: "Bad request", ...jsonError },
|
|
1209
|
+
404: { description: "GitHub App not found", ...jsonError },
|
|
1210
|
+
422: { description: "Invalid field", ...jsonError },
|
|
1211
|
+
503: { description: "GitHub Apps not configured", ...jsonError },
|
|
1212
|
+
},
|
|
1213
|
+
});
|
|
1214
|
+
|
|
1215
|
+
const deleteGitHubAppRoute = createRoute({
|
|
1216
|
+
method: "delete",
|
|
1217
|
+
path: "/agents/{id}/github-apps/{appRowId}",
|
|
1218
|
+
summary: "Remove a GitHub App",
|
|
1219
|
+
description:
|
|
1220
|
+
"Removes the app row by id. 404 when the id does not exist or belongs to a different agent.",
|
|
1221
|
+
request: { params: AgentGitHubAppIdParamSchema },
|
|
1222
|
+
responses: {
|
|
1223
|
+
204: { description: "GitHub App removed" },
|
|
1224
|
+
404: { description: "GitHub App not found", ...jsonError },
|
|
1225
|
+
503: { description: "GitHub Apps not configured", ...jsonError },
|
|
1226
|
+
},
|
|
1227
|
+
});
|
|
1228
|
+
|
|
1123
1229
|
const createCronRoute = createRoute({
|
|
1124
1230
|
method: "post",
|
|
1125
1231
|
path: "/agents/{id}/crons",
|
|
@@ -3352,6 +3458,46 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
3352
3458
|
return c.body(null, 204);
|
|
3353
3459
|
});
|
|
3354
3460
|
|
|
3461
|
+
// ─── GitHub Apps (multi-github-apps 1.2) ───────────────────────────────────
|
|
3462
|
+
// Keys are write-only: every response goes through serializeAgentGitHubApp.
|
|
3463
|
+
|
|
3464
|
+
const gitHubAppService = () => {
|
|
3465
|
+
if (!deps.agentGitHubAppService) {
|
|
3466
|
+
throw new ApiError(503, "GitHub Apps are not configured");
|
|
3467
|
+
}
|
|
3468
|
+
return deps.agentGitHubAppService;
|
|
3469
|
+
};
|
|
3470
|
+
|
|
3471
|
+
app.openapi(listGitHubAppsRoute, async (c) => {
|
|
3472
|
+
const { id: agentId } = c.req.valid("param");
|
|
3473
|
+
const rows = await gitHubAppService().list(agentId);
|
|
3474
|
+
return c.json({ apps: rows.map(serializeAgentGitHubApp) }, 200);
|
|
3475
|
+
});
|
|
3476
|
+
|
|
3477
|
+
app.openapi(createGitHubAppRoute, async (c) => {
|
|
3478
|
+
const { id: agentId } = c.req.valid("param");
|
|
3479
|
+
const row = await gitHubAppService().create(agentId, c.req.valid("json"));
|
|
3480
|
+
return c.json({ app: serializeAgentGitHubApp(row) }, 201);
|
|
3481
|
+
});
|
|
3482
|
+
|
|
3483
|
+
app.openapi(updateGitHubAppRoute, async (c) => {
|
|
3484
|
+
const { id: agentId, appRowId } = c.req.valid("param");
|
|
3485
|
+
const row = await gitHubAppService().update(
|
|
3486
|
+
agentId,
|
|
3487
|
+
appRowId,
|
|
3488
|
+
c.req.valid("json"),
|
|
3489
|
+
);
|
|
3490
|
+
return c.json({ app: serializeAgentGitHubApp(row) }, 200);
|
|
3491
|
+
});
|
|
3492
|
+
|
|
3493
|
+
app.openapi(deleteGitHubAppRoute, async (c) => {
|
|
3494
|
+
const { id: agentId, appRowId } = c.req.valid("param");
|
|
3495
|
+
if (!(await gitHubAppService().delete(agentId, appRowId))) {
|
|
3496
|
+
throw new NotFoundError("GitHub App not found");
|
|
3497
|
+
}
|
|
3498
|
+
return c.body(null, 204);
|
|
3499
|
+
});
|
|
3500
|
+
|
|
3355
3501
|
// ─── Cron-run token stats ──────────────────────────────────────────────────
|
|
3356
3502
|
|
|
3357
3503
|
// GET /agents/all/cron-runs/stats — aggregated token stats across all agents
|