@app-vitals/agent-admin 1.125.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/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,266 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
planGitHubAppBackfill,
|
|
5
|
+
sortAgentGitHubApps,
|
|
6
|
+
} from "./agent-github-apps.ts";
|
|
7
|
+
import type { TokenCrypto } from "./token-crypto.ts";
|
|
8
|
+
|
|
9
|
+
// Reversible fake cipher so the tests can tell ciphertext from plain text.
|
|
10
|
+
const fakeCrypto: TokenCrypto = {
|
|
11
|
+
encrypt: (s) => `enc(${s})`,
|
|
12
|
+
decrypt: (s) => {
|
|
13
|
+
const m = /^enc\((.*)\)$/s.exec(s);
|
|
14
|
+
if (!m) throw new Error("not ciphertext");
|
|
15
|
+
return m[1] as string;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function envRows(env: Record<string, string>) {
|
|
20
|
+
return Object.entries(env).map(([key, value]) => ({
|
|
21
|
+
key,
|
|
22
|
+
value: fakeCrypto.encrypt(value),
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const PEM =
|
|
27
|
+
"-----BEGIN RSA PRIVATE KEY-----\nMIIfake\n-----END RSA PRIVATE KEY-----";
|
|
28
|
+
|
|
29
|
+
describe("sortAgentGitHubApps", () => {
|
|
30
|
+
const t = (s: number) => new Date(Date.UTC(2026, 0, 1, 0, 0, s));
|
|
31
|
+
|
|
32
|
+
it("orders by priority ascending", () => {
|
|
33
|
+
const sorted = sortAgentGitHubApps([
|
|
34
|
+
{ id: "c", appId: "3", priority: 2, createdAt: t(0) },
|
|
35
|
+
{ id: "a", appId: "1", priority: 0, createdAt: t(0) },
|
|
36
|
+
{ id: "b", appId: "2", priority: 1, createdAt: t(0) },
|
|
37
|
+
]);
|
|
38
|
+
expect(sorted.map((a) => a.id)).toEqual(["a", "b", "c"]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("breaks priority ties by createdAt, then numeric appId, then id", () => {
|
|
42
|
+
const sorted = sortAgentGitHubApps([
|
|
43
|
+
{ id: "z", appId: "100", priority: 0, createdAt: t(1) },
|
|
44
|
+
{ id: "y", appId: "20", priority: 0, createdAt: t(1) },
|
|
45
|
+
{ id: "x", appId: "999", priority: 0, createdAt: t(0) },
|
|
46
|
+
{ id: "w", appId: "20", priority: 0, createdAt: t(1) },
|
|
47
|
+
]);
|
|
48
|
+
expect(sorted.map((a) => a.id)).toEqual(["x", "w", "y", "z"]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("does not mutate its input", () => {
|
|
52
|
+
const input = [
|
|
53
|
+
{ id: "b", appId: "2", priority: 1, createdAt: t(0) },
|
|
54
|
+
{ id: "a", appId: "1", priority: 0, createdAt: t(0) },
|
|
55
|
+
];
|
|
56
|
+
sortAgentGitHubApps(input);
|
|
57
|
+
expect(input.map((a) => a.id)).toEqual(["b", "a"]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("is deterministic regardless of input order", () => {
|
|
61
|
+
const apps = [
|
|
62
|
+
{ id: "a", appId: "5", priority: 1, createdAt: t(2) },
|
|
63
|
+
{ id: "b", appId: "5", priority: 1, createdAt: t(1) },
|
|
64
|
+
{ id: "c", appId: "4", priority: 0, createdAt: t(3) },
|
|
65
|
+
];
|
|
66
|
+
const forward = sortAgentGitHubApps(apps).map((a) => a.id);
|
|
67
|
+
const reversed = sortAgentGitHubApps([...apps].reverse()).map((a) => a.id);
|
|
68
|
+
expect(forward).toEqual(["c", "b", "a"]);
|
|
69
|
+
expect(reversed).toEqual(forward);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("planGitHubAppBackfill", () => {
|
|
74
|
+
it("full set: plain columns decrypted, secrets copied as ciphertext", () => {
|
|
75
|
+
const rows = envRows({
|
|
76
|
+
GH_APP_ID: "1001",
|
|
77
|
+
GH_APP_SLUG: "example-app",
|
|
78
|
+
GH_APP_CLIENT_ID: "Iv1.abc123",
|
|
79
|
+
GH_APP_CLIENT_SECRET: "client-secret-value",
|
|
80
|
+
GH_APP_PRIVATE_KEY: PEM,
|
|
81
|
+
GH_APP_INSTALLATION_ID: "555",
|
|
82
|
+
});
|
|
83
|
+
const plan = planGitHubAppBackfill(rows, fakeCrypto);
|
|
84
|
+
expect(plan).toEqual({
|
|
85
|
+
kind: "create",
|
|
86
|
+
data: {
|
|
87
|
+
appId: "1001",
|
|
88
|
+
slug: "example-app",
|
|
89
|
+
clientId: "Iv1.abc123",
|
|
90
|
+
privateKeyEncrypted: `enc(${PEM})`,
|
|
91
|
+
clientSecretEncrypted: "enc(client-secret-value)",
|
|
92
|
+
installationId: "555",
|
|
93
|
+
priority: 0,
|
|
94
|
+
},
|
|
95
|
+
warnings: [],
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("set without GH_APP_INSTALLATION_ID leaves the pin null", () => {
|
|
100
|
+
const plan = planGitHubAppBackfill(
|
|
101
|
+
envRows({
|
|
102
|
+
GH_APP_ID: "1002",
|
|
103
|
+
GH_APP_SLUG: "example-app-two",
|
|
104
|
+
GH_APP_CLIENT_ID: "Iv1.def456",
|
|
105
|
+
GH_APP_CLIENT_SECRET: "s",
|
|
106
|
+
GH_APP_PRIVATE_KEY: PEM,
|
|
107
|
+
}),
|
|
108
|
+
fakeCrypto,
|
|
109
|
+
);
|
|
110
|
+
expect(plan.kind).toBe("create");
|
|
111
|
+
if (plan.kind !== "create") return;
|
|
112
|
+
expect(plan.data.installationId).toBeNull();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("manual-paste set (ID, INSTALLATION_ID, KEY only) creates with null slug/clientId/secret", () => {
|
|
116
|
+
const plan = planGitHubAppBackfill(
|
|
117
|
+
envRows({
|
|
118
|
+
GH_APP_ID: "1003",
|
|
119
|
+
GH_APP_INSTALLATION_ID: "777",
|
|
120
|
+
GH_APP_PRIVATE_KEY: PEM,
|
|
121
|
+
}),
|
|
122
|
+
fakeCrypto,
|
|
123
|
+
);
|
|
124
|
+
expect(plan).toEqual({
|
|
125
|
+
kind: "create",
|
|
126
|
+
data: {
|
|
127
|
+
appId: "1003",
|
|
128
|
+
slug: null,
|
|
129
|
+
clientId: null,
|
|
130
|
+
privateKeyEncrypted: `enc(${PEM})`,
|
|
131
|
+
clientSecretEncrypted: null,
|
|
132
|
+
installationId: "777",
|
|
133
|
+
priority: 0,
|
|
134
|
+
},
|
|
135
|
+
warnings: [],
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("ID only is skipped: no private key", () => {
|
|
140
|
+
const plan = planGitHubAppBackfill(
|
|
141
|
+
envRows({ GH_APP_ID: "1004" }),
|
|
142
|
+
fakeCrypto,
|
|
143
|
+
);
|
|
144
|
+
expect(plan).toEqual({
|
|
145
|
+
kind: "skip",
|
|
146
|
+
appId: "1004",
|
|
147
|
+
reason: "GH_APP_PRIVATE_KEY missing",
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("ID + SLUG without key is skipped", () => {
|
|
152
|
+
const plan = planGitHubAppBackfill(
|
|
153
|
+
envRows({ GH_APP_ID: "1005", GH_APP_SLUG: "example-app" }),
|
|
154
|
+
fakeCrypto,
|
|
155
|
+
);
|
|
156
|
+
expect(plan.kind).toBe("skip");
|
|
157
|
+
if (plan.kind !== "skip") return;
|
|
158
|
+
expect(plan.reason).toBe("GH_APP_PRIVATE_KEY missing");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("an empty stored private key value counts as missing", () => {
|
|
162
|
+
const plan = planGitHubAppBackfill(
|
|
163
|
+
[
|
|
164
|
+
...envRows({ GH_APP_ID: "1006" }),
|
|
165
|
+
{ key: "GH_APP_PRIVATE_KEY", value: "" },
|
|
166
|
+
],
|
|
167
|
+
fakeCrypto,
|
|
168
|
+
);
|
|
169
|
+
expect(plan.kind).toBe("skip");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("a non-numeric GH_APP_ID is skipped", () => {
|
|
173
|
+
const plan = planGitHubAppBackfill(
|
|
174
|
+
envRows({ GH_APP_ID: "not-a-number", GH_APP_PRIVATE_KEY: PEM }),
|
|
175
|
+
fakeCrypto,
|
|
176
|
+
);
|
|
177
|
+
expect(plan).toEqual({
|
|
178
|
+
kind: "skip",
|
|
179
|
+
appId: null,
|
|
180
|
+
reason: "GH_APP_ID is not a positive integer",
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("no GH_APP_ID row means nothing to backfill", () => {
|
|
185
|
+
expect(
|
|
186
|
+
planGitHubAppBackfill(envRows({ GH_APP_SLUG: "x" }), fakeCrypto),
|
|
187
|
+
).toEqual({ kind: "none" });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("drops malformed optional fields with a warning instead of skipping", () => {
|
|
191
|
+
const plan = planGitHubAppBackfill(
|
|
192
|
+
envRows({
|
|
193
|
+
GH_APP_ID: " 1007 ",
|
|
194
|
+
GH_APP_SLUG: "bad slug/with path",
|
|
195
|
+
GH_APP_CLIENT_ID: "bad?id",
|
|
196
|
+
GH_APP_INSTALLATION_ID: "abc",
|
|
197
|
+
GH_APP_PRIVATE_KEY: PEM,
|
|
198
|
+
}),
|
|
199
|
+
fakeCrypto,
|
|
200
|
+
);
|
|
201
|
+
expect(plan.kind).toBe("create");
|
|
202
|
+
if (plan.kind !== "create") return;
|
|
203
|
+
expect(plan.data.appId).toBe("1007");
|
|
204
|
+
expect(plan.data.slug).toBeNull();
|
|
205
|
+
expect(plan.data.clientId).toBeNull();
|
|
206
|
+
expect(plan.data.installationId).toBeNull();
|
|
207
|
+
expect(plan.warnings).toEqual([
|
|
208
|
+
"GH_APP_SLUG malformed, dropped",
|
|
209
|
+
"GH_APP_CLIENT_ID malformed, dropped",
|
|
210
|
+
"GH_APP_INSTALLATION_ID not a positive integer, dropped",
|
|
211
|
+
]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("never decrypts the private key or client secret", () => {
|
|
215
|
+
let decrypted: string[] = [];
|
|
216
|
+
const spy: TokenCrypto = {
|
|
217
|
+
encrypt: fakeCrypto.encrypt,
|
|
218
|
+
decrypt: (s) => {
|
|
219
|
+
const v = fakeCrypto.decrypt(s);
|
|
220
|
+
decrypted.push(v);
|
|
221
|
+
return v;
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
decrypted = [];
|
|
225
|
+
planGitHubAppBackfill(
|
|
226
|
+
envRows({
|
|
227
|
+
GH_APP_ID: "1008",
|
|
228
|
+
GH_APP_PRIVATE_KEY: PEM,
|
|
229
|
+
GH_APP_CLIENT_SECRET: "top-secret",
|
|
230
|
+
}),
|
|
231
|
+
spy,
|
|
232
|
+
);
|
|
233
|
+
expect(decrypted).not.toContain(PEM);
|
|
234
|
+
expect(decrypted).not.toContain("top-secret");
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe("20261204000000_add_agent_github_app migration", () => {
|
|
239
|
+
// Consumers run an additive-only migration guard: CREATE TABLE / INDEX and a
|
|
240
|
+
// FK on a table created in the same migration only. The GH_APP_* backfill
|
|
241
|
+
// therefore lives in backfillAgentGitHubAppsFromEnv, never in this SQL.
|
|
242
|
+
it("is additive-only: no INSERT/UPDATE/DELETE/DROP, only allowed statements", async () => {
|
|
243
|
+
const sql = await Bun.file(
|
|
244
|
+
join(
|
|
245
|
+
import.meta.dir,
|
|
246
|
+
"../prisma/migrations/20261204000000_add_agent_github_app/migration.sql",
|
|
247
|
+
),
|
|
248
|
+
).text();
|
|
249
|
+
const statements = sql
|
|
250
|
+
.split("\n")
|
|
251
|
+
.filter((line) => !line.trimStart().startsWith("--"))
|
|
252
|
+
.join("\n")
|
|
253
|
+
.split(";")
|
|
254
|
+
.map((s) => s.trim())
|
|
255
|
+
.filter(Boolean);
|
|
256
|
+
expect(statements.length).toBeGreaterThan(0);
|
|
257
|
+
for (const statement of statements) {
|
|
258
|
+
expect(statement).toMatch(
|
|
259
|
+
/^(CREATE TABLE "AgentGitHubApp"|CREATE (UNIQUE )?INDEX|ALTER TABLE "AgentGitHubApp" ADD CONSTRAINT "\w+" FOREIGN KEY)/,
|
|
260
|
+
);
|
|
261
|
+
expect(
|
|
262
|
+
statement.replace(/\bON (DELETE|UPDATE) CASCADE/g, ""),
|
|
263
|
+
).not.toMatch(/\b(INSERT|UPDATE|DELETE|DROP|TRUNCATE|RENAME)\b/i);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
});
|
|
@@ -23,6 +23,13 @@ const ALLOWED = new Set([
|
|
|
23
23
|
"admin/src/account-move.ts",
|
|
24
24
|
]);
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Files whose own table also has a `privateKeyEncrypted` column (the agent's
|
|
28
|
+
* GitHub Apps, multi-github-apps 1.1). They are exempt from the column-name
|
|
29
|
+
* match only; the enterprise service/table matches still apply to them.
|
|
30
|
+
*/
|
|
31
|
+
const OWN_PRIVATE_KEY_COLUMN = new Set(["admin/src/agent-github-apps.ts"]);
|
|
32
|
+
|
|
26
33
|
function sources(dir: string, out: string[] = []): string[] {
|
|
27
34
|
for (const name of readdirSync(dir)) {
|
|
28
35
|
if (name === "node_modules" || name === "client" || name.startsWith(".")) {
|
|
@@ -64,7 +71,7 @@ describe("GitHub Enterprise key isolation", () => {
|
|
|
64
71
|
/github-enterprise-apps/.test(src) ||
|
|
65
72
|
/gitHubEnterpriseApp\b/.test(src) ||
|
|
66
73
|
/GitHubEnterpriseApp\b/.test(src) ||
|
|
67
|
-
/privateKeyEncrypted/.test(src)
|
|
74
|
+
(!OWN_PRIVATE_KEY_COLUMN.has(rel) && /privateKeyEncrypted/.test(src))
|
|
68
75
|
);
|
|
69
76
|
});
|
|
70
77
|
expect(offenders).toEqual([]);
|