The best way to control Claude Code, Codex and more from any computer or phone

main
ts 193 lines 6.62 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as compute from "@distilled.cloud/gcp/compute_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const region = "us-central1";
15
16 const waitUntilGone = (project: string, firewallPolicy: string) =>
17 compute
18 .getRegionNetworkFirewallPolicies({
19 project,
20 region,
21 firewallPolicy,
22 })
23 .pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 const ruleAt = (rules: compute.FirewallPolicyRule[] | undefined, priority: number) =>
34 (rules ?? []).find((rule) => rule.priority === priority);
35
36 const hasReservedGotoNext = (rules: compute.FirewallPolicyRule[] | undefined) =>
37 (rules ?? []).some(
38 (rule) =>
39 rule.action === "goto_next" &&
40 rule.priority !== undefined &&
41 rule.priority >= 2147483548 &&
42 rule.priority <= 2147483647,
43 );
44
45 const nextName = (name: string) => {
46 const flip = name.endsWith("x") ? "z" : "x";
47 return name.length < 63 ? `${name}${flip}` : `${name.slice(0, -1)}${flip}`;
48 };
49
50 test.provider(
51 "getRegionNetworkFirewallPolicies on a missing policy fails with NotFound",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55 const { project } = yield* GcpEnvironment.current;
56 const error = yield* Effect.flip(
57 compute.getRegionNetworkFirewallPolicies({
58 project,
59 region,
60 firewallPolicy: "alchemy-missing-rnfw",
61 }),
62 );
63 expect(error._tag).toBe("NotFound");
64 yield* stack.destroy();
65 }).pipe(logLevel),
66 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
67 );
68
69 test.provider(
70 "create, update, replace, and delete a regional network firewall policy",
71 (stack) =>
72 Effect.gen(function* () {
73 yield* stack.destroy();
74
75 const created = yield* stack.deploy(
76 Effect.gen(function* () {
77 return yield* GCP.Compute.RegionNetworkFirewallPolicy("VpcFw", {
78 region,
79 description: "allow internal http",
80 rules: [
81 {
82 action: "allow",
83 priority: 1000,
84 direction: "INGRESS",
85 match: {
86 srcIpRanges: ["10.0.0.0/8"],
87 layer4Configs: [{ ipProtocol: "tcp", ports: ["80"] }],
88 },
89 },
90 ],
91 });
92 }),
93 );
94
95 expect(created.firewallPolicyName).toEqual(expect.any(String));
96 expect(created.region).toEqual(region);
97 expect(created.policyType).toEqual("VPC_POLICY");
98 expect(created.description).toEqual("allow internal http");
99 expect(ruleAt(created.rules, 1000)?.action).toEqual("allow");
100 expect(hasReservedGotoNext(created.rules)).toEqual(true);
101
102 const fetched = yield* compute.getRegionNetworkFirewallPolicies({
103 project: created.project,
104 region,
105 firewallPolicy: created.firewallPolicyName,
106 });
107 expect(fetched.name).toEqual(created.firewallPolicyName);
108 expect(fetched.description).toContain("[alchemy ");
109 expect(fetched.description).toContain("allow internal http");
110 expect(ruleAt(fetched.rules, 1000)?.match?.srcIpRanges).toEqual(
111 expect.arrayContaining(["10.0.0.0/8"]),
112 );
113
114 const listed = yield* compute.listRegionNetworkFirewallPolicies({
115 project: created.project,
116 region,
117 maxResults: 500,
118 });
119 expect(
120 (listed.items ?? []).some((policy) => policy.name === created.firewallPolicyName),
121 ).toEqual(true);
122
123 const updated = yield* stack.deploy(
124 Effect.gen(function* () {
125 return yield* GCP.Compute.RegionNetworkFirewallPolicy("VpcFw", {
126 firewallPolicyName: created.firewallPolicyName,
127 region,
128 description: "updated vpc fw",
129 rules: [
130 {
131 action: "allow",
132 priority: 1000,
133 direction: "INGRESS",
134 description: "http and https",
135 match: {
136 srcIpRanges: ["10.0.0.0/8", "192.168.0.0/16"],
137 layer4Configs: [{ ipProtocol: "tcp", ports: ["80", "443"] }],
138 },
139 },
140 ],
141 });
142 }),
143 );
144
145 expect(updated.firewallPolicyName).toEqual(created.firewallPolicyName);
146 expect(updated.firewallPolicyId).toEqual(created.firewallPolicyId);
147 expect(updated.description).toEqual("updated vpc fw");
148 expect(ruleAt(updated.rules, 1000)?.match?.srcIpRanges?.slice().sort()).toEqual([
149 "10.0.0.0/8",
150 "192.168.0.0/16",
151 ]);
152
153 const fetchedUpdate = yield* compute.getRegionNetworkFirewallPolicies({
154 project: updated.project,
155 region,
156 firewallPolicy: updated.firewallPolicyName,
157 });
158 expect(fetchedUpdate.description).toContain("updated vpc fw");
159 expect(ruleAt(fetchedUpdate.rules, 1000)?.description).toEqual("http and https");
160
161 const replaced = yield* stack.deploy(
162 Effect.gen(function* () {
163 return yield* GCP.Compute.RegionNetworkFirewallPolicy("VpcFw", {
164 firewallPolicyName: nextName(created.firewallPolicyName),
165 region,
166 description: "replaced vpc fw",
167 rules: [
168 {
169 action: "deny",
170 priority: 1000,
171 direction: "INGRESS",
172 match: {
173 srcIpRanges: ["0.0.0.0/0"],
174 layer4Configs: [{ ipProtocol: "tcp", ports: ["23"] }],
175 },
176 },
177 ],
178 });
179 }),
180 );
181
182 expect(replaced.firewallPolicyName).toEqual(nextName(created.firewallPolicyName));
183 expect(replaced.firewallPolicyId).not.toEqual(created.firewallPolicyId);
184 expect(replaced.description).toEqual("replaced vpc fw");
185 expect(ruleAt(replaced.rules, 1000)?.action).toEqual("deny");
186
187 yield* stack.destroy();
188
189 const gone = yield* waitUntilGone(replaced.project, replaced.firewallPolicyName);
190 expect(gone).toEqual("gone");
191 }).pipe(logLevel),
192 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 },
193 );