@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 125 lines 4 KB
Raw
1 import * as iam from "@distilled.cloud/gcp/unstable/iam_v1";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import * as Stream from "effect/Stream";
6 import { GcpEnvironment } from "../Environment.ts";
7 import {
8 ALCHEMY_HOST_SA_DISPLAY_NAME,
9 deleteHostServiceAccount,
10 hostServiceAccountEmail,
11 } from "../Host.ts";
12 import type { Providers } from "../Providers.ts";
13
14 /**
15 * Nuke-only enumeration of Alchemy-minted Cloud Run / Functions runtime
16 * service accounts (`alch-*` with display name {@link ALCHEMY_HOST_SA_DISPLAY_NAME}).
17 * Not a user-facing resource.
18 *
19 * @internal
20 */
21 export type HostServiceAccount = Resource<
22 "GCP.IAM.HostServiceAccount",
23 object,
24 {
25 name: string;
26 email: string;
27 project: string;
28 accountId: string;
29 },
30 never,
31 Providers
32 >;
33
34 export const HostServiceAccount = Resource<HostServiceAccount>("GCP.IAM.HostServiceAccount");
35
36 const toAttrs = (
37 account: iam.ServiceAccount,
38 project: string,
39 ): HostServiceAccount["Attributes"] => {
40 const email = account.email ?? "";
41 const accountId = email.split("@")[0] ?? "";
42 return {
43 name: account.name ?? "",
44 email,
45 project,
46 accountId,
47 };
48 };
49
50 const isManaged = (account: iam.ServiceAccount, project: string) => {
51 const email = account.email ?? "";
52 return (
53 account.displayName === ALCHEMY_HOST_SA_DISPLAY_NAME &&
54 email.startsWith("alch-") &&
55 email.endsWith(`@${project}.iam.gserviceaccount.com`)
56 );
57 };
58
59 export const HostServiceAccountProvider = () =>
60 Provider.succeed(HostServiceAccount, {
61 stables: ["name", "email", "project", "accountId"],
62
63 read: Effect.fn(function* ({ output }) {
64 if (output === undefined) return undefined;
65 const env = yield* GcpEnvironment.current;
66 const accountId = output.accountId;
67 const email = hostServiceAccountEmail(env.project, accountId);
68 const name = `projects/${env.project}/serviceAccounts/${email}`;
69 const existing = yield* iam
70 .getProjectsServiceAccounts({ name })
71 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
72 if (existing === undefined || !isManaged(existing, env.project)) {
73 return undefined;
74 }
75 return toAttrs(existing, env.project);
76 }),
77
78 // Host accounts are minted by their host (`GCP.Run.Service`, …); this
79 // resource only exists so `nuke` can list and delete leaked ones.
80 reconcile: Effect.fn(function* ({ id, output }) {
81 const env = yield* GcpEnvironment.current;
82 const accountId = output?.accountId ?? `alch-${id.toLowerCase()}`.slice(0, 30);
83 const email = hostServiceAccountEmail(env.project, accountId);
84 const name = `projects/${env.project}/serviceAccounts/${email}`;
85 const existing = yield* iam
86 .getProjectsServiceAccounts({ name })
87 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
88 if (existing === undefined) {
89 return {
90 name,
91 email,
92 project: env.project,
93 accountId,
94 };
95 }
96 return toAttrs(existing, env.project);
97 }),
98
99 list: () =>
100 Effect.gen(function* () {
101 const env = yield* GcpEnvironment.current;
102 return yield* iam.listProjectsServiceAccounts
103 .pages({
104 name: `projects/${env.project}`,
105 pageSize: 100,
106 })
107 .pipe(
108 Stream.flatMap((page) => Stream.fromIterable(page.accounts ?? [])),
109 Stream.filter((account) => isManaged(account, env.project)),
110 Stream.map((account) => toAttrs(account, env.project)),
111 Stream.runCollect,
112 Effect.map((chunk) => Array.from(chunk)),
113 Effect.catchTag("NotFound", () =>
114 Effect.succeed([] as HostServiceAccount["Attributes"][]),
115 ),
116 );
117 }),
118
119 delete: Effect.fn(function* ({ output }) {
120 yield* deleteHostServiceAccount({
121 project: output.project,
122 email: hostServiceAccountEmail(output.project, output.accountId),
123 });
124 }),
125 });