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

main
ts 278 lines 9.21 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as bigquery from "@distilled.cloud/gcp/bigquery_v2";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import BigQueryBindingsHost, {
10 Analytics,
11 Inserted,
12 Listed,
13 Queried,
14 ReadRows,
15 ReadWrite,
16 Written,
17 } from "./fixtures/bindings-host.ts";
18
19 const testOptions = { providers: GCP.providers() };
20 const { test, beforeAll, afterAll } = Test.make(testOptions);
21 const sharedStack = Core.scratchStack(testOptions, "BigQueryBindings");
22
23 let baseUrl: string;
24 let hostAccount: string;
25 let dataset: { project: string; datasetId: string; location: string };
26 let tables: {
27 queried: string;
28 listed: string;
29 inserted: string;
30 readRows: string;
31 written: string;
32 readWrite: string;
33 };
34
35 /** Run a GoogleSQL statement as the deploying principal. */
36 const runQuery = (sql: string) =>
37 bigquery
38 .queryJobs({
39 projectId: dataset.project,
40 body: {
41 query: sql,
42 useLegacySql: false,
43 location: dataset.location,
44 defaultDataset: {
45 projectId: dataset.project,
46 datasetId: dataset.datasetId,
47 },
48 timeoutMs: 60_000,
49 },
50 })
51 .pipe(
52 Effect.map((response) => {
53 expect(response.jobComplete).toEqual(true);
54 return (response.rows ?? []).map((row) => row.f?.[0]?.v);
55 }),
56 );
57
58 /**
59 * Seed one row with DML: committed storage is immediately visible to
60 * tabledata.list (streamed rows may still sit in the buffer).
61 */
62 const seed = (table: string) => runQuery(`INSERT INTO ${table} (id) VALUES ('seed')`);
63
64 /** Every project-level role (and its IAM Condition) `account` holds. */
65 const projectGrantsOf = (account: string) =>
66 resourcemanager
67 .getIamPolicyProjects({
68 resource: `projects/${dataset.project}`,
69 body: { options: { requestedPolicyVersion: 3 } },
70 })
71 .pipe(
72 Effect.map((policy) =>
73 (policy.bindings ?? [])
74 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
75 .map((binding) => ({
76 role: binding.role,
77 condition: binding.condition?.expression,
78 })),
79 ),
80 );
81
82 /** bigquery.jobs.create is only grantable on the project. */
83 const JOB_USER = [{ role: "roles/bigquery.jobUser", condition: undefined }];
84
85 /** Roles `account` holds on a table's own IAM policy. */
86 const tableRolesOf = (table: string, account: string) =>
87 bigquery
88 .getIamPolicyTables({
89 resource: table,
90 body: { options: { requestedPolicyVersion: 1 } },
91 })
92 .pipe(
93 Effect.map((policy) =>
94 (policy.bindings ?? [])
95 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
96 .map((binding) => binding.role),
97 ),
98 );
99
100 const LEGACY_DATASET_ROLES: Record<string, string> = {
101 READER: "roles/bigquery.dataViewer",
102 WRITER: "roles/bigquery.dataEditor",
103 OWNER: "roles/bigquery.dataOwner",
104 };
105
106 describe(
107 "BigQuery Bindings",
108 {
109 tags: ["provider:gcp", "provider:gcp:bigquery", "provider:gcp:run", "live"],
110 },
111 () => {
112 beforeAll(
113 Effect.gen(function* () {
114 yield* sharedStack.destroy();
115 const out = yield* sharedStack.deploy(
116 Effect.gen(function* () {
117 const host = yield* BigQueryBindingsHost;
118 const analytics = yield* Analytics;
119 return {
120 uri: host.uri,
121 serviceAccount: host.serviceAccount,
122 dataset: {
123 project: analytics.project,
124 datasetId: analytics.datasetId,
125 location: analytics.location,
126 },
127 tables: {
128 // Not bound by any table binding: deployed for Query.
129 queried: (yield* Queried).name,
130 listed: (yield* Listed).name,
131 inserted: (yield* Inserted).name,
132 readRows: (yield* ReadRows).name,
133 written: (yield* Written).name,
134 readWrite: (yield* ReadWrite).name,
135 },
136 };
137 }),
138 );
139 baseUrl = out.uri!;
140 hostAccount = out.serviceAccount!;
141 dataset = out.dataset;
142 tables = out.tables;
143 }),
144 { timeout: 900_000 },
145 );
146
147 afterAll(sharedStack.destroy(), { timeout: 600_000 });
148
149 describe("Query", () => {
150 test.provider(
151 "queries the dataset as the host's service account, reading only that dataset",
152 (_stack) =>
153 Effect.gen(function* () {
154 yield* seed("queried");
155 const response = yield* expectProbe<bigquery.QueryResponse>(baseUrl, "query");
156 expect(response.jobComplete).toEqual(true);
157 expect((response.rows ?? []).map((row) => row.f?.[0]?.v)).toEqual(["seed"]);
158
159 const live = yield* bigquery.getDatasets({
160 projectId: dataset.project,
161 datasetId: dataset.datasetId,
162 });
163 const datasetRoles = (live.access ?? [])
164 .filter((item) => item.userByEmail === hostAccount)
165 .map((item) => LEGACY_DATASET_ROLES[item.role!] ?? item.role);
166 expect(datasetRoles).toEqual(["roles/bigquery.dataViewer"]);
167 expect(yield* projectGrantsOf(hostAccount)).toEqual(JOB_USER);
168 }),
169 {
170 tags: ["provider:gcp", "provider:gcp:bigquery", "live"],
171 timeout: 600_000,
172 },
173 );
174 });
175
176 describe("InsertAll", () => {
177 test.provider(
178 "streams a row as the host's service account, granted on the table only",
179 (_stack) =>
180 Effect.gen(function* () {
181 const response = yield* expectProbe<{
182 insertErrors?: unknown[];
183 }>(baseUrl, "insertAll");
184 expect(response.insertErrors ?? []).toEqual([]);
185 expect(yield* runQuery("SELECT id FROM inserted")).toEqual(["insert-all"]);
186 expect(yield* tableRolesOf(tables.inserted, hostAccount)).toEqual([
187 "roles/bigquery.dataEditor",
188 ]);
189 }),
190 {
191 tags: ["provider:gcp", "provider:gcp:bigquery", "live"],
192 timeout: 600_000,
193 },
194 );
195 });
196
197 describe("ListTabledata", () => {
198 test.provider(
199 "lists rows as the host's service account, granted on the table only",
200 (_stack) =>
201 Effect.gen(function* () {
202 yield* seed("listed");
203 const page = yield* expectProbe<bigquery.TableDataList>(baseUrl, "listTabledata");
204 expect((page.rows ?? []).map((row) => row.f?.[0]?.v)).toEqual(["seed"]);
205 expect(yield* tableRolesOf(tables.listed, hostAccount)).toEqual([
206 "roles/bigquery.dataViewer",
207 ]);
208 }),
209 {
210 tags: ["provider:gcp", "provider:gcp:bigquery", "live"],
211 timeout: 600_000,
212 },
213 );
214 });
215
216 describe("ReadTable", () => {
217 test.provider(
218 "lists and queries the table as the host's service account",
219 (_stack) =>
220 Effect.gen(function* () {
221 yield* seed("read_rows");
222 const out = yield* expectProbe<{
223 listed: Array<{ id: string }>;
224 queried: Array<{ id: string }>;
225 }>(baseUrl, "readTable");
226 expect(out.listed).toEqual([{ id: "seed" }]);
227 expect(out.queried).toEqual([{ id: "seed" }]);
228 expect(yield* tableRolesOf(tables.readRows, hostAccount)).toEqual([
229 "roles/bigquery.dataViewer",
230 ]);
231 expect(yield* projectGrantsOf(hostAccount)).toEqual(JOB_USER);
232 }),
233 {
234 tags: ["provider:gcp", "provider:gcp:bigquery", "live"],
235 timeout: 600_000,
236 },
237 );
238 });
239
240 describe("WriteTable", () => {
241 test.provider(
242 "inserts a row as the host's service account, granted on the table only",
243 (_stack) =>
244 Effect.gen(function* () {
245 yield* expectProbe(baseUrl, "writeTable");
246 expect(yield* runQuery("SELECT id FROM written")).toEqual(["written"]);
247 expect(yield* tableRolesOf(tables.written, hostAccount)).toEqual([
248 "roles/bigquery.dataEditor",
249 ]);
250 }),
251 {
252 tags: ["provider:gcp", "provider:gcp:bigquery", "live"],
253 timeout: 600_000,
254 },
255 );
256 });
257
258 describe("ReadWriteTable", () => {
259 test.provider(
260 "inserts and reads back a row as the host's service account",
261 (_stack) =>
262 Effect.gen(function* () {
263 const rows = yield* expectProbe<Array<{ id: string }>>(baseUrl, "readWriteTable");
264 expect(rows).toEqual([{ id: "read-write" }]);
265 expect(yield* runQuery("SELECT id FROM read_write")).toEqual(["read-write"]);
266 expect(yield* tableRolesOf(tables.readWrite, hostAccount)).toEqual([
267 "roles/bigquery.dataEditor",
268 ]);
269 expect(yield* projectGrantsOf(hostAccount)).toEqual(JOB_USER);
270 }),
271 {
272 tags: ["provider:gcp", "provider:gcp:bigquery", "live"],
273 timeout: 600_000,
274 },
275 );
276 });
277 },
278 );