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
| 1 | import * as dlp from "@distilled.cloud/gcp/dlp_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 Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | DEFAULT_LOCATION, |
| 13 | DlpNotResolved, |
| 14 | encodeDisplayName, |
| 15 | fingerprint, |
| 16 | hasOwnershipMarker, |
| 17 | lastSegment, |
| 18 | locationParentsOf, |
| 19 | organizationLocationParent, |
| 20 | normalizeLocation, |
| 21 | organizationIdOf, |
| 22 | ownedByAlchemy, |
| 23 | parseName, |
| 24 | parseOwnership, |
| 25 | replaceOn, |
| 26 | resolveOrganization, |
| 27 | sameText, |
| 28 | toPhysicalId, |
| 29 | tryResolveOrganization, |
| 30 | updateMaskOf, |
| 31 | } from "./internal.ts"; |
| 32 | |
| 33 | export type DiscoveryTarget = dlp.GooglePrivacyDlpV2DiscoveryTarget; |
| 34 | export type OrgConfig = dlp.GooglePrivacyDlpV2OrgConfig; |
| 35 | export type ProcessingLocation = dlp.GooglePrivacyDlpV2ProcessingLocation; |
| 36 | export type OtherCloudDiscoveryStartingLocation = |
| 37 | dlp.GooglePrivacyDlpV2OtherCloudDiscoveryStartingLocation; |
| 38 | export type DiscoveryConfigStatus = dlp.GooglePrivacyDlpV2DiscoveryConfigStatusEnum | (string & {}); |
| 39 | |
| 40 | export type OrganizationsLocationsDiscoveryConfigProps = { |
| 41 | /** |
| 42 | * Config id (the `{discoveryConfig}` segment of |
| 43 | * `organizations/{organization}/locations/{location}/discoveryConfigs/{discoveryConfig}`). |
| 44 | * If omitted, a unique id is generated. Letters, digits, hyphens, and |
| 45 | * underscores; max 100 characters. Immutable — changing it replaces |
| 46 | * the config. |
| 47 | */ |
| 48 | configId?: string; |
| 49 | /** |
| 50 | * Parent organization (`organizations/{organization}` or the numeric |
| 51 | * id). Defaults to `GOOGLE_ORGANIZATION_ID` or the project's Resource |
| 52 | * Manager ancestor. Immutable — changing it replaces the config. |
| 53 | */ |
| 54 | organization?: string; |
| 55 | /** |
| 56 | * Processing location (`us-central1`, `global`, `us`, …). Immutable — |
| 57 | * changing it replaces the config. |
| 58 | * @default "global" |
| 59 | */ |
| 60 | location?: string; |
| 61 | /** |
| 62 | * Display name (max 100 characters). Discovery configs have no labels |
| 63 | * field, so Alchemy ownership is stored in a `[alchemy …]` prefix and |
| 64 | * stripped from attributes. |
| 65 | */ |
| 66 | displayName?: string; |
| 67 | /** |
| 68 | * Config status. Prefer `PAUSED` unless the org should be scanned. |
| 69 | * @default "PAUSED" |
| 70 | */ |
| 71 | status?: DiscoveryConfigStatus; |
| 72 | /** |
| 73 | * Organization scan location and the project that runs discovery. |
| 74 | * Required for organization-parented configs. |
| 75 | */ |
| 76 | orgConfig: OrgConfig; |
| 77 | /** |
| 78 | * Targets that decide what to scan and how often. |
| 79 | */ |
| 80 | targets: DiscoveryTarget[]; |
| 81 | /** |
| 82 | * Inspect templates used for profile generation. |
| 83 | */ |
| 84 | inspectTemplates?: string[]; |
| 85 | /** |
| 86 | * Processing location configuration. |
| 87 | */ |
| 88 | processingLocation?: ProcessingLocation; |
| 89 | /** |
| 90 | * Starting location when scanning other clouds. |
| 91 | */ |
| 92 | otherCloudStartingLocation?: OtherCloudDiscoveryStartingLocation; |
| 93 | }; |
| 94 | |
| 95 | export type OrganizationsLocationsDiscoveryConfig = Resource< |
| 96 | "GCP.DLP.OrganizationsLocationsDiscoveryConfig", |
| 97 | OrganizationsLocationsDiscoveryConfigProps, |
| 98 | { |
| 99 | /** Full resource name. */ |
| 100 | name: string; |
| 101 | /** Config id (last path segment). */ |
| 102 | configId: string; |
| 103 | /** Organization resource name. */ |
| 104 | organization: string; |
| 105 | /** Organization id. */ |
| 106 | organizationId: string; |
| 107 | /** Location id. */ |
| 108 | location: string; |
| 109 | /** Project id of the deploying stack. */ |
| 110 | project: string; |
| 111 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 112 | displayName: string | undefined; |
| 113 | /** Config status. */ |
| 114 | status: string; |
| 115 | /** Organization scan configuration. */ |
| 116 | orgConfig: OrgConfig | undefined; |
| 117 | /** Discovery targets. */ |
| 118 | targets: DiscoveryTarget[]; |
| 119 | /** Inspect templates. */ |
| 120 | inspectTemplates: string[]; |
| 121 | /** Processing location, if set. */ |
| 122 | processingLocation: ProcessingLocation | undefined; |
| 123 | /** Other-cloud starting location, if set. */ |
| 124 | otherCloudStartingLocation: OtherCloudDiscoveryStartingLocation | undefined; |
| 125 | /** RFC3339 creation timestamp. */ |
| 126 | createTime: string | undefined; |
| 127 | /** RFC3339 last-update timestamp. */ |
| 128 | updateTime: string | undefined; |
| 129 | /** RFC3339 last-run timestamp. */ |
| 130 | lastRunTime: string | undefined; |
| 131 | }, |
| 132 | never, |
| 133 | Providers |
| 134 | >; |
| 135 | |
| 136 | /** |
| 137 | * An organization-scoped Sensitive Data Protection discovery config that |
| 138 | * scans storage and builds data profiles. |
| 139 | * |
| 140 | * Discovery configs have no labels field — Alchemy stamps ownership into |
| 141 | * the display name so `list` / nuke can find them. Config id, organization, |
| 142 | * and location are identity. Status, targets, and org config update in |
| 143 | * place. Keep `status` as `PAUSED` unless the organization should be |
| 144 | * scanned. |
| 145 | * |
| 146 | * ### Creating a Discovery Config |
| 147 | * **Example:** Paused BigQuery catch-all |
| 148 | * ```typescript |
| 149 | * const config = yield* GCP.DLP.OrganizationsLocationsDiscoveryConfig( |
| 150 | * "OrgProfiles", |
| 151 | * { |
| 152 | * status: "PAUSED", |
| 153 | * orgConfig: { |
| 154 | * projectId: "my-project", |
| 155 | * location: { organizationId: "123456789" }, |
| 156 | * }, |
| 157 | * targets: [ |
| 158 | * { |
| 159 | * bigQueryTarget: { |
| 160 | * filter: { otherTables: {} }, |
| 161 | * disabled: {}, |
| 162 | * }, |
| 163 | * }, |
| 164 | * ], |
| 165 | * }, |
| 166 | * ); |
| 167 | * ``` |
| 168 | * |
| 169 | * @resource |
| 170 | * @category DLP |
| 171 | */ |
| 172 | export const OrganizationsLocationsDiscoveryConfig = |
| 173 | Resource<OrganizationsLocationsDiscoveryConfig>("GCP.DLP.OrganizationsLocationsDiscoveryConfig"); |
| 174 | |
| 175 | const DEFAULT_STATUS = "PAUSED" satisfies DiscoveryConfigStatus; |
| 176 | |
| 177 | const resourceName = (organization: string, location: string, configId: string) => |
| 178 | `${organizationLocationParent(organization, location)}/discoveryConfigs/${configId}`; |
| 179 | |
| 180 | const toAttrs = ( |
| 181 | config: dlp.GooglePrivacyDlpV2DiscoveryConfig, |
| 182 | organization: string, |
| 183 | project: string, |
| 184 | ) => { |
| 185 | const name = config.name ?? ""; |
| 186 | const parsed = parseName(name, "discoveryConfigs"); |
| 187 | const ownership = parseOwnership(config.displayName); |
| 188 | return { |
| 189 | name, |
| 190 | configId: parsed.id || lastSegment(name), |
| 191 | organization, |
| 192 | organizationId: organizationIdOf(organization), |
| 193 | location: parsed.location || DEFAULT_LOCATION, |
| 194 | project, |
| 195 | displayName: ownership.text, |
| 196 | status: config.status ?? DEFAULT_STATUS, |
| 197 | orgConfig: config.orgConfig, |
| 198 | targets: config.targets ?? [], |
| 199 | inspectTemplates: config.inspectTemplates ?? [], |
| 200 | processingLocation: config.processingLocation, |
| 201 | otherCloudStartingLocation: config.otherCloudStartingLocation, |
| 202 | createTime: config.createTime, |
| 203 | updateTime: config.updateTime, |
| 204 | lastRunTime: config.lastRunTime, |
| 205 | }; |
| 206 | }; |
| 207 | |
| 208 | const getByName = (name: string) => |
| 209 | name.length === 0 |
| 210 | ? Effect.undefined |
| 211 | : dlp |
| 212 | .getOrganizationsLocationsDiscoveryConfigs({ name }) |
| 213 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 214 | |
| 215 | const listAt = (parent: string, organization: string, project: string) => |
| 216 | dlp.listOrganizationsLocationsDiscoveryConfigs.pages({ parent, pageSize: 100 }).pipe( |
| 217 | Stream.flatMap((page) => Stream.fromIterable(page.discoveryConfigs ?? [])), |
| 218 | Stream.filter((config) => hasOwnershipMarker(config.displayName)), |
| 219 | Stream.map((config) => toAttrs(config, organization, project)), |
| 220 | Stream.runCollect, |
| 221 | Effect.map((chunk) => Array.from(chunk)), |
| 222 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 223 | ); |
| 224 | |
| 225 | const statusOf = (value: string | undefined) => value ?? DEFAULT_STATUS; |
| 226 | |
| 227 | export const OrganizationsLocationsDiscoveryConfigProvider = () => |
| 228 | Provider.succeed(OrganizationsLocationsDiscoveryConfig, { |
| 229 | stables: [ |
| 230 | "name", |
| 231 | "configId", |
| 232 | "organization", |
| 233 | "organizationId", |
| 234 | "location", |
| 235 | "project", |
| 236 | "createTime", |
| 237 | ], |
| 238 | |
| 239 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 240 | if (!isResolved(news)) return undefined; |
| 241 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 242 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 243 | return ( |
| 244 | replaceOn(olds?.configId ?? output?.configId, news.configId) ?? |
| 245 | replaceOn(olds?.organization ?? output?.organization, news.organization) ?? |
| 246 | replaceOn(previousLocation, nextLocation) |
| 247 | ); |
| 248 | }), |
| 249 | |
| 250 | read: Effect.fn(function* ({ id, olds, output }) { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const organization = yield* resolveOrganization( |
| 253 | olds?.organization ?? output?.organization, |
| 254 | output?.organization, |
| 255 | ); |
| 256 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 257 | const configId = yield* toPhysicalId(id, olds?.configId, output?.configId); |
| 258 | const name = output?.name ?? resourceName(organization, location, configId); |
| 259 | const existing = yield* getByName(name); |
| 260 | if (existing === undefined) return undefined; |
| 261 | const attrs = toAttrs(existing, organization, env.project); |
| 262 | return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs); |
| 263 | }), |
| 264 | |
| 265 | list: () => |
| 266 | Effect.gen(function* () { |
| 267 | const env = yield* GcpEnvironment.current; |
| 268 | const organization = yield* tryResolveOrganization(); |
| 269 | if (organization === undefined) return []; |
| 270 | const pages = yield* Effect.forEach( |
| 271 | locationParentsOf(organization, env.region), |
| 272 | (parent) => listAt(parent, organization, env.project), |
| 273 | { concurrency: 3 }, |
| 274 | ); |
| 275 | return pages.flat(); |
| 276 | }), |
| 277 | |
| 278 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 279 | const env = yield* GcpEnvironment.current; |
| 280 | const organization = yield* resolveOrganization(news.organization, output?.organization); |
| 281 | const location = normalizeLocation(news.location ?? output?.location); |
| 282 | const configId = yield* toPhysicalId(id, news.configId, output?.configId); |
| 283 | const parent = organizationLocationParent(organization, location); |
| 284 | const name = resourceName(organization, location, configId); |
| 285 | const ownership = yield* createInternalLabels(id); |
| 286 | const displayName = encodeDisplayName(ownership, news.displayName); |
| 287 | const status = statusOf(news.status); |
| 288 | const orgConfig = news.orgConfig; |
| 289 | const targets = news.targets; |
| 290 | const inspectTemplates = news.inspectTemplates; |
| 291 | const processingLocation = news.processingLocation; |
| 292 | const otherCloudStartingLocation = news.otherCloudStartingLocation; |
| 293 | |
| 294 | let current = yield* getByName(output?.name ?? name); |
| 295 | |
| 296 | if (current === undefined) { |
| 297 | const created = yield* dlp |
| 298 | .createOrganizationsLocationsDiscoveryConfigs({ |
| 299 | parent, |
| 300 | body: { |
| 301 | configId, |
| 302 | discoveryConfig: { |
| 303 | displayName, |
| 304 | status, |
| 305 | orgConfig, |
| 306 | targets, |
| 307 | inspectTemplates, |
| 308 | processingLocation, |
| 309 | otherCloudStartingLocation, |
| 310 | }, |
| 311 | }, |
| 312 | }) |
| 313 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 314 | current = created ?? undefined; |
| 315 | } |
| 316 | |
| 317 | if (current === undefined) { |
| 318 | return yield* new DlpNotResolved({ resourceName: name }); |
| 319 | } |
| 320 | |
| 321 | const currentName = current.name ?? name; |
| 322 | const displayChanged = !sameText(current.displayName, displayName); |
| 323 | const statusChanged = !sameText(current.status, status); |
| 324 | const orgChanged = fingerprint(current.orgConfig) !== fingerprint(orgConfig); |
| 325 | const targetsChanged = fingerprint(current.targets) !== fingerprint(targets); |
| 326 | const templatesChanged = |
| 327 | fingerprint(current.inspectTemplates) !== fingerprint(inspectTemplates); |
| 328 | const processingChanged = |
| 329 | fingerprint(current.processingLocation) !== fingerprint(processingLocation); |
| 330 | const otherChanged = |
| 331 | fingerprint(current.otherCloudStartingLocation) !== fingerprint(otherCloudStartingLocation); |
| 332 | const updateMask = updateMaskOf( |
| 333 | displayChanged ? "displayName" : undefined, |
| 334 | statusChanged ? "status" : undefined, |
| 335 | orgChanged ? "orgConfig" : undefined, |
| 336 | targetsChanged ? "targets" : undefined, |
| 337 | templatesChanged ? "inspectTemplates" : undefined, |
| 338 | processingChanged ? "processingLocation" : undefined, |
| 339 | otherChanged ? "otherCloudStartingLocation" : undefined, |
| 340 | ); |
| 341 | |
| 342 | if (updateMask.length > 0) { |
| 343 | current = yield* dlp.patchOrganizationsLocationsDiscoveryConfigs({ |
| 344 | name: currentName, |
| 345 | body: { |
| 346 | updateMask, |
| 347 | discoveryConfig: { |
| 348 | displayName, |
| 349 | status, |
| 350 | orgConfig, |
| 351 | targets, |
| 352 | inspectTemplates, |
| 353 | processingLocation, |
| 354 | otherCloudStartingLocation, |
| 355 | }, |
| 356 | }, |
| 357 | }); |
| 358 | } |
| 359 | |
| 360 | return toAttrs(current, organization, env.project); |
| 361 | }), |
| 362 | |
| 363 | delete: Effect.fn(function* ({ output }) { |
| 364 | yield* dlp |
| 365 | .deleteOrganizationsLocationsDiscoveryConfigs({ name: output.name }) |
| 366 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 367 | }), |
| 368 | }); |