The best way to control Claude Code, Codex and more from any computer or phone
| 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 * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | NetworksecurityNotResolved, |
| 13 | canonicalizeLink, |
| 14 | changedFields, |
| 15 | collectPages, |
| 16 | encodeOwnership, |
| 17 | hasOwnershipMarker, |
| 18 | linkKey, |
| 19 | normalizeLocation, |
| 20 | parentOf, |
| 21 | parseName, |
| 22 | parseOwnership, |
| 23 | rfc1035, |
| 24 | toPhysicalId, |
| 25 | waitForOperation, |
| 26 | waitUntilGone, |
| 27 | waitUntilPresent, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "rules"; |
| 31 | |
| 32 | export type GatewaySecurityPolicyRuleBasicProfile = |
| 33 | | networksecurity.GatewaySecurityPolicyRuleBasicProfileEnum |
| 34 | | (string & {}); |
| 35 | |
| 36 | export type GatewaySecurityPoliciesRuleProps = { |
| 37 | /** |
| 38 | * Parent GatewaySecurityPolicy resource name |
| 39 | * (`projects/{project}/locations/{location}/gatewaySecurityPolicies/{gatewaySecurityPolicy}`) |
| 40 | * or the policy id. Immutable — changing it replaces the rule. |
| 41 | */ |
| 42 | gatewaySecurityPolicy: string; |
| 43 | /** |
| 44 | * Rule id (the `{rule}` segment of |
| 45 | * `.../gatewaySecurityPolicies/{gatewaySecurityPolicy}/rules/{rule}`). |
| 46 | * If omitted, a unique RFC1035 name is generated. Must be 4-63 |
| 47 | * characters matching `[a-z]([a-z0-9-]{0,61}[a-z0-9])?`. Immutable — |
| 48 | * changing it replaces the rule. |
| 49 | */ |
| 50 | gatewaySecurityPolicyRuleId?: string; |
| 51 | /** |
| 52 | * Region of the parent policy. Inferred from |
| 53 | * `gatewaySecurityPolicy` when that value is a full resource name. |
| 54 | * Immutable — changing it replaces the rule. |
| 55 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 56 | */ |
| 57 | location?: string; |
| 58 | /** |
| 59 | * Primitive action (`ALLOW` or `DENY`). |
| 60 | */ |
| 61 | basicProfile: GatewaySecurityPolicyRuleBasicProfile; |
| 62 | /** |
| 63 | * Rule priority. Lower numbers take precedence. |
| 64 | */ |
| 65 | priority: number; |
| 66 | /** |
| 67 | * Whether the rule is enforced. |
| 68 | * @default true |
| 69 | */ |
| 70 | enabled?: boolean; |
| 71 | /** |
| 72 | * CEL expression matching session criteria (L4). |
| 73 | */ |
| 74 | sessionMatcher: string; |
| 75 | /** |
| 76 | * Optional CEL expression matching L7 / application criteria. |
| 77 | */ |
| 78 | applicationMatcher?: string; |
| 79 | /** |
| 80 | * Enable TLS inspection for matching traffic. The parent policy must |
| 81 | * reference a TlsInspectionPolicy. |
| 82 | * @default false |
| 83 | */ |
| 84 | tlsInspectionEnabled?: boolean; |
| 85 | /** |
| 86 | * Human-readable description. Rules have no labels field, so Alchemy |
| 87 | * stamps ownership into a `[alchemy …]` prefix and strips it from |
| 88 | * attributes. |
| 89 | */ |
| 90 | description?: string; |
| 91 | }; |
| 92 | |
| 93 | export type GatewaySecurityPoliciesRule = Resource< |
| 94 | "GCP.NetworkSecurity.GatewaySecurityPoliciesRule", |
| 95 | GatewaySecurityPoliciesRuleProps, |
| 96 | { |
| 97 | /** Full resource name `projects/{project}/locations/{location}/gatewaySecurityPolicies/{gatewaySecurityPolicy}/rules/{rule}`. */ |
| 98 | name: string; |
| 99 | /** Rule id (last path segment). */ |
| 100 | gatewaySecurityPolicyRuleId: string; |
| 101 | /** Parent GatewaySecurityPolicy resource name. */ |
| 102 | gatewaySecurityPolicy: string; |
| 103 | /** Project id. */ |
| 104 | project: string; |
| 105 | /** Location id (`us-central1`). */ |
| 106 | location: string; |
| 107 | /** Primitive action (`ALLOW` or `DENY`). */ |
| 108 | basicProfile: string | undefined; |
| 109 | /** Rule priority. */ |
| 110 | priority: number | undefined; |
| 111 | /** Whether the rule is enforced. */ |
| 112 | enabled: boolean; |
| 113 | /** Session-level CEL matcher. */ |
| 114 | sessionMatcher: string | undefined; |
| 115 | /** Application-level CEL matcher, if set. */ |
| 116 | applicationMatcher: string | undefined; |
| 117 | /** Whether TLS inspection is enabled. */ |
| 118 | tlsInspectionEnabled: boolean; |
| 119 | /** User description with the Alchemy ownership prefix stripped. */ |
| 120 | description: string | undefined; |
| 121 | /** RFC3339 creation timestamp. */ |
| 122 | createTime: string | undefined; |
| 123 | /** RFC3339 last-update timestamp. */ |
| 124 | updateTime: string | undefined; |
| 125 | }, |
| 126 | never, |
| 127 | Providers |
| 128 | >; |
| 129 | |
| 130 | /** |
| 131 | * A GatewaySecurityPolicyRule nested under a GatewaySecurityPolicy. |
| 132 | * Each rule matches traffic with CEL and applies ALLOW or DENY. |
| 133 | * |
| 134 | * The API has no labels field, so Alchemy stamps ownership into the |
| 135 | * description for `list` / nuke. Changing the parent policy, rule id, |
| 136 | * or location replaces the rule. Matchers, priority, profile, enabled |
| 137 | * flag, TLS inspection, and description update in place. |
| 138 | * |
| 139 | * ### Creating a GatewaySecurityPoliciesRule |
| 140 | * **Example:** Allow all sessions |
| 141 | * ```typescript |
| 142 | * const rule = yield* GCP.NetworkSecurity.GatewaySecurityPoliciesRule("Allow", { |
| 143 | * gatewaySecurityPolicy: policy.name, |
| 144 | * basicProfile: "ALLOW", |
| 145 | * priority: 1000, |
| 146 | * sessionMatcher: "true", |
| 147 | * }); |
| 148 | * ``` |
| 149 | * |
| 150 | * **Example:** Deny by host |
| 151 | * ```typescript |
| 152 | * const rule = yield* GCP.NetworkSecurity.GatewaySecurityPoliciesRule("Block", { |
| 153 | * gatewaySecurityPolicy: policy.name, |
| 154 | * basicProfile: "DENY", |
| 155 | * priority: 100, |
| 156 | * enabled: true, |
| 157 | * sessionMatcher: "host() == 'blocked.example.com'", |
| 158 | * description: "block listed host", |
| 159 | * }); |
| 160 | * ``` |
| 161 | * |
| 162 | * @resource |
| 163 | * @category NetworkSecurity |
| 164 | */ |
| 165 | export const GatewaySecurityPoliciesRule = Resource<GatewaySecurityPoliciesRule>( |
| 166 | "GCP.NetworkSecurity.GatewaySecurityPoliciesRule", |
| 167 | ); |
| 168 | |
| 169 | const parentPolicyName = (project: string, location: string, gatewaySecurityPolicy: string) => { |
| 170 | const canonical = canonicalizeLink(gatewaySecurityPolicy); |
| 171 | if (canonical.includes("/gatewaySecurityPolicies/")) return canonical; |
| 172 | return `projects/${project}/locations/${location}/gatewaySecurityPolicies/${canonical}`; |
| 173 | }; |
| 174 | |
| 175 | const resourceNameOf = (parent: string, ruleId: string) => `${parent}/rules/${ruleId}`; |
| 176 | |
| 177 | const parentFromRuleName = (name: string) => { |
| 178 | const index = name.lastIndexOf("/rules/"); |
| 179 | return index >= 0 ? name.slice(0, index) : name; |
| 180 | }; |
| 181 | |
| 182 | const locationFromParent = (gatewaySecurityPolicy: string, fallback: string) => { |
| 183 | const canonical = canonicalizeLink(gatewaySecurityPolicy); |
| 184 | if (!canonical.includes("/locations/")) return fallback; |
| 185 | return parseName(canonical, "gatewaySecurityPolicies", fallback).location; |
| 186 | }; |
| 187 | |
| 188 | const toAttrs = ( |
| 189 | rule: networksecurity.GatewaySecurityPolicyRule, |
| 190 | project: string, |
| 191 | region: string, |
| 192 | ) => { |
| 193 | const name = rule.name ?? ""; |
| 194 | const parsed = parseName(name, COLLECTION, region); |
| 195 | const ownership = parseOwnership(rule.description); |
| 196 | const parent = parentFromRuleName(name); |
| 197 | return { |
| 198 | name, |
| 199 | gatewaySecurityPolicyRuleId: parsed.id, |
| 200 | gatewaySecurityPolicy: parent, |
| 201 | project: parsed.project || project, |
| 202 | location: parsed.location || region, |
| 203 | basicProfile: rule.basicProfile, |
| 204 | priority: rule.priority, |
| 205 | enabled: rule.enabled !== false, |
| 206 | sessionMatcher: rule.sessionMatcher, |
| 207 | applicationMatcher: rule.applicationMatcher, |
| 208 | tlsInspectionEnabled: rule.tlsInspectionEnabled === true, |
| 209 | description: ownership.text, |
| 210 | createTime: rule.createTime, |
| 211 | updateTime: rule.updateTime, |
| 212 | }; |
| 213 | }; |
| 214 | |
| 215 | const getByName = (name: string) => |
| 216 | networksecurity |
| 217 | .getProjectsLocationsGatewaySecurityPoliciesRules({ name }) |
| 218 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 219 | |
| 220 | export const GatewaySecurityPoliciesRuleProvider = () => |
| 221 | Provider.succeed(GatewaySecurityPoliciesRule, { |
| 222 | stables: [ |
| 223 | "name", |
| 224 | "gatewaySecurityPolicyRuleId", |
| 225 | "gatewaySecurityPolicy", |
| 226 | "project", |
| 227 | "location", |
| 228 | "createTime", |
| 229 | ], |
| 230 | |
| 231 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 232 | if (!isResolved(news)) return undefined; |
| 233 | const env = yield* GcpEnvironment.current; |
| 234 | const previousId = olds?.gatewaySecurityPolicyRuleId ?? output?.gatewaySecurityPolicyRuleId; |
| 235 | const nextId = news.gatewaySecurityPolicyRuleId |
| 236 | ? rfc1035(news.gatewaySecurityPolicyRuleId, "rule") |
| 237 | : previousId; |
| 238 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 239 | const nextLocation = normalizeLocation( |
| 240 | news.location ?? locationFromParent(news.gatewaySecurityPolicy, previousLocation), |
| 241 | env.region, |
| 242 | ); |
| 243 | const previousParent = linkKey(olds?.gatewaySecurityPolicy ?? output?.gatewaySecurityPolicy); |
| 244 | const nextParent = linkKey(news.gatewaySecurityPolicy); |
| 245 | if ( |
| 246 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 247 | previousLocation !== nextLocation || |
| 248 | (previousParent.length > 0 && previousParent !== nextParent) |
| 249 | ) { |
| 250 | return { action: "replace" as const }; |
| 251 | } |
| 252 | return undefined; |
| 253 | }), |
| 254 | |
| 255 | read: Effect.fn(function* ({ id, olds, output }) { |
| 256 | const env = yield* GcpEnvironment.current; |
| 257 | const location = normalizeLocation( |
| 258 | olds?.location ?? |
| 259 | output?.location ?? |
| 260 | (olds?.gatewaySecurityPolicy |
| 261 | ? locationFromParent(olds.gatewaySecurityPolicy, env.region) |
| 262 | : undefined), |
| 263 | env.region, |
| 264 | ); |
| 265 | const parent = parentPolicyName( |
| 266 | env.project, |
| 267 | location, |
| 268 | olds?.gatewaySecurityPolicy ?? output?.gatewaySecurityPolicy ?? "", |
| 269 | ); |
| 270 | const ruleId = yield* toPhysicalId( |
| 271 | id, |
| 272 | olds?.gatewaySecurityPolicyRuleId, |
| 273 | output?.gatewaySecurityPolicyRuleId, |
| 274 | "rule", |
| 275 | ); |
| 276 | const name = output?.name ?? resourceNameOf(parent, ruleId); |
| 277 | const existing = yield* getByName(name); |
| 278 | if (existing === undefined) return undefined; |
| 279 | const attrs = toAttrs(existing, env.project, env.region); |
| 280 | const { labels } = parseOwnership(existing.description); |
| 281 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 282 | }), |
| 283 | |
| 284 | list: () => |
| 285 | Effect.gen(function* () { |
| 286 | const env = yield* GcpEnvironment.current; |
| 287 | const policies = yield* collectPages( |
| 288 | networksecurity.listProjectsLocationsGatewaySecurityPolicies.pages({ |
| 289 | parent: parentOf(env.project, "-"), |
| 290 | pageSize: 1000, |
| 291 | }), |
| 292 | (page) => page.gatewaySecurityPolicies, |
| 293 | ); |
| 294 | const nested = yield* Effect.forEach( |
| 295 | policies.filter((policy) => (policy.name ?? "").length > 0), |
| 296 | (policy) => |
| 297 | collectPages( |
| 298 | networksecurity.listProjectsLocationsGatewaySecurityPoliciesRules.pages({ |
| 299 | parent: policy.name ?? "", |
| 300 | pageSize: 1000, |
| 301 | }), |
| 302 | (page) => page.gatewaySecurityPolicyRules, |
| 303 | ), |
| 304 | { concurrency: 4 }, |
| 305 | ); |
| 306 | return nested |
| 307 | .flat() |
| 308 | .filter((item) => hasOwnershipMarker(item.description)) |
| 309 | .map((item) => toAttrs(item, env.project, env.region)); |
| 310 | }), |
| 311 | |
| 312 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 313 | const env = yield* GcpEnvironment.current; |
| 314 | const location = normalizeLocation( |
| 315 | news.location ?? |
| 316 | output?.location ?? |
| 317 | locationFromParent(news.gatewaySecurityPolicy, env.region), |
| 318 | env.region, |
| 319 | ); |
| 320 | const parent = parentPolicyName(env.project, location, news.gatewaySecurityPolicy); |
| 321 | const ruleId = yield* toPhysicalId( |
| 322 | id, |
| 323 | news.gatewaySecurityPolicyRuleId, |
| 324 | output?.gatewaySecurityPolicyRuleId, |
| 325 | "rule", |
| 326 | ); |
| 327 | const name = resourceNameOf(parent, ruleId); |
| 328 | const ownership = yield* createInternalLabels(id); |
| 329 | const desiredDescription = encodeOwnership(ownership, news.description); |
| 330 | const enabled = news.enabled !== false; |
| 331 | const tlsInspectionEnabled = news.tlsInspectionEnabled === true; |
| 332 | |
| 333 | let current = yield* getByName(name); |
| 334 | |
| 335 | if (current === undefined) { |
| 336 | const created = yield* networksecurity |
| 337 | .createProjectsLocationsGatewaySecurityPoliciesRules({ |
| 338 | parent, |
| 339 | gatewaySecurityPolicyRuleId: ruleId, |
| 340 | body: { |
| 341 | basicProfile: news.basicProfile, |
| 342 | priority: news.priority, |
| 343 | enabled, |
| 344 | sessionMatcher: news.sessionMatcher, |
| 345 | applicationMatcher: news.applicationMatcher, |
| 346 | tlsInspectionEnabled, |
| 347 | description: desiredDescription, |
| 348 | }, |
| 349 | }) |
| 350 | .pipe( |
| 351 | Effect.retry({ |
| 352 | while: (error) => error._tag === "Conflict", |
| 353 | times: 5, |
| 354 | schedule: Schedule.spaced("2 seconds"), |
| 355 | }), |
| 356 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 357 | ); |
| 358 | if (created !== undefined) { |
| 359 | yield* waitForOperation(created); |
| 360 | } |
| 361 | current = yield* waitUntilPresent(getByName(name), name); |
| 362 | } |
| 363 | |
| 364 | if (current === undefined) { |
| 365 | return yield* new NetworksecurityNotResolved({ resourceName: name }); |
| 366 | } |
| 367 | |
| 368 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 369 | const profileChanged = (current.basicProfile ?? "") !== news.basicProfile; |
| 370 | const priorityChanged = (current.priority ?? 0) !== news.priority; |
| 371 | const enabledChanged = (current.enabled !== false) !== enabled; |
| 372 | const sessionChanged = (current.sessionMatcher ?? "") !== news.sessionMatcher; |
| 373 | const applicationChanged = |
| 374 | (current.applicationMatcher ?? "") !== (news.applicationMatcher ?? ""); |
| 375 | const tlsChanged = (current.tlsInspectionEnabled === true) !== tlsInspectionEnabled; |
| 376 | const updateMask = changedFields([ |
| 377 | ["description", descriptionChanged], |
| 378 | ["basicProfile", profileChanged], |
| 379 | ["priority", priorityChanged], |
| 380 | ["enabled", enabledChanged], |
| 381 | ["sessionMatcher", sessionChanged], |
| 382 | ["applicationMatcher", applicationChanged], |
| 383 | ["tlsInspectionEnabled", tlsChanged], |
| 384 | ]); |
| 385 | |
| 386 | if (updateMask.length > 0) { |
| 387 | const operation = yield* networksecurity.patchProjectsLocationsGatewaySecurityPoliciesRules( |
| 388 | { |
| 389 | name: current.name ?? name, |
| 390 | updateMask: updateMask.join(","), |
| 391 | body: { |
| 392 | name: current.name ?? name, |
| 393 | description: desiredDescription, |
| 394 | basicProfile: news.basicProfile, |
| 395 | priority: news.priority, |
| 396 | enabled, |
| 397 | sessionMatcher: news.sessionMatcher, |
| 398 | applicationMatcher: news.applicationMatcher, |
| 399 | tlsInspectionEnabled, |
| 400 | }, |
| 401 | }, |
| 402 | ); |
| 403 | yield* waitForOperation(operation); |
| 404 | current = yield* waitUntilPresent(getByName(current.name ?? name), current.name ?? name); |
| 405 | } |
| 406 | |
| 407 | return toAttrs(current, env.project, env.region); |
| 408 | }), |
| 409 | |
| 410 | delete: Effect.fn(function* ({ output }) { |
| 411 | const operation = yield* networksecurity |
| 412 | .deleteProjectsLocationsGatewaySecurityPoliciesRules({ |
| 413 | name: output.name, |
| 414 | }) |
| 415 | .pipe( |
| 416 | Effect.retry({ |
| 417 | while: (error) => error._tag === "Conflict", |
| 418 | times: 8, |
| 419 | schedule: Schedule.spaced("2 seconds"), |
| 420 | }), |
| 421 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 422 | ); |
| 423 | if (operation !== undefined) { |
| 424 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 425 | } |
| 426 | yield* waitUntilGone(getByName(output.name), output.name); |
| 427 | }), |
| 428 | }); |