@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 83 lines 3.25 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as iam from "@distilled.cloud/gcp/unstable/iam_v1";
5 import * as Effect from "effect/Effect";
6 import * as Redacted from "effect/Redacted";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 // New keys read inconsistently for a few seconds after create — as 404, or
15 // as a 403 "Permission iam.serviceAccountKeys.get is required".
16 const getKey = (name: string) =>
17 iam.getProjectsServiceAccountsKeys({ name }).pipe(
18 Effect.retry({
19 while: (error) => error._tag === "NotFound" || error._tag === "Forbidden",
20 schedule: Schedule.exponential("500 millis"),
21 times: 8,
22 }),
23 );
24
25 // Once the owning account is deleted too, reading its key is a 403, not a 404.
26 const waitUntilGone = (name: string, options?: { accountDeleted?: boolean }) =>
27 iam.getProjectsServiceAccountsKeys({ name }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.catchTag("Forbidden", (error) =>
31 options?.accountDeleted ? Effect.succeed("gone" as const) : Effect.fail(error),
32 ),
33 Effect.repeat({
34 schedule: Schedule.spaced("1 second"),
35 until: (status) => status === "gone",
36 times: 15,
37 }),
38 );
39
40 const keyFor = (account: "A" | "B") =>
41 Effect.gen(function* () {
42 const a = yield* GCP.IAM.ServiceAccount("KeyOwnerA", {});
43 const b = yield* GCP.IAM.ServiceAccount("KeyOwnerB", {});
44 const key = yield* GCP.IAM.ServiceAccountKey("Key", {
45 serviceAccount: account === "A" ? a.email : b.email,
46 });
47 return { a: a.email, b: b.email, key };
48 });
49
50 test.provider(
51 "create, keep, replace, and delete a service account key",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(keyFor("A"));
57 expect(created.key.serviceAccount).toEqual(created.a);
58 expect(created.key.name).toMatch(
59 new RegExp(
60 `^projects/[^/]+/serviceAccounts/${created.a}/keys/${created.key.privateKeyId}$`,
61 ),
62 );
63 expect(Redacted.value(created.key.privateKey)).toMatch(/^-----BEGIN PRIVATE KEY-----/);
64
65 const fetched = yield* getKey(created.key.name);
66 expect(fetched.name).toEqual(created.key.name);
67
68 // The private half is only returned on create: a redeploy keeps the key.
69 const kept = yield* stack.deploy(keyFor("A"));
70 expect(kept.key.name).toEqual(created.key.name);
71
72 // Moving the key to another account replaces it.
73 const replaced = yield* stack.deploy(keyFor("B"));
74 expect(replaced.key.serviceAccount).toEqual(replaced.b);
75 expect(replaced.key.name).not.toEqual(created.key.name);
76 yield* getKey(replaced.key.name);
77 expect(yield* waitUntilGone(created.key.name)).toEqual("gone");
78
79 yield* stack.destroy();
80 expect(yield* waitUntilGone(replaced.key.name, { accountDeleted: true })).toEqual("gone");
81 }).pipe(logLevel),
82 { timeout: 180_000, tags: ["provider:gcp", "provider:gcp:iam", "live"] },
83 );