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 | userLabels, |
| 22 | } from "./names.ts"; |
| 23 | import { waitForOperation } from "./operations.ts"; |
| 24 | import { sameJson } from "./ownership.ts"; |
| 25 | |
| 26 | const COLLECTION = "securityProfiles"; |
| 27 | const DEFAULT_TYPE = "THREAT_PREVENTION"; |
| 28 | |
| 29 | export type SecurityProfileType = |
| 30 | | "THREAT_PREVENTION" |
| 31 | | "CUSTOM_MIRRORING" |
| 32 | | "CUSTOM_INTERCEPT" |
| 33 | | "URL_FILTERING" |
| 34 | | (string & {}); |
| 35 | |
| 36 | export type AntivirusOverride = { |
| 37 | /** Protocol to match (`HTTP`, `SMTP`, `FTP`, …). */ |
| 38 | protocol?: networksecurity.AntivirusOverrideProtocolEnum | (string & {}); |
| 39 | /** Threat action override (`ALLOW`, `ALERT`, `DENY`, …). */ |
| 40 | action?: networksecurity.AntivirusOverrideActionEnum | (string & {}); |
| 41 | }; |
| 42 | |
| 43 | export type SeverityOverride = { |
| 44 | /** Severity level to match (`INFORMATIONAL`, `LOW`, `MEDIUM`, `HIGH`, `CRITICAL`). */ |
| 45 | severity?: networksecurity.SeverityOverrideSeverityEnum | (string & {}); |
| 46 | /** Threat action override (`ALLOW`, `ALERT`, `DENY`, …). */ |
| 47 | action?: networksecurity.SeverityOverrideActionEnum | (string & {}); |
| 48 | }; |
| 49 | |
| 50 | export type ThreatOverride = { |
| 51 | /** Vendor-specific threat id to override. */ |
| 52 | threatId?: string; |
| 53 | /** Threat action override (`ALLOW`, `ALERT`, `DENY`, …). */ |
| 54 | action?: networksecurity.ThreatOverrideActionEnum | (string & {}); |
| 55 | }; |
| 56 | |
| 57 | export type ThreatPreventionProfile = { |
| 58 | /** Antivirus action overrides per protocol. */ |
| 59 | antivirusOverrides?: AntivirusOverride[]; |
| 60 | /** Action overrides by severity. */ |
| 61 | severityOverrides?: SeverityOverride[]; |
| 62 | /** Action overrides by threat id. Wins over a matching severity override. */ |
| 63 | threatOverrides?: ThreatOverride[]; |
| 64 | }; |
| 65 | |
| 66 | export type CustomMirroringProfile = { |
| 67 | /** |
| 68 | * Target MirroringEndpointGroup. Immutable — changing it replaces the |
| 69 | * profile. |
| 70 | */ |
| 71 | mirroringEndpointGroup?: string; |
| 72 | }; |
| 73 | |
| 74 | export type CustomInterceptProfile = { |
| 75 | /** |
| 76 | * Target InterceptEndpointGroup. Immutable — changing it replaces the |
| 77 | * profile. |
| 78 | */ |
| 79 | interceptEndpointGroup?: string; |
| 80 | }; |
| 81 | |
| 82 | export type UrlFilter = { |
| 83 | /** Action taken when this filter matches (`ALLOW` or `DENY`). */ |
| 84 | filteringAction?: networksecurity.UrlFilterFilteringActionEnum | (string & {}); |
| 85 | /** URL strings that must match for this filter to apply. */ |
| 86 | urls?: string[]; |
| 87 | /** |
| 88 | * Priority within the profile. Lower integers are higher priority. |
| 89 | * Must be unique within a URL Filtering Profile. |
| 90 | */ |
| 91 | priority?: number; |
| 92 | }; |
| 93 | |
| 94 | export type UrlFilteringProfile = { |
| 95 | /** Filtering configs, each defining an action for some URL match. */ |
| 96 | urlFilters?: UrlFilter[]; |
| 97 | }; |
| 98 | |
| 99 | export type SecurityProfileProps = { |
| 100 | /** |
| 101 | * SecurityProfile id (the `{securityProfile}` segment of |
| 102 | * `projects/{project}/locations/{location}/securityProfiles/{securityProfile}`). |
| 103 | * If omitted, a unique name is generated from the stack, stage, and |
| 104 | * logical id. Must be 1-63 characters, letters, numbers, hyphens, and |
| 105 | * underscores, and must not start with a number. Immutable — changing |
| 106 | * it replaces the profile. |
| 107 | */ |
| 108 | securityProfileId?: string; |
| 109 | /** |
| 110 | * Location. Security profiles live in `global`. Immutable — changing |
| 111 | * it replaces the profile. |
| 112 | * @default "global" |
| 113 | */ |
| 114 | location?: string; |
| 115 | /** |
| 116 | * Human-readable description (max 512 characters). |
| 117 | */ |
| 118 | description?: string; |
| 119 | /** |
| 120 | * User labels. Alchemy ownership labels are merged in automatically. |
| 121 | */ |
| 122 | labels?: Record<string, string>; |
| 123 | /** |
| 124 | * Single ProfileType this resource configures. Inferred from nested |
| 125 | * profile blocks when omitted. Immutable — changing it replaces the |
| 126 | * profile. |
| 127 | * @default "THREAT_PREVENTION" |
| 128 | */ |
| 129 | type?: SecurityProfileType; |
| 130 | /** |
| 131 | * Threat-prevention configuration. Used when `type` is |
| 132 | * `THREAT_PREVENTION`. |
| 133 | */ |
| 134 | threatPreventionProfile?: ThreatPreventionProfile; |
| 135 | /** |
| 136 | * Out-of-band packet mirroring configuration. Used when `type` is |
| 137 | * `CUSTOM_MIRRORING`. Immutable nested target — changing |
| 138 | * `mirroringEndpointGroup` replaces the profile. |
| 139 | */ |
| 140 | customMirroringProfile?: CustomMirroringProfile; |
| 141 | /** |
| 142 | * In-band intercept configuration. Used when `type` is |
| 143 | * `CUSTOM_INTERCEPT`. Immutable nested target — changing |
| 144 | * `interceptEndpointGroup` replaces the profile. |
| 145 | */ |
| 146 | customInterceptProfile?: CustomInterceptProfile; |
| 147 | /** |
| 148 | * URL filtering configuration. Used when `type` is `URL_FILTERING`. |
| 149 | */ |
| 150 | urlFilteringProfile?: UrlFilteringProfile; |
| 151 | }; |
| 152 | |
| 153 | export type SecurityProfile = Resource< |
| 154 | "GCP.NetworkSecurity.SecurityProfile", |
| 155 | SecurityProfileProps, |
| 156 | { |
| 157 | /** Full resource name `projects/{project}/locations/{location}/securityProfiles/{securityProfile}`. */ |
| 158 | name: string; |
| 159 | /** SecurityProfile id (last path segment). */ |
| 160 | securityProfileId: string; |
| 161 | /** Project id. */ |
| 162 | project: string; |
| 163 | /** Location id. Typically `"global"`. */ |
| 164 | location: string; |
| 165 | /** Configured profile type. */ |
| 166 | type: string | undefined; |
| 167 | /** User-provided description. */ |
| 168 | description: string | undefined; |
| 169 | /** User labels (Alchemy ownership labels stripped). */ |
| 170 | labels: Record<string, string>; |
| 171 | /** Threat-prevention configuration, if this is a THREAT_PREVENTION profile. */ |
| 172 | threatPreventionProfile: ThreatPreventionProfile | undefined; |
| 173 | /** Custom mirroring configuration, if this is a CUSTOM_MIRRORING profile. */ |
| 174 | customMirroringProfile: CustomMirroringProfile | undefined; |
| 175 | /** Custom intercept configuration, if this is a CUSTOM_INTERCEPT profile. */ |
| 176 | customInterceptProfile: CustomInterceptProfile | undefined; |
| 177 | /** URL filtering configuration, if this is a URL_FILTERING profile. */ |
| 178 | urlFilteringProfile: UrlFilteringProfile | undefined; |
| 179 | /** Server-computed checksum used on update and delete. */ |
| 180 | etag: string | undefined; |
| 181 | /** RFC3339 creation timestamp. */ |
| 182 | createTime: string | undefined; |
| 183 | /** RFC3339 last-update timestamp. */ |
| 184 | updateTime: string | undefined; |
| 185 | }, |
| 186 | never, |
| 187 | Providers |
| 188 | >; |
| 189 | |
| 190 | /** |
| 191 | * A Network Security SecurityProfile — the behavior for one ProfileType |
| 192 | * (threat prevention, URL filtering, custom mirroring, or custom |
| 193 | * intercept). |
| 194 | * |
| 195 | * Changing `securityProfileId`, `location`, `type`, or an immutable |
| 196 | * nested target (`mirroringEndpointGroup` / `interceptEndpointGroup`) |
| 197 | * replaces the profile. Description, labels, and the mutable nested |
| 198 | * profile configuration update in place. |
| 199 | * |
| 200 | * ### Creating a SecurityProfile |
| 201 | * **Example:** Threat prevention with a severity override |
| 202 | * ```typescript |
| 203 | * const profile = yield* GCP.NetworkSecurity.SecurityProfile("Threat", { |
| 204 | * type: "THREAT_PREVENTION", |
| 205 | * threatPreventionProfile: { |
| 206 | * severityOverrides: [{ severity: "HIGH", action: "ALERT" }], |
| 207 | * }, |
| 208 | * }); |
| 209 | * ``` |
| 210 | * |
| 211 | * **Example:** Named profile with labels |
| 212 | * ```typescript |
| 213 | * const profile = yield* GCP.NetworkSecurity.SecurityProfile("Threat", { |
| 214 | * securityProfileId: "app-threat", |
| 215 | * description: "prod threat prevention", |
| 216 | * labels: { env: "prod" }, |
| 217 | * type: "THREAT_PREVENTION", |
| 218 | * }); |
| 219 | * ``` |
| 220 | * |
| 221 | * ### Updating a SecurityProfile |
| 222 | * **Example:** Change severity overrides |
| 223 | * ```typescript |
| 224 | * const profile = yield* GCP.NetworkSecurity.SecurityProfile("Threat", { |
| 225 | * securityProfileId: "app-threat", |
| 226 | * type: "THREAT_PREVENTION", |
| 227 | * description: "prod threat prevention v2", |
| 228 | * labels: { env: "prod", role: "ngfw" }, |
| 229 | * threatPreventionProfile: { |
| 230 | * severityOverrides: [{ severity: "CRITICAL", action: "DENY" }], |
| 231 | * }, |
| 232 | * }); |
| 233 | * ``` |
| 234 | * |
| 235 | * @resource |
| 236 | * @category NetworkSecurity |
| 237 | */ |
| 238 | export const SecurityProfile = Resource<SecurityProfile>("GCP.NetworkSecurity.SecurityProfile"); |
| 239 | |
| 240 | export class SecurityProfileNotResolved extends Data.TaggedError( |
| 241 | "GCP.NetworkSecurity.SecurityProfileNotResolved", |
| 242 | )<{ |
| 243 | resourceName: string; |
| 244 | }> {} |
| 245 | |
| 246 | export class SecurityProfileStillExists extends Data.TaggedError( |
| 247 | "GCP.NetworkSecurity.SecurityProfileStillExists", |
| 248 | )<{ |
| 249 | resourceName: string; |
| 250 | }> {} |
| 251 | |
| 252 | const typeOf = ( |
| 253 | props: { |
| 254 | type?: string; |
| 255 | threatPreventionProfile?: ThreatPreventionProfile; |
| 256 | customMirroringProfile?: CustomMirroringProfile; |
| 257 | customInterceptProfile?: CustomInterceptProfile; |
| 258 | urlFilteringProfile?: UrlFilteringProfile; |
| 259 | }, |
| 260 | fallback: string = DEFAULT_TYPE, |
| 261 | ): string => { |
| 262 | if (props.type !== undefined && props.type !== "PROFILE_TYPE_UNSPECIFIED") { |
| 263 | return props.type; |
| 264 | } |
| 265 | if ((props.customMirroringProfile?.mirroringEndpointGroup ?? "") !== "") { |
| 266 | return "CUSTOM_MIRRORING"; |
| 267 | } |
| 268 | if ((props.customInterceptProfile?.interceptEndpointGroup ?? "") !== "") { |
| 269 | return "CUSTOM_INTERCEPT"; |
| 270 | } |
| 271 | if ((props.urlFilteringProfile?.urlFilters?.length ?? 0) > 0) { |
| 272 | return "URL_FILTERING"; |
| 273 | } |
| 274 | if (props.threatPreventionProfile !== undefined) { |
| 275 | return "THREAT_PREVENTION"; |
| 276 | } |
| 277 | return fallback; |
| 278 | }; |
| 279 | |
| 280 | const toThreatPrevention = ( |
| 281 | value: networksecurity.ThreatPreventionProfile | ThreatPreventionProfile | undefined, |
| 282 | ): ThreatPreventionProfile | undefined => { |
| 283 | if (value === undefined) return undefined; |
| 284 | return { |
| 285 | antivirusOverrides: (value.antivirusOverrides ?? []).map((entry) => ({ |
| 286 | protocol: entry.protocol, |
| 287 | action: entry.action, |
| 288 | })), |
| 289 | severityOverrides: (value.severityOverrides ?? []).map((entry) => ({ |
| 290 | severity: entry.severity, |
| 291 | action: entry.action, |
| 292 | })), |
| 293 | threatOverrides: (value.threatOverrides ?? []).map((entry) => ({ |
| 294 | threatId: entry.threatId, |
| 295 | action: entry.action, |
| 296 | })), |
| 297 | }; |
| 298 | }; |
| 299 | |
| 300 | const toUrlFiltering = ( |
| 301 | value: networksecurity.UrlFilteringProfile | UrlFilteringProfile | undefined, |
| 302 | ): UrlFilteringProfile | undefined => { |
| 303 | if (value === undefined) return undefined; |
| 304 | return { |
| 305 | urlFilters: (value.urlFilters ?? []).map((entry) => ({ |
| 306 | filteringAction: entry.filteringAction, |
| 307 | urls: entry.urls, |
| 308 | priority: entry.priority, |
| 309 | })), |
| 310 | }; |
| 311 | }; |
| 312 | |
| 313 | const toAttrs = (profile: networksecurity.SecurityProfile, project: string) => { |
| 314 | const name = profile.name ?? ""; |
| 315 | const parsed = parseResourceName(name, COLLECTION); |
| 316 | return { |
| 317 | name, |
| 318 | securityProfileId: parsed.id, |
| 319 | project: parsed.project || project, |
| 320 | location: parsed.location || DEFAULT_LOCATION, |
| 321 | type: profile.type, |
| 322 | description: profile.description, |
| 323 | labels: userLabels(profile.labels), |
| 324 | threatPreventionProfile: toThreatPrevention(profile.threatPreventionProfile), |
| 325 | customMirroringProfile: profile.customMirroringProfile |
| 326 | ? { |
| 327 | mirroringEndpointGroup: profile.customMirroringProfile.mirroringEndpointGroup, |
| 328 | } |
| 329 | : undefined, |
| 330 | customInterceptProfile: profile.customInterceptProfile |
| 331 | ? { |
| 332 | interceptEndpointGroup: profile.customInterceptProfile.interceptEndpointGroup, |
| 333 | } |
| 334 | : undefined, |
| 335 | urlFilteringProfile: toUrlFiltering(profile.urlFilteringProfile), |
| 336 | etag: profile.etag, |
| 337 | createTime: profile.createTime, |
| 338 | updateTime: profile.updateTime, |
| 339 | }; |
| 340 | }; |
| 341 | |
| 342 | const getByName = (name: string) => |
| 343 | networksecurity |
| 344 | .getProjectsLocationsSecurityProfiles({ name }) |
| 345 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 346 | |
| 347 | const waitUntilExists = (name: string) => |
| 348 | getByName(name).pipe( |
| 349 | Effect.flatMap((profile) => |
| 350 | profile |
| 351 | ? Effect.succeed(profile) |
| 352 | : Effect.fail(new SecurityProfileNotResolved({ resourceName: name })), |
| 353 | ), |
| 354 | Effect.retry({ |
| 355 | while: (error) => error._tag === "GCP.NetworkSecurity.SecurityProfileNotResolved", |
| 356 | times: 8, |
| 357 | schedule: Schedule.spaced("1 second"), |
| 358 | }), |
| 359 | ); |
| 360 | |
| 361 | const waitUntilGone = (name: string) => |
| 362 | getByName(name).pipe( |
| 363 | Effect.flatMap((profile) => |
| 364 | profile === undefined |
| 365 | ? Effect.void |
| 366 | : Effect.fail(new SecurityProfileStillExists({ resourceName: name })), |
| 367 | ), |
| 368 | Effect.retry({ |
| 369 | while: (error) => error._tag === "GCP.NetworkSecurity.SecurityProfileStillExists", |
| 370 | times: 10, |
| 371 | schedule: Schedule.spaced("2 seconds"), |
| 372 | }), |
| 373 | ); |
| 374 | |
| 375 | const listOwned = (project: string) => |
| 376 | networksecurity.listProjectsLocationsSecurityProfiles |
| 377 | .pages({ |
| 378 | parent: parentOf(project, "-"), |
| 379 | pageSize: 1000, |
| 380 | }) |
| 381 | .pipe( |
| 382 | Stream.flatMap((page) => Stream.fromIterable(page.securityProfiles ?? [])), |
| 383 | Stream.filter((profile) => |
| 384 | Object.keys(profile.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 385 | ), |
| 386 | Stream.map((profile) => toAttrs(profile, project)), |
| 387 | Stream.runCollect, |
| 388 | Effect.map((chunk) => Array.from(chunk)), |
| 389 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 390 | ); |
| 391 | |
| 392 | const toCreateBody = ( |
| 393 | news: SecurityProfileProps, |
| 394 | type: string, |
| 395 | desiredLabels: Record<string, string>, |
| 396 | ): networksecurity.SecurityProfile => { |
| 397 | const body: networksecurity.SecurityProfile = { |
| 398 | description: news.description, |
| 399 | labels: desiredLabels, |
| 400 | type, |
| 401 | }; |
| 402 | if (type === "THREAT_PREVENTION") { |
| 403 | body.threatPreventionProfile = toThreatPrevention(news.threatPreventionProfile) ?? {}; |
| 404 | } else if (type === "CUSTOM_MIRRORING") { |
| 405 | body.customMirroringProfile = { |
| 406 | mirroringEndpointGroup: news.customMirroringProfile?.mirroringEndpointGroup, |
| 407 | }; |
| 408 | } else if (type === "CUSTOM_INTERCEPT") { |
| 409 | body.customInterceptProfile = { |
| 410 | interceptEndpointGroup: news.customInterceptProfile?.interceptEndpointGroup, |
| 411 | }; |
| 412 | } else if (type === "URL_FILTERING") { |
| 413 | body.urlFilteringProfile = toUrlFiltering(news.urlFilteringProfile) ?? {}; |
| 414 | } |
| 415 | return body; |
| 416 | }; |
| 417 | |
| 418 | const immutableChanged = ( |
| 419 | news: SecurityProfileProps, |
| 420 | olds: Partial<SecurityProfileProps> | undefined, |
| 421 | output: SecurityProfile["Attributes"] | undefined, |
| 422 | ) => { |
| 423 | const previousType = typeOf( |
| 424 | { |
| 425 | type: olds?.type ?? output?.type, |
| 426 | threatPreventionProfile: olds?.threatPreventionProfile ?? output?.threatPreventionProfile, |
| 427 | customMirroringProfile: olds?.customMirroringProfile ?? output?.customMirroringProfile, |
| 428 | customInterceptProfile: olds?.customInterceptProfile ?? output?.customInterceptProfile, |
| 429 | urlFilteringProfile: olds?.urlFilteringProfile ?? output?.urlFilteringProfile, |
| 430 | }, |
| 431 | output?.type ?? DEFAULT_TYPE, |
| 432 | ); |
| 433 | const nextType = typeOf(news, previousType); |
| 434 | if (nextType !== previousType) return true; |
| 435 | if (nextType === "CUSTOM_MIRRORING") { |
| 436 | const previous = |
| 437 | olds?.customMirroringProfile?.mirroringEndpointGroup ?? |
| 438 | output?.customMirroringProfile?.mirroringEndpointGroup ?? |
| 439 | ""; |
| 440 | const next = news.customMirroringProfile?.mirroringEndpointGroup ?? previous; |
| 441 | if (next !== previous) return true; |
| 442 | } |
| 443 | if (nextType === "CUSTOM_INTERCEPT") { |
| 444 | const previous = |
| 445 | olds?.customInterceptProfile?.interceptEndpointGroup ?? |
| 446 | output?.customInterceptProfile?.interceptEndpointGroup ?? |
| 447 | ""; |
| 448 | const next = news.customInterceptProfile?.interceptEndpointGroup ?? previous; |
| 449 | if (next !== previous) return true; |
| 450 | } |
| 451 | return false; |
| 452 | }; |
| 453 | |
| 454 | export const SecurityProfileProvider = () => |
| 455 | Provider.succeed(SecurityProfile, { |
| 456 | stables: ["name", "securityProfileId", "project", "location", "type", "createTime"], |
| 457 | |
| 458 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 459 | if (!isResolved(news)) return undefined; |
| 460 | const previousId = olds?.securityProfileId ?? output?.securityProfileId; |
| 461 | const nextId = news.securityProfileId ?? previousId; |
| 462 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 463 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 464 | if ( |
| 465 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 466 | previousLocation !== nextLocation || |
| 467 | immutableChanged(news, olds, output) |
| 468 | ) { |
| 469 | return { action: "replace" as const }; |
| 470 | } |
| 471 | return undefined; |
| 472 | }), |
| 473 | |
| 474 | read: Effect.fn(function* ({ id, olds, output }) { |
| 475 | const env = yield* GcpEnvironment.current; |
| 476 | const securityProfileId = yield* toId( |
| 477 | id, |
| 478 | olds?.securityProfileId, |
| 479 | output?.securityProfileId, |
| 480 | "secprofile", |
| 481 | ); |
| 482 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 483 | const name = |
| 484 | output?.name ?? resourceName(env.project, location, COLLECTION, securityProfileId); |
| 485 | const existing = yield* getByName(name); |
| 486 | if (existing === undefined) return undefined; |
| 487 | const attrs = toAttrs(existing, env.project); |
| 488 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 489 | }), |
| 490 | |
| 491 | list: () => |
| 492 | Effect.gen(function* () { |
| 493 | const env = yield* GcpEnvironment.current; |
| 494 | return yield* listOwned(env.project); |
| 495 | }), |
| 496 | |
| 497 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 498 | const env = yield* GcpEnvironment.current; |
| 499 | const securityProfileId = yield* toId( |
| 500 | id, |
| 501 | news.securityProfileId, |
| 502 | output?.securityProfileId, |
| 503 | "secprofile", |
| 504 | ); |
| 505 | const location = normalizeLocation(news.location ?? output?.location); |
| 506 | const type = typeOf(news, output?.type ?? DEFAULT_TYPE); |
| 507 | const name = resourceName(env.project, location, COLLECTION, securityProfileId); |
| 508 | const desiredLabels = { |
| 509 | ...toLabels(news.labels), |
| 510 | ...(yield* createInternalLabels(id)), |
| 511 | }; |
| 512 | |
| 513 | let current = yield* getByName(output?.name ?? name); |
| 514 | |
| 515 | if (current === undefined) { |
| 516 | const created = yield* networksecurity |
| 517 | .createProjectsLocationsSecurityProfiles({ |
| 518 | parent: parentOf(env.project, location), |
| 519 | securityProfileId, |
| 520 | body: toCreateBody(news, type, desiredLabels), |
| 521 | }) |
| 522 | .pipe( |
| 523 | Effect.retry({ |
| 524 | while: (error) => error._tag === "Conflict", |
| 525 | times: 5, |
| 526 | schedule: Schedule.spaced("2 seconds"), |
| 527 | }), |
| 528 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 529 | ); |
| 530 | if (created !== undefined) { |
| 531 | yield* waitForOperation(created); |
| 532 | } |
| 533 | current = yield* waitUntilExists(name); |
| 534 | } |
| 535 | |
| 536 | if (current === undefined) { |
| 537 | return yield* new SecurityProfileNotResolved({ resourceName: name }); |
| 538 | } |
| 539 | |
| 540 | const observedLabels = tagRecord(current.labels); |
| 541 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 542 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 543 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 544 | const threatChanged = |
| 545 | type === "THREAT_PREVENTION" && |
| 546 | !sameJson( |
| 547 | toThreatPrevention(current.threatPreventionProfile) ?? {}, |
| 548 | toThreatPrevention(news.threatPreventionProfile) ?? {}, |
| 549 | ); |
| 550 | const urlChanged = |
| 551 | type === "URL_FILTERING" && |
| 552 | !sameJson( |
| 553 | toUrlFiltering(current.urlFilteringProfile) ?? {}, |
| 554 | toUrlFiltering(news.urlFilteringProfile) ?? {}, |
| 555 | ); |
| 556 | |
| 557 | if (labelsChanged || descriptionChanged || threatChanged || urlChanged) { |
| 558 | const updateMask = [ |
| 559 | labelsChanged ? "labels" : undefined, |
| 560 | descriptionChanged ? "description" : undefined, |
| 561 | threatChanged ? "threatPreventionProfile" : undefined, |
| 562 | urlChanged ? "urlFilteringProfile" : undefined, |
| 563 | ].filter((field): field is string => field !== undefined); |
| 564 | const operation = yield* networksecurity.patchProjectsLocationsSecurityProfiles({ |
| 565 | name: current.name ?? name, |
| 566 | updateMask: updateMask.join(","), |
| 567 | body: { |
| 568 | name: current.name ?? name, |
| 569 | etag: current.etag, |
| 570 | labels: desiredLabels, |
| 571 | description: news.description, |
| 572 | threatPreventionProfile: |
| 573 | type === "THREAT_PREVENTION" |
| 574 | ? (toThreatPrevention(news.threatPreventionProfile) ?? {}) |
| 575 | : undefined, |
| 576 | urlFilteringProfile: |
| 577 | type === "URL_FILTERING" |
| 578 | ? (toUrlFiltering(news.urlFilteringProfile) ?? {}) |
| 579 | : undefined, |
| 580 | }, |
| 581 | }); |
| 582 | yield* waitForOperation(operation); |
| 583 | current = yield* waitUntilExists(current.name ?? name); |
| 584 | } |
| 585 | |
| 586 | return toAttrs(current, env.project); |
| 587 | }), |
| 588 | |
| 589 | delete: Effect.fn(function* ({ output }) { |
| 590 | const operation = yield* networksecurity |
| 591 | .deleteProjectsLocationsSecurityProfiles({ |
| 592 | name: output.name, |
| 593 | etag: output.etag, |
| 594 | }) |
| 595 | .pipe( |
| 596 | Effect.retry({ |
| 597 | while: (error) => error._tag === "Conflict", |
| 598 | times: 8, |
| 599 | schedule: Schedule.spaced("2 seconds"), |
| 600 | }), |
| 601 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 602 | ); |
| 603 | if (operation !== undefined) { |
| 604 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 605 | } |
| 606 | yield* waitUntilGone(output.name); |
| 607 | }), |
| 608 | }); |