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

main
ts 549 lines 19.3 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2";
7 import * as compute from "@distilled.cloud/gcp/compute_v1";
8 import * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
9 import * as storage from "@distilled.cloud/gcp/storage_v1";
10 import { DestroyError } from "alchemy/Apply";
11 import * as Cause from "effect/Cause";
12 import * as Data from "effect/Data";
13 import * as Effect from "effect/Effect";
14 import * as HttpClient from "effect/http/HttpClient";
15 import * as Schedule from "effect/Schedule";
16 import EgressFunction, {
17 APP_CIDR,
18 AppSubnet,
19 DATA_CIDR,
20 DataSubnet,
21 EGRESS_TAG,
22 PODS_CIDR,
23 Probes,
24 type ProbeResult,
25 REGION,
26 Results,
27 SERVICES_CIDR,
28 SmokeNetwork,
29 } from "./fixtures/egress-function.ts";
30
31 /**
32 * GCP counterpart of the AWS VPC smoke: a custom-mode VPC with two regional
33 * subnetworks (one with secondary ranges), a Cloud Router hosting Cloud NAT
34 * on a reserved static IP, ingress/egress firewall rules (allow-internal,
35 * deny-all egress overridden by a tagged tcp:443 allow), a custom route, and
36 * an internal-only Effect-native Cloud Function using Direct VPC egress.
37 * The function is driven through Pub/Sub, probes the internet, and records
38 * the egress IP it observed — which must be the NAT's static address.
39 */
40
41 const testOptions = { providers: GCP.providers() };
42 const { test, beforeAll, afterAll } = Test.make(testOptions);
43 // Durable state: a subnet held by Cloud Run (below) stays resumable, so a
44 // later run's destroy drains it instead of orphaning it.
45 const sharedStack = Core.scratchStack(
46 testOptions,
47 "NetworkSmoke",
48 "test/GCP/Compute/Network.smoke.test.ts",
49 );
50
51 // Cloud Run keeps the Direct VPC egress IPs it reserved in a subnet
52 // (`serverless-ipv4-*` addresses, purpose SERVERLESS) for up to 1-2 hours
53 // after the function is deleted, and the subnet cannot be deleted until they
54 // are released. Only that documented hold is tolerated: it fails the
55 // subnet's delete and skips the network's; every other failure surfaces.
56 // https://cloud.google.com/run/docs/configuring/vpc-direct-vpc
57 const HELD_BY_CLOUD_RUN = /already being used by \S+\/addresses\/serverless-ipv4-/;
58
59 const destroyStack = sharedStack.destroy().pipe(
60 Effect.as([] as ReadonlyArray<string>),
61 Effect.catch((error: unknown) =>
62 error instanceof DestroyError &&
63 error.failures.every(
64 (failure) =>
65 failure.resourceType === "GCP.Compute.Subnetwork" &&
66 HELD_BY_CLOUD_RUN.test(Cause.pretty(failure.cause)),
67 ) &&
68 error.blocked.every((blocked) => blocked.resourceType === "GCP.Compute.Network")
69 ? Effect.succeed([
70 ...error.failures.map((failure) => failure.logicalId),
71 ...error.blocked.map((blocked) => blocked.logicalId),
72 ])
73 : Effect.fail(error),
74 ),
75 );
76
77 // private.googleapis.com — the canonical custom route for Private Google
78 // Access without the default internet route.
79 const PRIVATE_GOOGLE_APIS = "199.36.153.8/30";
80
81 interface StackOutputs {
82 networkName: string;
83 appSubnetName: string;
84 dataSubnetName: string;
85 routerName: string;
86 natName: string;
87 addressName: string;
88 address: string;
89 addressSelfLink: string;
90 allowInternalName: string;
91 denyEgressName: string;
92 allowHttpsEgressName: string;
93 routeName: string;
94 functionName: string;
95 functionUrl: string;
96 bucketName: string;
97 topicName: string;
98 }
99
100 let outputs: StackOutputs;
101
102 class StillExists extends Data.TaggedError("StillExists")<{
103 readonly what: string;
104 }> {}
105
106 class ProbeNotRecorded extends Data.TaggedError("ProbeNotRecorded")<{
107 readonly object: string;
108 }> {}
109
110 // Bounded wait until an out-of-band probe reports the resource gone.
111 const waitUntilGone = <E, R>(what: string, probe: Effect.Effect<boolean, E, R>) =>
112 probe.pipe(
113 Effect.flatMap((gone) => (gone ? Effect.void : Effect.fail(new StillExists({ what })))),
114 Effect.retry({
115 while: (e) => e instanceof StillExists,
116 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(30)]),
117 }),
118 );
119
120 const NAT_NAME = "smoke-egress";
121
122 const deployProgram = Effect.gen(function* () {
123 const network = yield* SmokeNetwork;
124 const appSubnet = yield* AppSubnet;
125 const dataSubnet = yield* DataSubnet;
126
127 const egressIp = yield* GCP.Compute.Address("EgressIp", {
128 region: REGION,
129 addressType: "EXTERNAL",
130 description: "cloud nat egress",
131 });
132
133 const router = yield* GCP.Compute.Router("NatRouter", {
134 region: REGION,
135 network: network.networkName,
136 description: "network smoke nat",
137 nats: [
138 {
139 name: NAT_NAME,
140 sourceSubnetworkIpRangesToNat: "LIST_OF_SUBNETWORKS",
141 subnetworks: [{ name: appSubnet.subnetworkName }],
142 natIpAllocateOption: "MANUAL_ONLY",
143 natIps: [egressIp.selfLink.as<string>()],
144 logConfig: { enable: true, filter: "ERRORS_ONLY" },
145 },
146 ],
147 });
148
149 const allowInternal = yield* GCP.Compute.Firewall("AllowInternal", {
150 network: network.networkName,
151 direction: "INGRESS",
152 priority: 1000,
153 sourceRanges: [APP_CIDR, DATA_CIDR, PODS_CIDR, SERVICES_CIDR],
154 allowed: [{ protocol: "tcp" }, { protocol: "udp" }, { protocol: "icmp" }],
155 });
156
157 const denyEgress = yield* GCP.Compute.Firewall("DenyAllEgress", {
158 network: network.networkName,
159 direction: "EGRESS",
160 priority: 65000,
161 destinationRanges: ["0.0.0.0/0"],
162 denied: [{ protocol: "all" }],
163 });
164
165 const allowHttpsEgress = yield* GCP.Compute.Firewall("AllowHttpsEgress", {
166 network: network.networkName,
167 direction: "EGRESS",
168 priority: 1000,
169 destinationRanges: ["0.0.0.0/0"],
170 targetTags: [EGRESS_TAG],
171 allowed: [{ protocol: "tcp", ports: ["443"] }],
172 });
173
174 const route = yield* GCP.Compute.Route("PrivateGoogleApis", {
175 network: network.networkName,
176 destRange: PRIVATE_GOOGLE_APIS,
177 nextHopGateway: "default-internet-gateway",
178 priority: 900,
179 description: "private.googleapis.com",
180 });
181
182 const fn = yield* EgressFunction;
183 const bucket = yield* Results;
184 const topic = yield* Probes;
185
186 return {
187 networkName: network.networkName,
188 appSubnetName: appSubnet.subnetworkName,
189 dataSubnetName: dataSubnet.subnetworkName,
190 routerName: router.routerName,
191 natName: NAT_NAME,
192 addressName: egressIp.addressName,
193 address: egressIp.address,
194 addressSelfLink: egressIp.selfLink,
195 allowInternalName: allowInternal.firewallName,
196 denyEgressName: denyEgress.firewallName,
197 allowHttpsEgressName: allowHttpsEgress.firewallName,
198 routeName: route.routeName,
199 functionName: fn.name,
200 functionUrl: fn.url,
201 bucketName: bucket.bucketName,
202 topicName: topic.name,
203 };
204 });
205
206 const lastSegment = (value: string | undefined) => value?.split("/").at(-1);
207
208 describe.skipIf(!!process.env.FAST).sequential(
209 "Network smoke",
210 {
211 tags: [
212 "provider:gcp",
213 "provider:gcp:compute",
214 "provider:gcp:cloudfunctions",
215 "provider:gcp:pubsub",
216 "provider:gcp:storage",
217 "live",
218 ],
219 },
220 () => {
221 beforeAll(
222 Effect.gen(function* () {
223 yield* Effect.logInfo("Network smoke: destroying previous stack");
224 const held = yield* destroyStack;
225 if (held.length > 0) {
226 yield* Effect.logInfo(
227 `Network smoke: still held by Cloud Run, reconciled in place: ${held.join(", ")}`,
228 );
229 }
230
231 yield* Effect.logInfo("Network smoke: deploying stack");
232 outputs = (yield* sharedStack.deploy(deployProgram)) as StackOutputs;
233 }),
234 { timeout: 900_000 },
235 );
236
237 afterAll(destroyStack, { timeout: 600_000 });
238
239 test.provider(
240 "custom-mode network, subnetworks, and route match the declaration",
241 (_stack) =>
242 Effect.gen(function* () {
243 const { project } = yield* GcpEnvironment.current;
244
245 const network = yield* compute.getNetworks({
246 project,
247 network: outputs.networkName,
248 });
249 expect(network.autoCreateSubnetworks).toBe(false);
250 expect(network.routingConfig?.routingMode).toBe("REGIONAL");
251 expect((network.subnetworks ?? []).map(lastSegment).sort()).toEqual(
252 [outputs.appSubnetName, outputs.dataSubnetName].sort(),
253 );
254
255 const app = yield* compute.getSubnetworks({
256 project,
257 region: REGION,
258 subnetwork: outputs.appSubnetName,
259 });
260 expect(app.ipCidrRange).toBe(APP_CIDR);
261 expect(app.privateIpGoogleAccess).toBe(true);
262 expect(lastSegment(app.network)).toBe(outputs.networkName);
263
264 const data = yield* compute.getSubnetworks({
265 project,
266 region: REGION,
267 subnetwork: outputs.dataSubnetName,
268 });
269 expect(data.ipCidrRange).toBe(DATA_CIDR);
270 expect(
271 (data.secondaryIpRanges ?? [])
272 .map((r) => ({
273 rangeName: r.rangeName,
274 ipCidrRange: r.ipCidrRange,
275 }))
276 .sort((a, b) => a.rangeName!.localeCompare(b.rangeName!)),
277 ).toEqual([
278 { rangeName: "pods", ipCidrRange: PODS_CIDR },
279 { rangeName: "services", ipCidrRange: SERVICES_CIDR },
280 ]);
281
282 const route = yield* compute.getRoutes({
283 project,
284 route: outputs.routeName,
285 });
286 expect(route.destRange).toBe(PRIVATE_GOOGLE_APIS);
287 expect(route.priority).toBe(900);
288 expect(lastSegment(route.network)).toBe(outputs.networkName);
289 expect(lastSegment(route.nextHopGateway)).toBe("default-internet-gateway");
290 }),
291 {
292 tags: ["provider:gcp", "provider:gcp:compute", "live"],
293 timeout: 60_000,
294 },
295 );
296
297 test.provider(
298 "firewall: allow-internal ingress, deny-all egress, tagged tcp:443 override",
299 (_stack) =>
300 Effect.gen(function* () {
301 const { project } = yield* GcpEnvironment.current;
302 const get = (firewall: string) => compute.getFirewalls({ project, firewall });
303
304 const internal = yield* get(outputs.allowInternalName);
305 expect(internal.direction).toBe("INGRESS");
306 expect(lastSegment(internal.network)).toBe(outputs.networkName);
307 expect([...(internal.sourceRanges ?? [])].sort()).toEqual(
308 [APP_CIDR, DATA_CIDR, PODS_CIDR, SERVICES_CIDR].sort(),
309 );
310 expect((internal.allowed ?? []).map((rule) => rule.IPProtocol).sort()).toEqual([
311 "icmp",
312 "tcp",
313 "udp",
314 ]);
315
316 const deny = yield* get(outputs.denyEgressName);
317 expect(deny.direction).toBe("EGRESS");
318 expect(deny.priority).toBe(65000);
319 expect(deny.destinationRanges).toEqual(["0.0.0.0/0"]);
320 expect(deny.denied?.map((rule) => rule.IPProtocol)).toEqual(["all"]);
321
322 const https = yield* get(outputs.allowHttpsEgressName);
323 expect(https.direction).toBe("EGRESS");
324 expect(https.priority).toBe(1000);
325 expect(https.targetTags).toEqual([EGRESS_TAG]);
326 expect(https.allowed).toEqual([{ IPProtocol: "tcp", ports: ["443"] }]);
327 }),
328 {
329 tags: ["provider:gcp", "provider:gcp:compute", "live"],
330 timeout: 60_000,
331 },
332 );
333
334 test.provider(
335 "Cloud Router hosts NAT for the app subnet on the static address",
336 (_stack) =>
337 Effect.gen(function* () {
338 const { project } = yield* GcpEnvironment.current;
339
340 const router = yield* compute.getRouters({
341 project,
342 region: REGION,
343 router: outputs.routerName,
344 });
345 expect(lastSegment(router.network)).toBe(outputs.networkName);
346 expect(router.nats).toHaveLength(1);
347 const nat = router.nats![0]!;
348 expect(nat.name).toBe(outputs.natName);
349 expect(nat.sourceSubnetworkIpRangesToNat).toBe("LIST_OF_SUBNETWORKS");
350 expect(nat.subnetworks?.map((s) => lastSegment(s.name))).toEqual([outputs.appSubnetName]);
351 expect(nat.natIpAllocateOption).toBe("MANUAL_ONLY");
352 expect(nat.natIps?.map(lastSegment)).toEqual([outputs.addressName]);
353 expect(nat.logConfig).toEqual({
354 enable: true,
355 filter: "ERRORS_ONLY",
356 });
357
358 const address = yield* compute.getAddresses({
359 project,
360 region: REGION,
361 address: outputs.addressName,
362 });
363 expect(address.address).toBe(outputs.address);
364 expect(address.addressType).toBe("EXTERNAL");
365 expect(address.status).toBe("IN_USE");
366 }),
367 {
368 tags: ["provider:gcp", "provider:gcp:compute", "live"],
369 timeout: 60_000,
370 },
371 );
372
373 test.provider(
374 "function is internal-only and egresses directly into the app subnet",
375 (_stack) =>
376 Effect.gen(function* () {
377 const fn = yield* cloudfunctions.getProjectsLocationsFunctions({
378 name: outputs.functionName,
379 });
380 expect(fn.state).toBe("ACTIVE");
381 expect(fn.serviceConfig?.ingressSettings).toBe("ALLOW_INTERNAL_ONLY");
382 expect(fn.serviceConfig?.directVpcEgress).toBe("VPC_EGRESS_ALL_TRAFFIC");
383 const nic = fn.serviceConfig?.directVpcNetworkInterface?.[0];
384 expect(lastSegment(nic?.network)).toBe(outputs.networkName);
385 expect(lastSegment(nic?.subnetwork)).toBe(outputs.appSubnetName);
386 expect(nic?.tags).toEqual([EGRESS_TAG]);
387
388 // Internal-only ingress: the public URL refuses outside callers.
389 const response = yield* HttpClient.get(outputs.functionUrl);
390 expect([403, 404]).toContain(response.status);
391 }),
392 {
393 tags: ["provider:gcp", "provider:gcp:compute", "live"],
394 timeout: 60_000,
395 },
396 );
397
398 test.provider(
399 "a Pub/Sub-driven probe egresses through Cloud NAT and the firewall",
400 (_stack) =>
401 Effect.gen(function* () {
402 const published = yield* pubsub.publishProjectsTopics({
403 topic: outputs.topicName,
404 body: { messages: [{ data: btoa("probe") }] },
405 });
406 const messageId = published.messageIds?.[0];
407 expect(messageId).toEqual(expect.any(String));
408 const object = `probe-${messageId}`;
409
410 // Cold start + Direct VPC egress interface attach + push delivery.
411 const probe: ProbeResult = yield* storage
412 .getObjects({ bucket: outputs.bucketName, object })
413 .pipe(
414 Effect.map((found): ProbeResult =>
415 Object.fromEntries(
416 Object.entries(found.metadata ?? {}).filter(
417 (entry): entry is [string, string] => entry[1] !== undefined,
418 ),
419 ),
420 ),
421 Effect.catchTag("NotFound", () => Effect.fail(new ProbeNotRecorded({ object }))),
422 Effect.retry({
423 while: (e) => e._tag === "ProbeNotRecorded",
424 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(36)]),
425 }),
426 );
427
428 expect(probe.ipError).toBeUndefined();
429 // The public IP the internet saw is the NAT's static address.
430 expect(probe.ip).toBe(outputs.address);
431 // tcp:80 falls through to the deny-all egress rule.
432 expect(probe.port80Blocked).toBe("true");
433 }),
434 {
435 tags: ["provider:gcp", "provider:gcp:compute", "live"],
436 timeout: 300_000,
437 },
438 );
439
440 test.provider(
441 "destroy removes the function, NAT router, firewalls, route, subnets, and network",
442 (_stack) =>
443 Effect.gen(function* () {
444 const { project } = yield* GcpEnvironment.current;
445 const held = yield* destroyStack;
446 // Only the function's egress subnet (and its network) may linger.
447 expect(held.every((id) => id === "AppSubnet" || id === "SmokeNetwork")).toBe(true);
448 if (held.includes("AppSubnet")) {
449 // Out-of-band: the only users of the subnet are Cloud Run's
450 // serverless IP reservations.
451 const addresses = yield* compute.listAddresses({
452 project,
453 region: REGION,
454 filter: `subnetwork eq .*/subnetworks/${outputs.appSubnetName}$`,
455 });
456 expect(addresses.items?.length ?? 0).toBeGreaterThan(0);
457 for (const address of addresses.items ?? []) {
458 expect(address.purpose).toBe("SERVERLESS");
459 expect(address.name).toMatch(/^serverless-ipv4-/);
460 }
461 }
462
463 const notFound = () => Effect.succeed(true);
464
465 yield* waitUntilGone(
466 "function",
467 cloudfunctions
468 .getProjectsLocationsFunctions({
469 name: outputs.functionName,
470 })
471 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
472 );
473 yield* waitUntilGone(
474 "router",
475 compute
476 .getRouters({
477 project,
478 region: REGION,
479 router: outputs.routerName,
480 })
481 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
482 );
483 yield* waitUntilGone(
484 "address",
485 compute
486 .getAddresses({
487 project,
488 region: REGION,
489 address: outputs.addressName,
490 })
491 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
492 );
493 for (const firewall of [
494 outputs.allowInternalName,
495 outputs.denyEgressName,
496 outputs.allowHttpsEgressName,
497 ]) {
498 yield* waitUntilGone(
499 `firewall ${firewall}`,
500 compute
501 .getFirewalls({ project, firewall })
502 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
503 );
504 }
505 yield* waitUntilGone(
506 "route",
507 compute
508 .getRoutes({ project, route: outputs.routeName })
509 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
510 );
511 for (const subnetwork of [
512 ...(held.includes("AppSubnet") ? [] : [outputs.appSubnetName]),
513 outputs.dataSubnetName,
514 ]) {
515 yield* waitUntilGone(
516 `subnetwork ${subnetwork}`,
517 compute
518 .getSubnetworks({ project, region: REGION, subnetwork })
519 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
520 );
521 }
522 if (!held.includes("SmokeNetwork")) {
523 yield* waitUntilGone(
524 "network",
525 compute
526 .getNetworks({ project, network: outputs.networkName })
527 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
528 );
529 }
530 yield* waitUntilGone(
531 "bucket",
532 storage
533 .getBuckets({ bucket: outputs.bucketName })
534 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
535 );
536 yield* waitUntilGone(
537 "topic",
538 pubsub
539 .getProjectsTopics({ topic: outputs.topicName })
540 .pipe(Effect.as(false), Effect.catchTag("NotFound", notFound)),
541 );
542 }),
543 {
544 tags: ["provider:gcp", "provider:gcp:compute", "live"],
545 timeout: 600_000,
546 },
547 );
548 },
549 );