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, 04:45 UTC
| 1 | import * as networksecurity from "@distilled.cloud/gcp/networksecurity_v1"; |
| 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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { |
| 15 | DEFAULT_LOCATION, |
| 16 | normalizeLocation, |
| 17 | parentOf, |
| 18 | parseResourceName, |
| 19 | resourceName, |
| 20 | toId, |
| 21 | toResourcePath, |
| 22 | userLabels, |
| 23 | } from "./names.ts"; |
| 24 | import { waitForOperation } from "./operations.ts"; |
| 25 | |
| 26 | const COLLECTION = "securityProfileGroups"; |
| 27 | |
| 28 | export type SecurityProfileGroupProps = { |
| 29 | /** |
| 30 | * SecurityProfileGroup id (the `{securityProfileGroup}` segment of |
| 31 | * `projects/{project}/locations/{location}/securityProfileGroups/{securityProfileGroup}`). |
| 32 | * If omitted, a unique name is generated from the stack, stage, and |
| 33 | * logical id. Must be 1-63 characters, letters, numbers, hyphens, and |
| 34 | * underscores, and must not start with a number. Immutable — changing |
| 35 | * it replaces the group. |
| 36 | */ |
| 37 | securityProfileGroupId?: string; |
| 38 | /** |
| 39 | * Location. Security profile groups live in `global`. Immutable — |
| 40 | * changing it replaces the group. |
| 41 | * @default "global" |
| 42 | */ |
| 43 | location?: string; |
| 44 | /** |
| 45 | * Human-readable description (max 2048 characters). |
| 46 | */ |
| 47 | description?: string; |
| 48 | /** |
| 49 | * User labels. Alchemy ownership labels are merged in automatically. |
| 50 | */ |
| 51 | labels?: Record<string, string>; |
| 52 | /** |
| 53 | * SecurityProfile resource name with ThreatPrevention configuration. |
| 54 | */ |
| 55 | threatPreventionProfile?: string; |
| 56 | /** |
| 57 | * SecurityProfile resource name with UrlFiltering configuration. |
| 58 | */ |
| 59 | urlFilteringProfile?: string; |
| 60 | /** |
| 61 | * SecurityProfile resource name with CustomMirroring configuration. |
| 62 | */ |
| 63 | customMirroringProfile?: string; |
| 64 | /** |
| 65 | * SecurityProfile resource name with CustomIntercept configuration. |
| 66 | */ |
| 67 | customInterceptProfile?: string; |
| 68 | }; |
| 69 | |
| 70 | export type SecurityProfileGroup = Resource< |
| 71 | "GCP.NetworkSecurity.SecurityProfileGroup", |
| 72 | SecurityProfileGroupProps, |
| 73 | { |
| 74 | /** Full resource name `projects/{project}/locations/{location}/securityProfileGroups/{securityProfileGroup}`. */ |
| 75 | name: string; |
| 76 | /** SecurityProfileGroup id (last path segment). */ |
| 77 | securityProfileGroupId: string; |
| 78 | /** Project id. */ |
| 79 | project: string; |
| 80 | /** Location id. Typically `"global"`. */ |
| 81 | location: string; |
| 82 | /** User-provided description. */ |
| 83 | description: string | undefined; |
| 84 | /** User labels (Alchemy ownership labels stripped). */ |
| 85 | labels: Record<string, string>; |
| 86 | /** Threat-prevention profile name, if set. */ |
| 87 | threatPreventionProfile: string | undefined; |
| 88 | /** URL-filtering profile name, if set. */ |
| 89 | urlFilteringProfile: string | undefined; |
| 90 | /** Custom mirroring profile name, if set. */ |
| 91 | customMirroringProfile: string | undefined; |
| 92 | /** Custom intercept profile name, if set. */ |
| 93 | customInterceptProfile: string | undefined; |
| 94 | /** Data-path identifier unique within `{container, location}`. */ |
| 95 | dataPathId: string | undefined; |
| 96 | /** Server-computed checksum used on update and delete. */ |
| 97 | etag: string | undefined; |
| 98 | /** RFC3339 creation timestamp. */ |
| 99 | createTime: string | undefined; |
| 100 | /** RFC3339 last-update timestamp. */ |
| 101 | updateTime: string | undefined; |
| 102 | }, |
| 103 | never, |
| 104 | Providers |
| 105 | >; |
| 106 | |
| 107 | /** |
| 108 | * A Network Security SecurityProfileGroup — a named bundle of |
| 109 | * SecurityProfile references applied together by firewall policy rules. |
| 110 | * |
| 111 | * Changing `securityProfileGroupId` or `location` replaces the group. |
| 112 | * Description, labels, and profile references update in place. |
| 113 | * |
| 114 | * ### Creating a SecurityProfileGroup |
| 115 | * **Example:** Empty group |
| 116 | * ```typescript |
| 117 | * const group = yield* GCP.NetworkSecurity.SecurityProfileGroup("Ngfw", {}); |
| 118 | * ``` |
| 119 | * |
| 120 | * **Example:** Group bound to a threat-prevention profile |
| 121 | * ```typescript |
| 122 | * const profile = yield* GCP.NetworkSecurity.SecurityProfile("Threat", { |
| 123 | * type: "THREAT_PREVENTION", |
| 124 | * }); |
| 125 | * const group = yield* GCP.NetworkSecurity.SecurityProfileGroup("Ngfw", { |
| 126 | * securityProfileGroupId: "app-ngfw", |
| 127 | * description: "prod ngfw", |
| 128 | * labels: { env: "prod" }, |
| 129 | * threatPreventionProfile: profile.name, |
| 130 | * }); |
| 131 | * ``` |
| 132 | * |
| 133 | * ### Updating a SecurityProfileGroup |
| 134 | * **Example:** Attach a profile and change labels |
| 135 | * ```typescript |
| 136 | * const group = yield* GCP.NetworkSecurity.SecurityProfileGroup("Ngfw", { |
| 137 | * securityProfileGroupId: "app-ngfw", |
| 138 | * description: "prod ngfw v2", |
| 139 | * labels: { env: "prod", role: "ngfw" }, |
| 140 | * threatPreventionProfile: profile.name, |
| 141 | * }); |
| 142 | * ``` |
| 143 | * |
| 144 | * @resource |
| 145 | * @category NetworkSecurity |
| 146 | */ |
| 147 | export const SecurityProfileGroup = Resource<SecurityProfileGroup>( |
| 148 | "GCP.NetworkSecurity.SecurityProfileGroup", |
| 149 | ); |
| 150 | |
| 151 | export class SecurityProfileGroupNotResolved extends Data.TaggedError( |
| 152 | "GCP.NetworkSecurity.SecurityProfileGroupNotResolved", |
| 153 | )<{ |
| 154 | resourceName: string; |
| 155 | }> {} |
| 156 | |
| 157 | export class SecurityProfileGroupStillExists extends Data.TaggedError( |
| 158 | "GCP.NetworkSecurity.SecurityProfileGroupStillExists", |
| 159 | )<{ |
| 160 | resourceName: string; |
| 161 | }> {} |
| 162 | |
| 163 | const refOf = (value: string | undefined) => { |
| 164 | if (value === undefined || value.length === 0) return undefined; |
| 165 | return toResourcePath(value); |
| 166 | }; |
| 167 | |
| 168 | const toAttrs = (group: networksecurity.SecurityProfileGroup, project: string) => { |
| 169 | const name = group.name ?? ""; |
| 170 | const parsed = parseResourceName(name, COLLECTION); |
| 171 | return { |
| 172 | name, |
| 173 | securityProfileGroupId: parsed.id, |
| 174 | project: parsed.project || project, |
| 175 | location: parsed.location || DEFAULT_LOCATION, |
| 176 | description: group.description, |
| 177 | labels: userLabels(group.labels), |
| 178 | threatPreventionProfile: group.threatPreventionProfile, |
| 179 | urlFilteringProfile: group.urlFilteringProfile, |
| 180 | customMirroringProfile: group.customMirroringProfile, |
| 181 | customInterceptProfile: group.customInterceptProfile, |
| 182 | dataPathId: group.dataPathId, |
| 183 | etag: group.etag, |
| 184 | createTime: group.createTime, |
| 185 | updateTime: group.updateTime, |
| 186 | }; |
| 187 | }; |
| 188 | |
| 189 | const getByName = (name: string) => |
| 190 | networksecurity |
| 191 | .getProjectsLocationsSecurityProfileGroups({ name }) |
| 192 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 193 | |
| 194 | const waitUntilExists = (name: string) => |
| 195 | getByName(name).pipe( |
| 196 | Effect.flatMap((group) => |
| 197 | group |
| 198 | ? Effect.succeed(group) |
| 199 | : Effect.fail(new SecurityProfileGroupNotResolved({ resourceName: name })), |
| 200 | ), |
| 201 | Effect.retry({ |
| 202 | while: (error) => error._tag === "GCP.NetworkSecurity.SecurityProfileGroupNotResolved", |
| 203 | times: 8, |
| 204 | schedule: Schedule.spaced("1 second"), |
| 205 | }), |
| 206 | ); |
| 207 | |
| 208 | const waitUntilGone = (name: string) => |
| 209 | getByName(name).pipe( |
| 210 | Effect.flatMap((group) => |
| 211 | group === undefined |
| 212 | ? Effect.void |
| 213 | : Effect.fail(new SecurityProfileGroupStillExists({ resourceName: name })), |
| 214 | ), |
| 215 | Effect.retry({ |
| 216 | while: (error) => error._tag === "GCP.NetworkSecurity.SecurityProfileGroupStillExists", |
| 217 | times: 10, |
| 218 | schedule: Schedule.spaced("2 seconds"), |
| 219 | }), |
| 220 | ); |
| 221 | |
| 222 | const listOwned = (project: string) => |
| 223 | networksecurity.listProjectsLocationsSecurityProfileGroups |
| 224 | .pages({ |
| 225 | parent: parentOf(project, "-"), |
| 226 | pageSize: 1000, |
| 227 | }) |
| 228 | .pipe( |
| 229 | Stream.flatMap((page) => Stream.fromIterable(page.securityProfileGroups ?? [])), |
| 230 | Stream.filter((group) => |
| 231 | Object.keys(group.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 232 | ), |
| 233 | Stream.map((group) => toAttrs(group, project)), |
| 234 | Stream.runCollect, |
| 235 | Effect.map((chunk) => Array.from(chunk)), |
| 236 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 237 | ); |
| 238 | |
| 239 | export const SecurityProfileGroupProvider = () => |
| 240 | Provider.succeed(SecurityProfileGroup, { |
| 241 | stables: ["name", "securityProfileGroupId", "project", "location", "dataPathId", "createTime"], |
| 242 | |
| 243 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 244 | if (!isResolved(news)) return undefined; |
| 245 | const previousId = olds?.securityProfileGroupId ?? output?.securityProfileGroupId; |
| 246 | const nextId = news.securityProfileGroupId ?? previousId; |
| 247 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 248 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 249 | if ( |
| 250 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 251 | previousLocation !== nextLocation |
| 252 | ) { |
| 253 | return { action: "replace" as const }; |
| 254 | } |
| 255 | return undefined; |
| 256 | }), |
| 257 | |
| 258 | read: Effect.fn(function* ({ id, olds, output }) { |
| 259 | const env = yield* GcpEnvironment.current; |
| 260 | const securityProfileGroupId = yield* toId( |
| 261 | id, |
| 262 | olds?.securityProfileGroupId, |
| 263 | output?.securityProfileGroupId, |
| 264 | "secgroup", |
| 265 | ); |
| 266 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 267 | const name = |
| 268 | output?.name ?? resourceName(env.project, location, COLLECTION, securityProfileGroupId); |
| 269 | const existing = yield* getByName(name); |
| 270 | if (existing === undefined) return undefined; |
| 271 | const attrs = toAttrs(existing, env.project); |
| 272 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 273 | }), |
| 274 | |
| 275 | list: () => |
| 276 | Effect.gen(function* () { |
| 277 | const env = yield* GcpEnvironment.current; |
| 278 | return yield* listOwned(env.project); |
| 279 | }), |
| 280 | |
| 281 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 282 | const env = yield* GcpEnvironment.current; |
| 283 | const securityProfileGroupId = yield* toId( |
| 284 | id, |
| 285 | news.securityProfileGroupId, |
| 286 | output?.securityProfileGroupId, |
| 287 | "secgroup", |
| 288 | ); |
| 289 | const location = normalizeLocation(news.location ?? output?.location); |
| 290 | const name = resourceName(env.project, location, COLLECTION, securityProfileGroupId); |
| 291 | const desiredLabels = { |
| 292 | ...toLabels(news.labels), |
| 293 | ...(yield* createInternalLabels(id)), |
| 294 | }; |
| 295 | const threatPreventionProfile = refOf(news.threatPreventionProfile); |
| 296 | const urlFilteringProfile = refOf(news.urlFilteringProfile); |
| 297 | const customMirroringProfile = refOf(news.customMirroringProfile); |
| 298 | const customInterceptProfile = refOf(news.customInterceptProfile); |
| 299 | |
| 300 | let current = yield* getByName(output?.name ?? name); |
| 301 | |
| 302 | if (current === undefined) { |
| 303 | const created = yield* networksecurity |
| 304 | .createProjectsLocationsSecurityProfileGroups({ |
| 305 | parent: parentOf(env.project, location), |
| 306 | securityProfileGroupId, |
| 307 | body: { |
| 308 | description: news.description, |
| 309 | labels: desiredLabels, |
| 310 | threatPreventionProfile, |
| 311 | urlFilteringProfile, |
| 312 | customMirroringProfile, |
| 313 | customInterceptProfile, |
| 314 | }, |
| 315 | }) |
| 316 | .pipe( |
| 317 | Effect.retry({ |
| 318 | while: (error) => error._tag === "Conflict", |
| 319 | times: 5, |
| 320 | schedule: Schedule.spaced("2 seconds"), |
| 321 | }), |
| 322 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 323 | ); |
| 324 | if (created !== undefined) { |
| 325 | yield* waitForOperation(created); |
| 326 | } |
| 327 | current = yield* waitUntilExists(name); |
| 328 | } |
| 329 | |
| 330 | if (current === undefined) { |
| 331 | return yield* new SecurityProfileGroupNotResolved({ resourceName: name }); |
| 332 | } |
| 333 | |
| 334 | const observedLabels = tagRecord(current.labels); |
| 335 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 336 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 337 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 338 | const threatChanged = |
| 339 | (current.threatPreventionProfile ?? "") !== (threatPreventionProfile ?? ""); |
| 340 | const urlChanged = (current.urlFilteringProfile ?? "") !== (urlFilteringProfile ?? ""); |
| 341 | const mirroringChanged = |
| 342 | (current.customMirroringProfile ?? "") !== (customMirroringProfile ?? ""); |
| 343 | const interceptChanged = |
| 344 | (current.customInterceptProfile ?? "") !== (customInterceptProfile ?? ""); |
| 345 | |
| 346 | if ( |
| 347 | labelsChanged || |
| 348 | descriptionChanged || |
| 349 | threatChanged || |
| 350 | urlChanged || |
| 351 | mirroringChanged || |
| 352 | interceptChanged |
| 353 | ) { |
| 354 | const updateMask = [ |
| 355 | labelsChanged ? "labels" : undefined, |
| 356 | descriptionChanged ? "description" : undefined, |
| 357 | threatChanged ? "threatPreventionProfile" : undefined, |
| 358 | urlChanged ? "urlFilteringProfile" : undefined, |
| 359 | mirroringChanged ? "customMirroringProfile" : undefined, |
| 360 | interceptChanged ? "customInterceptProfile" : undefined, |
| 361 | ].filter((field): field is string => field !== undefined); |
| 362 | const operation = yield* networksecurity.patchProjectsLocationsSecurityProfileGroups({ |
| 363 | name: current.name ?? name, |
| 364 | updateMask: updateMask.join(","), |
| 365 | body: { |
| 366 | name: current.name ?? name, |
| 367 | etag: current.etag, |
| 368 | labels: desiredLabels, |
| 369 | description: news.description, |
| 370 | threatPreventionProfile, |
| 371 | urlFilteringProfile, |
| 372 | customMirroringProfile, |
| 373 | customInterceptProfile, |
| 374 | }, |
| 375 | }); |
| 376 | yield* waitForOperation(operation); |
| 377 | current = yield* waitUntilExists(current.name ?? name); |
| 378 | } |
| 379 | |
| 380 | return toAttrs(current, env.project); |
| 381 | }), |
| 382 | |
| 383 | delete: Effect.fn(function* ({ output }) { |
| 384 | const operation = yield* networksecurity |
| 385 | .deleteProjectsLocationsSecurityProfileGroups({ |
| 386 | name: output.name, |
| 387 | etag: output.etag, |
| 388 | }) |
| 389 | .pipe( |
| 390 | Effect.retry({ |
| 391 | while: (error) => error._tag === "Conflict", |
| 392 | times: 8, |
| 393 | schedule: Schedule.spaced("2 seconds"), |
| 394 | }), |
| 395 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 396 | ); |
| 397 | if (operation !== undefined) { |
| 398 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 399 | } |
| 400 | yield* waitUntilGone(output.name); |
| 401 | }), |
| 402 | }); |