@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 114 lines 3.79 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 bigquery from "@distilled.cloud/gcp/bigquery_v2";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8
9 /**
10 * Grants on services without a per-resource `setIamPolicy`: BigQuery
11 * datasets (the dataset access list) and Firestore databases (a project
12 * grant under an IAM Condition naming the database). Image-only hosts, so
13 * no container build is needed.
14 */
15
16 const { test } = Test.make({ providers: GCP.providers() });
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 const HELLO_IMAGE = "us-docker.pkg.dev/cloudrun/container/hello";
21
22 const Warehouse = GCP.BigQuery.Dataset("ScopedWarehouse", {
23 location: "US-CENTRAL1",
24 forceDestroy: true,
25 });
26
27 const Docs = GCP.Firestore.Database("ScopedDocs", {
28 location: "us-central1",
29 type: "FIRESTORE_NATIVE",
30 });
31
32 class ScopedService extends GCP.Function<ScopedService>()(
33 "ScopedService",
34 {
35 location: "us-central1",
36 template: { containers: [{ image: HELLO_IMAGE }] },
37 },
38 Effect.gen(function* () {
39 yield* GCP.BigQuery.Query(Warehouse);
40 yield* GCP.Firestore.ReadWriteDatabase(Docs);
41 return {};
42 }).pipe(
43 Effect.provide(GCP.BigQuery.QueryHttp),
44 Effect.provide(GCP.Firestore.ReadWriteDatabaseHttp),
45 ),
46 ) {}
47
48 const rolesOf = (
49 bindings: ReadonlyArray<{
50 role?: string;
51 members?: ReadonlyArray<string>;
52 condition?: unknown;
53 }>,
54 member: string,
55 ) =>
56 bindings
57 .filter((binding) => (binding.members ?? []).includes(member))
58 .map((binding) => ({
59 role: binding.role,
60 condition: (binding.condition as { expression?: string } | undefined)?.expression,
61 }));
62
63 test.provider(
64 "dataset grants use the access list; Firestore grants are conditioned on the database",
65 (stack) =>
66 Effect.gen(function* () {
67 yield* stack.destroy();
68
69 const out = yield* stack.deploy(
70 Effect.gen(function* () {
71 const service = yield* ScopedService;
72 const dataset = yield* Warehouse;
73 const docs = yield* Docs;
74 return {
75 database: docs.name,
76 project: service.project,
77 serviceAccount: service.serviceAccount,
78 datasetId: dataset.datasetId,
79 };
80 }),
81 );
82 const email = out.serviceAccount!;
83
84 const projectPolicy = yield* resourcemanager.getIamPolicyProjects({
85 resource: `projects/${out.project}`,
86 body: { options: { requestedPolicyVersion: 3 } },
87 });
88 // bigquery.jobs.create is project-only; data access is not.
89 expect(rolesOf(projectPolicy.bindings ?? [], `serviceAccount:${email}`)).toEqual(
90 expect.arrayContaining([
91 { role: "roles/bigquery.jobUser", condition: undefined },
92 // Firestore has no per-database policy: a project grant scoped
93 // to this database by an IAM Condition.
94 {
95 role: "roles/datastore.user",
96 condition: `resource.name == "${out.database}" || resource.name.startsWith("${out.database}/")`,
97 },
98 ]),
99 );
100 expect(rolesOf(projectPolicy.bindings ?? [], `serviceAccount:${email}`)).toHaveLength(2);
101
102 const dataset = yield* bigquery.getDatasets({
103 projectId: out.project,
104 datasetId: out.datasetId,
105 });
106 expect(
107 (dataset.access ?? []).filter((item) => item.userByEmail === email),
108 // BigQuery stores roles/bigquery.dataViewer as the legacy READER.
109 ).toEqual([{ role: "READER", userByEmail: email }]);
110
111 yield* stack.destroy();
112 }).pipe(logLevel),
113 { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 240_000 },
114 );