The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolve4, resolveNs, Resolver } from "node:dns/promises"; |
| 2 | /** |
| 3 | * `domain.dns` on a Fly website, deployed for real: the DNS adapter |
| 4 | * publishes the Fly Certificate's ACME challenge `CNAME` and the site's |
| 5 | * `A` / `AAAA` records, Fly issues the certificate, and the custom hostname |
| 6 | * serves over HTTPS. |
| 7 | */ |
| 8 | import * as AWS from "@alchemy.run/aws"; |
| 9 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 10 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 11 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 12 | import * as Fly from "@alchemy.run/fly"; |
| 13 | import { expect } from "@alchemy.run/test"; |
| 14 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 15 | import * as route53 from "@distilled.cloud/aws/route-53"; |
| 16 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 17 | import * as machines from "@distilled.cloud/fly-io/machines"; |
| 18 | import * as Output from "alchemy/Output"; |
| 19 | import * as Data from "effect/Data"; |
| 20 | import * as Effect from "effect/Effect"; |
| 21 | import * as Layer from "effect/Layer"; |
| 22 | import * as Path from "effect/Path"; |
| 23 | import { MinimumLogLevel } from "effect/References"; |
| 24 | import * as Schedule from "effect/Schedule"; |
| 25 | import * as Stream from "effect/Stream"; |
| 26 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 27 | import { assertAppGone, getText } from "./fixtures/deployment.ts"; |
| 28 | |
| 29 | const { test } = Test.make({ |
| 30 | providers: Layer.mergeAll(Fly.providers(), Cloudflare.providers(), AWS.providers()), |
| 31 | }); |
| 32 | |
| 33 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 34 | |
| 35 | const ZONE = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 36 | const CLOUDFLARE_HOST = `fly-dns.${ZONE}`; |
| 37 | const ROUTE53_ZONE = `fly-dns-r53.${ZONE}`; |
| 38 | const ROUTE53_HOST = ROUTE53_ZONE; |
| 39 | /** Route 53 always assigns four name servers to a public hosted zone. */ |
| 40 | const ROUTE53_NAME_SERVER_COUNT = 4; |
| 41 | |
| 42 | const normalize = (name: string | null | undefined) => |
| 43 | (name ?? "").replace(/\.$/, "").toLowerCase(); |
| 44 | |
| 45 | class NotPublished extends Data.TaggedError("NotPublished")<{ |
| 46 | readonly server: string; |
| 47 | readonly name: string; |
| 48 | }> {} |
| 49 | |
| 50 | class CertificateNotIssued extends Data.TaggedError("CertificateNotIssued")<{ |
| 51 | readonly hostname: string; |
| 52 | readonly status: string | undefined; |
| 53 | }> {} |
| 54 | |
| 55 | const cloudflareZoneId = Effect.gen(function* () { |
| 56 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 57 | const zone = yield* findZoneByName({ accountId, name: ZONE }); |
| 58 | if (!zone) { |
| 59 | return yield* Effect.die(new Error(`zone "${ZONE}" not found in account`)); |
| 60 | } |
| 61 | return zone.id; |
| 62 | }); |
| 63 | |
| 64 | const cloudflareRecords = (zoneId: string, name: string, type: "A" | "AAAA" | "CNAME" | "NS") => |
| 65 | dns.listRecords.items({ zoneId, name: { exact: name }, type }).pipe( |
| 66 | Stream.runCollect, |
| 67 | Effect.map((chunk) => Array.from(chunk)), |
| 68 | ); |
| 69 | |
| 70 | const route53Records = (hostedZoneId: string) => |
| 71 | route53 |
| 72 | .listResourceRecordSets({ HostedZoneId: hostedZoneId, MaxItems: 100 }) |
| 73 | .pipe(Effect.map((response) => response.ResourceRecordSets ?? [])); |
| 74 | |
| 75 | const assertHostedZoneGone = (hostedZoneId: string) => |
| 76 | route53.getHostedZone({ Id: hostedZoneId }).pipe( |
| 77 | Effect.as(false), |
| 78 | Effect.catchTag("NoSuchHostedZone", () => Effect.succeed(true)), |
| 79 | Effect.repeat({ |
| 80 | until: (gone) => gone, |
| 81 | times: 10, |
| 82 | schedule: Schedule.spaced("2 seconds"), |
| 83 | }), |
| 84 | Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))), |
| 85 | ); |
| 86 | |
| 87 | /** |
| 88 | * Wait until every authoritative name server answers `name` with the |
| 89 | * `expected` A records, so the first HTTPS request never resolves (and |
| 90 | * negatively caches) the hostname before it exists. |
| 91 | */ |
| 92 | const waitForAuthoritativeA = ( |
| 93 | nameServers: readonly string[], |
| 94 | name: string, |
| 95 | expected: readonly string[], |
| 96 | ) => |
| 97 | Effect.gen(function* () { |
| 98 | const servers = (yield* Effect.forEach(nameServers, (ns) => |
| 99 | Effect.tryPromise(() => resolve4(normalize(ns))).pipe(Effect.orDie), |
| 100 | )).flat(); |
| 101 | yield* Effect.forEach(servers, (server) => |
| 102 | Effect.gen(function* () { |
| 103 | const resolver = yield* Effect.sync(() => { |
| 104 | const r = new Resolver(); |
| 105 | r.setServers([server]); |
| 106 | return r; |
| 107 | }); |
| 108 | const answers = yield* Effect.tryPromise(() => resolver.resolve4(name)).pipe( |
| 109 | Effect.orElseSucceed(() => [] as string[]), |
| 110 | ); |
| 111 | if (!expected.every((ip) => answers.includes(ip))) { |
| 112 | return yield* Effect.fail(new NotPublished({ server, name })); |
| 113 | } |
| 114 | }).pipe(Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 })), |
| 115 | ); |
| 116 | }); |
| 117 | |
| 118 | /** |
| 119 | * Fly issues the ACME certificate once its challenge `CNAME` resolves; |
| 120 | * `checkAppCertificate` re-runs validation on each poll. |
| 121 | */ |
| 122 | const waitForIssuedCertificate = (appName: string, hostname: string) => |
| 123 | machines.checkAppCertificate({ app_name: appName, hostname }).pipe( |
| 124 | Effect.flatMap((checked) => |
| 125 | (checked.certificates ?? []).some( |
| 126 | (certificate) => certificate.source === "fly" && certificate.status === "active", |
| 127 | ) |
| 128 | ? Effect.succeed(checked) |
| 129 | : Effect.fail(new CertificateNotIssued({ hostname, status: checked.status })), |
| 130 | ), |
| 131 | Effect.retry({ |
| 132 | while: (error) => error._tag === "CertificateNotIssued", |
| 133 | schedule: Schedule.spaced("10 seconds"), |
| 134 | times: 30, |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | const serveOverHttps = (hostname: string) => |
| 139 | getText(`https://${hostname}/`).pipe( |
| 140 | Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 24 }), |
| 141 | ); |
| 142 | |
| 143 | const cloneSite = Effect.gen(function* () { |
| 144 | const path = yield* Path.Path; |
| 145 | return yield* cloneFixture( |
| 146 | path.resolve(import.meta.dirname, "../../Cloudflare/Website/staticsite-fixture"), |
| 147 | { |
| 148 | prefix: "alchemy-fly-dns-", |
| 149 | tempRoot: path.resolve(import.meta.dirname, "../../../.tmp"), |
| 150 | entries: ["src", "build.sh"], |
| 151 | }, |
| 152 | ); |
| 153 | }); |
| 154 | |
| 155 | const siteProps = (path: string) => ({ |
| 156 | path, |
| 157 | build: { command: "bash build.sh", output: "dist" }, |
| 158 | memo: { include: ["src/**", "build.sh"] }, |
| 159 | }); |
| 160 | |
| 161 | const tags = [ |
| 162 | "provider:fly", |
| 163 | "provider:fly:certificate", |
| 164 | "provider:fly:website", |
| 165 | "provider:cloudflare", |
| 166 | "provider:cloudflare:dns", |
| 167 | "live", |
| 168 | ]; |
| 169 | |
| 170 | test.provider( |
| 171 | "Cloudflare DNS: ACME challenge CNAME + A/AAAA published, certificate issued, HTTPS 200, records removed on destroy", |
| 172 | (stack) => |
| 173 | Effect.gen(function* () { |
| 174 | yield* stack.destroy(); |
| 175 | const zoneId = yield* cloudflareZoneId; |
| 176 | const cwd = yield* cloneSite; |
| 177 | |
| 178 | yield* Effect.gen(function* () { |
| 179 | const { site } = yield* stack.deploy( |
| 180 | Effect.gen(function* () { |
| 181 | const site = yield* Fly.Website.StaticSite("Site", { |
| 182 | ...siteProps(cwd), |
| 183 | domain: { |
| 184 | name: CLOUDFLARE_HOST, |
| 185 | dns: Cloudflare.DNS.Adapter(), |
| 186 | }, |
| 187 | }); |
| 188 | return { site }; |
| 189 | }), |
| 190 | ); |
| 191 | const appName = site.app!.appName; |
| 192 | expect(site.url).toBe(`https://${CLOUDFLARE_HOST}`); |
| 193 | const requirements = site.certificate!.dnsRequirements; |
| 194 | const challenge = requirements?.acmeChallenge; |
| 195 | expect(challenge?.target).toEqual(expect.any(String)); |
| 196 | const ip = site.ip!.ip; |
| 197 | |
| 198 | // The ACME challenge CNAME Fly lists, DNS-only. |
| 199 | const [cname] = yield* cloudflareRecords( |
| 200 | zoneId, |
| 201 | `_acme-challenge.${CLOUDFLARE_HOST}`, |
| 202 | "CNAME", |
| 203 | ); |
| 204 | expect(normalize(cname?.content)).toBe(normalize(challenge!.target)); |
| 205 | expect(cname?.proxied).toBe(false); |
| 206 | |
| 207 | // A at the shared IPv4, AAAA at every IPv6 Fly lists. |
| 208 | const a = yield* cloudflareRecords(zoneId, CLOUDFLARE_HOST, "A"); |
| 209 | expect(a.map((record) => record.content)).toEqual([ip]); |
| 210 | const aaaa = yield* cloudflareRecords(zoneId, CLOUDFLARE_HOST, "AAAA"); |
| 211 | expect(aaaa.map((record) => record.content).sort()).toEqual( |
| 212 | [...(requirements?.aaaa ?? [])].sort(), |
| 213 | ); |
| 214 | |
| 215 | yield* waitForIssuedCertificate(appName, CLOUDFLARE_HOST); |
| 216 | |
| 217 | const nameServers = yield* Effect.tryPromise(() => resolveNs(ZONE)).pipe(Effect.orDie); |
| 218 | yield* waitForAuthoritativeA(nameServers, CLOUDFLARE_HOST, [ip]); |
| 219 | expect(yield* serveOverHttps(CLOUDFLARE_HOST)).toContain("StaticSite fixture v1"); |
| 220 | |
| 221 | yield* stack.destroy(); |
| 222 | expect( |
| 223 | yield* cloudflareRecords(zoneId, `_acme-challenge.${CLOUDFLARE_HOST}`, "CNAME"), |
| 224 | ).toEqual([]); |
| 225 | expect(yield* cloudflareRecords(zoneId, CLOUDFLARE_HOST, "A")).toEqual([]); |
| 226 | expect(yield* cloudflareRecords(zoneId, CLOUDFLARE_HOST, "AAAA")).toEqual([]); |
| 227 | yield* assertAppGone(appName); |
| 228 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 229 | }).pipe(logLevel), |
| 230 | // Image build + Machine boot + ACME issuance through public DNS. |
| 231 | { tags, timeout: 900_000 }, |
| 232 | ); |
| 233 | |
| 234 | test.provider( |
| 235 | "Route 53 DNS: records published in a delegated hosted zone, certificate issued, HTTPS 200", |
| 236 | (stack) => |
| 237 | Effect.gen(function* () { |
| 238 | yield* stack.destroy(); |
| 239 | const zoneId = yield* cloudflareZoneId; |
| 240 | const cwd = yield* cloneSite; |
| 241 | |
| 242 | yield* Effect.gen(function* () { |
| 243 | const { site, zone } = yield* stack.deploy( |
| 244 | Effect.gen(function* () { |
| 245 | const zone = yield* AWS.Route53.HostedZone("Zone", { |
| 246 | name: ROUTE53_ZONE, |
| 247 | forceDestroy: true, |
| 248 | }); |
| 249 | // Delegate the hosted zone from the Cloudflare zone. |
| 250 | for (let index = 0; index < ROUTE53_NAME_SERVER_COUNT; index++) { |
| 251 | yield* Cloudflare.DNS.Record(`Delegation${index}`, { |
| 252 | zoneId, |
| 253 | name: ROUTE53_ZONE, |
| 254 | type: "NS", |
| 255 | content: Output.map(zone.nameServers, (nameServers) => nameServers[index]!), |
| 256 | }); |
| 257 | } |
| 258 | const site = yield* Fly.Website.StaticSite("Site", { |
| 259 | ...siteProps(cwd), |
| 260 | domain: { |
| 261 | name: ROUTE53_HOST, |
| 262 | dns: AWS.Route53.Adapter({ hostedZoneId: zone.id }), |
| 263 | }, |
| 264 | }); |
| 265 | return { site, zone }; |
| 266 | }), |
| 267 | ); |
| 268 | const appName = site.app!.appName; |
| 269 | expect(site.url).toBe(`https://${ROUTE53_HOST}`); |
| 270 | expect(zone.nameServers).toHaveLength(ROUTE53_NAME_SERVER_COUNT); |
| 271 | const requirements = site.certificate!.dnsRequirements; |
| 272 | const challenge = requirements?.acmeChallenge; |
| 273 | expect(challenge?.target).toEqual(expect.any(String)); |
| 274 | const ip = site.ip!.ip; |
| 275 | |
| 276 | // The delegation is live in Cloudflare. |
| 277 | const delegation = yield* cloudflareRecords(zoneId, ROUTE53_ZONE, "NS"); |
| 278 | expect(delegation.map((record) => normalize(record.content)).sort()).toEqual( |
| 279 | zone.nameServers.map(normalize).sort(), |
| 280 | ); |
| 281 | |
| 282 | // The records live in Route 53, not Cloudflare. |
| 283 | const sets = yield* route53Records(zone.id); |
| 284 | const find = (name: string, type: string) => |
| 285 | sets.find((set) => normalize(set.Name) === name && set.Type === type); |
| 286 | expect( |
| 287 | find(`_acme-challenge.${ROUTE53_HOST}`, "CNAME")?.ResourceRecords?.map((record) => |
| 288 | normalize(record.Value), |
| 289 | ), |
| 290 | ).toEqual([normalize(challenge!.target)]); |
| 291 | expect(find(ROUTE53_HOST, "A")?.ResourceRecords?.map((record) => record.Value)).toEqual([ |
| 292 | ip, |
| 293 | ]); |
| 294 | expect(yield* cloudflareRecords(zoneId, ROUTE53_HOST, "A")).toEqual([]); |
| 295 | |
| 296 | yield* waitForIssuedCertificate(appName, ROUTE53_HOST); |
| 297 | |
| 298 | yield* waitForAuthoritativeA(zone.nameServers, ROUTE53_HOST, [ip]); |
| 299 | expect(yield* serveOverHttps(ROUTE53_HOST)).toContain("StaticSite fixture v1"); |
| 300 | |
| 301 | yield* stack.destroy(); |
| 302 | yield* assertHostedZoneGone(zone.id); |
| 303 | expect(yield* cloudflareRecords(zoneId, ROUTE53_ZONE, "NS")).toEqual([]); |
| 304 | yield* assertAppGone(appName); |
| 305 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 306 | }).pipe(logLevel), |
| 307 | // Image build + Machine boot + ACME issuance through a delegated zone. |
| 308 | { tags: [...tags, "provider:aws", "provider:aws:route53"], timeout: 900_000 }, |
| 309 | ); |