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

main
ts 449 lines 16.9 KB
Raw
1 import { resolve4, resolveNs, Resolver } from "node:dns/promises";
2 /**
3 * `AWS.ECS.Service` load-balancer custom domains, served live over HTTPS:
4 *
5 * - `domain.dns: Cloudflare.DNS.Adapter()` — ACM validated through
6 * Cloudflare, each name a Cloudflare CNAME to the ALB.
7 * - the Route 53 default (no `dns`) on a Route 53 zone publicly delegated
8 * from the standing Cloudflare test zone — alias `A` + `AAAA` records.
9 *
10 * Skipped under --fast: an ALB, ACM issuance and a Fargate task
11 * take minutes. Both tests share one ECS cluster (deployed once per file)
12 * and the default VPC's per-AZ subnets.
13 */
14 import * as AWS from "@alchemy.run/aws";
15 import { Cluster } from "@alchemy.run/aws/ECS/Cluster";
16 import { Service } from "@alchemy.run/aws/ECS/Service";
17 import { HostedZone } from "@alchemy.run/aws/Route53";
18 import * as Cloudflare from "@alchemy.run/cloudflare";
19 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
20 import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup";
21 import { describe, expect } from "@alchemy.run/test";
22 import * as Test from "@alchemy.run/test/Alchemy";
23 import * as acm from "@distilled.cloud/aws/acm";
24 import * as ec2 from "@distilled.cloud/aws/ec2";
25 import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2";
26 import * as route53 from "@distilled.cloud/aws/route-53";
27 import * as dns from "@distilled.cloud/cloudflare/dns";
28 import * as Alchemy from "alchemy";
29 import * as Output from "alchemy/Output";
30 import { isResourceState, State } from "alchemy/State";
31 import * as Data from "effect/Data";
32 import * as Effect from "effect/Effect";
33 import * as HttpClient from "effect/http/HttpClient";
34 import * as Layer from "effect/Layer";
35 import * as Schedule from "effect/Schedule";
36 import * as Stream from "effect/Stream";
37 import { getDefaultVpcNetwork } from "../DefaultVpc.ts";
38
39 const providers = Layer.mergeAll(AWS.providers(), Cloudflare.providers());
40 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
41 providers,
42 });
43
44 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
45
46 // Cloudflare-hosted names.
47 const CF_NAME = `ecs-dns.${zoneName}`;
48 const CF_ALIAS = `www.ecs-dns.${zoneName}`;
49 // Route 53 zone delegated from the Cloudflare zone, and the served name.
50 const R53_ZONE = `ecs-dns-r53.${zoneName}`;
51 const R53_NAME = `api.${R53_ZONE}`;
52
53 /** Logical id prefix the service gives each domain name's records. */
54 const domainRecordId = (name: string) => `Domain-${name.replaceAll(/[^a-zA-Z0-9-]/g, "-")}`;
55
56 class DnsNotPublished extends Data.TaggedError("DnsNotPublished")<{
57 readonly name: string;
58 readonly server: string;
59 }> {}
60
61 class NotServing extends Data.TaggedError("NotServing")<{
62 readonly url: string;
63 readonly status: number;
64 }> {}
65
66 class RecordsStillPresent extends Data.TaggedError("RecordsStillPresent")<{
67 readonly name: string;
68 readonly count: number;
69 }> {}
70
71 const resolveCloudflareZoneId = Effect.gen(function* () {
72 const { accountId } = yield* yield* CloudflareEnvironment;
73 const zone = yield* findZoneByName({ accountId, name: zoneName });
74 if (!zone) {
75 return yield* Effect.die(new Error(`zone "${zoneName}" not found`));
76 }
77 return zone.id;
78 });
79
80 /** The default VPC and one default subnet per AZ (an ALB rejects two per AZ). */
81 const defaultNetwork = Effect.gen(function* () {
82 const net = yield* getDefaultVpcNetwork;
83 const subnets = yield* ec2
84 .describeSubnets({
85 Filters: [
86 { Name: "vpc-id", Values: [net.vpcId] },
87 { Name: "default-for-az", Values: ["true"] },
88 ],
89 })
90 .pipe(Effect.map((r) => (r.Subnets ?? []).flatMap((s) => (s.SubnetId ? [s.SubnetId] : []))));
91 return { vpcId: net.vpcId as string, subnets };
92 });
93
94 /** An echo server behind the service's ALB, answering `text` on `/`. */
95 const echoService = (text: string) =>
96 Effect.gen(function* () {
97 const network = yield* defaultNetwork;
98 return {
99 image: "hashicorp/http-echo",
100 command: [`-text=${text}`],
101 port: 5678,
102 desiredCount: 1,
103 vpcId: network.vpcId,
104 subnets: network.subnets,
105 // Default-VPC public subnets: the task pulls from ECR without a NAT.
106 assignPublicIp: true,
107 };
108 });
109
110 /**
111 * Wait until every authoritative nameserver answers `check` for `name`,
112 * so the first HTTPS request never resolves (and negatively caches) the
113 * name before it is published.
114 */
115 const waitForAuthoritative = (
116 nameServers: readonly string[],
117 name: string,
118 check: (resolver: Resolver) => Promise<boolean>,
119 ) =>
120 Effect.gen(function* () {
121 const servers = (yield* Effect.forEach(nameServers, (ns) =>
122 Effect.tryPromise(() => resolve4(ns)).pipe(Effect.orDie),
123 )).flat();
124 yield* Effect.forEach(servers, (server) =>
125 Effect.gen(function* () {
126 const resolver = yield* Effect.sync(() => {
127 const r = new Resolver();
128 r.setServers([server]);
129 return r;
130 });
131 const published = yield* Effect.tryPromise(() => check(resolver)).pipe(
132 Effect.orElseSucceed(() => false),
133 );
134 if (!published) {
135 return yield* Effect.fail(new DnsNotPublished({ name, server }));
136 }
137 }).pipe(Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 40 })),
138 );
139 });
140
141 /** GET `https://{name}/` until it answers 200 with `text` (bounded). */
142 const expectServes = (name: string, text: string) =>
143 Effect.gen(function* () {
144 const client = yield* HttpClient.HttpClient;
145 const url = `https://${name}/`;
146 // ALB provisioning, Fargate start and health checks — the happy path
147 // exits on the first 200.
148 const body = yield* client.get(url).pipe(
149 Effect.flatMap((res): Effect.Effect<string, unknown> =>
150 res.status === 200 ? res.text : Effect.fail(new NotServing({ url, status: res.status })),
151 ),
152 Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 84 }),
153 );
154 expect(body).toContain(text);
155 });
156
157 const albDnsName = (loadBalancerArn: string) =>
158 elbv2
159 .describeLoadBalancers({ LoadBalancerArns: [loadBalancerArn] })
160 .pipe(
161 Effect.map((r) => (r.LoadBalancers?.[0]?.DNSName ?? "").replace(/\.$/, "").toLowerCase()),
162 );
163
164 const loadBalancerGone = (loadBalancerArn: string) =>
165 elbv2.describeLoadBalancers({ LoadBalancerArns: [loadBalancerArn] }).pipe(
166 Effect.map((r) => (r.LoadBalancers ?? []).length === 0),
167 Effect.catchTag("LoadBalancerNotFoundException", () => Effect.succeed(true)),
168 );
169
170 /** The HTTPS listener's certificate: ISSUED, for `names`, in `region`. */
171 const expectIssuedCertificate = (
172 loadBalancerArn: string,
173 names: readonly string[],
174 region: string,
175 ) =>
176 Effect.gen(function* () {
177 const listeners = yield* elbv2.describeListeners({
178 LoadBalancerArn: loadBalancerArn,
179 });
180 const https = (listeners.Listeners ?? []).find((l) => l.Port === 443);
181 expect(https?.Protocol).toBe("HTTPS");
182 const certificateArn = https?.Certificates?.[0]?.CertificateArn;
183 expect(certificateArn).toBeDefined();
184 expect(certificateArn!.split(":")[3]).toBe(region);
185 const { Certificate } = yield* acm.describeCertificate({
186 CertificateArn: certificateArn!,
187 });
188 expect(Certificate?.Status).toBe("ISSUED");
189 expect(Certificate?.DomainName).toBe(names[0]);
190 expect([...(Certificate?.SubjectAlternativeNames ?? [])].sort()).toEqual([...names].sort());
191 });
192
193 /** Persisted state rows of the scratch stack, keyed by FQN. */
194 const stateRows = (stack: Test.ScratchStack) =>
195 Effect.gen(function* () {
196 const state = yield* yield* State;
197 const fqns = yield* state.list({ stack: stack.name, stage: stack.stage });
198 const rows = yield* Effect.forEach(fqns, (fqn) =>
199 state
200 .get({ stack: stack.name, stage: stack.stage, fqn })
201 .pipe(Effect.map((row) => [fqn, row] as const)),
202 );
203 return new Map(
204 rows.flatMap(([fqn, row]) =>
205 isResourceState(row) ? [[fqn, row.resourceType] as const] : [],
206 ),
207 );
208 });
209
210 // One cluster for the whole file.
211 const ClusterStack = Alchemy.Stack(
212 "EcsDnsCluster",
213 { providers: AWS.providers(), state: Alchemy.localState() },
214 Effect.gen(function* () {
215 const cluster = yield* Cluster("Cluster", {
216 clusterName: "alchemy-test-ecs-dns",
217 });
218 return { clusterArn: cluster.clusterArn };
219 }),
220 );
221
222 describe.skipIf(!!process.env.FAST)(
223 "AWS.ECS.Service custom domain DNS (live)",
224 {
225 tags: [
226 "provider:aws",
227 "provider:aws:acm",
228 "provider:aws:ecs",
229 "provider:aws:elbv2",
230 "provider:aws:route53",
231 "provider:cloudflare",
232 "provider:cloudflare:dns",
233 "live",
234 ],
235 },
236 () => {
237 const shared = beforeAll(deploy(ClusterStack));
238 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(ClusterStack), {
239 timeout: 300_000,
240 });
241
242 test.provider(
243 "Cloudflare DNS: ACM validated through Cloudflare, both names CNAME to the ALB and serve HTTPS",
244 (stack) =>
245 Effect.gen(function* () {
246 yield* stack.destroy();
247 const { clusterArn } = yield* shared;
248 const zoneId = yield* resolveCloudflareZoneId;
249 const base = yield* echoService("ecs-dns-cloudflare");
250
251 const deployed = yield* stack.deploy(
252 Effect.gen(function* () {
253 const service = yield* Service("CfSvc", {
254 ...base,
255 cluster: clusterArn,
256 loadBalancer: {
257 domain: {
258 name: CF_NAME,
259 aliases: [CF_ALIAS],
260 dns: Cloudflare.DNS.Adapter(),
261 },
262 },
263 });
264 return {
265 url: service.url.as<string>(),
266 serviceArn: service.serviceArn.as<string>(),
267 loadBalancerArn: service.loadBalancerArn.as<string>(),
268 };
269 }),
270 );
271 expect(deployed.url).toBe(`https://${CF_NAME}`);
272
273 // Each name is a DNS-only Cloudflare CNAME to the ALB.
274 const albDns = yield* albDnsName(deployed.loadBalancerArn);
275 expect(albDns).not.toBe("");
276 const listCnames = (name: string) =>
277 dns.listRecords.items({ zoneId, name: { exact: name }, type: "CNAME" }).pipe(
278 Stream.runCollect,
279 Effect.map((chunk) => Array.from(chunk)),
280 );
281 for (const name of [CF_NAME, CF_ALIAS]) {
282 const cnames = yield* listCnames(name);
283 expect(cnames).toHaveLength(1);
284 expect(cnames[0]!.content?.toLowerCase()).toBe(albDns);
285 expect(cnames[0]!.proxied).toBe(false);
286 }
287 const rows = yield* stateRows(stack);
288 for (const name of [CF_NAME, CF_ALIAS]) {
289 expect(rows.get(`CfSvc/${domainRecordId(name)}-CNAME`)).toBe("Cloudflare.DNS.Records");
290 }
291
292 // The certificate is issued in the service's region.
293 yield* expectIssuedCertificate(
294 deployed.loadBalancerArn,
295 [CF_NAME, CF_ALIAS],
296 deployed.serviceArn.split(":")[3]!,
297 );
298
299 // HTTPS 200 on both names through the ALB.
300 const cloudflareNs = yield* Effect.tryPromise(() => resolveNs(zoneName)).pipe(
301 Effect.orDie,
302 );
303 for (const name of [CF_NAME, CF_ALIAS]) {
304 yield* waitForAuthoritative(cloudflareNs, name, (resolver) =>
305 resolver
306 .resolveCname(name)
307 .then((answers) =>
308 answers.some((a) => a.replace(/\.$/, "").toLowerCase() === albDns),
309 ),
310 );
311 yield* expectServes(name, "ecs-dns-cloudflare");
312 }
313
314 // Destroy removes the ALB and both CNAMEs.
315 yield* stack.destroy();
316 expect(yield* loadBalancerGone(deployed.loadBalancerArn)).toBe(true);
317 for (const name of [CF_NAME, CF_ALIAS]) {
318 yield* listCnames(name).pipe(
319 Effect.flatMap((cnames) =>
320 cnames.length === 0
321 ? Effect.void
322 : Effect.fail(new RecordsStillPresent({ name, count: cnames.length })),
323 ),
324 Effect.retry({
325 schedule: Schedule.spaced("2 seconds"),
326 times: 10,
327 }),
328 );
329 }
330 }),
331 { timeout: 1_200_000 },
332 );
333
334 test.provider(
335 "Route 53 default: alias A + AAAA records in a delegated zone serve HTTPS",
336 (stack) =>
337 Effect.gen(function* () {
338 yield* stack.destroy();
339 const { clusterArn } = yield* shared;
340 const cloudflareZoneId = yield* resolveCloudflareZoneId;
341 const base = yield* echoService("ecs-dns-route53");
342
343 const program = (includeService: boolean) =>
344 Effect.gen(function* () {
345 // ACM's validation CNAME is retained in the zone by design
346 // (reused across certificates), so the zone is never empty.
347 const zone = yield* HostedZone("Zone", {
348 name: R53_ZONE,
349 forceDestroy: true,
350 });
351 // Delegate the sub-zone publicly. One NS record: several
352 // `Cloudflare.DNS.Record`s sharing (name, type) would adopt
353 // each other's record instead of creating siblings.
354 yield* Cloudflare.DNS.Record("Delegation", {
355 zoneId: cloudflareZoneId,
356 name: R53_ZONE,
357 type: "NS",
358 content: Output.map(zone.nameServers, (ns) => ns[0]!),
359 ttl: 60,
360 });
361 const outputs = {
362 zoneId: zone.id.as<string>(),
363 nameServers: zone.nameServers.as<string[]>(),
364 };
365 if (!includeService) {
366 return { ...outputs, service: undefined };
367 }
368 // No `dns`: the Route 53 default, whose zone lookup runs while
369 // the program is built — so the zone is deployed first.
370 const service = yield* Service("R53Svc", {
371 ...base,
372 cluster: clusterArn,
373 loadBalancer: { domain: { name: R53_NAME } },
374 });
375 return {
376 ...outputs,
377 service: {
378 url: service.url.as<string>(),
379 serviceArn: service.serviceArn.as<string>(),
380 loadBalancerArn: service.loadBalancerArn.as<string>(),
381 },
382 };
383 });
384
385 yield* stack.deploy(program(false));
386 const deployed = yield* stack.deploy(program(true));
387 const service = deployed.service!;
388 const hostedZoneId = deployed.zoneId.replace(/^\/hostedzone\//, "");
389 expect(service.url).toBe(`https://${R53_NAME}`);
390
391 // Out-of-band: alias A + AAAA records to the ALB.
392 const albDns = yield* albDnsName(service.loadBalancerArn);
393 const recordSets = yield* route53.listResourceRecordSets({
394 HostedZoneId: hostedZoneId,
395 });
396 const aliases = (recordSets.ResourceRecordSets ?? []).filter(
397 (record) => record.Name === `${R53_NAME}.` && record.AliasTarget,
398 );
399 expect(aliases.map((r) => r.Type).sort()).toEqual(["A", "AAAA"]);
400 for (const record of aliases) {
401 // Route 53 may prefix ELB alias targets with `dualstack.`.
402 expect(record.AliasTarget!.DNSName!.replace(/\.$/, "").toLowerCase()).toMatch(
403 new RegExp(`^(dualstack\\.)?${albDns.replaceAll(".", "\\.")}$`),
404 );
405 }
406 // The logical ids predate DNS adapters: `Domain-{name}-A/AAAA`.
407 const rows = yield* stateRows(stack);
408 for (const type of ["A", "AAAA"]) {
409 expect(rows.get(`R53Svc/${domainRecordId(R53_NAME)}-${type}`)).toBe(
410 "AWS.Route53.Record",
411 );
412 }
413
414 yield* expectIssuedCertificate(
415 service.loadBalancerArn,
416 [R53_NAME],
417 service.serviceArn.split(":")[3]!,
418 );
419
420 // HTTPS 200 through the delegation.
421 yield* waitForAuthoritative(deployed.nameServers, R53_NAME, (resolver) =>
422 resolver.resolve4(R53_NAME).then((ips) => ips.length > 0),
423 );
424 yield* expectServes(R53_NAME, "ecs-dns-route53");
425
426 // Destroy removes the zone (and its records) and the delegation.
427 yield* stack.destroy();
428 expect(yield* loadBalancerGone(service.loadBalancerArn)).toBe(true);
429 const zoneGone = yield* route53.getHostedZone({ Id: hostedZoneId }).pipe(
430 Effect.map(() => false),
431 Effect.catchTag("NoSuchHostedZone", () => Effect.succeed(true)),
432 );
433 expect(zoneGone).toBe(true);
434 const delegation = yield* dns.listRecords
435 .items({
436 zoneId: cloudflareZoneId,
437 name: { exact: R53_ZONE },
438 type: "NS",
439 })
440 .pipe(
441 Stream.runCollect,
442 Effect.map((chunk) => Array.from(chunk)),
443 );
444 expect(delegation).toHaveLength(0);
445 }),
446 { timeout: 1_200_000 },
447 );
448 },
449 );