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

main
ts 575 lines 20.1 KB
Raw
1 import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1";
2 import * as bigquery from "@distilled.cloud/gcp/bigquery_v2";
3 import * as bigqueryconnection from "@distilled.cloud/gcp/bigqueryconnection_v1";
4 import * as bigtableadmin from "@distilled.cloud/gcp/bigtableadmin_v2";
5 import * as binaryauthorization from "@distilled.cloud/gcp/binaryauthorization_v1";
6 import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2";
7 import * as kms from "@distilled.cloud/gcp/cloudkms_v1";
8 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
9 import * as cloudtasks from "@distilled.cloud/gcp/cloudtasks_v2";
10 import * as compute from "@distilled.cloud/gcp/compute_v1";
11 import * as containeranalysis from "@distilled.cloud/gcp/containeranalysis_v1";
12 import * as dataproc from "@distilled.cloud/gcp/dataproc_v1";
13 import * as ml from "@distilled.cloud/gcp/ml_v1";
14 import * as privateca from "@distilled.cloud/gcp/privateca_v1";
15 import type { GcpOpContext } from "@distilled.cloud/gcp/Protocol";
16 import * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
17 import * as run from "@distilled.cloud/gcp/run_v2";
18 import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1";
19 import * as servicedirectory from "@distilled.cloud/gcp/servicedirectory_v1";
20 import * as spanner from "@distilled.cloud/gcp/spanner_v1";
21 import * as storage from "@distilled.cloud/gcp/storage_v1";
22 import * as iam from "@distilled.cloud/gcp/unstable/iam_v1";
23 import * as workstations from "@distilled.cloud/gcp/workstations_v1";
24 import * as Data from "effect/Data";
25 import * as Effect from "effect/Effect";
26 import * as Schedule from "effect/Schedule";
27
28 /**
29 * The subset of a Google IAM `Policy` Alchemy reads and writes. Every
30 * service's generated `Policy` type is structurally compatible.
31 */
32 export interface IamPolicy {
33 version?: number;
34 etag?: string;
35 bindings?: ReadonlyArray<IamPolicyBinding>;
36 auditConfigs?: ReadonlyArray<unknown>;
37 }
38
39 export interface IamPolicyBinding {
40 role?: string;
41 members?: ReadonlyArray<string>;
42 condition?: IamCondition;
43 }
44
45 /**
46 * An IAM Condition (CEL). Alchemy uses it to narrow a project-level grant
47 * to one resource on services with no per-resource IAM policy (Firestore,
48 * Cloud SQL, Memorystore, …): `resource.name == "projects/p/…"`.
49 */
50 export interface IamCondition {
51 title?: string;
52 expression?: string;
53 description?: string;
54 }
55
56 /** Condition identity: two conditions are the same binding iff equal. */
57 const sameCondition = (left: IamCondition | undefined, right: IamCondition | undefined) =>
58 (left === undefined && right === undefined) ||
59 (left !== undefined &&
60 right !== undefined &&
61 left.expression === right.expression &&
62 (left.title ?? "") === (right.title ?? ""));
63
64 /**
65 * Resource kinds whose own IAM policy a binding can grant on. Kinds map
66 * 1:1 onto a service's `getIamPolicy` / `setIamPolicy` pair, so a grant
67 * reaches exactly one resource (the GCP analog of an IAM statement's
68 * `Resource: [arn]`).
69 */
70 export type GcpIamResourceKind =
71 | "project"
72 | "artifactregistry.repository"
73 | "bigquery.dataset"
74 | "bigquery.table"
75 | "bigqueryconnection.connection"
76 | "bigtable.instance"
77 | "bigtable.table"
78 | "binaryauthorization.attestor"
79 | "cloudfunctions.function"
80 | "cloudtasks.queue"
81 | "compute.instance"
82 | "containeranalysis.note"
83 | "dataproc.cluster"
84 | "iam.serviceAccount"
85 | "kms.cryptoKey"
86 | "ml.model"
87 | "privateca.caPool"
88 | "pubsub.schema"
89 | "pubsub.subscription"
90 | "pubsub.topic"
91 | "run.job"
92 | "run.service"
93 | "run.workerPool"
94 | "secretmanager.secret"
95 | "servicedirectory.namespace"
96 | "servicedirectory.service"
97 | "spanner.database"
98 | "spanner.instance"
99 | "storage.bucket"
100 | "workstations.workstation"
101 | "workstations.workstationConfig";
102
103 /** Error from any IAM policy operation, narrowed to the fields Alchemy reads. */
104 type IamPolicyError = { readonly _tag: string; readonly message?: string };
105
106 interface IamPolicyTarget {
107 get: Effect.Effect<IamPolicy, IamPolicyError, GcpOpContext>;
108 set: (policy: IamPolicy) => Effect.Effect<unknown, IamPolicyError, GcpOpContext>;
109 }
110
111 // The generated per-service `Policy` shapes are structurally identical to
112 // `IamPolicy`, but each is a distinct nominal type; these adapters bridge
113 // the two once instead of casting at every call site.
114 type QueryGet = (input: {
115 resource: string;
116 "options.requestedPolicyVersion"?: number;
117 }) => Effect.Effect<any, IamPolicyError, GcpOpContext>;
118 type BodyGet = (input: {
119 resource: string;
120 body?: { options?: { requestedPolicyVersion?: number } };
121 }) => Effect.Effect<any, IamPolicyError, GcpOpContext>;
122 type Set = (input: {
123 resource: string;
124 body?: { policy?: any };
125 }) => Effect.Effect<any, IamPolicyError, GcpOpContext>;
126
127 const queryStyle =
128 (get: QueryGet, set: Set) =>
129 (resource: string): IamPolicyTarget => ({
130 get: get({ resource, "options.requestedPolicyVersion": 3 }),
131 set: (policy) => set({ resource, body: { policy } }),
132 });
133
134 const bodyStyle =
135 (get: BodyGet, set: Set, policyVersion = 3) =>
136 (resource: string): IamPolicyTarget => ({
137 get: get({
138 resource,
139 body: { options: { requestedPolicyVersion: policyVersion } },
140 }),
141 set: (policy) => set({ resource, body: { policy } }),
142 });
143
144 const bucketName = (name: string) =>
145 name.replace(/^projects\/_\/buckets\//, "").replace(/^buckets\//, "");
146
147 const computeInstance = (name: string): IamPolicyTarget => {
148 const match = /projects\/([^/]+)\/zones\/([^/]+)\/instances\/([^/]+)/.exec(name);
149 const [, project = "", zone = "", resource = ""] = match ?? [];
150 return {
151 get: compute.getIamPolicyInstances({
152 project,
153 zone,
154 resource,
155 optionsRequestedPolicyVersion: 3,
156 }),
157 set: (policy) =>
158 compute.setIamPolicyInstances({
159 project,
160 zone,
161 resource,
162 body: { policy: policy as compute.Policy },
163 }),
164 };
165 };
166
167 type DatasetAccessItem = bigquery.DatasetAccessItem;
168
169 /** BigQuery stores the three basic dataset roles under legacy names. */
170 const LEGACY_DATASET_ROLES: Record<string, string> = {
171 READER: "roles/bigquery.dataViewer",
172 WRITER: "roles/bigquery.dataEditor",
173 OWNER: "roles/bigquery.dataOwner",
174 };
175 const datasetRole = (role: string) => LEGACY_DATASET_ROLES[role] ?? role;
176
177 const principalOf = (item: DatasetAccessItem): string | undefined =>
178 item.iamMember ??
179 (item.userByEmail
180 ? `${item.userByEmail.endsWith(".gserviceaccount.com") ? "serviceAccount" : "user"}:${item.userByEmail}`
181 : item.groupByEmail
182 ? `group:${item.groupByEmail}`
183 : item.domain
184 ? `domain:${item.domain}`
185 : undefined);
186
187 const accessItemOf = (role: string, member: string): DatasetAccessItem => {
188 const [type, id = ""] = member.split(/:(.*)/s);
189 switch (type) {
190 case "serviceAccount":
191 case "user":
192 return { role, userByEmail: id };
193 case "group":
194 return { role, groupByEmail: id };
195 case "domain":
196 return { role, domain: id };
197 default:
198 return { role, iamMember: member };
199 }
200 };
201
202 /**
203 * BigQuery datasets have no `setIamPolicy`; their access list is the
204 * policy. Principal entries map to bindings; view, routine, dataset,
205 * special-group, and conditional entries are carried through untouched.
206 */
207 const bigqueryDataset = (name: string): IamPolicyTarget => {
208 const match = /projects\/([^/]+)\/datasets\/([^/]+)/.exec(name);
209 const [, projectId = "", datasetId = ""] = match ?? [];
210 const isPrincipal = (item: DatasetAccessItem) =>
211 item.condition === undefined && item.role !== undefined && principalOf(item) !== undefined;
212 const read = bigquery.getDatasets({ projectId, datasetId });
213 return {
214 get: read.pipe(
215 Effect.map((dataset) => {
216 const byRole = new Map<string, string[]>();
217 for (const item of dataset.access ?? []) {
218 if (!isPrincipal(item)) continue;
219 const role = datasetRole(item.role!);
220 const members = byRole.get(role) ?? [];
221 members.push(principalOf(item)!);
222 byRole.set(role, members);
223 }
224 return {
225 etag: dataset.etag,
226 bindings: [...byRole].map(([role, members]) => ({ role, members })),
227 } satisfies IamPolicy;
228 }),
229 ),
230 set: (policy) =>
231 read.pipe(
232 Effect.flatMap((dataset) =>
233 bigquery.patchDatasets({
234 projectId,
235 datasetId,
236 body: {
237 etag: dataset.etag,
238 access: [
239 ...(dataset.access ?? []).filter((item) => !isPrincipal(item)),
240 ...(policy.bindings ?? []).flatMap((binding) =>
241 (binding.members ?? []).map((member) => accessItemOf(binding.role ?? "", member)),
242 ),
243 ],
244 },
245 }),
246 ),
247 ),
248 };
249 };
250
251 const secret = (name: string): IamPolicyTarget =>
252 /\/locations\//.test(name)
253 ? queryStyle(
254 secretmanager.getIamPolicyProjectsLocationsSecrets,
255 secretmanager.setIamPolicyProjectsLocationsSecrets,
256 )(name)
257 : queryStyle(
258 secretmanager.getIamPolicyProjectsSecrets,
259 secretmanager.setIamPolicyProjectsSecrets,
260 )(name);
261
262 const TARGETS: Record<GcpIamResourceKind, (name: string) => IamPolicyTarget> = {
263 project: bodyStyle(resourcemanager.getIamPolicyProjects, resourcemanager.setIamPolicyProjects),
264 "artifactregistry.repository": queryStyle(
265 artifactregistry.getIamPolicyProjectsLocationsRepositories,
266 artifactregistry.setIamPolicyProjectsLocationsRepositories,
267 ),
268 "bigquery.dataset": bigqueryDataset,
269 // BigQuery table policies reject requestedPolicyVersion 3.
270 "bigquery.table": bodyStyle(bigquery.getIamPolicyTables, bigquery.setIamPolicyTables, 1),
271 "bigqueryconnection.connection": bodyStyle(
272 bigqueryconnection.getIamPolicyProjectsLocationsConnections,
273 bigqueryconnection.setIamPolicyProjectsLocationsConnections,
274 ),
275 "bigtable.instance": bodyStyle(
276 bigtableadmin.getIamPolicyProjectsInstances,
277 bigtableadmin.setIamPolicyProjectsInstances,
278 ),
279 "bigtable.table": bodyStyle(
280 bigtableadmin.getIamPolicyProjectsInstancesTables,
281 bigtableadmin.setIamPolicyProjectsInstancesTables,
282 ),
283 "binaryauthorization.attestor": queryStyle(
284 binaryauthorization.getIamPolicyProjectsAttestors,
285 binaryauthorization.setIamPolicyProjectsAttestors,
286 ),
287 "cloudfunctions.function": queryStyle(
288 cloudfunctions.getIamPolicyProjectsLocationsFunctions,
289 cloudfunctions.setIamPolicyProjectsLocationsFunctions,
290 ),
291 "cloudtasks.queue": bodyStyle(
292 cloudtasks.getIamPolicyProjectsLocationsQueues,
293 cloudtasks.setIamPolicyProjectsLocationsQueues,
294 ),
295 "compute.instance": computeInstance,
296 "containeranalysis.note": bodyStyle(
297 containeranalysis.getIamPolicyProjectsNotes,
298 containeranalysis.setIamPolicyProjectsNotes,
299 ),
300 "dataproc.cluster": bodyStyle(
301 dataproc.getIamPolicyProjectsRegionsClusters,
302 dataproc.setIamPolicyProjectsRegionsClusters,
303 ),
304 "iam.serviceAccount": queryStyle(
305 iam.getIamPolicyProjectsServiceAccounts,
306 iam.setIamPolicyProjectsServiceAccounts,
307 ),
308 "kms.cryptoKey": queryStyle(
309 kms.getIamPolicyProjectsLocationsKeyRingsCryptoKeys,
310 kms.setIamPolicyProjectsLocationsKeyRingsCryptoKeys,
311 ),
312 "ml.model": queryStyle(ml.getIamPolicyProjectsModels, ml.setIamPolicyProjectsModels),
313 "privateca.caPool": queryStyle(
314 privateca.getIamPolicyProjectsLocationsCaPools,
315 privateca.setIamPolicyProjectsLocationsCaPools,
316 ),
317 "pubsub.schema": queryStyle(
318 pubsub.getIamPolicyProjectsSchemas,
319 pubsub.setIamPolicyProjectsSchemas,
320 ),
321 "pubsub.subscription": queryStyle(
322 pubsub.getIamPolicyProjectsSubscriptions,
323 pubsub.setIamPolicyProjectsSubscriptions,
324 ),
325 "pubsub.topic": queryStyle(pubsub.getIamPolicyProjectsTopics, pubsub.setIamPolicyProjectsTopics),
326 "run.job": queryStyle(
327 run.getIamPolicyProjectsLocationsJobs,
328 run.setIamPolicyProjectsLocationsJobs,
329 ),
330 "run.service": queryStyle(
331 run.getIamPolicyProjectsLocationsServices,
332 run.setIamPolicyProjectsLocationsServices,
333 ),
334 "run.workerPool": queryStyle(
335 run.getIamPolicyProjectsLocationsWorkerPools,
336 run.setIamPolicyProjectsLocationsWorkerPools,
337 ),
338 "secretmanager.secret": secret,
339 "servicedirectory.namespace": bodyStyle(
340 servicedirectory.getIamPolicyProjectsLocationsNamespaces,
341 servicedirectory.setIamPolicyProjectsLocationsNamespaces,
342 ),
343 "servicedirectory.service": bodyStyle(
344 servicedirectory.getIamPolicyProjectsLocationsNamespacesServices,
345 servicedirectory.setIamPolicyProjectsLocationsNamespacesServices,
346 ),
347 "spanner.database": bodyStyle(
348 spanner.getIamPolicyProjectsInstancesDatabases,
349 spanner.setIamPolicyProjectsInstancesDatabases,
350 ),
351 "spanner.instance": bodyStyle(
352 spanner.getIamPolicyProjectsInstances,
353 spanner.setIamPolicyProjectsInstances,
354 ),
355 "storage.bucket": (name) => ({
356 get: storage.getIamPolicyBuckets({
357 bucket: bucketName(name),
358 optionsRequestedPolicyVersion: 3,
359 }),
360 set: (policy) =>
361 storage.setIamPolicyBuckets({
362 bucket: bucketName(name),
363 body: policy as storage.Policy,
364 }),
365 }),
366 "workstations.workstation": queryStyle(
367 workstations.getIamPolicyProjectsLocationsWorkstationClustersWorkstationConfigsWorkstations,
368 workstations.setIamPolicyProjectsLocationsWorkstationClustersWorkstationConfigsWorkstations,
369 ),
370 "workstations.workstationConfig": queryStyle(
371 workstations.getIamPolicyProjectsLocationsWorkstationClustersWorkstationConfigs,
372 workstations.setIamPolicyProjectsLocationsWorkstationClustersWorkstationConfigs,
373 ),
374 };
375
376 /** A single `role` → `member` edge on one resource's IAM policy. */
377 export interface IamMembership {
378 kind: GcpIamResourceKind;
379 /** Full resource name (`projects/p/topics/t`) or project id for `project`. */
380 name: string;
381 role: string;
382 member: string;
383 }
384
385 const principal = (member: string) =>
386 /^(serviceAccount|user|group|domain|principal|principalSet):/.test(member) ||
387 member === "allUsers" ||
388 member === "allAuthenticatedUsers"
389 ? member
390 : `serviceAccount:${member}`;
391
392 const targetName = (kind: GcpIamResourceKind, name: string) =>
393 kind === "project" && !name.startsWith("projects/") ? `projects/${name}` : name;
394
395 /**
396 * Rewrite `policy` so `member` holds exactly `add` roles out of `managed`
397 * (roles in `managed` but not in `add` are revoked from the member).
398 * Conditional bindings are never touched — only unconditional
399 * role → members edges are Alchemy's to manage. Returns `undefined` when
400 * the policy already matches.
401 */
402 const rewrite = (
403 policy: IamPolicy,
404 member: string,
405 add: ReadonlySet<string>,
406 remove: ReadonlySet<string>,
407 condition?: IamCondition,
408 ): IamPolicy | undefined => {
409 const bindings = (policy.bindings ?? []).map((binding) => ({
410 ...binding,
411 members: [...(binding.members ?? [])],
412 }));
413 let dirty = false;
414 for (const role of add) {
415 const existing = bindings.find(
416 (binding) => binding.role === role && sameCondition(binding.condition, condition),
417 );
418 if (existing === undefined) {
419 bindings.push(
420 condition === undefined
421 ? { role, members: [member] }
422 : { role, members: [member], condition },
423 );
424 dirty = true;
425 } else if (!existing.members.includes(member)) {
426 existing.members.push(member);
427 dirty = true;
428 }
429 }
430 for (const binding of bindings) {
431 if (!sameCondition(binding.condition, condition)) continue;
432 if (binding.role === undefined || !remove.has(binding.role)) continue;
433 if (add.has(binding.role)) continue;
434 const next = binding.members.filter((item) => item !== member);
435 if (next.length !== binding.members.length) {
436 binding.members = next;
437 dirty = true;
438 }
439 }
440 if (!dirty) return undefined;
441 // Keep the version the policy was read at: conditional bindings only
442 // come back (and must be written) at v3, and some resources (BigQuery
443 // tables) accept nothing above v1.
444 return {
445 ...policy,
446 // Writing a conditional binding requires policy version 3.
447 ...(condition !== undefined ? { version: 3 } : {}),
448 bindings: bindings.filter((binding) => binding.members.length > 0),
449 };
450 };
451
452 /**
453 * Read-modify-write one resource's IAM policy under its etag, re-reading
454 * on a concurrent-write `Conflict` (Cloud Storage: `IamPolicyEtagMismatch`)
455 * so parallel host deploys converge
456 * instead of failing. A freshly created service account can take a few
457 * seconds to become a valid policy member, surfaced as `BadRequest`.
458 */
459 export const updateIamMembership = (options: {
460 kind: GcpIamResourceKind;
461 name: string;
462 member: string;
463 add?: Iterable<string>;
464 remove?: Iterable<string>;
465 /** Manage the bindings carrying exactly this condition. */
466 condition?: IamCondition;
467 }) => {
468 const target = TARGETS[options.kind](targetName(options.kind, options.name));
469 const member = principal(options.member);
470 const add = new Set(options.add ?? []);
471 const remove = new Set(options.remove ?? []);
472 const write = Effect.gen(function* () {
473 const policy = yield* target.get;
474 const next = rewrite(policy, member, add, remove, options.condition);
475 if (next === undefined) return;
476 yield* target.set(next);
477 }).pipe(
478 Effect.retry({
479 while: (error) =>
480 error._tag === "Conflict" ||
481 // Cloud Storage reports a stale etag as 412, not 409.
482 error._tag === "IamPolicyEtagMismatch" ||
483 // A just-created service account the policy service can't see yet.
484 error._tag === "IamMemberNotFound" ||
485 (error._tag === "BadRequest" && /does not exist/i.test(error.message ?? "")),
486 // ~60s total: long enough for a new service account to propagate.
487 times: 6,
488 schedule: Schedule.exponential("1 second"),
489 }),
490 );
491 // Don't report success until GCP reads the change back: callers (and
492 // the resources that depend on them) must never race a write that has
493 // not landed. A lost write (read-after-write lag, a concurrent writer
494 // without etag) is re-applied.
495 const confirmed = target.get.pipe(
496 Effect.flatMap((policy) =>
497 rewrite(policy, member, add, remove, options.condition) === undefined
498 ? Effect.void
499 : Effect.fail(new IamPolicyNotConverged({ resourceName: options.name })),
500 ),
501 );
502 return write.pipe(
503 Effect.andThen(confirmed),
504 Effect.retry({
505 while: (error) => error._tag === "GCP.IamPolicyNotConverged",
506 times: 10,
507 schedule: Schedule.spaced("2 seconds"),
508 }),
509 );
510 };
511
512 /** A written IAM policy did not read back with the change applied. */
513 export class IamPolicyNotConverged extends Data.TaggedError("GCP.IamPolicyNotConverged")<{
514 resourceName: string;
515 }> {}
516
517 /**
518 * Remove `member` from every unconditional binding of `roles` on the
519 * resource. A resource that is already gone has nothing to revoke.
520 */
521 export const revokeIamMembership = (options: {
522 kind: GcpIamResourceKind;
523 name: string;
524 member: string;
525 roles: Iterable<string>;
526 condition?: IamCondition;
527 }) =>
528 updateIamMembership({
529 kind: options.kind,
530 name: options.name,
531 member: options.member,
532 remove: options.roles,
533 condition: options.condition,
534 }).pipe(
535 Effect.catchIf(
536 (error) => error._tag === "NotFound",
537 () => Effect.void,
538 ),
539 );
540
541 /** Roles `member` currently holds unconditionally on the project. */
542 export const projectRolesOf = (project: string, member: string) =>
543 TARGETS.project(targetName("project", project)).get.pipe(
544 Effect.map((policy) => {
545 const principalId = principal(member);
546 return (policy.bindings ?? [])
547 .filter(
548 (binding) =>
549 binding.condition === undefined && (binding.members ?? []).includes(principalId),
550 )
551 .flatMap((binding) => (binding.role ? [binding.role] : []));
552 }),
553 );
554
555 /** True when `member` holds `role` unconditionally on the resource. */
556 export const hasIamMembership = (options: {
557 kind: GcpIamResourceKind;
558 name: string;
559 member: string;
560 role: string;
561 }) =>
562 TARGETS[options.kind](targetName(options.kind, options.name)).get.pipe(
563 Effect.map((policy) =>
564 (policy.bindings ?? []).some(
565 (binding) =>
566 binding.role === options.role &&
567 binding.condition === undefined &&
568 (binding.members ?? []).includes(principal(options.member)),
569 ),
570 ),
571 Effect.catchIf(
572 (error) => error._tag === "NotFound",
573 () => Effect.succeed(false),
574 ),
575 );