@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 651 lines 21.9 KB
Raw
1 import * as compute from "@distilled.cloud/gcp/compute_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
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 { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import type {
15 FirewallPolicyAssociation,
16 FirewallPolicyRule,
17 FirewallPolicyRuleMatcher,
18 FirewallPolicyType,
19 } from "./FirewallPolicy.ts";
20 import { ignoredCodes } from "./internal.ts";
21 import { waitRegionOperation } from "./operations.ts";
22
23 const DEFAULT_POLICY_TYPE = "VPC_POLICY";
24 const RESERVED_PRIORITY_MIN = 2147483548;
25 const RESERVED_PRIORITY_MAX = 2147483647;
26 const MAX_NAME_LENGTH = 63;
27
28 const isReservedPriority = (priority: number | undefined) =>
29 priority !== undefined && priority >= RESERVED_PRIORITY_MIN && priority <= RESERVED_PRIORITY_MAX;
30
31 export type RegionNetworkFirewallPolicyProps = {
32 /**
33 * Policy name (RFC1035, 1-63 characters). If omitted, a unique name is
34 * generated from the stack, stage, and logical id. Immutable — changing
35 * it replaces the policy.
36 */
37 firewallPolicyName?: string;
38 /**
39 * Region the policy lives in (e.g. `us-central1`). Immutable — changing
40 * it replaces the policy. `US-CENTRAL1` is accepted and normalized to
41 * `us-central1`.
42 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
43 */
44 region?: string;
45 /**
46 * Optional description. Network firewall policies have no labels field,
47 * so Alchemy ownership (`alchemy-stack` / `alchemy-stage` /
48 * `alchemy-id`) is stored in a `[alchemy …]` prefix for `list` / nuke.
49 */
50 description?: string;
51 /**
52 * Policy type. Set only at create time — changing it replaces the
53 * policy.
54 * @default "VPC_POLICY"
55 */
56 policyType?: FirewallPolicyType;
57 /**
58 * Network firewall rules. GCP always keeps default `goto_next` rules
59 * in the reserved priority range `2147483548`–`2147483647`; Alchemy
60 * never add/patch/remove those. When this field is set, user rules
61 * are synced with `addRule` / `patchRule` / `removeRule` (not
62 * `patch`). When omitted on later updates, observed rules are left
63 * in place.
64 */
65 rules?: FirewallPolicyRule[];
66 };
67
68 export type RegionNetworkFirewallPolicy = Resource<
69 "GCP.Compute.RegionNetworkFirewallPolicy",
70 RegionNetworkFirewallPolicyProps,
71 {
72 /** User-provided RFC1035 name. */
73 firewallPolicyName: string;
74 /** Project id. */
75 project: string;
76 /** Region short name (`us-central1`). */
77 region: string;
78 /** User description with the Alchemy ownership prefix stripped. */
79 description: string | undefined;
80 /** Policy type (`VPC_POLICY`, `RDMA_ROCE_POLICY`, `ULL_POLICY`). */
81 policyType: string;
82 /** Rules currently attached (including the default). */
83 rules: FirewallPolicyRule[];
84 /** Associations currently attached to this policy. */
85 associations: FirewallPolicyAssociation[];
86 /** Optimistic-locking fingerprint for metadata patches. */
87 fingerprint: string | undefined;
88 /** Server-defined URL. */
89 selfLink: string | undefined;
90 /** Server-defined URL including the numeric id. */
91 selfLinkWithId: string | undefined;
92 /** RFC3339 creation timestamp. */
93 creationTimestamp: string | undefined;
94 /** Total count of rule tuples. */
95 ruleTupleCount: number | undefined;
96 /** Server-assigned numeric id. */
97 firewallPolicyId: string | undefined;
98 /** Resource kind. */
99 kind: string | undefined;
100 },
101 never,
102 Providers
103 >;
104
105 /**
106 * A regional network firewall policy attached to a VPC network.
107 *
108 * Policies live under `projects/{project}/regions/{region}/firewallPolicies`
109 * and are identified by a user-provided RFC1035 name. Name, region, and
110 * `policyType` are immutable — changing any of them replaces the policy.
111 * Description updates in place via `regionNetworkFirewallPolicies.patch`.
112 * Rules are synced with `addRule` / `patchRule` / `removeRule`.
113 *
114 * Compute Engine network firewall policies have no resource labels.
115 * Alchemy stamps ownership into the description so `read` / `list` (and
116 * `pnpm nuke:gcp`) can find them.
117 *
118 * ### Creating a Regional Network Firewall Policy
119 * **Example:** Generated name with an allow rule
120 * ```typescript
121 * const policy = yield* GCP.Compute.RegionNetworkFirewallPolicy("VpcFw", {
122 * region: "us-central1",
123 * description: "allow internal http",
124 * rules: [
125 * {
126 * action: "allow",
127 * priority: 1000,
128 * direction: "INGRESS",
129 * match: {
130 * srcIpRanges: ["10.0.0.0/8"],
131 * layer4Configs: [{ ipProtocol: "tcp", ports: ["80"] }],
132 * },
133 * },
134 * ],
135 * });
136 * ```
137 *
138 * @resource
139 * @category Compute
140 */
141 export const RegionNetworkFirewallPolicy = Resource<RegionNetworkFirewallPolicy>(
142 "GCP.Compute.RegionNetworkFirewallPolicy",
143 );
144
145 export class RegionNetworkFirewallPolicyNotResolved extends Data.TaggedError(
146 "GCP.Compute.RegionNetworkFirewallPolicyNotResolved",
147 )<{
148 firewallPolicyName: string;
149 region: string;
150 }> {}
151
152 export class RegionNetworkFirewallPolicyStillExists extends Data.TaggedError(
153 "GCP.Compute.RegionNetworkFirewallPolicyStillExists",
154 )<{
155 firewallPolicyName: string;
156 region: string;
157 }> {}
158
159 const lastSegment = (value: string | undefined): string => {
160 if (value === undefined || value.length === 0) return "";
161 const trimmed = value.replace(/\/+$/, "");
162 const parts = trimmed.split("/");
163 return parts[parts.length - 1] || trimmed;
164 };
165
166 const normalizeRegion = (region: string | undefined, defaultRegion: string) =>
167 lastSegment(region ?? defaultRegion).toLowerCase();
168
169 const rfc1035 = (name: string): string => {
170 let next = name
171 .toLowerCase()
172 .replace(/[^a-z0-9-]/g, "-")
173 .replace(/-+/g, "-")
174 .replace(/^-+/, "")
175 .replace(/-+$/, "");
176 if (!/^[a-z]/.test(next)) {
177 next = `f${next}`;
178 }
179 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, "");
180 return next.length > 0 ? next : "policy";
181 };
182
183 const toName = (id: string, name: string | undefined, existing?: string) =>
184 Effect.gen(function* () {
185 if (name !== undefined) return rfc1035(name);
186 if (existing !== undefined) return existing;
187 return rfc1035(
188 yield* createPhysicalName({
189 id,
190 maxLength: MAX_NAME_LENGTH,
191 lowercase: true,
192 }),
193 );
194 });
195
196 const typeOf = (value: string | undefined) => (value ?? DEFAULT_POLICY_TYPE).toUpperCase();
197
198 const encodeDescription = (
199 labels: Record<string, string>,
200 description: string | undefined,
201 ): string => {
202 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
203 return description ? `${marker}\n${description}` : marker;
204 };
205
206 const parseDescription = (
207 description: string | undefined,
208 ): {
209 labels: Record<string, string>;
210 description: string | undefined;
211 } => {
212 if (!description?.startsWith("[alchemy ")) {
213 return { labels: {}, description };
214 }
215 const end = description.indexOf("]");
216 if (end < 0) return { labels: {}, description };
217 const labels: Record<string, string> = {};
218 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
219 const eq = part.indexOf("=");
220 if (eq > 0) {
221 labels[part.slice(0, eq)] = part.slice(eq + 1);
222 }
223 }
224 const rest = description.slice(end + 1).replace(/^\n/, "");
225 return { labels, description: rest.length > 0 ? rest : undefined };
226 };
227
228 const hasOwnershipMarker = (description: string | undefined) =>
229 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
230
231 const sorted = (values: readonly string[] | undefined) => [...(values ?? [])].slice().sort();
232
233 const sameJson = (left: unknown, right: unknown) =>
234 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
235
236 const canonMatch = (match: FirewallPolicyRuleMatcher | undefined) => {
237 if (match === undefined) return undefined;
238 return {
239 srcIpRanges: sorted(match.srcIpRanges),
240 destIpRanges: sorted(match.destIpRanges),
241 srcFqdns: sorted(match.srcFqdns),
242 destFqdns: sorted(match.destFqdns),
243 srcRegionCodes: sorted(match.srcRegionCodes),
244 destRegionCodes: sorted(match.destRegionCodes),
245 srcAddressGroups: sorted(match.srcAddressGroups),
246 destAddressGroups: sorted(match.destAddressGroups),
247 srcThreatIntelligences: sorted(match.srcThreatIntelligences),
248 destThreatIntelligences: sorted(match.destThreatIntelligences),
249 srcNetworks: sorted(match.srcNetworks),
250 srcNetworkType: match.srcNetworkType,
251 destNetworkType: match.destNetworkType,
252 layer4Configs: [...(match.layer4Configs ?? [])]
253 .map((config) => ({
254 ipProtocol: (config.ipProtocol ?? "").toLowerCase(),
255 ports: sorted(config.ports),
256 }))
257 .sort(
258 (left, right) =>
259 left.ipProtocol.localeCompare(right.ipProtocol) ||
260 JSON.stringify(left.ports).localeCompare(JSON.stringify(right.ports)),
261 ),
262 };
263 };
264
265 const canonRule = (rule: FirewallPolicyRule) => ({
266 priority: rule.priority,
267 action: rule.action,
268 description: rule.description ?? "",
269 direction: (rule.direction ?? "INGRESS").toUpperCase(),
270 enableLogging: rule.enableLogging === true,
271 disabled: rule.disabled === true,
272 ruleName: rule.ruleName ?? "",
273 match: canonMatch(rule.match),
274 targetResources: sorted(rule.targetResources),
275 targetServiceAccounts: sorted(rule.targetServiceAccounts),
276 });
277
278 const ruleEquals = (left: FirewallPolicyRule, right: FirewallPolicyRule) =>
279 sameJson(canonRule(left), canonRule(right));
280
281 const toRuleBody = (rule: FirewallPolicyRule): FirewallPolicyRule => ({
282 priority: rule.priority,
283 action: rule.action,
284 description: rule.description,
285 direction: rule.direction,
286 enableLogging: rule.enableLogging === true ? true : undefined,
287 disabled: rule.disabled === true ? true : undefined,
288 ruleName: rule.ruleName,
289 match: rule.match,
290 targetResources: rule.targetResources,
291 targetServiceAccounts: rule.targetServiceAccounts,
292 targetSecureTags: rule.targetSecureTags,
293 });
294
295 const desiredRules = (news: RegionNetworkFirewallPolicyProps): FirewallPolicyRule[] | undefined => {
296 if (news.rules === undefined) return undefined;
297 const byPriority = new Map<number, FirewallPolicyRule>();
298 for (const rule of news.rules) {
299 if (rule.priority === undefined) continue;
300 if (isReservedPriority(rule.priority)) continue;
301 byPriority.set(rule.priority, rule);
302 }
303 return [...byPriority.values()].sort(
304 (left, right) => (left.priority ?? 0) - (right.priority ?? 0),
305 );
306 };
307
308 const toAttrs = (
309 policy: compute.FirewallPolicy,
310 project: string,
311 ): RegionNetworkFirewallPolicy["Attributes"] => {
312 const parsed = parseDescription(policy.description);
313 return {
314 firewallPolicyName: policy.name ?? policy.id ?? "",
315 project,
316 region: lastSegment(policy.region).toLowerCase(),
317 description: parsed.description,
318 policyType: typeOf(policy.policyType),
319 rules: policy.rules ?? [],
320 associations: policy.associations ?? [],
321 fingerprint: policy.fingerprint,
322 selfLink: policy.selfLink,
323 selfLinkWithId: policy.selfLinkWithId,
324 creationTimestamp: policy.creationTimestamp,
325 ruleTupleCount: policy.ruleTupleCount,
326 firewallPolicyId: policy.id,
327 kind: policy.kind,
328 };
329 };
330
331 const getByName = (project: string, region: string, firewallPolicy: string) =>
332 compute
333 .getRegionNetworkFirewallPolicies({ project, region, firewallPolicy })
334 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
335
336 const awaitResource = (project: string, region: string, firewallPolicyName: string) =>
337 getByName(project, region, firewallPolicyName).pipe(
338 Effect.flatMap((policy) =>
339 policy !== undefined
340 ? Effect.succeed(policy)
341 : Effect.fail(
342 new RegionNetworkFirewallPolicyNotResolved({
343 firewallPolicyName,
344 region,
345 }),
346 ),
347 ),
348 Effect.retry({
349 while: (error) => error._tag === "GCP.Compute.RegionNetworkFirewallPolicyNotResolved",
350 times: 8,
351 schedule: Schedule.spaced("1 second"),
352 }),
353 );
354
355 const waitUntilGone = (project: string, region: string, firewallPolicyName: string) =>
356 getByName(project, region, firewallPolicyName).pipe(
357 Effect.flatMap((policy) =>
358 policy === undefined
359 ? Effect.void
360 : Effect.fail(
361 new RegionNetworkFirewallPolicyStillExists({
362 firewallPolicyName,
363 region,
364 }),
365 ),
366 ),
367 Effect.retry({
368 while: (error) => error._tag === "GCP.Compute.RegionNetworkFirewallPolicyStillExists",
369 times: 10,
370 schedule: Schedule.spaced("1 second"),
371 }),
372 Effect.catchTag("GCP.Compute.RegionNetworkFirewallPolicyStillExists", () => Effect.void),
373 );
374
375 const runOp = <E extends { readonly _tag: string }, R>(
376 project: string,
377 region: string,
378 firewallPolicyName: string,
379 start: Effect.Effect<compute.Operation, E, R>,
380 options?: { ignoreAlreadyExists?: boolean; ignoreNotFound?: boolean },
381 ) =>
382 start.pipe(
383 Effect.flatMap((operation) =>
384 waitRegionOperation(project, region, operation, {
385 ignore: ignoredCodes(options),
386 }),
387 ),
388 Effect.retry({
389 while: (error) => error._tag === "Conflict",
390 times: 5,
391 schedule: Schedule.spaced("1 second"),
392 }),
393 );
394
395 const insertBody = (
396 firewallPolicyName: string,
397 news: RegionNetworkFirewallPolicyProps,
398 description: string,
399 ): compute.FirewallPolicy => ({
400 name: firewallPolicyName,
401 description,
402 policyType: typeOf(news.policyType),
403 });
404
405 const syncRules = (
406 project: string,
407 region: string,
408 firewallPolicyName: string,
409 observed: readonly FirewallPolicyRule[],
410 desired: readonly FirewallPolicyRule[],
411 ) =>
412 Effect.gen(function* () {
413 const observedByPriority = new Map<number, FirewallPolicyRule>();
414 for (const rule of observed) {
415 if (rule.priority !== undefined) {
416 observedByPriority.set(rule.priority, rule);
417 }
418 }
419 const desiredByPriority = new Map<number, FirewallPolicyRule>();
420 for (const rule of desired) {
421 if (rule.priority !== undefined) {
422 desiredByPriority.set(rule.priority, rule);
423 }
424 }
425
426 for (const [priority, rule] of desiredByPriority) {
427 if (isReservedPriority(priority)) continue;
428 const current = observedByPriority.get(priority);
429 if (current === undefined) {
430 yield* runOp(
431 project,
432 region,
433 firewallPolicyName,
434 compute.addRuleRegionNetworkFirewallPolicies({
435 project,
436 region,
437 firewallPolicy: firewallPolicyName,
438 body: toRuleBody(rule),
439 }),
440 { ignoreAlreadyExists: true },
441 );
442 continue;
443 }
444 if (!ruleEquals(current, rule)) {
445 yield* runOp(
446 project,
447 region,
448 firewallPolicyName,
449 compute.patchRuleRegionNetworkFirewallPolicies({
450 project,
451 region,
452 firewallPolicy: firewallPolicyName,
453 priority,
454 body: toRuleBody(rule),
455 }),
456 );
457 }
458 }
459
460 for (const priority of observedByPriority.keys()) {
461 if (desiredByPriority.has(priority)) continue;
462 if (isReservedPriority(priority)) continue;
463 yield* runOp(
464 project,
465 region,
466 firewallPolicyName,
467 compute.removeRuleRegionNetworkFirewallPolicies({
468 project,
469 region,
470 firewallPolicy: firewallPolicyName,
471 priority,
472 }),
473 { ignoreNotFound: true },
474 ).pipe(Effect.catchTag("NotFound", () => Effect.void));
475 }
476 });
477
478 export const RegionNetworkFirewallPolicyProvider = () =>
479 Provider.succeed(RegionNetworkFirewallPolicy, {
480 stables: [
481 "firewallPolicyName",
482 "project",
483 "region",
484 "policyType",
485 "firewallPolicyId",
486 "selfLink",
487 "selfLinkWithId",
488 "creationTimestamp",
489 ],
490
491 diff: Effect.fn(function* ({ id, news, olds, output }) {
492 if (!isResolved(news)) return undefined;
493 const env = yield* GcpEnvironment.current;
494
495 // Name is immutable on GCP. Resolve the desired name the same way
496 // create does — do not fall back to previousName when news omits
497 // firewallPolicyName, or a generated-name change looks like a no-op.
498 const previousName = output?.firewallPolicyName ?? olds?.firewallPolicyName;
499 const nextName = yield* toName(id, news.firewallPolicyName);
500 const nameChanged = previousName !== undefined && previousName !== nextName;
501
502 const previousRegion = normalizeRegion(olds?.region ?? output?.region, env.region);
503 const nextRegion = normalizeRegion(news.region ?? output?.region, env.region);
504 const regionChanged = previousRegion !== nextRegion;
505
506 const previousType = typeOf(olds?.policyType ?? output?.policyType);
507 const nextType = typeOf(news.policyType ?? output?.policyType);
508 const typeChanged = previousType !== nextType;
509
510 if (nameChanged) {
511 return { action: "replace" as const, deleteFirst: false };
512 }
513 if (regionChanged || typeChanged) {
514 return { action: "replace" as const, deleteFirst: !regionChanged };
515 }
516 return undefined;
517 }),
518
519 read: Effect.fn(function* ({ id, olds, output }) {
520 const env = yield* GcpEnvironment.current;
521 const firewallPolicyName = yield* toName(
522 id,
523 olds?.firewallPolicyName,
524 output?.firewallPolicyName,
525 );
526 const region = normalizeRegion(olds?.region ?? output?.region, env.region);
527 const existing = yield* getByName(env.project, region, firewallPolicyName);
528 if (existing === undefined) return undefined;
529 const attrs = toAttrs(existing, env.project);
530 const { labels } = parseDescription(existing.description);
531 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
532 }),
533
534 list: () =>
535 Effect.gen(function* () {
536 const env = yield* GcpEnvironment.current;
537 const pages = yield* compute.aggregatedListNetworkFirewallPolicies
538 .pages({
539 project: env.project,
540 maxResults: 500,
541 returnPartialSuccess: true,
542 })
543 .pipe(Stream.runCollect);
544 return Array.from(pages).flatMap((page) =>
545 Object.values(page.items ?? {}).flatMap((scoped) =>
546 (scoped?.firewallPolicies ?? [])
547 .filter((policy) => (policy.region ?? "").length > 0)
548 .filter((policy) => hasOwnershipMarker(policy.description))
549 .map((policy) => toAttrs(policy, env.project)),
550 ),
551 );
552 }),
553
554 reconcile: Effect.fn(function* ({ id, news, output }) {
555 const env = yield* GcpEnvironment.current;
556 const firewallPolicyName = yield* toName(
557 id,
558 news.firewallPolicyName,
559 output?.firewallPolicyName,
560 );
561 const region = normalizeRegion(news.region ?? output?.region, env.region);
562 const ownership = yield* createInternalLabels(id);
563 const desiredDescription = encodeDescription(ownership, news.description);
564
565 let current = yield* getByName(env.project, region, firewallPolicyName);
566
567 if (current === undefined) {
568 yield* compute
569 .insertRegionNetworkFirewallPolicies({
570 project: env.project,
571 region,
572 body: insertBody(firewallPolicyName, news, desiredDescription),
573 })
574 .pipe(
575 Effect.flatMap((operation) =>
576 waitRegionOperation(env.project, region, operation, {
577 ignore: ["RESOURCE_ALREADY_EXISTS"],
578 }),
579 ),
580 Effect.catchTag("Conflict", () => Effect.void),
581 );
582 current = yield* awaitResource(env.project, region, firewallPolicyName);
583 }
584
585 if (current === undefined) {
586 return yield* new RegionNetworkFirewallPolicyNotResolved({
587 firewallPolicyName,
588 region,
589 });
590 }
591
592 if ((current.description ?? "") !== desiredDescription) {
593 yield* runOp(
594 env.project,
595 region,
596 firewallPolicyName,
597 compute.patchRegionNetworkFirewallPolicies({
598 project: env.project,
599 region,
600 firewallPolicy: firewallPolicyName,
601 body: {
602 fingerprint: current.fingerprint,
603 description: desiredDescription,
604 },
605 }),
606 );
607 current = (yield* getByName(env.project, region, firewallPolicyName)) ?? current;
608 }
609
610 const nextRules = desiredRules(news);
611 if (nextRules !== undefined) {
612 yield* syncRules(env.project, region, firewallPolicyName, current.rules ?? [], nextRules);
613 current = (yield* getByName(env.project, region, firewallPolicyName)) ?? current;
614 }
615
616 if (current === undefined) {
617 return yield* new RegionNetworkFirewallPolicyNotResolved({
618 firewallPolicyName,
619 region,
620 });
621 }
622
623 return toAttrs(current, env.project);
624 }),
625
626 delete: Effect.fn(function* ({ output }) {
627 const env = yield* GcpEnvironment.current;
628 const project = output.project || env.project;
629 const region = normalizeRegion(output.region, env.region);
630 yield* compute
631 .deleteRegionNetworkFirewallPolicies({
632 project,
633 region,
634 firewallPolicy: output.firewallPolicyName,
635 })
636 .pipe(
637 Effect.flatMap((operation) =>
638 waitRegionOperation(project, region, operation, {
639 ignore: ["RESOURCE_NOT_FOUND"],
640 }),
641 ),
642 Effect.catchTag("NotFound", () => Effect.void),
643 Effect.retry({
644 while: (error) => error._tag === "Conflict",
645 times: 8,
646 schedule: Schedule.spaced("2 seconds"),
647 }),
648 );
649 yield* waitUntilGone(project, region, output.firewallPolicyName);
650 }),
651 });