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

main
ts 452 lines 15.5 KB
Raw
1 import * as bigquery from "@distilled.cloud/gcp/bigquery_v2";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { ALCHEMY_LABEL_PREFIX } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13
14 const MAX_POLICY_ID_LENGTH = 256;
15
16 export type RowAccessPolicyProps = {
17 /**
18 * Dataset id (the `{dataset}` segment of
19 * `projects/{project}/datasets/{dataset}`) or a full dataset resource
20 * name. Immutable — changing it replaces the policy.
21 */
22 datasetId: string;
23 /**
24 * Table id (the `{table}` segment of
25 * `projects/{project}/datasets/{dataset}/tables/{table}`) or a full
26 * table resource name. Immutable — changing it replaces the policy.
27 */
28 tableId: string;
29 /**
30 * Policy id (the `{policy}` segment of
31 * `projects/{project}/datasets/{dataset}/tables/{table}/rowAccessPolicies/{policy}`).
32 * If omitted, a unique name is generated from the stack, stage, and
33 * logical id. Letters, numbers, and underscores; max 256 characters.
34 * Immutable — changing it replaces the policy.
35 */
36 policyId?: string;
37 /**
38 * SQL boolean expression that selects the rows this policy covers,
39 * similar to a `WHERE` clause. Sent verbatim.
40 */
41 filterPredicate: string;
42 /**
43 * Initial IAM members granted this policy (`user:`, `group:`,
44 * `serviceAccount:`, `domain:`, `allAuthenticatedUsers`, `allUsers`).
45 * Input-only — sent on create, not readable after insert.
46 */
47 grantees?: string[];
48 };
49
50 export type RowAccessPolicy = Resource<
51 "GCP.BigQuery.RowAccessPolicy",
52 RowAccessPolicyProps,
53 {
54 /**
55 * Resource path
56 * `projects/{project}/datasets/{dataset}/tables/{table}/rowAccessPolicies/{policy}`.
57 */
58 name: string;
59 /** Policy id (last path segment). */
60 policyId: string;
61 /** Table id. */
62 tableId: string;
63 /** Dataset id. */
64 datasetId: string;
65 /** Project id. */
66 project: string;
67 /** Filter predicate (a legacy Alchemy ownership suffix is stripped). */
68 filterPredicate: string;
69 /** Server etag. */
70 etag: string | undefined;
71 /** Creation time in milliseconds since epoch. */
72 creationTime: string | undefined;
73 /** Last-modified time in milliseconds since epoch. */
74 lastModifiedTime: string | undefined;
75 },
76 never,
77 Providers
78 >;
79
80 /**
81 * A Google BigQuery row-access policy — a filter predicate plus IAM
82 * members that together hide rows from unauthorized readers.
83 *
84 * Row-access policies have no labels, and Alchemy never edits the filter
85 * predicate: `list` / `pnpm nuke:gcp` return the policies on tables that
86 * carry `alchemy-*` labels, and `read` reports a policy with an explicit
87 * id that Alchemy has no state for as unowned. `datasetId`, `tableId`, and `policyId`
88 * are immutable (changing them replaces the policy). `filterPredicate`
89 * updates in place. `grantees` is create-only.
90 *
91 * ### Creating a Row Access Policy
92 * **Example:** Filter non-null rows
93 * ```typescript
94 * const dataset = yield* GCP.BigQuery.Dataset("Analytics", {
95 * forceDestroy: true,
96 * });
97 * const table = yield* GCP.BigQuery.Table("Events", {
98 * datasetId: dataset.datasetId,
99 * schema: [{ name: "nullable_field", type: "STRING" }],
100 * });
101 * const policy = yield* GCP.BigQuery.RowAccessPolicy("Visible", {
102 * datasetId: dataset.datasetId,
103 * tableId: table.tableId,
104 * filterPredicate: "nullable_field IS NOT NULL",
105 * });
106 * ```
107 *
108 * **Example:** Grant a domain
109 * ```typescript
110 * const policy = yield* GCP.BigQuery.RowAccessPolicy("Domain", {
111 * datasetId: dataset.datasetId,
112 * tableId: table.tableId,
113 * filterPredicate: "TRUE",
114 * grantees: ["domain:example.com"],
115 * });
116 * ```
117 *
118 * @resource
119 * @category BigQuery
120 */
121 export const RowAccessPolicy = Resource<RowAccessPolicy>("GCP.BigQuery.RowAccessPolicy");
122
123 export class RowAccessPolicyNotResolved extends Data.TaggedError(
124 "GCP.BigQuery.RowAccessPolicyNotResolved",
125 )<{
126 resourceName: string;
127 }> {}
128
129 const lastSegment = (value: string) => {
130 const trimmed = value.replace(/\/+$/, "");
131 const parts = trimmed.split("/");
132 return parts[parts.length - 1] || trimmed;
133 };
134
135 const datasetIdOf = (datasetId: string) =>
136 datasetId.includes("/") ? lastSegment(datasetId) : datasetId;
137
138 const tableIdOf = (tableId: string) => (tableId.includes("/") ? lastSegment(tableId) : tableId);
139
140 const resourceName = (project: string, datasetId: string, tableId: string, policyId: string) =>
141 `projects/${project}/datasets/${datasetId}/tables/${tableId}/rowAccessPolicies/${policyId}`;
142
143 const toId = (id: string, policyId: string | undefined, existing?: string) =>
144 Effect.gen(function* () {
145 if (policyId !== undefined) return policyId;
146 if (existing !== undefined) return existing;
147 const generated = yield* createPhysicalName({
148 id,
149 maxLength: MAX_POLICY_ID_LENGTH,
150 lowercase: true,
151 delimiter: "_",
152 });
153 return generated.replaceAll("-", "_");
154 });
155
156 const MARKER_SUFFIX = /\s+AND\s+\('\[alchemy ([^\]]+)\]'\s+IS\s+NOT\s+NULL\)\s*$/i;
157
158 const parseMarkerLabels = (encoded: string): Record<string, string> => {
159 const labels: Record<string, string> = {};
160 for (const part of encoded.split(/\s+/)) {
161 const eq = part.indexOf("=");
162 if (eq > 0) {
163 labels[part.slice(0, eq)] = part.slice(eq + 1);
164 }
165 }
166 return labels;
167 };
168
169 const parseFilter = (
170 filterPredicate: string | undefined,
171 ): {
172 labels: Record<string, string>;
173 filterPredicate: string | undefined;
174 } => {
175 if (filterPredicate === undefined) {
176 return { labels: {}, filterPredicate };
177 }
178 const match = filterPredicate.match(MARKER_SUFFIX);
179 if (match === null || match.index === undefined) {
180 return { labels: {}, filterPredicate };
181 }
182 const labels = parseMarkerLabels(match[1] ?? "");
183 let user = filterPredicate.slice(0, match.index).trim();
184 if (user.startsWith("(") && user.endsWith(")")) {
185 user = user.slice(1, -1);
186 }
187 return {
188 labels,
189 filterPredicate: user.length > 0 ? user : undefined,
190 };
191 };
192
193 const hasAlchemyDatasetLabels = (labels: Record<string, string | undefined> | null | undefined) =>
194 Object.keys(labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX));
195
196 const toAttrs = (policy: bigquery.RowAccessPolicy, project: string) => {
197 const ref = policy.rowAccessPolicyReference;
198 const policyId = ref?.policyId ?? "";
199 const datasetId = ref?.datasetId ?? "";
200 const tableId = ref?.tableId ?? "";
201 const projectId = ref?.projectId ?? project;
202 return {
203 name: resourceName(projectId, datasetId, tableId, policyId),
204 policyId,
205 tableId,
206 datasetId,
207 project: projectId,
208 filterPredicate:
209 parseFilter(policy.filterPredicate).filterPredicate ?? policy.filterPredicate ?? "",
210 etag: policy.etag,
211 creationTime: policy.creationTime,
212 lastModifiedTime: policy.lastModifiedTime,
213 };
214 };
215
216 const getByRef = (projectId: string, datasetId: string, tableId: string, policyId: string) =>
217 bigquery
218 .getRowAccessPolicies({
219 projectId,
220 datasetId,
221 tableId,
222 policyId,
223 })
224 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
225
226 const toPolicyBody = (
227 projectId: string,
228 datasetId: string,
229 tableId: string,
230 policyId: string,
231 news: RowAccessPolicyProps,
232 filter: string,
233 includeGrantees: boolean,
234 ): bigquery.RowAccessPolicy => {
235 const body: bigquery.RowAccessPolicy = {
236 rowAccessPolicyReference: {
237 projectId,
238 datasetId,
239 tableId,
240 policyId,
241 },
242 filterPredicate: filter,
243 };
244 if (includeGrantees && news.grantees !== undefined) {
245 body.grantees = news.grantees;
246 }
247 return body;
248 };
249
250 const sameFilter = (left: string, right: string) =>
251 left.replace(/\s+/g, " ").trim() === right.replace(/\s+/g, " ").trim();
252
253 export const RowAccessPolicyProvider = () =>
254 Provider.succeed(RowAccessPolicy, {
255 stables: ["name", "policyId", "tableId", "datasetId", "project", "creationTime"],
256
257 diff: Effect.fn(function* ({ news, olds, output }) {
258 if (!isResolved(news)) return undefined;
259 const previousPolicyId = olds?.policyId ?? output?.policyId;
260 const nextPolicyId = news.policyId ?? previousPolicyId;
261 const policyIdChanged =
262 news.policyId !== undefined &&
263 previousPolicyId !== undefined &&
264 news.policyId !== previousPolicyId;
265
266 const previousDataset = olds?.datasetId ?? output?.datasetId;
267 const nextDataset = datasetIdOf(news.datasetId);
268 const datasetChanged =
269 previousDataset !== undefined && datasetIdOf(previousDataset) !== nextDataset;
270
271 const previousTable = olds?.tableId ?? output?.tableId;
272 const nextTable = tableIdOf(news.tableId);
273 const tableChanged = previousTable !== undefined && tableIdOf(previousTable) !== nextTable;
274
275 if (!policyIdChanged && !datasetChanged && !tableChanged) {
276 return undefined;
277 }
278 return {
279 action: "replace" as const,
280 deleteFirst:
281 !policyIdChanged &&
282 !datasetChanged &&
283 !tableChanged &&
284 nextPolicyId !== undefined &&
285 previousPolicyId !== undefined &&
286 nextPolicyId === previousPolicyId,
287 };
288 }),
289
290 read: Effect.fn(function* ({ id, olds, output }) {
291 const env = yield* GcpEnvironment.current;
292 const policyId = yield* toId(id, olds?.policyId, output?.policyId);
293 const datasetId = datasetIdOf(olds?.datasetId ?? output?.datasetId ?? "");
294 const tableId = tableIdOf(olds?.tableId ?? output?.tableId ?? "");
295 if (datasetId.length === 0 || tableId.length === 0) return undefined;
296 const existing = yield* getByRef(env.project, datasetId, tableId, policyId);
297 if (existing === undefined) return undefined;
298 const attrs = toAttrs(existing, env.project);
299 // No labels: a generated id derives from this stack, stage, logical
300 // id and instance; an explicit id is only ours when state has it.
301 return output !== undefined || olds?.policyId === undefined ? attrs : Unowned(attrs);
302 }),
303
304 list: () =>
305 Effect.gen(function* () {
306 const env = yield* GcpEnvironment.current;
307 const datasets = yield* bigquery.listDatasets
308 .pages({
309 projectId: env.project,
310 maxResults: 1000,
311 all: true,
312 })
313 .pipe(
314 Stream.flatMap((page) => Stream.fromIterable(page.datasets ?? [])),
315 Stream.filter((dataset) => hasAlchemyDatasetLabels(dataset.labels)),
316 Stream.runCollect,
317 Effect.map((chunk) => Array.from(chunk)),
318 Effect.catchTag("NotFound", () =>
319 Effect.succeed([] as bigquery.DatasetListDatasetsItem[]),
320 ),
321 );
322 const pages = yield* Effect.forEach(
323 datasets,
324 (dataset) => {
325 const datasetId = dataset.datasetReference?.datasetId;
326 if (datasetId === undefined) {
327 return Effect.succeed([] as ReturnType<typeof toAttrs>[]);
328 }
329 return bigquery.listTables
330 .pages({
331 projectId: env.project,
332 datasetId,
333 maxResults: 1000,
334 })
335 .pipe(
336 Stream.flatMap((page) => Stream.fromIterable(page.tables ?? [])),
337 Stream.filter((table) => hasAlchemyDatasetLabels(table.labels)),
338 Stream.map((table) => table.tableReference?.tableId),
339 Stream.filter(
340 (tableId): tableId is string => tableId !== undefined && tableId.length > 0,
341 ),
342 Stream.runCollect,
343 Effect.flatMap((tableIds) =>
344 Effect.forEach(
345 Array.from(tableIds),
346 (tableId) =>
347 bigquery.listRowAccessPolicies
348 .pages({
349 projectId: env.project,
350 datasetId,
351 tableId,
352 pageSize: 1000,
353 })
354 .pipe(
355 Stream.flatMap((page) =>
356 Stream.fromIterable(page.rowAccessPolicies ?? []),
357 ),
358 Stream.map((policy) => toAttrs(policy, env.project)),
359 Stream.runCollect,
360 Effect.map((chunk) => Array.from(chunk)),
361 Effect.catchTag("NotFound", () =>
362 Effect.succeed([] as ReturnType<typeof toAttrs>[]),
363 ),
364 ),
365 { concurrency: 4 },
366 ),
367 ),
368 Effect.map((nested) => nested.flat()),
369 Effect.catchTag("NotFound", () =>
370 Effect.succeed([] as ReturnType<typeof toAttrs>[]),
371 ),
372 );
373 },
374 { concurrency: 4 },
375 );
376 return pages.flat();
377 }),
378
379 reconcile: Effect.fn(function* ({ id, news, output }) {
380 const env = yield* GcpEnvironment.current;
381 const policyId = yield* toId(id, news.policyId, output?.policyId);
382 const datasetId = datasetIdOf(news.datasetId);
383 const tableId = tableIdOf(news.tableId);
384 const name = resourceName(env.project, datasetId, tableId, policyId);
385 const filter = news.filterPredicate;
386
387 let current = yield* getByRef(env.project, datasetId, tableId, policyId);
388
389 if (current === undefined) {
390 const created = yield* bigquery
391 .insertRowAccessPolicies({
392 projectId: env.project,
393 datasetId,
394 tableId,
395 body: toPolicyBody(env.project, datasetId, tableId, policyId, news, filter, true),
396 })
397 .pipe(
398 Effect.catchTag("Conflict", () => getByRef(env.project, datasetId, tableId, policyId)),
399 );
400 current = created ?? undefined;
401 }
402
403 if (current === undefined) {
404 return yield* new RowAccessPolicyNotResolved({ resourceName: name });
405 }
406
407 const observedFilter = current.filterPredicate ?? "";
408 if (!sameFilter(observedFilter, filter)) {
409 current = yield* bigquery
410 .updateRowAccessPolicies({
411 projectId: env.project,
412 datasetId,
413 tableId,
414 policyId,
415 body: toPolicyBody(env.project, datasetId, tableId, policyId, news, filter, false),
416 })
417 .pipe(
418 Effect.catchTag("NotFound", () =>
419 bigquery
420 .insertRowAccessPolicies({
421 projectId: env.project,
422 datasetId,
423 tableId,
424 body: toPolicyBody(env.project, datasetId, tableId, policyId, news, filter, true),
425 })
426 .pipe(
427 Effect.catchTag("Conflict", () =>
428 getByRef(env.project, datasetId, tableId, policyId),
429 ),
430 ),
431 ),
432 );
433 if (current === undefined) {
434 return yield* new RowAccessPolicyNotResolved({ resourceName: name });
435 }
436 }
437
438 return toAttrs(current, env.project);
439 }),
440
441 delete: Effect.fn(function* ({ output }) {
442 yield* bigquery
443 .deleteRowAccessPolicies({
444 projectId: output.project,
445 datasetId: output.datasetId,
446 tableId: output.tableId,
447 policyId: output.policyId,
448 force: true,
449 })
450 .pipe(Effect.catchTag("NotFound", () => Effect.void));
451 }),
452 });