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

main
ts 415 lines 14.4 KB
Raw
1 import * as bqdp from "@distilled.cloud/gcp/bigquerydatapolicy_v2";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 LIST_LOCATIONS,
14 compact,
15 hasOwnershipMarker,
16 lastSegment,
17 normalizeLocation,
18 normalizePolicyType,
19 ownedByAlchemy,
20 parentOf,
21 parseName,
22 resourceNameOf,
23 sameJson,
24 sameStringList,
25 sameText,
26 sortedStrings,
27 toDataPolicyId,
28 updateMaskOf,
29 } from "./internal.ts";
30
31 export type DataPolicyType = bqdp.DataPolicyDataPolicyTypeEnum | (string & {});
32
33 export type DataMaskingPolicyPredefinedExpression =
34 | bqdp.DataMaskingPolicyPredefinedExpressionEnum
35 | (string & {});
36
37 export type DataMaskingPolicy = {
38 /**
39 * Predefined masking expression (`SHA256`, `ALWAYS_NULL`,
40 * `DEFAULT_MASKING_VALUE`, `LAST_FOUR_CHARACTERS`,
41 * `FIRST_FOUR_CHARACTERS`, `EMAIL_MASK`, `DATE_YEAR_MASK`,
42 * `RANDOM_HASH`). Mutually exclusive with `routine`.
43 */
44 predefinedExpression?: DataMaskingPolicyPredefinedExpression;
45 /**
46 * BigQuery routine that implements a custom masking function, as
47 * `projects/{project}/datasets/{dataset}/routines/{routine}`. Mutually
48 * exclusive with `predefinedExpression`.
49 */
50 routine?: string;
51 };
52
53 export type DataGovernanceTag = {
54 /**
55 * Namespaced tag key (`parent-id/pii`). Tag keys are globally unique
56 * Resource Manager keys.
57 */
58 key?: string;
59 /**
60 * Short tag value (`sensitive`).
61 */
62 value?: string;
63 };
64
65 export type DataPolicyProps = {
66 /**
67 * Data policy id (the `{dataPolicy}` segment of
68 * `projects/{project}/locations/{location}/dataPolicies/{dataPolicy}`).
69 * If omitted, a unique name is generated from the stack, stage, and
70 * logical id. Letters, digits, and underscores; max 200 characters.
71 * Data policies have no labels field, so Alchemy stamps ownership into
72 * this id (`alch___…`) for `list` / nuke. Immutable — changing it
73 * replaces the policy.
74 */
75 dataPolicyId?: string;
76 /**
77 * BigQuery location (`us-central1`, `US`, `EU`, …). Immutable —
78 * changing it replaces the policy. Multi-regions `US` and `EU` stay
79 * uppercase; regional ids are lowercased (`US-CENTRAL1` becomes
80 * `us-central1`).
81 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
82 */
83 location?: string;
84 /**
85 * Type of data policy. `COLUMN_LEVEL_SECURITY_POLICY` is deprecated in
86 * the v2 API (GET/LIST of v1 policies only).
87 * @default "RAW_DATA_ACCESS_POLICY"
88 */
89 dataPolicyType?: DataPolicyType;
90 /**
91 * Masking rule. Required when `dataPolicyType` is
92 * `DATA_MASKING_POLICY`.
93 */
94 dataMaskingPolicy?: DataMaskingPolicy;
95 /**
96 * IAM V2 principals with fine-grained access to the governed data
97 * (`principal://goog/subject/user@example.com`). V2 policies only.
98 */
99 grantees?: string[];
100 /**
101 * Resource Manager data-governance tag bound to this policy.
102 */
103 dataGovernanceTag?: DataGovernanceTag;
104 };
105
106 export type DataPolicy = Resource<
107 "GCP.BigQueryDataPolicy.DataPolicy",
108 DataPolicyProps,
109 {
110 /** Full resource name `projects/{project}/locations/{location}/dataPolicies/{dataPolicy}`. */
111 name: string;
112 /** Data policy id (last path segment), including the Alchemy prefix. */
113 dataPolicyId: string;
114 /** Project id used when the policy was reconciled. */
115 project: string;
116 /** Location id (`us-central1`, `US`, …). */
117 location: string;
118 /** Policy type. */
119 dataPolicyType: string | undefined;
120 /** Masking rule, if this is a data-masking policy. */
121 dataMaskingPolicy: DataMaskingPolicy | undefined;
122 /** Fine-grained access principals. */
123 grantees: string[];
124 /** Bound data-governance tag, if any. */
125 dataGovernanceTag: DataGovernanceTag | undefined;
126 /** Policy-tag resource name (v1 policies only). */
127 policyTag: string | undefined;
128 /** Server-reported policy version (`V1`, `V2`). */
129 version: string | undefined;
130 /** Server etag used for concurrent updates. */
131 etag: string | undefined;
132 },
133 never,
134 Providers
135 >;
136
137 /**
138 * A BigQuery Data Policy (v2) — raw-data access or column data masking.
139 *
140 * Data policies have no labels, so Alchemy stamps ownership into the
141 * policy id (`alch___…`) so `list` / `pnpm nuke:gcp` can find them.
142 * `dataPolicyId` and `location` are identity — changing either replaces
143 * the policy. Type, masking rule, grantees, and the data-governance tag
144 * update in place. Patches send the observed etag.
145 *
146 * ### Creating a Data Policy
147 * **Example:** Raw-data access
148 * ```typescript
149 * const policy = yield* GCP.BigQueryDataPolicy.DataPolicy("Raw", {
150 * dataPolicyType: "RAW_DATA_ACCESS_POLICY",
151 * });
152 * ```
153 *
154 * **Example:** SHA256 masking
155 * ```typescript
156 * const policy = yield* GCP.BigQueryDataPolicy.DataPolicy("Mask", {
157 * location: "us-central1",
158 * dataPolicyType: "DATA_MASKING_POLICY",
159 * dataMaskingPolicy: { predefinedExpression: "SHA256" },
160 * });
161 * ```
162 *
163 * ### Updating a Data Policy
164 * Change props on the same logical id; the engine keeps the physical id.
165 *
166 * **Example:** Change the masking expression
167 * ```typescript
168 * const policy = yield* GCP.BigQueryDataPolicy.DataPolicy("Mask", {
169 * location: "us-central1",
170 * dataPolicyType: "DATA_MASKING_POLICY",
171 * dataMaskingPolicy: { predefinedExpression: "ALWAYS_NULL" },
172 * });
173 * ```
174 *
175 * @resource
176 * @category BigQueryDataPolicy
177 */
178 export const DataPolicy = Resource<DataPolicy>("GCP.BigQueryDataPolicy.DataPolicy");
179
180 export class DataPolicyNotResolved extends Data.TaggedError(
181 "GCP.BigQueryDataPolicy.DataPolicyNotResolved",
182 )<{
183 resourceName: string;
184 }> {}
185
186 const toMasking = (policy: bqdp.DataMaskingPolicy | undefined): DataMaskingPolicy | undefined => {
187 if (policy === undefined) return undefined;
188 const next = compact({
189 predefinedExpression: policy.predefinedExpression,
190 routine: policy.routine,
191 });
192 return Object.keys(next).length > 0 ? next : undefined;
193 };
194
195 const toGovernanceTag = (
196 tag: bqdp.DataGovernanceTag | undefined,
197 ): DataGovernanceTag | undefined => {
198 if (tag === undefined) return undefined;
199 const next = compact({ key: tag.key, value: tag.value });
200 return Object.keys(next).length > 0 ? next : undefined;
201 };
202
203 const toAttrs = (policy: bqdp.DataPolicy, project: string, region: string) => {
204 const name = policy.name ?? "";
205 const parsed = parseName(name, project, region);
206 return {
207 name,
208 dataPolicyId: policy.dataPolicyId ?? parsed.dataPolicyId,
209 project,
210 location: parsed.location,
211 dataPolicyType: policy.dataPolicyType,
212 dataMaskingPolicy: toMasking(policy.dataMaskingPolicy),
213 grantees: sortedStrings(policy.grantees),
214 dataGovernanceTag: toGovernanceTag(policy.dataGovernanceTag),
215 policyTag: policy.policyTag,
216 version: policy.version,
217 etag: policy.etag,
218 };
219 };
220
221 const getByName = (name: string) =>
222 name.length === 0
223 ? Effect.undefined
224 : bqdp
225 .getProjectsLocationsDataPolicies({ name })
226 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
227
228 const listAt = (project: string, location: string) =>
229 bqdp.listProjectsLocationsDataPolicies
230 .pages({
231 parent: parentOf(project, location),
232 pageSize: 1000,
233 })
234 .pipe(
235 Stream.flatMap((page) => Stream.fromIterable(page.dataPolicies ?? [])),
236 Stream.runCollect,
237 Effect.map((chunk) => Array.from(chunk)),
238 Effect.catchTag("NotFound", () => Effect.succeed([] as bqdp.DataPolicy[])),
239 );
240
241 const findOwned = (items: readonly bqdp.DataPolicy[], id: string, name?: string) =>
242 Effect.gen(function* () {
243 if (name !== undefined && name.length > 0) {
244 const match = items.find((item) => item.name === name);
245 if (match !== undefined) return match;
246 }
247 for (const item of items) {
248 const resourceId = item.dataPolicyId ?? lastSegment(item.name ?? "");
249 if (yield* ownedByAlchemy(id, resourceId)) return item;
250 }
251 return undefined;
252 });
253
254 const observe = (id: string, name: string | undefined, project: string, location: string) =>
255 Effect.gen(function* () {
256 if (name !== undefined && name.length > 0) {
257 const existing = yield* getByName(name);
258 if (existing !== undefined) return existing;
259 }
260 const items = yield* listAt(project, location);
261 return yield* findOwned(items, id, name);
262 });
263
264 const toCreateBody = (news: DataPolicyProps, dataPolicyType: string) =>
265 compact({
266 dataPolicyType,
267 dataMaskingPolicy: news.dataMaskingPolicy,
268 grantees: news.grantees,
269 dataGovernanceTag: news.dataGovernanceTag,
270 });
271
272 const patchPolicy = (name: string, updateMask: string, body: bqdp.DataPolicy) =>
273 Effect.gen(function* () {
274 const latest = yield* getByName(name);
275 if (latest === undefined) {
276 return yield* new DataPolicyNotResolved({ resourceName: name });
277 }
278 return yield* bqdp.patchProjectsLocationsDataPolicies({
279 name,
280 updateMask,
281 body: compact({
282 ...body,
283 etag: latest.etag,
284 }),
285 });
286 }).pipe(
287 Effect.retry({
288 while: (error) => error._tag === "Conflict",
289 times: 8,
290 schedule: Schedule.spaced("1 second"),
291 }),
292 );
293
294 export const DataPolicyProvider = () =>
295 Provider.succeed(DataPolicy, {
296 stables: ["name", "dataPolicyId", "project", "location"],
297
298 diff: Effect.fn(function* ({ id, news, olds, output }) {
299 if (!isResolved(news)) return undefined;
300 const env = yield* GcpEnvironment.current;
301 const previousId = olds?.dataPolicyId ?? output?.dataPolicyId;
302 const nextId = yield* toDataPolicyId(id, news.dataPolicyId, previousId);
303 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
304 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
305 if (
306 (previousId === undefined || nextId === previousId) &&
307 previousLocation === nextLocation
308 ) {
309 return undefined;
310 }
311 return {
312 action: "replace" as const,
313 deleteFirst: false,
314 };
315 }),
316
317 read: Effect.fn(function* ({ id, olds, output }) {
318 const env = yield* GcpEnvironment.current;
319 const dataPolicyId = yield* toDataPolicyId(id, olds?.dataPolicyId, output?.dataPolicyId);
320 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
321 const name = output?.name ?? resourceNameOf(env.project, location, dataPolicyId);
322 const existing = yield* observe(id, name, env.project, location);
323 if (existing === undefined) return undefined;
324 const attrs = toAttrs(existing, env.project, env.region);
325 const resourceId = existing.dataPolicyId ?? lastSegment(existing.name ?? "");
326 return (yield* ownedByAlchemy(id, resourceId)) ? attrs : Unowned(attrs);
327 }),
328
329 list: () =>
330 Effect.gen(function* () {
331 const env = yield* GcpEnvironment.current;
332 const pages = yield* Effect.forEach(
333 Array.from(new Set<string>([...LIST_LOCATIONS, env.region])),
334 (location) => listAt(env.project, location),
335 { concurrency: 4 },
336 );
337 const byName = new Map<string, ReturnType<typeof toAttrs>>();
338 for (const item of pages.flat()) {
339 const resourceId = item.dataPolicyId ?? lastSegment(item.name ?? "");
340 if (!hasOwnershipMarker(resourceId)) continue;
341 const attrs = toAttrs(item, env.project, env.region);
342 if (attrs.name.length > 0) byName.set(attrs.name, attrs);
343 }
344 return Array.from(byName.values());
345 }),
346
347 reconcile: Effect.fn(function* ({ id, news, output }) {
348 const env = yield* GcpEnvironment.current;
349 const dataPolicyId = yield* toDataPolicyId(id, news.dataPolicyId, output?.dataPolicyId);
350 const location = normalizeLocation(news.location ?? output?.location, env.region);
351 const parent = parentOf(env.project, location);
352 const name = output?.name ?? resourceNameOf(env.project, location, dataPolicyId);
353 const dataPolicyType = normalizePolicyType(news.dataPolicyType ?? output?.dataPolicyType);
354
355 let current = yield* observe(id, name, env.project, location);
356
357 if (current === undefined) {
358 const created = yield* bqdp
359 .createProjectsLocationsDataPolicies({
360 parent,
361 body: {
362 dataPolicyId,
363 dataPolicy: toCreateBody(news, dataPolicyType),
364 },
365 })
366 .pipe(Effect.catchTag("Conflict", () => observe(id, name, env.project, location)));
367 current = created ?? undefined;
368 }
369
370 if (current === undefined) {
371 return yield* new DataPolicyNotResolved({ resourceName: name });
372 }
373
374 const currentName = current.name ?? name;
375 const desiredType = normalizePolicyType(news.dataPolicyType ?? current.dataPolicyType);
376 const observedType = normalizePolicyType(current.dataPolicyType);
377 const typeChanged = !sameText(observedType, desiredType);
378 const maskingChanged =
379 news.dataMaskingPolicy !== undefined &&
380 !sameJson(toMasking(current.dataMaskingPolicy), news.dataMaskingPolicy);
381 const granteesChanged =
382 news.grantees !== undefined && !sameStringList(current.grantees, news.grantees);
383 const tagChanged =
384 news.dataGovernanceTag !== undefined &&
385 !sameJson(toGovernanceTag(current.dataGovernanceTag), news.dataGovernanceTag);
386
387 const updateMask = updateMaskOf(
388 typeChanged ? "dataPolicyType" : undefined,
389 maskingChanged ? "dataMaskingPolicy" : undefined,
390 granteesChanged ? "grantees" : undefined,
391 tagChanged ? "dataGovernanceTag" : undefined,
392 );
393
394 if (updateMask.length > 0) {
395 current = yield* patchPolicy(
396 currentName,
397 updateMask,
398 compact({
399 dataPolicyType: typeChanged ? desiredType : undefined,
400 dataMaskingPolicy: maskingChanged ? news.dataMaskingPolicy : undefined,
401 grantees: granteesChanged ? news.grantees : undefined,
402 dataGovernanceTag: tagChanged ? news.dataGovernanceTag : undefined,
403 }),
404 );
405 }
406
407 return toAttrs(current, env.project, env.region);
408 }),
409
410 delete: Effect.fn(function* ({ output }) {
411 yield* bqdp
412 .deleteProjectsLocationsDataPolicies({ name: output.name })
413 .pipe(Effect.catchTag("NotFound", () => Effect.void));
414 }),
415 });