@app-vitals/agent-admin 1.109.0 → 1.110.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.
@@ -8,6 +8,7 @@ import {
8
8
  withLookupSnapshots,
9
9
  } from "./github-installation.fixtures.ts";
10
10
  import {
11
+ accessCache,
11
12
  createGithubInstallationRepos,
12
13
  createGithubSync,
13
14
  } from "./github-installation-wiring.ts";
@@ -15,6 +16,14 @@ import type { GitHubEnterpriseAppMetadata } from "./github-enterprise-apps.ts";
15
16
  import type { InstallationReposPort } from "./installation-repos-service.ts";
16
17
 
17
18
  const noApp = { get: async () => null, getPrivateKey: async () => null };
19
+ // A deployment-wide lookup App exists: its cached access rows apply.
20
+ const withDeploymentLookup = {
21
+ get: async (role: string, accountId?: string | null) =>
22
+ role === "lookup" && !accountId
23
+ ? ({ accountId: null, appId: 1 } as never)
24
+ : null,
25
+ getPrivateKey: async () => null,
26
+ };
18
27
 
19
28
  const portWith = (
20
29
  seen: number[],
@@ -88,7 +97,7 @@ describe("createGithubSync", () => {
88
97
  const verified: string[] = [];
89
98
  const svc = createGithubSync({
90
99
  prisma,
91
- appService: noApp,
100
+ appService: withDeploymentLookup,
92
101
  access: accessFor(["acme/api"]),
93
102
  agentEnv: envWith({ GH_APP_ID: "42" }),
94
103
  installationRepos: portWith([]),
@@ -259,3 +268,31 @@ describe("createGithubSync lookup snapshot read", () => {
259
268
  ]);
260
269
  });
261
270
  });
271
+
272
+ describe("accessCache", () => {
273
+ const query = (accountId: string) => ({
274
+ accountId,
275
+ org: "acme",
276
+ username: "dev",
277
+ repos: ["api"],
278
+ });
279
+
280
+ it("ignores stale deployment-key access rows once no deployment-wide lookup App exists", async () => {
281
+ const { prisma } = fakePrisma(
282
+ ["acme/api"],
283
+ freshCache("acme", ["acme/api"], ["acme/api"], new Date()),
284
+ );
285
+ const cache = accessCache({ prisma, appService: noApp });
286
+ expect(await cache.getAccessRows(query("deployment"))).toEqual([]);
287
+ expect((await cache.getAccessRows(query("acct-1"))).length).toBe(1);
288
+ });
289
+
290
+ it("keeps deployment-key rows while the deployment-wide lookup App exists", async () => {
291
+ const { prisma } = fakePrisma(
292
+ ["acme/api"],
293
+ freshCache("acme", ["acme/api"], ["acme/api"], new Date()),
294
+ );
295
+ const cache = accessCache({ prisma, appService: withDeploymentLookup });
296
+ expect((await cache.getAccessRows(query("deployment"))).length).toBe(1);
297
+ });
298
+ });
@@ -29,11 +29,18 @@ const port: InstallationReposPort = {
29
29
  removeRepos: async () => {},
30
30
  };
31
31
 
32
+ // A deployment-wide lookup App exists, so cached rows under the deployment key apply.
33
+ const deploymentLookupApp = {
34
+ get: async () =>
35
+ ({ accountId: null }) as unknown as GitHubEnterpriseAppMetadata,
36
+ getPrivateKey: async () => null,
37
+ };
38
+
32
39
  function preview(cache: ReturnType<typeof freshCache>, now = new Date()) {
33
40
  const { prisma } = fakePrisma(REPOS, cache);
34
41
  const svc = createGithubSync({
35
42
  prisma,
36
- appService: { get: async () => null, getPrivateKey: async () => null },
43
+ appService: deploymentLookupApp,
37
44
  access: accessFor([]), // the live resolver must not be consulted
38
45
  agentEnv: envWith({ GH_APP_ID: "42" }),
39
46
  installationRepos: port,
@@ -118,7 +125,7 @@ describe("installation list access detail", () => {
118
125
  const { prisma } = fakePrisma(REPOS, cache);
119
126
  const svc = createGithubInstallationRepos({
120
127
  prisma,
121
- appService: { get: async () => null, getPrivateKey: async () => null },
128
+ appService: deploymentLookupApp,
122
129
  access: accessFor([]),
123
130
  agentEnv: envWith({ GH_APP_ID: "42" }),
124
131
  installationRepos: port,
@@ -311,7 +311,13 @@ export class InstallationReposService {
311
311
  const snapshots = cache
312
312
  ? await cache.listAgentSnapshots(accountId, String(agent.appId))
313
313
  : [];
314
- const username = agent.githubUsername?.trim();
314
+ const state = this.deps.appsState
315
+ ? await this.deps.appsState(agent.accountId)
316
+ : "both";
317
+ // Without a usable lookup App no access rows apply (they may be left over
318
+ // from a removed deployment-wide App).
319
+ const username =
320
+ state === "lookup_missing" ? undefined : agent.githubUsername?.trim();
315
321
  const orgs: InstallationOrgView[] = [];
316
322
  for (const s of snapshots) {
317
323
  const names = s.repos.map((r) => r.fullName);
@@ -353,9 +359,6 @@ export class InstallationReposService {
353
359
  ...(s.lastTickAt ? { lastTickAt: iso(s.lastTickAt) } : {}),
354
360
  });
355
361
  }
356
- const state = this.deps.appsState
357
- ? await this.deps.appsState(agent.accountId)
358
- : "both";
359
362
  return {
360
363
  status: "ok",
361
364
  appId: agent.appId,
@@ -97,12 +97,27 @@ describeOrSkip("SlackConfigTokenService (integration)", () => {
97
97
  await service.clear(acctA); // no-op
98
98
  });
99
99
 
100
+ /** A deployment-wide row that predates the second account. */
101
+ const seedDefault = async () => {
102
+ const row = await prisma.slackConfigToken.create({
103
+ data: {
104
+ accountId: null,
105
+ refreshTokenEncrypted: (
106
+ makeStrictTokenCrypto(KEY) as TokenCrypto
107
+ ).encrypt(REFRESH),
108
+ },
109
+ });
110
+ return row;
111
+ };
112
+
100
113
  it("allows a token with no access token", async () => {
114
+ await prisma.account.deleteMany();
101
115
  const saved = await service.save(null, { refreshToken: REFRESH });
102
116
  expect(saved.accessTokenSet).toBe(false);
103
117
  });
104
118
 
105
119
  it("upserts one row per scope, including two NULL-scope saves", async () => {
120
+ await prisma.account.deleteMany();
106
121
  const first = await service.save(null, { refreshToken: REFRESH });
107
122
  const second = await service.save(null, {
108
123
  refreshToken: "test-refresh-OTHER",
@@ -114,6 +129,8 @@ describeOrSkip("SlackConfigTokenService (integration)", () => {
114
129
  await prisma.slackConfigToken.count({ where: { accountId: null } }),
115
130
  ).toBe(1);
116
131
 
132
+ acctA = (await prisma.account.create({ data: { name: "A" } })).id;
133
+ acctB = (await prisma.account.create({ data: { name: "B" } })).id;
117
134
  const a1 = await service.save(acctA, { refreshToken: REFRESH });
118
135
  const a2 = await service.save(acctA, { refreshToken: REFRESH });
119
136
  expect(a2.id).toBe(a1.id);
@@ -145,7 +162,7 @@ describeOrSkip("SlackConfigTokenService (integration)", () => {
145
162
 
146
163
  it("resolveForAccount prefers the account row, then default, then null", async () => {
147
164
  expect(await service.resolveForAccount(acctA)).toBeNull();
148
- const def = await service.save(null, { refreshToken: REFRESH });
165
+ const def = await seedDefault();
149
166
  expect((await service.resolveForAccount(acctA))?.id).toBe(def.id);
150
167
  const own = await service.save(acctA, { refreshToken: REFRESH });
151
168
  expect((await service.resolveForAccount(acctA))?.id).toBe(own.id);
@@ -156,7 +173,7 @@ describeOrSkip("SlackConfigTokenService (integration)", () => {
156
173
  });
157
174
 
158
175
  it("clears only the targeted scope", async () => {
159
- await service.save(null, { refreshToken: REFRESH });
176
+ await seedDefault();
160
177
  await service.save(acctA, { refreshToken: REFRESH });
161
178
  await service.clear(acctA);
162
179
  expect(await service.get(null)).not.toBeNull();
@@ -19,6 +19,7 @@
19
19
 
20
20
  import type { PrismaClient } from "../prisma/client/client.ts";
21
21
  import { type Clock, SystemClock } from "./clock.ts";
22
+ import { assertDeploymentWideAllowed } from "./deployment-wide-settings.ts";
22
23
  import {
23
24
  BadGatewayError,
24
25
  NotFoundError,
@@ -202,7 +203,8 @@ export class SlackConfigTokenService {
202
203
 
203
204
  /**
204
205
  * Create or replace the token for a scope (accountId null = deployment-wide
205
- * default). Resets lastError; stamps lastRotatedAt.
206
+ * default, refused while there are several accounts). Resets lastError;
207
+ * stamps lastRotatedAt.
206
208
  */
207
209
  async save(
208
210
  accountId: string | null,
@@ -212,6 +214,9 @@ export class SlackConfigTokenService {
212
214
  if (!input.refreshToken) {
213
215
  throw new UnprocessableEntityError("refreshToken is required");
214
216
  }
217
+ if (accountId === null) {
218
+ await assertDeploymentWideAllowed(this.prisma, "the Slack config token");
219
+ }
215
220
  const now = this.clock.now();
216
221
  const data = {
217
222
  refreshTokenEncrypted: this.crypto.encrypt(input.refreshToken),