@app-vitals/agent-admin 1.124.0 → 1.126.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/package.json +2 -2
- package/prisma/client/browser.ts +5 -0
- package/prisma/client/client.ts +5 -0
- package/prisma/client/internal/class.ts +14 -4
- package/prisma/client/internal/prismaNamespace.ts +94 -1
- package/prisma/client/internal/prismaNamespaceBrowser.ts +18 -0
- package/prisma/client/models/Agent.ts +306 -0
- package/prisma/client/models/AgentGitHubApp.ts +1605 -0
- package/prisma/client/models.ts +1 -0
- package/prisma/migrations/20261204000000_add_agent_github_app/migration.sql +31 -0
- package/prisma/schema/schema.prisma +32 -2
- package/src/admin-boot.boot-order.unit.test.ts +12 -0
- package/src/admin-boot.ts +18 -2
- package/src/admin-ui-settings-layout.ts +8 -14
- package/src/admin-ui-settings-layout.unit.test.ts +42 -1
- package/src/admin-ui-settings-nav-context.smoke.test.ts +1 -1
- package/src/admin-ui-settings.smoke.test.ts +2 -2
- package/src/agent-github-apps.integration.test.ts +618 -0
- package/src/agent-github-apps.ts +591 -0
- package/src/agent-github-apps.unit.test.ts +266 -0
- package/src/github-enterprise-isolation.unit.test.ts +8 -1
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/agent-github-apps.integration.test.ts
|
|
3
|
+
* AgentGitHubAppService and the GH_APP_* env backfill against a real
|
|
4
|
+
* PostgreSQL DB, with real AES-256-GCM crypto.
|
|
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 type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
|
+
import { AgentEnvService } from "./agent-envs.ts";
|
|
12
|
+
import {
|
|
13
|
+
AgentGitHubAppService,
|
|
14
|
+
backfillAgentGitHubAppsFromEnv,
|
|
15
|
+
} from "./agent-github-apps.ts";
|
|
16
|
+
import {
|
|
17
|
+
ConflictError,
|
|
18
|
+
NotFoundError,
|
|
19
|
+
UnprocessableEntityError,
|
|
20
|
+
} from "./errors.ts";
|
|
21
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
22
|
+
import { makeTokenCrypto, type TokenCrypto } from "./token-crypto.ts";
|
|
23
|
+
|
|
24
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
25
|
+
|
|
26
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
27
|
+
|
|
28
|
+
const TEST_KEY =
|
|
29
|
+
"0000000000000000000000000000000000000000000000000000000000000001";
|
|
30
|
+
|
|
31
|
+
const PEM_A =
|
|
32
|
+
"-----BEGIN RSA PRIVATE KEY-----\nMIIfakeKeyA\n-----END RSA PRIVATE KEY-----";
|
|
33
|
+
const PEM_B =
|
|
34
|
+
"-----BEGIN RSA PRIVATE KEY-----\nMIIfakeKeyB\n-----END RSA PRIVATE KEY-----";
|
|
35
|
+
|
|
36
|
+
const silentLogger = { log: () => {}, warn: () => {} };
|
|
37
|
+
|
|
38
|
+
// Fixtures mirror the distinct GH_APP_* sets the provisioning paths write.
|
|
39
|
+
const FULL_SET = {
|
|
40
|
+
// manifest flow, then installed on one org
|
|
41
|
+
GH_APP_ID: "2001",
|
|
42
|
+
GH_APP_SLUG: "example-agent-app",
|
|
43
|
+
GH_APP_CLIENT_ID: "Iv1.0123abcd",
|
|
44
|
+
GH_APP_CLIENT_SECRET: "example-client-secret",
|
|
45
|
+
GH_APP_PRIVATE_KEY: PEM_A,
|
|
46
|
+
GH_APP_INSTALLATION_ID: "90001",
|
|
47
|
+
};
|
|
48
|
+
const NO_INSTALLATION_SET = {
|
|
49
|
+
// manifest flow, installations discovered at runtime (no pin)
|
|
50
|
+
GH_APP_ID: "2002",
|
|
51
|
+
GH_APP_SLUG: "example-agent-app-two",
|
|
52
|
+
GH_APP_CLIENT_ID: "Iv1.4567efgh",
|
|
53
|
+
GH_APP_CLIENT_SECRET: "another-client-secret",
|
|
54
|
+
GH_APP_PRIVATE_KEY: PEM_B,
|
|
55
|
+
};
|
|
56
|
+
const MANUAL_PASTE_SET = {
|
|
57
|
+
// manual paste: no slug, client id or client secret
|
|
58
|
+
GH_APP_ID: "2003",
|
|
59
|
+
GH_APP_INSTALLATION_ID: "90003",
|
|
60
|
+
GH_APP_PRIVATE_KEY: PEM_A,
|
|
61
|
+
};
|
|
62
|
+
const ID_ONLY_SET = { GH_APP_ID: "2004" };
|
|
63
|
+
const ID_SLUG_NO_KEY_SET = {
|
|
64
|
+
GH_APP_ID: "2005",
|
|
65
|
+
GH_APP_SLUG: "example-half-app",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
describeOrSkip("AgentGitHubAppService (integration)", () => {
|
|
69
|
+
let prisma: PrismaClient;
|
|
70
|
+
let crypto: TokenCrypto;
|
|
71
|
+
let service: AgentGitHubAppService;
|
|
72
|
+
let envService: AgentEnvService;
|
|
73
|
+
|
|
74
|
+
async function createAgent(name = "Test Agent"): Promise<string> {
|
|
75
|
+
return (await prisma.agent.create({ data: { name } })).id;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function seedEnv(
|
|
79
|
+
agentId: string,
|
|
80
|
+
env: Record<string, string>,
|
|
81
|
+
): Promise<void> {
|
|
82
|
+
await envService.patch(
|
|
83
|
+
agentId,
|
|
84
|
+
env,
|
|
85
|
+
new Set(["GH_APP_PRIVATE_KEY", "GH_APP_CLIENT_SECRET"]),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function envSnapshot(agentId: string) {
|
|
90
|
+
return prisma.agentEnv.findMany({
|
|
91
|
+
where: { agentId },
|
|
92
|
+
orderBy: { key: "asc" },
|
|
93
|
+
select: {
|
|
94
|
+
key: true,
|
|
95
|
+
value: true,
|
|
96
|
+
enabled: true,
|
|
97
|
+
secret: true,
|
|
98
|
+
updatedAt: true,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
beforeEach(async () => {
|
|
104
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
105
|
+
// Child tables cascade with Agent; deleted explicitly for clarity.
|
|
106
|
+
await prisma.agentGitHubApp.deleteMany();
|
|
107
|
+
await prisma.agentToken.deleteMany();
|
|
108
|
+
await prisma.agentCronJob.deleteMany();
|
|
109
|
+
await prisma.agentTool.deleteMany();
|
|
110
|
+
await prisma.agentEnv.deleteMany();
|
|
111
|
+
await prisma.agent.deleteMany();
|
|
112
|
+
crypto = makeTokenCrypto(TEST_KEY);
|
|
113
|
+
service = new AgentGitHubAppService(prisma, crypto);
|
|
114
|
+
envService = new AgentEnvService(prisma, crypto);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
afterEach(async () => {
|
|
118
|
+
await prisma.$disconnect();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// ── CRUD ──────────────────────────────────────────────────────────────────
|
|
122
|
+
|
|
123
|
+
describe("CRUD", () => {
|
|
124
|
+
it("create() returns a view with key flags and no key material", async () => {
|
|
125
|
+
const agentId = await createAgent();
|
|
126
|
+
const view = await service.create(agentId, {
|
|
127
|
+
appId: "3001",
|
|
128
|
+
slug: "example-app",
|
|
129
|
+
clientId: "Iv1.abc",
|
|
130
|
+
privateKey: PEM_A,
|
|
131
|
+
clientSecret: "secret-1",
|
|
132
|
+
installationId: "123",
|
|
133
|
+
});
|
|
134
|
+
expect(view).toMatchObject({
|
|
135
|
+
agentId,
|
|
136
|
+
appId: "3001",
|
|
137
|
+
slug: "example-app",
|
|
138
|
+
clientId: "Iv1.abc",
|
|
139
|
+
installationId: "123",
|
|
140
|
+
priority: 0,
|
|
141
|
+
hasPrivateKey: true,
|
|
142
|
+
hasClientSecret: true,
|
|
143
|
+
});
|
|
144
|
+
const serialized = JSON.stringify(view);
|
|
145
|
+
expect(serialized).not.toContain("MIIfakeKeyA");
|
|
146
|
+
expect(serialized).not.toContain("secret-1");
|
|
147
|
+
expect(view).not.toHaveProperty("privateKeyEncrypted");
|
|
148
|
+
expect(view).not.toHaveProperty("clientSecretEncrypted");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("create() defaults priority to one past the current highest", async () => {
|
|
152
|
+
const agentId = await createAgent();
|
|
153
|
+
const a = await service.create(agentId, {
|
|
154
|
+
appId: "3001",
|
|
155
|
+
privateKey: PEM_A,
|
|
156
|
+
});
|
|
157
|
+
const b = await service.create(agentId, {
|
|
158
|
+
appId: "3002",
|
|
159
|
+
privateKey: PEM_B,
|
|
160
|
+
});
|
|
161
|
+
const c = await service.create(agentId, {
|
|
162
|
+
appId: "3003",
|
|
163
|
+
privateKey: PEM_B,
|
|
164
|
+
priority: 10,
|
|
165
|
+
});
|
|
166
|
+
const d = await service.create(agentId, {
|
|
167
|
+
appId: "3004",
|
|
168
|
+
privateKey: PEM_B,
|
|
169
|
+
});
|
|
170
|
+
expect([a.priority, b.priority, c.priority, d.priority]).toEqual([
|
|
171
|
+
0, 1, 10, 11,
|
|
172
|
+
]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("create() validates input and the agent", async () => {
|
|
176
|
+
const agentId = await createAgent();
|
|
177
|
+
await expect(
|
|
178
|
+
service.create(agentId, { appId: "abc", privateKey: PEM_A }),
|
|
179
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
180
|
+
await expect(
|
|
181
|
+
service.create(agentId, { appId: "3001", privateKey: " " }),
|
|
182
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
183
|
+
await expect(
|
|
184
|
+
service.create(agentId, {
|
|
185
|
+
appId: "3001",
|
|
186
|
+
privateKey: PEM_A,
|
|
187
|
+
installationId: "x1",
|
|
188
|
+
}),
|
|
189
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
190
|
+
await expect(
|
|
191
|
+
service.create(agentId, {
|
|
192
|
+
appId: "3001",
|
|
193
|
+
privateKey: PEM_A,
|
|
194
|
+
priority: -1,
|
|
195
|
+
}),
|
|
196
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
197
|
+
await expect(
|
|
198
|
+
service.create("missing-agent", { appId: "3001", privateKey: PEM_A }),
|
|
199
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
200
|
+
expect(await prisma.agentGitHubApp.count()).toBe(0);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("create() rejects a duplicate agentId+appId with ConflictError", async () => {
|
|
204
|
+
const agentId = await createAgent();
|
|
205
|
+
await service.create(agentId, { appId: "3001", privateKey: PEM_A });
|
|
206
|
+
await expect(
|
|
207
|
+
service.create(agentId, { appId: "3001", privateKey: PEM_B }),
|
|
208
|
+
).rejects.toBeInstanceOf(ConflictError);
|
|
209
|
+
expect(await prisma.agentGitHubApp.count({ where: { agentId } })).toBe(1);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("the same appId is allowed on two different agents", async () => {
|
|
213
|
+
const one = await createAgent("one");
|
|
214
|
+
const two = await createAgent("two");
|
|
215
|
+
await service.create(one, { appId: "3001", privateKey: PEM_A });
|
|
216
|
+
await service.create(two, { appId: "3001", privateKey: PEM_A });
|
|
217
|
+
expect(await prisma.agentGitHubApp.count()).toBe(2);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("list() returns the agent's apps in priority order only", async () => {
|
|
221
|
+
const agentId = await createAgent();
|
|
222
|
+
const other = await createAgent("other");
|
|
223
|
+
await service.create(agentId, {
|
|
224
|
+
appId: "3002",
|
|
225
|
+
privateKey: PEM_A,
|
|
226
|
+
priority: 5,
|
|
227
|
+
});
|
|
228
|
+
await service.create(agentId, {
|
|
229
|
+
appId: "3001",
|
|
230
|
+
privateKey: PEM_A,
|
|
231
|
+
priority: 1,
|
|
232
|
+
});
|
|
233
|
+
await service.create(other, { appId: "3009", privateKey: PEM_A });
|
|
234
|
+
const list = await service.list(agentId);
|
|
235
|
+
expect(list.map((a) => a.appId)).toEqual(["3001", "3002"]);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("get() is scoped to the agent", async () => {
|
|
239
|
+
const agentId = await createAgent();
|
|
240
|
+
const other = await createAgent("other");
|
|
241
|
+
const created = await service.create(agentId, {
|
|
242
|
+
appId: "3001",
|
|
243
|
+
privateKey: PEM_A,
|
|
244
|
+
});
|
|
245
|
+
expect((await service.get(agentId, created.id))?.appId).toBe("3001");
|
|
246
|
+
expect(await service.get(other, created.id)).toBeNull();
|
|
247
|
+
expect(await service.get(agentId, "nope")).toBeNull();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("update() patches fields, rotates and clears secrets", async () => {
|
|
251
|
+
const agentId = await createAgent();
|
|
252
|
+
const created = await service.create(agentId, {
|
|
253
|
+
appId: "3001",
|
|
254
|
+
slug: "example-app",
|
|
255
|
+
privateKey: PEM_A,
|
|
256
|
+
clientSecret: "secret-1",
|
|
257
|
+
installationId: "123",
|
|
258
|
+
});
|
|
259
|
+
const updated = await service.update(agentId, created.id, {
|
|
260
|
+
slug: null,
|
|
261
|
+
installationId: "456",
|
|
262
|
+
priority: 3,
|
|
263
|
+
privateKey: PEM_B,
|
|
264
|
+
clientSecret: null,
|
|
265
|
+
});
|
|
266
|
+
expect(updated).toMatchObject({
|
|
267
|
+
appId: "3001",
|
|
268
|
+
slug: null,
|
|
269
|
+
installationId: "456",
|
|
270
|
+
priority: 3,
|
|
271
|
+
hasPrivateKey: true,
|
|
272
|
+
hasClientSecret: false,
|
|
273
|
+
});
|
|
274
|
+
const [creds] = await service.listCredentials(agentId);
|
|
275
|
+
expect(creds?.privateKey).toBe(PEM_B);
|
|
276
|
+
expect(creds?.clientSecret).toBeNull();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("update() leaves omitted fields unchanged", async () => {
|
|
280
|
+
const agentId = await createAgent();
|
|
281
|
+
const created = await service.create(agentId, {
|
|
282
|
+
appId: "3001",
|
|
283
|
+
slug: "example-app",
|
|
284
|
+
clientId: "Iv1.abc",
|
|
285
|
+
privateKey: PEM_A,
|
|
286
|
+
clientSecret: "secret-1",
|
|
287
|
+
});
|
|
288
|
+
await service.update(agentId, created.id, { priority: 7 });
|
|
289
|
+
const [creds] = await service.listCredentials(agentId);
|
|
290
|
+
expect(creds).toMatchObject({
|
|
291
|
+
slug: "example-app",
|
|
292
|
+
clientId: "Iv1.abc",
|
|
293
|
+
privateKey: PEM_A,
|
|
294
|
+
clientSecret: "secret-1",
|
|
295
|
+
priority: 7,
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("update() 404s for another agent's app and validates input", async () => {
|
|
300
|
+
const agentId = await createAgent();
|
|
301
|
+
const other = await createAgent("other");
|
|
302
|
+
const created = await service.create(agentId, {
|
|
303
|
+
appId: "3001",
|
|
304
|
+
privateKey: PEM_A,
|
|
305
|
+
});
|
|
306
|
+
await expect(
|
|
307
|
+
service.update(other, created.id, { priority: 1 }),
|
|
308
|
+
).rejects.toBeInstanceOf(NotFoundError);
|
|
309
|
+
await expect(
|
|
310
|
+
service.update(agentId, created.id, { privateKey: "" }),
|
|
311
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
312
|
+
await expect(
|
|
313
|
+
service.update(agentId, created.id, { slug: "bad slug" }),
|
|
314
|
+
).rejects.toBeInstanceOf(UnprocessableEntityError);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("delete() removes only the agent's own app", async () => {
|
|
318
|
+
const agentId = await createAgent();
|
|
319
|
+
const other = await createAgent("other");
|
|
320
|
+
const created = await service.create(agentId, {
|
|
321
|
+
appId: "3001",
|
|
322
|
+
privateKey: PEM_A,
|
|
323
|
+
});
|
|
324
|
+
expect(await service.delete(other, created.id)).toBe(false);
|
|
325
|
+
expect(await service.delete(agentId, created.id)).toBe(true);
|
|
326
|
+
expect(await service.delete(agentId, created.id)).toBe(false);
|
|
327
|
+
expect(await service.list(agentId)).toEqual([]);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("rows cascade-delete with their agent", async () => {
|
|
331
|
+
const agentId = await createAgent();
|
|
332
|
+
await service.create(agentId, { appId: "3001", privateKey: PEM_A });
|
|
333
|
+
await prisma.agent.delete({ where: { id: agentId } });
|
|
334
|
+
expect(await prisma.agentGitHubApp.count()).toBe(0);
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
// ── Encryption ────────────────────────────────────────────────────────────
|
|
339
|
+
|
|
340
|
+
describe("encryption", () => {
|
|
341
|
+
it("stores ciphertext and listCredentials() round-trips the plain values", async () => {
|
|
342
|
+
const agentId = await createAgent();
|
|
343
|
+
await service.create(agentId, {
|
|
344
|
+
appId: "3001",
|
|
345
|
+
privateKey: PEM_A,
|
|
346
|
+
clientSecret: "secret-1",
|
|
347
|
+
});
|
|
348
|
+
const raw = await prisma.agentGitHubApp.findFirstOrThrow({
|
|
349
|
+
where: { agentId },
|
|
350
|
+
});
|
|
351
|
+
expect(raw.privateKeyEncrypted).not.toBe(PEM_A);
|
|
352
|
+
expect(raw.privateKeyEncrypted).not.toContain("MIIfakeKeyA");
|
|
353
|
+
expect(raw.privateKeyEncrypted).toContain(":"); // iv:ciphertext:authTag
|
|
354
|
+
expect(raw.clientSecretEncrypted).not.toBe("secret-1");
|
|
355
|
+
expect(raw.clientSecretEncrypted).toContain(":");
|
|
356
|
+
|
|
357
|
+
const [creds] = await service.listCredentials(agentId);
|
|
358
|
+
expect(creds?.privateKey).toBe(PEM_A);
|
|
359
|
+
expect(creds?.clientSecret).toBe("secret-1");
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
// ── Backfill ──────────────────────────────────────────────────────────────
|
|
364
|
+
|
|
365
|
+
describe("backfillAgentGitHubAppsFromEnv", () => {
|
|
366
|
+
it("creates one row per agent from each distinct GH_APP_* pattern", async () => {
|
|
367
|
+
const full = await createAgent("full");
|
|
368
|
+
const noInstall = await createAgent("no-install");
|
|
369
|
+
const manual = await createAgent("manual");
|
|
370
|
+
const noApp = await createAgent("no-app");
|
|
371
|
+
await seedEnv(full, { ...FULL_SET, OTHER_VAR: "x" });
|
|
372
|
+
await seedEnv(noInstall, NO_INSTALLATION_SET);
|
|
373
|
+
await seedEnv(manual, MANUAL_PASTE_SET);
|
|
374
|
+
await seedEnv(noApp, { OTHER_VAR: "y" });
|
|
375
|
+
|
|
376
|
+
const result = await backfillAgentGitHubAppsFromEnv({
|
|
377
|
+
prisma,
|
|
378
|
+
crypto,
|
|
379
|
+
logger: silentLogger,
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
expect(result.created.map((c) => [c.agentId, c.appId]).sort()).toEqual(
|
|
383
|
+
[
|
|
384
|
+
[full, "2001"],
|
|
385
|
+
[noInstall, "2002"],
|
|
386
|
+
[manual, "2003"],
|
|
387
|
+
].sort(),
|
|
388
|
+
);
|
|
389
|
+
expect(result.alreadyPresent).toEqual([]);
|
|
390
|
+
expect(result.skipped).toEqual([]);
|
|
391
|
+
|
|
392
|
+
const fullCreds = await service.listCredentials(full);
|
|
393
|
+
expect(fullCreds).toEqual([
|
|
394
|
+
{
|
|
395
|
+
id: expect.any(String),
|
|
396
|
+
appId: "2001",
|
|
397
|
+
slug: "example-agent-app",
|
|
398
|
+
clientId: "Iv1.0123abcd",
|
|
399
|
+
installationId: "90001",
|
|
400
|
+
priority: 0,
|
|
401
|
+
privateKey: PEM_A,
|
|
402
|
+
clientSecret: "example-client-secret",
|
|
403
|
+
},
|
|
404
|
+
]);
|
|
405
|
+
const [noInstallView] = await service.list(noInstall);
|
|
406
|
+
expect(noInstallView).toMatchObject({
|
|
407
|
+
appId: "2002",
|
|
408
|
+
installationId: null,
|
|
409
|
+
hasClientSecret: true,
|
|
410
|
+
});
|
|
411
|
+
const [manualView] = await service.list(manual);
|
|
412
|
+
expect(manualView).toMatchObject({
|
|
413
|
+
appId: "2003",
|
|
414
|
+
slug: null,
|
|
415
|
+
clientId: null,
|
|
416
|
+
installationId: "90003",
|
|
417
|
+
hasPrivateKey: true,
|
|
418
|
+
hasClientSecret: false,
|
|
419
|
+
});
|
|
420
|
+
expect(await service.list(noApp)).toEqual([]);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("copies the env ciphertext verbatim (no re-encryption)", async () => {
|
|
424
|
+
const agentId = await createAgent();
|
|
425
|
+
await seedEnv(agentId, FULL_SET);
|
|
426
|
+
await backfillAgentGitHubAppsFromEnv({
|
|
427
|
+
prisma,
|
|
428
|
+
crypto,
|
|
429
|
+
logger: silentLogger,
|
|
430
|
+
});
|
|
431
|
+
const keyRow = await prisma.agentEnv.findFirstOrThrow({
|
|
432
|
+
where: { agentId, key: "GH_APP_PRIVATE_KEY" },
|
|
433
|
+
});
|
|
434
|
+
const secretRow = await prisma.agentEnv.findFirstOrThrow({
|
|
435
|
+
where: { agentId, key: "GH_APP_CLIENT_SECRET" },
|
|
436
|
+
});
|
|
437
|
+
const app = await prisma.agentGitHubApp.findFirstOrThrow({
|
|
438
|
+
where: { agentId },
|
|
439
|
+
});
|
|
440
|
+
expect(app.privateKeyEncrypted).toBe(keyRow.value);
|
|
441
|
+
expect(app.clientSecretEncrypted).toBe(secretRow.value);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it("skips and reports partial sets without a private key", async () => {
|
|
445
|
+
const idOnly = await createAgent("id-only");
|
|
446
|
+
const idSlug = await createAgent("id-slug");
|
|
447
|
+
const badId = await createAgent("bad-id");
|
|
448
|
+
await seedEnv(idOnly, ID_ONLY_SET);
|
|
449
|
+
await seedEnv(idSlug, ID_SLUG_NO_KEY_SET);
|
|
450
|
+
await seedEnv(badId, { GH_APP_ID: "abc", GH_APP_PRIVATE_KEY: PEM_A });
|
|
451
|
+
|
|
452
|
+
const warned: string[] = [];
|
|
453
|
+
const result = await backfillAgentGitHubAppsFromEnv({
|
|
454
|
+
prisma,
|
|
455
|
+
crypto,
|
|
456
|
+
logger: { log: () => {}, warn: (m: string) => warned.push(m) },
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
expect(result.created).toEqual([]);
|
|
460
|
+
expect(
|
|
461
|
+
[...result.skipped].sort((a, b) => a.agentId.localeCompare(b.agentId)),
|
|
462
|
+
).toEqual(
|
|
463
|
+
[
|
|
464
|
+
{
|
|
465
|
+
agentId: idOnly,
|
|
466
|
+
appId: "2004",
|
|
467
|
+
reason: "GH_APP_PRIVATE_KEY missing",
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
agentId: idSlug,
|
|
471
|
+
appId: "2005",
|
|
472
|
+
reason: "GH_APP_PRIVATE_KEY missing",
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
agentId: badId,
|
|
476
|
+
appId: null,
|
|
477
|
+
reason: "GH_APP_ID is not a positive integer",
|
|
478
|
+
},
|
|
479
|
+
].sort((a, b) => a.agentId.localeCompare(b.agentId)),
|
|
480
|
+
);
|
|
481
|
+
expect(await prisma.agentGitHubApp.count()).toBe(0);
|
|
482
|
+
for (const id of [idOnly, idSlug, badId]) {
|
|
483
|
+
expect(warned.some((m) => m.includes(id))).toBe(true);
|
|
484
|
+
}
|
|
485
|
+
// Logs never carry key material.
|
|
486
|
+
expect(warned.join("\n")).not.toContain("MIIfake");
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it("ignores disabled (tombstoned) GH_APP_ID rows", async () => {
|
|
490
|
+
const agentId = await createAgent();
|
|
491
|
+
await seedEnv(agentId, FULL_SET);
|
|
492
|
+
await prisma.agentEnv.updateMany({
|
|
493
|
+
where: { agentId, key: "GH_APP_ID" },
|
|
494
|
+
data: { enabled: false },
|
|
495
|
+
});
|
|
496
|
+
const result = await backfillAgentGitHubAppsFromEnv({
|
|
497
|
+
prisma,
|
|
498
|
+
crypto,
|
|
499
|
+
logger: silentLogger,
|
|
500
|
+
});
|
|
501
|
+
expect(result).toEqual({ created: [], alreadyPresent: [], skipped: [] });
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it("is idempotent: a second run creates nothing and env rows are untouched", async () => {
|
|
505
|
+
const full = await createAgent("full");
|
|
506
|
+
const manual = await createAgent("manual");
|
|
507
|
+
const idOnly = await createAgent("id-only");
|
|
508
|
+
await seedEnv(full, FULL_SET);
|
|
509
|
+
await seedEnv(manual, MANUAL_PASTE_SET);
|
|
510
|
+
await seedEnv(idOnly, ID_ONLY_SET);
|
|
511
|
+
const before = {
|
|
512
|
+
full: await envSnapshot(full),
|
|
513
|
+
manual: await envSnapshot(manual),
|
|
514
|
+
idOnly: await envSnapshot(idOnly),
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
const first = await backfillAgentGitHubAppsFromEnv({
|
|
518
|
+
prisma,
|
|
519
|
+
crypto,
|
|
520
|
+
logger: silentLogger,
|
|
521
|
+
});
|
|
522
|
+
expect(first.created).toHaveLength(2);
|
|
523
|
+
const rowsAfterFirst = await prisma.agentGitHubApp.findMany({
|
|
524
|
+
orderBy: { id: "asc" },
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
const second = await backfillAgentGitHubAppsFromEnv({
|
|
528
|
+
prisma,
|
|
529
|
+
crypto,
|
|
530
|
+
logger: silentLogger,
|
|
531
|
+
});
|
|
532
|
+
expect(second.created).toEqual([]);
|
|
533
|
+
expect(
|
|
534
|
+
second.alreadyPresent.map((p) => [p.agentId, p.appId]).sort(),
|
|
535
|
+
).toEqual(
|
|
536
|
+
[
|
|
537
|
+
[full, "2001"],
|
|
538
|
+
[manual, "2003"],
|
|
539
|
+
].sort(),
|
|
540
|
+
);
|
|
541
|
+
expect(second.skipped).toEqual([
|
|
542
|
+
{
|
|
543
|
+
agentId: idOnly,
|
|
544
|
+
appId: "2004",
|
|
545
|
+
reason: "GH_APP_PRIVATE_KEY missing",
|
|
546
|
+
},
|
|
547
|
+
]);
|
|
548
|
+
expect(
|
|
549
|
+
await prisma.agentGitHubApp.findMany({ orderBy: { id: "asc" } }),
|
|
550
|
+
).toEqual(rowsAfterFirst);
|
|
551
|
+
|
|
552
|
+
expect(await envSnapshot(full)).toEqual(before.full);
|
|
553
|
+
expect(await envSnapshot(manual)).toEqual(before.manual);
|
|
554
|
+
expect(await envSnapshot(idOnly)).toEqual(before.idOnly);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it("treats an existing row for the same appId as already present", async () => {
|
|
558
|
+
const agentId = await createAgent();
|
|
559
|
+
await seedEnv(agentId, FULL_SET);
|
|
560
|
+
await service.create(agentId, {
|
|
561
|
+
appId: "2001",
|
|
562
|
+
privateKey: PEM_B,
|
|
563
|
+
priority: 4,
|
|
564
|
+
});
|
|
565
|
+
const result = await backfillAgentGitHubAppsFromEnv({
|
|
566
|
+
prisma,
|
|
567
|
+
crypto,
|
|
568
|
+
logger: silentLogger,
|
|
569
|
+
});
|
|
570
|
+
expect(result.alreadyPresent).toEqual([{ agentId, appId: "2001" }]);
|
|
571
|
+
const [creds] = await service.listCredentials(agentId);
|
|
572
|
+
// The existing row wins; the backfill never overwrites it.
|
|
573
|
+
expect(creds).toMatchObject({ privateKey: PEM_B, priority: 4 });
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
it("skips an agent that already has rows for other apps", async () => {
|
|
577
|
+
const agentId = await createAgent();
|
|
578
|
+
await seedEnv(agentId, FULL_SET);
|
|
579
|
+
await service.create(agentId, { appId: "9999", privateKey: PEM_B });
|
|
580
|
+
const result = await backfillAgentGitHubAppsFromEnv({
|
|
581
|
+
prisma,
|
|
582
|
+
crypto,
|
|
583
|
+
logger: silentLogger,
|
|
584
|
+
});
|
|
585
|
+
expect(result.created).toEqual([]);
|
|
586
|
+
expect(result.skipped).toEqual([
|
|
587
|
+
{
|
|
588
|
+
agentId,
|
|
589
|
+
appId: "2001",
|
|
590
|
+
reason: "agent already has other GitHub App rows",
|
|
591
|
+
},
|
|
592
|
+
]);
|
|
593
|
+
expect((await service.list(agentId)).map((a) => a.appId)).toEqual([
|
|
594
|
+
"9999",
|
|
595
|
+
]);
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it("two concurrent runs create exactly one row per agent", async () => {
|
|
599
|
+
const agentId = await createAgent();
|
|
600
|
+
await seedEnv(agentId, FULL_SET);
|
|
601
|
+
const [a, b] = await Promise.all([
|
|
602
|
+
backfillAgentGitHubAppsFromEnv({
|
|
603
|
+
prisma,
|
|
604
|
+
crypto,
|
|
605
|
+
logger: silentLogger,
|
|
606
|
+
}),
|
|
607
|
+
backfillAgentGitHubAppsFromEnv({
|
|
608
|
+
prisma,
|
|
609
|
+
crypto,
|
|
610
|
+
logger: silentLogger,
|
|
611
|
+
}),
|
|
612
|
+
]);
|
|
613
|
+
expect(a.created.length + b.created.length).toBe(1);
|
|
614
|
+
expect(a.alreadyPresent.length + b.alreadyPresent.length).toBe(1);
|
|
615
|
+
expect(await prisma.agentGitHubApp.count({ where: { agentId } })).toBe(1);
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
});
|