The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolve4, Resolver } from "node:dns/promises"; |
| 2 | /** |
| 3 | * The DNS adapter contract (`alchemy/DNS`) against its three built-in hosts. |
| 4 | * |
| 5 | * Every test deploys a real stack whose program resolves an adapter with |
| 6 | * `DNS.resolve(config)` and calls its functions (`alias`, `aliasSet`, |
| 7 | * `records`), then verifies the published records out-of-band through the |
| 8 | * host's distilled SDK and — for the Route 53 sub-zone (delegated from the |
| 9 | * Cloudflare test zone) and the Hetzner zone — by querying the zone's |
| 10 | * authoritative nameservers directly. |
| 11 | */ |
| 12 | import * as AWS from "@alchemy.run/aws"; |
| 13 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 14 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 15 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 16 | import * as Hetzner from "@alchemy.run/hetzner"; |
| 17 | import { describe, expect } from "@alchemy.run/test"; |
| 18 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 19 | import * as route53 from "@distilled.cloud/aws/route-53"; |
| 20 | import * as cfdns from "@distilled.cloud/cloudflare/dns"; |
| 21 | import * as HetznerErrors from "@distilled.cloud/hetzner"; |
| 22 | import * as zoneRrsets from "@distilled.cloud/hetzner/zone_rrsets"; |
| 23 | import * as hetznerZones from "@distilled.cloud/hetzner/zones"; |
| 24 | import * as DNS from "alchemy/DNS"; |
| 25 | import * as Output from "alchemy/Output"; |
| 26 | import * as Cause from "effect/Cause"; |
| 27 | import * as Data from "effect/Data"; |
| 28 | import * as Effect from "effect/Effect"; |
| 29 | import * as Layer from "effect/Layer"; |
| 30 | import * as Schedule from "effect/Schedule"; |
| 31 | import * as Stream from "effect/Stream"; |
| 32 | |
| 33 | const { test } = Test.make({ |
| 34 | providers: Layer.mergeAll(Cloudflare.providers(), AWS.providers(), Hetzner.providers()), |
| 35 | }); |
| 36 | |
| 37 | // A stack WITHOUT the Hetzner (or AWS) host registered. |
| 38 | const cloudflareOnly = Test.make({ providers: Cloudflare.providers() }); |
| 39 | |
| 40 | const CF_ZONE = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 41 | |
| 42 | const TARGET_1 = "example.net"; |
| 43 | const TARGET_2 = "example.org"; |
| 44 | const TXT_1 = "dns-adapter=v1"; |
| 45 | const TXT_2 = "dns-adapter=v2"; |
| 46 | |
| 47 | const hasHetznerCreds = !!process.env.HCLOUD_TOKEN; |
| 48 | |
| 49 | // --------------------------------------------------------------------------- |
| 50 | // Shared helpers |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
| 53 | type RecordType = "A" | "AAAA" | "CNAME" | "TXT"; |
| 54 | |
| 55 | /** Lowercase, no trailing dot, TXT unquoted. */ |
| 56 | const normalize = (value: string) => value.replace(/^"|"$/g, "").replace(/\.$/, "").toLowerCase(); |
| 57 | |
| 58 | const sorted = (values: readonly string[]) => [...values].map(normalize).sort(); |
| 59 | |
| 60 | const resolveCloudflareZoneId = Effect.gen(function* () { |
| 61 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 62 | const zone = yield* findZoneByName({ accountId, name: CF_ZONE }); |
| 63 | if (!zone) { |
| 64 | return yield* Effect.die(new Error(`zone "${CF_ZONE}" not found`)); |
| 65 | } |
| 66 | return zone.id; |
| 67 | }); |
| 68 | |
| 69 | /** Cloudflare records at `(name, type)` (the harness's fresh token may 403 briefly). */ |
| 70 | const cloudflareRecords = (zoneId: string, name: string, type: RecordType | "NS") => |
| 71 | cfdns.listRecords.items({ zoneId, name: { exact: name }, type }).pipe( |
| 72 | Stream.filter((r) => normalize(r.name) === name && r.type === type), |
| 73 | Stream.runCollect, |
| 74 | Effect.map((chunk) => Array.from(chunk)), |
| 75 | Effect.retry({ |
| 76 | while: (e) => e._tag === "Forbidden", |
| 77 | schedule: Schedule.exponential("500 millis"), |
| 78 | times: 8, |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | const cloudflareValues = (zoneId: string, name: string, type: RecordType) => |
| 83 | cloudflareRecords(zoneId, name, type).pipe( |
| 84 | Effect.map((records) => sorted(records.map((r) => r.content ?? ""))), |
| 85 | ); |
| 86 | |
| 87 | /** |
| 88 | * Delegate `name` from the standing Cloudflare zone to a sub-zone's |
| 89 | * nameservers — one `NS` record per nameserver. |
| 90 | */ |
| 91 | const delegate = <Req>( |
| 92 | cfZoneId: string, |
| 93 | name: string, |
| 94 | nameServers: Output.Output<string[], Req>, |
| 95 | count: number, |
| 96 | ) => |
| 97 | Effect.forEach( |
| 98 | Array.from({ length: count }, (_, index) => index), |
| 99 | (index) => |
| 100 | Cloudflare.DNS.Record(`Delegation${index + 1}`, { |
| 101 | zoneId: cfZoneId, |
| 102 | name, |
| 103 | type: "NS", |
| 104 | content: Output.map(nameServers, (servers) => normalize(servers[index]!)), |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | class DnsQueryFailed extends Data.TaggedError("DnsQueryFailed")<{ |
| 109 | readonly code: string; |
| 110 | readonly server: string; |
| 111 | readonly name: string; |
| 112 | }> {} |
| 113 | |
| 114 | class AnswerMismatch extends Data.TaggedError("AnswerMismatch")<{ |
| 115 | readonly server: string; |
| 116 | readonly name: string; |
| 117 | readonly type: RecordType; |
| 118 | readonly answer: string[]; |
| 119 | readonly expected: string[]; |
| 120 | }> {} |
| 121 | |
| 122 | /** IPv4 addresses of a zone's nameservers. */ |
| 123 | const nameServerAddresses = (nameServers: readonly string[]) => |
| 124 | Effect.forEach(nameServers, (ns) => |
| 125 | Effect.tryPromise(() => resolve4(normalize(ns))).pipe( |
| 126 | Effect.retry({ schedule: Schedule.spaced("1 second"), times: 5 }), |
| 127 | Effect.orDie, |
| 128 | ), |
| 129 | ).pipe(Effect.map((addresses) => addresses.flat())); |
| 130 | |
| 131 | /** One non-recursive-style query against a single authoritative server. */ |
| 132 | const queryServer = (server: string, name: string, type: RecordType) => |
| 133 | Effect.gen(function* () { |
| 134 | const resolver = yield* Effect.sync(() => { |
| 135 | const r = new Resolver({ timeout: 3_000, tries: 1 }); |
| 136 | r.setServers([server]); |
| 137 | return r; |
| 138 | }); |
| 139 | const answer = yield* Effect.tryPromise({ |
| 140 | try: (): Promise<string[] | string[][]> => |
| 141 | type === "A" |
| 142 | ? resolver.resolve4(name) |
| 143 | : type === "AAAA" |
| 144 | ? resolver.resolve6(name) |
| 145 | : type === "CNAME" |
| 146 | ? resolver.resolveCname(name) |
| 147 | : resolver.resolveTxt(name), |
| 148 | catch: (error) => |
| 149 | new DnsQueryFailed({ |
| 150 | code: error instanceof Error && "code" in error ? String(error.code) : "UNKNOWN", |
| 151 | server, |
| 152 | name, |
| 153 | }), |
| 154 | }).pipe( |
| 155 | // No record (or no name) is an answer: the empty set. |
| 156 | Effect.catchIf( |
| 157 | (e) => e.code === "ENODATA" || e.code === "ENOTFOUND", |
| 158 | () => Effect.succeed([] as string[]), |
| 159 | ), |
| 160 | ); |
| 161 | return sorted(answer.map((v) => (Array.isArray(v) ? v.join("") : v))); |
| 162 | }); |
| 163 | |
| 164 | /** |
| 165 | * Wait until EVERY authoritative server answers `(name, type)` with exactly |
| 166 | * `expected` (the empty list = no such record). |
| 167 | */ |
| 168 | const waitForAuthoritative = ( |
| 169 | servers: readonly string[], |
| 170 | name: string, |
| 171 | type: RecordType, |
| 172 | expected: readonly string[], |
| 173 | ) => |
| 174 | Effect.forEach( |
| 175 | servers, |
| 176 | (server) => |
| 177 | queryServer(server, name, type).pipe( |
| 178 | Effect.flatMap((answer) => |
| 179 | JSON.stringify(answer) === JSON.stringify(sorted(expected)) |
| 180 | ? Effect.void |
| 181 | : Effect.fail( |
| 182 | new AnswerMismatch({ |
| 183 | server, |
| 184 | name, |
| 185 | type, |
| 186 | answer, |
| 187 | expected: sorted(expected), |
| 188 | }), |
| 189 | ), |
| 190 | ), |
| 191 | Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 30 }), |
| 192 | ), |
| 193 | { concurrency: "unbounded", discard: true }, |
| 194 | ); |
| 195 | |
| 196 | /** The value of type `T` a deploy died (or failed) with, if any. */ |
| 197 | const failureOf = <A, E, R, T>( |
| 198 | deploy: Effect.Effect<A, E, R>, |
| 199 | cls: abstract new (...args: never[]) => T, |
| 200 | ) => |
| 201 | deploy.pipe( |
| 202 | Effect.as(undefined), |
| 203 | Effect.catchCause((cause) => |
| 204 | Effect.succeed( |
| 205 | cause.reasons |
| 206 | .map((reason) => |
| 207 | Cause.isFailReason(reason) |
| 208 | ? reason.error |
| 209 | : Cause.isDieReason(reason) |
| 210 | ? reason.defect |
| 211 | : undefined, |
| 212 | ) |
| 213 | .find((value): value is T => value instanceof cls), |
| 214 | ), |
| 215 | ), |
| 216 | ); |
| 217 | |
| 218 | // --------------------------------------------------------------------------- |
| 219 | // Cloudflare |
| 220 | // --------------------------------------------------------------------------- |
| 221 | |
| 222 | const CF = { |
| 223 | cname: `dns-adapter-cname.${CF_ZONE}`, |
| 224 | addresses: `dns-adapter-addr.${CF_ZONE}`, |
| 225 | proxied: `dns-adapter-proxied.${CF_ZONE}`, |
| 226 | setA: `dns-adapter-set-a.${CF_ZONE}`, |
| 227 | setB: `dns-adapter-set-b.${CF_ZONE}`, |
| 228 | txt: `dns-adapter-txt.${CF_ZONE}`, |
| 229 | recordCname: `dns-adapter-rec-cname.${CF_ZONE}`, |
| 230 | }; |
| 231 | |
| 232 | const cloudflareProgram = (version: 1 | 2) => |
| 233 | Effect.gen(function* () { |
| 234 | const dns = yield* DNS.resolve(Cloudflare.DNS.Adapter({ zone: CF_ZONE })); |
| 235 | const proxied = yield* DNS.resolve(Cloudflare.DNS.Adapter({ zone: CF_ZONE, proxied: true })); |
| 236 | const target = version === 1 ? TARGET_1 : TARGET_2; |
| 237 | |
| 238 | yield* dns.alias("Cname", { name: CF.cname, target: { hostname: target } }); |
| 239 | yield* dns.alias("Addresses", { |
| 240 | name: CF.addresses, |
| 241 | target: |
| 242 | version === 1 ? { ipv4: ["192.0.2.10"], ipv6: ["2001:db8::10"] } : { ipv4: ["192.0.2.11"] }, |
| 243 | }); |
| 244 | yield* proxied.alias("Proxied", { |
| 245 | name: CF.proxied, |
| 246 | target: { hostname: TARGET_1 }, |
| 247 | }); |
| 248 | const set = yield* dns.aliasSet("Set", { |
| 249 | names: version === 1 ? [CF.setA] : [], |
| 250 | target: { hostname: target }, |
| 251 | }); |
| 252 | yield* set.bind`BoundSite`({ names: [CF.setB] }); |
| 253 | yield* dns.records("Records", { |
| 254 | records: |
| 255 | version === 1 |
| 256 | ? [ |
| 257 | { name: CF.txt, type: "TXT", value: TXT_1 }, |
| 258 | { name: CF.recordCname, type: "CNAME", value: TARGET_1 }, |
| 259 | ] |
| 260 | : [{ name: CF.txt, type: "TXT", value: TXT_2 }], |
| 261 | }); |
| 262 | }); |
| 263 | |
| 264 | describe("Cloudflare.DNS adapter", () => { |
| 265 | test.provider( |
| 266 | "alias, aliasSet (+ bound names) and records converge in Cloudflare", |
| 267 | (stack) => |
| 268 | Effect.gen(function* () { |
| 269 | const zoneId = yield* resolveCloudflareZoneId; |
| 270 | yield* stack.destroy(); |
| 271 | |
| 272 | yield* stack.deploy(cloudflareProgram(1)); |
| 273 | |
| 274 | const cname = yield* cloudflareRecords(zoneId, CF.cname, "CNAME"); |
| 275 | expect(sorted(cname.map((r) => r.content ?? ""))).toEqual([TARGET_1]); |
| 276 | expect(cname[0]?.proxied).toBe(false); |
| 277 | expect(yield* cloudflareValues(zoneId, CF.addresses, "A")).toEqual(["192.0.2.10"]); |
| 278 | expect(yield* cloudflareValues(zoneId, CF.addresses, "AAAA")).toEqual(["2001:db8::10"]); |
| 279 | const proxiedCname = yield* cloudflareRecords(zoneId, CF.proxied, "CNAME"); |
| 280 | expect(proxiedCname).toHaveLength(1); |
| 281 | expect(proxiedCname[0]?.proxied).toBe(true); |
| 282 | for (const name of [CF.setA, CF.setB]) { |
| 283 | const [record, ...extra] = yield* cloudflareRecords(zoneId, name, "CNAME"); |
| 284 | expect(extra).toHaveLength(0); |
| 285 | expect(normalize(record?.content ?? "")).toBe(TARGET_1); |
| 286 | expect(record?.proxied).toBe(false); |
| 287 | } |
| 288 | expect(yield* cloudflareValues(zoneId, CF.txt, "TXT")).toEqual([TXT_1]); |
| 289 | const recordCname = yield* cloudflareRecords(zoneId, CF.recordCname, "CNAME"); |
| 290 | expect(sorted(recordCname.map((r) => r.content ?? ""))).toEqual([TARGET_1]); |
| 291 | expect(recordCname[0]?.proxied).toBe(false); |
| 292 | |
| 293 | // Change targets, drop a declared set name, an address and a record. |
| 294 | yield* stack.deploy(cloudflareProgram(2)); |
| 295 | |
| 296 | expect(yield* cloudflareValues(zoneId, CF.cname, "CNAME")).toEqual([TARGET_2]); |
| 297 | expect(yield* cloudflareValues(zoneId, CF.addresses, "A")).toEqual(["192.0.2.11"]); |
| 298 | expect(yield* cloudflareValues(zoneId, CF.addresses, "AAAA")).toEqual([]); |
| 299 | expect(yield* cloudflareValues(zoneId, CF.setA, "CNAME")).toEqual([]); |
| 300 | expect(yield* cloudflareValues(zoneId, CF.setB, "CNAME")).toEqual([TARGET_2]); |
| 301 | expect(yield* cloudflareValues(zoneId, CF.txt, "TXT")).toEqual([TXT_2]); |
| 302 | expect(yield* cloudflareValues(zoneId, CF.recordCname, "CNAME")).toEqual([]); |
| 303 | |
| 304 | yield* stack.destroy(); |
| 305 | |
| 306 | for (const name of Object.values(CF)) { |
| 307 | for (const type of ["A", "AAAA", "CNAME", "TXT"] as const) { |
| 308 | expect(yield* cloudflareValues(zoneId, name, type)).toEqual([]); |
| 309 | } |
| 310 | } |
| 311 | }), |
| 312 | { |
| 313 | tags: ["provider:dns", "provider:cloudflare:dns", "live"], |
| 314 | timeout: 180_000, |
| 315 | }, |
| 316 | ); |
| 317 | }); |
| 318 | |
| 319 | // --------------------------------------------------------------------------- |
| 320 | // Route 53 — a hosted zone delegated from the Cloudflare test zone |
| 321 | // --------------------------------------------------------------------------- |
| 322 | |
| 323 | const R53_ZONE = `dns-adapter-r53.${CF_ZONE}`; |
| 324 | const R53 = { |
| 325 | origin: `origin.${R53_ZONE}`, |
| 326 | www: `www.${R53_ZONE}`, |
| 327 | cname: `cname.${R53_ZONE}`, |
| 328 | setA: `set-a.${R53_ZONE}`, |
| 329 | setB: `set-b.${R53_ZONE}`, |
| 330 | txt: `txt.${R53_ZONE}`, |
| 331 | recordCname: `rec-cname.${R53_ZONE}`, |
| 332 | }; |
| 333 | |
| 334 | const route53Program = (cfZoneId: string, version: 1 | 2) => |
| 335 | Effect.gen(function* () { |
| 336 | const zone = yield* AWS.Route53.HostedZone("Zone", { name: R53_ZONE }); |
| 337 | yield* delegate(cfZoneId, R53_ZONE, zone.nameServers, 4); |
| 338 | |
| 339 | const dns = yield* DNS.resolve(AWS.Route53.Adapter({ hostedZoneId: zone.id })); |
| 340 | const target = version === 1 ? TARGET_1 : TARGET_2; |
| 341 | |
| 342 | // An address target: the Route 53 adapter declares an |
| 343 | // `AWS.Route53.RecordList`, which is also the alias target below. |
| 344 | const origin = (yield* dns.alias("Origin", { |
| 345 | name: R53.origin, |
| 346 | target: { |
| 347 | ipv4: [version === 1 ? "192.0.2.20" : "192.0.2.21"], |
| 348 | ipv6: ["2001:db8::20"], |
| 349 | }, |
| 350 | })) as AWS.Route53.RecordList; |
| 351 | |
| 352 | // A dual-stack Route 53 alias at a record in the same zone — a real |
| 353 | // AWS alias target that needs no billable resource. |
| 354 | yield* dns.alias("Www", { |
| 355 | name: R53.www, |
| 356 | ipv6: true, |
| 357 | target: { |
| 358 | hostname: Output.map(origin.records, () => R53.origin), |
| 359 | route53Alias: { hostedZoneId: zone.id, evaluateTargetHealth: false }, |
| 360 | }, |
| 361 | }); |
| 362 | yield* dns.alias("Cname", { |
| 363 | name: R53.cname, |
| 364 | target: { hostname: target }, |
| 365 | }); |
| 366 | const set = yield* dns.aliasSet("Set", { |
| 367 | names: version === 1 ? [R53.setA] : [], |
| 368 | target: { hostname: target }, |
| 369 | }); |
| 370 | yield* set.bind`BoundSite`({ names: [R53.setB] }); |
| 371 | yield* dns.records("Records", { |
| 372 | records: |
| 373 | version === 1 |
| 374 | ? [ |
| 375 | { name: R53.txt, type: "TXT", value: TXT_1 }, |
| 376 | { name: R53.recordCname, type: "CNAME", value: TARGET_1 }, |
| 377 | ] |
| 378 | : [{ name: R53.txt, type: "TXT", value: TXT_2 }], |
| 379 | }); |
| 380 | return zone; |
| 381 | }); |
| 382 | |
| 383 | /** The simple record set `(name, type)` in a hosted zone, if any. */ |
| 384 | const route53Set = (hostedZoneId: string, name: string, type: RecordType) => |
| 385 | route53 |
| 386 | .listResourceRecordSets({ |
| 387 | HostedZoneId: hostedZoneId, |
| 388 | StartRecordName: `${name}.`, |
| 389 | StartRecordType: type, |
| 390 | MaxItems: 1, |
| 391 | }) |
| 392 | .pipe( |
| 393 | Effect.map((response) => |
| 394 | (response.ResourceRecordSets ?? []).find( |
| 395 | (set) => normalize(set.Name) === name && set.Type === type, |
| 396 | ), |
| 397 | ), |
| 398 | ); |
| 399 | |
| 400 | const route53Values = (hostedZoneId: string, name: string, type: RecordType) => |
| 401 | route53Set(hostedZoneId, name, type).pipe( |
| 402 | Effect.map((set) => sorted((set?.ResourceRecords ?? []).map((r) => r.Value))), |
| 403 | ); |
| 404 | |
| 405 | const waitUntilHostedZoneGone = (hostedZoneId: string) => |
| 406 | route53.getHostedZone({ Id: hostedZoneId }).pipe( |
| 407 | Effect.as("present" as const), |
| 408 | Effect.catchTag("NoSuchHostedZone", () => Effect.succeed("gone" as const)), |
| 409 | Effect.repeat({ |
| 410 | schedule: Schedule.spaced("2 seconds"), |
| 411 | until: (status) => status === "gone", |
| 412 | times: 10, |
| 413 | }), |
| 414 | ); |
| 415 | |
| 416 | describe("AWS.Route53 adapter", () => { |
| 417 | test.provider( |
| 418 | "alias (Route 53 alias, CNAME, addresses), aliasSet and records converge in a delegated hosted zone", |
| 419 | (stack) => |
| 420 | Effect.gen(function* () { |
| 421 | const cfZoneId = yield* resolveCloudflareZoneId; |
| 422 | yield* stack.destroy(); |
| 423 | |
| 424 | const zone = yield* stack.deploy(route53Program(cfZoneId, 1)); |
| 425 | const zoneId = zone.id; |
| 426 | |
| 427 | // Publicly delegated from the Cloudflare zone. |
| 428 | expect( |
| 429 | sorted((yield* cloudflareRecords(cfZoneId, R53_ZONE, "NS")).map((r) => r.content ?? "")), |
| 430 | ).toEqual(sorted(zone.nameServers)); |
| 431 | |
| 432 | // Route 53 state. |
| 433 | const www = yield* route53Set(zoneId, R53.www, "A"); |
| 434 | expect(normalize(www?.AliasTarget?.DNSName ?? "")).toBe(R53.origin); |
| 435 | expect(www?.AliasTarget?.HostedZoneId).toBe(zoneId); |
| 436 | expect( |
| 437 | normalize((yield* route53Set(zoneId, R53.www, "AAAA"))?.AliasTarget?.DNSName ?? ""), |
| 438 | ).toBe(R53.origin); |
| 439 | expect(yield* route53Values(zoneId, R53.origin, "A")).toEqual(["192.0.2.20"]); |
| 440 | expect(yield* route53Values(zoneId, R53.origin, "AAAA")).toEqual(["2001:db8::20"]); |
| 441 | for (const name of [R53.cname, R53.setA, R53.setB, R53.recordCname]) { |
| 442 | expect(yield* route53Values(zoneId, name, "CNAME")).toEqual([TARGET_1]); |
| 443 | } |
| 444 | expect(yield* route53Values(zoneId, R53.txt, "TXT")).toEqual([TXT_1]); |
| 445 | |
| 446 | // Authoritative answers from the zone's own nameservers. |
| 447 | const servers = yield* nameServerAddresses(zone.nameServers); |
| 448 | yield* waitForAuthoritative(servers, R53.www, "A", ["192.0.2.20"]); |
| 449 | yield* waitForAuthoritative(servers, R53.www, "AAAA", ["2001:db8::20"]); |
| 450 | yield* waitForAuthoritative(servers, R53.cname, "CNAME", [TARGET_1]); |
| 451 | yield* waitForAuthoritative(servers, R53.setB, "CNAME", [TARGET_1]); |
| 452 | yield* waitForAuthoritative(servers, R53.txt, "TXT", [TXT_1]); |
| 453 | |
| 454 | // Change targets and addresses, drop a declared set name and a record. |
| 455 | yield* stack.deploy(route53Program(cfZoneId, 2)); |
| 456 | |
| 457 | expect(yield* route53Values(zoneId, R53.origin, "A")).toEqual(["192.0.2.21"]); |
| 458 | expect(yield* route53Values(zoneId, R53.cname, "CNAME")).toEqual([TARGET_2]); |
| 459 | expect(yield* route53Values(zoneId, R53.setA, "CNAME")).toEqual([]); |
| 460 | expect(yield* route53Values(zoneId, R53.setB, "CNAME")).toEqual([TARGET_2]); |
| 461 | expect(yield* route53Values(zoneId, R53.txt, "TXT")).toEqual([TXT_2]); |
| 462 | expect(yield* route53Values(zoneId, R53.recordCname, "CNAME")).toEqual([]); |
| 463 | // The alias follows its target's new address. |
| 464 | yield* waitForAuthoritative(servers, R53.www, "A", ["192.0.2.21"]); |
| 465 | yield* waitForAuthoritative(servers, R53.cname, "CNAME", [TARGET_2]); |
| 466 | yield* waitForAuthoritative(servers, R53.setA, "CNAME", []); |
| 467 | yield* waitForAuthoritative(servers, R53.txt, "TXT", [TXT_2]); |
| 468 | yield* waitForAuthoritative(servers, R53.recordCname, "CNAME", []); |
| 469 | |
| 470 | yield* stack.destroy(); |
| 471 | |
| 472 | expect(yield* waitUntilHostedZoneGone(zoneId)).toBe("gone"); |
| 473 | expect(yield* cloudflareRecords(cfZoneId, R53_ZONE, "NS")).toEqual([]); |
| 474 | }), |
| 475 | { |
| 476 | tags: ["provider:dns", "provider:aws:route53", "live"], |
| 477 | timeout: 360_000, |
| 478 | }, |
| 479 | ); |
| 480 | }); |
| 481 | |
| 482 | // --------------------------------------------------------------------------- |
| 483 | // Hetzner — Hetzner DNS only hosts registrable domains (a sub-zone such as |
| 484 | // `dns-adapter-hz.alchemy-test-2.us` is rejected with `unsupported tld`, see |
| 485 | // the probe below), so the zone can't be delegated from the Cloudflare test |
| 486 | // zone. Hetzner's authoritative nameservers answer for every zone they host, |
| 487 | // delegated or not, so the records are verified against them directly. |
| 488 | // --------------------------------------------------------------------------- |
| 489 | |
| 490 | const HZ_SUBZONE = `dns-adapter-hz.${CF_ZONE}`; |
| 491 | const HZ_ZONE = "dns-adapter-hz-alchemy-test.us"; |
| 492 | const HZ = { |
| 493 | cname: `cname.${HZ_ZONE}`, |
| 494 | addresses: `addr.${HZ_ZONE}`, |
| 495 | setA: `set-a.${HZ_ZONE}`, |
| 496 | setB: `set-b.${HZ_ZONE}`, |
| 497 | txt: `txt.${HZ_ZONE}`, |
| 498 | recordCname: `rec-cname.${HZ_ZONE}`, |
| 499 | }; |
| 500 | |
| 501 | const hetznerProgram = (version: 1 | 2) => |
| 502 | Effect.gen(function* () { |
| 503 | const zone = yield* Hetzner.Zone("Zone", { name: HZ_ZONE, ttl: 60 }); |
| 504 | const dns = yield* DNS.resolve(Hetzner.DNS.Adapter({ zone })); |
| 505 | const target = version === 1 ? TARGET_1 : TARGET_2; |
| 506 | |
| 507 | yield* dns.alias("Cname", { name: HZ.cname, target: { hostname: target } }); |
| 508 | yield* dns.alias("Addresses", { |
| 509 | name: HZ.addresses, |
| 510 | target: |
| 511 | version === 1 ? { ipv4: ["192.0.2.30"], ipv6: ["2001:db8::30"] } : { ipv4: ["192.0.2.31"] }, |
| 512 | }); |
| 513 | const set = yield* dns.aliasSet("Set", { |
| 514 | names: version === 1 ? [HZ.setA] : [], |
| 515 | target: { hostname: target }, |
| 516 | }); |
| 517 | yield* set.bind`BoundSite`({ names: [HZ.setB] }); |
| 518 | yield* dns.records("Records", { |
| 519 | records: |
| 520 | version === 1 |
| 521 | ? [ |
| 522 | { name: HZ.txt, type: "TXT", value: TXT_1 }, |
| 523 | { name: HZ.recordCname, type: "CNAME", value: TARGET_1 }, |
| 524 | ] |
| 525 | : [{ name: HZ.txt, type: "TXT", value: TXT_2 }], |
| 526 | }); |
| 527 | return zone; |
| 528 | }); |
| 529 | |
| 530 | /** Live values of the RRSet `(name, type)`, normalized and sorted. */ |
| 531 | const hetznerValues = (zoneId: number, name: string, type: RecordType) => |
| 532 | zoneRrsets |
| 533 | .getZoneRrset({ |
| 534 | id_or_name: String(zoneId), |
| 535 | rr_name: name === HZ_ZONE ? "@" : name.slice(0, -(HZ_ZONE.length + 1)), |
| 536 | rr_type: type, |
| 537 | }) |
| 538 | .pipe( |
| 539 | Effect.map(({ rrset }) => sorted(rrset.records.map((r) => r.value))), |
| 540 | Effect.catchTag("NotFound", () => Effect.succeed([] as string[])), |
| 541 | ); |
| 542 | |
| 543 | const waitUntilHetznerZoneGone = (zoneId: number) => |
| 544 | hetznerZones.getZone({ id_or_name: String(zoneId) }).pipe( |
| 545 | Effect.as("found" as const), |
| 546 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 547 | Effect.repeat({ |
| 548 | schedule: Schedule.spaced("1 second"), |
| 549 | until: (status) => status === "gone", |
| 550 | times: 10, |
| 551 | }), |
| 552 | ); |
| 553 | |
| 554 | describe("Hetzner.DNS adapter", () => { |
| 555 | test.provider.skipIf(!hasHetznerCreds)( |
| 556 | "alias, aliasSet (+ bound names) and records converge in a Hetzner zone", |
| 557 | (stack) => |
| 558 | Effect.gen(function* () { |
| 559 | yield* stack.destroy(); |
| 560 | |
| 561 | const zone = yield* stack.deploy(hetznerProgram(1)); |
| 562 | const zoneId = zone.zoneId; |
| 563 | |
| 564 | expect(yield* hetznerValues(zoneId, HZ.cname, "CNAME")).toEqual([TARGET_1]); |
| 565 | expect(yield* hetznerValues(zoneId, HZ.addresses, "A")).toEqual(["192.0.2.30"]); |
| 566 | expect(yield* hetznerValues(zoneId, HZ.addresses, "AAAA")).toEqual(["2001:db8::30"]); |
| 567 | for (const name of [HZ.setA, HZ.setB, HZ.recordCname]) { |
| 568 | expect(yield* hetznerValues(zoneId, name, "CNAME")).toEqual([TARGET_1]); |
| 569 | } |
| 570 | expect(yield* hetznerValues(zoneId, HZ.txt, "TXT")).toEqual([TXT_1]); |
| 571 | |
| 572 | const servers = yield* nameServerAddresses(zone.assignedNameservers); |
| 573 | yield* waitForAuthoritative(servers, HZ.cname, "CNAME", [TARGET_1]); |
| 574 | yield* waitForAuthoritative(servers, HZ.addresses, "A", ["192.0.2.30"]); |
| 575 | yield* waitForAuthoritative(servers, HZ.setB, "CNAME", [TARGET_1]); |
| 576 | yield* waitForAuthoritative(servers, HZ.txt, "TXT", [TXT_1]); |
| 577 | |
| 578 | yield* stack.deploy(hetznerProgram(2)); |
| 579 | |
| 580 | expect(yield* hetznerValues(zoneId, HZ.cname, "CNAME")).toEqual([TARGET_2]); |
| 581 | expect(yield* hetznerValues(zoneId, HZ.addresses, "A")).toEqual(["192.0.2.31"]); |
| 582 | expect(yield* hetznerValues(zoneId, HZ.addresses, "AAAA")).toEqual([]); |
| 583 | expect(yield* hetznerValues(zoneId, HZ.setA, "CNAME")).toEqual([]); |
| 584 | expect(yield* hetznerValues(zoneId, HZ.setB, "CNAME")).toEqual([TARGET_2]); |
| 585 | expect(yield* hetznerValues(zoneId, HZ.txt, "TXT")).toEqual([TXT_2]); |
| 586 | expect(yield* hetznerValues(zoneId, HZ.recordCname, "CNAME")).toEqual([]); |
| 587 | yield* waitForAuthoritative(servers, HZ.cname, "CNAME", [TARGET_2]); |
| 588 | yield* waitForAuthoritative(servers, HZ.addresses, "A", ["192.0.2.31"]); |
| 589 | yield* waitForAuthoritative(servers, HZ.setA, "CNAME", []); |
| 590 | yield* waitForAuthoritative(servers, HZ.txt, "TXT", [TXT_2]); |
| 591 | |
| 592 | yield* stack.destroy(); |
| 593 | |
| 594 | expect(yield* waitUntilHetznerZoneGone(zoneId)).toBe("gone"); |
| 595 | }), |
| 596 | { |
| 597 | tags: ["provider:dns", "provider:hetzner:dns", "live"], |
| 598 | timeout: 300_000, |
| 599 | }, |
| 600 | ); |
| 601 | |
| 602 | test.provider.skipIf(!hasHetznerCreds)( |
| 603 | "a CNAME alias at the pinned zone apex dies with DnsAdapterError", |
| 604 | (stack) => |
| 605 | Effect.gen(function* () { |
| 606 | yield* stack.destroy(); |
| 607 | |
| 608 | const alias = yield* failureOf( |
| 609 | stack.deploy( |
| 610 | Effect.gen(function* () { |
| 611 | const dns = yield* DNS.resolve(Hetzner.DNS.Adapter({ zone: HZ_ZONE })); |
| 612 | yield* dns.alias("Apex", { |
| 613 | name: `${HZ_ZONE.toUpperCase()}.`, |
| 614 | target: { hostname: TARGET_1 }, |
| 615 | }); |
| 616 | }), |
| 617 | ), |
| 618 | DNS.DnsAdapterError, |
| 619 | ); |
| 620 | expect(alias).toBeInstanceOf(DNS.DnsAdapterError); |
| 621 | expect(alias?.message).toContain("zone apex"); |
| 622 | |
| 623 | const aliasSet = yield* failureOf( |
| 624 | stack.deploy( |
| 625 | Effect.gen(function* () { |
| 626 | const dns = yield* DNS.resolve(Hetzner.DNS.Adapter({ zone: HZ_ZONE })); |
| 627 | yield* dns.aliasSet("Set", { |
| 628 | names: [`www.${HZ_ZONE}`, HZ_ZONE], |
| 629 | target: { hostname: TARGET_1 }, |
| 630 | }); |
| 631 | }), |
| 632 | ), |
| 633 | DNS.DnsAdapterError, |
| 634 | ); |
| 635 | expect(aliasSet).toBeInstanceOf(DNS.DnsAdapterError); |
| 636 | |
| 637 | yield* stack.destroy(); |
| 638 | }), |
| 639 | { tags: ["provider:dns", "provider:hetzner:dns", "live"] }, |
| 640 | ); |
| 641 | |
| 642 | test.provider.skipIf(!hasHetznerCreds)( |
| 643 | "Hetzner DNS rejects a sub-zone of the Cloudflare test zone (why the zone is not delegated)", |
| 644 | (stack) => |
| 645 | Effect.gen(function* () { |
| 646 | yield* stack.destroy(); |
| 647 | |
| 648 | const failure = yield* failureOf( |
| 649 | stack.deploy(Hetzner.Zone("SubZone", { name: HZ_SUBZONE })), |
| 650 | HetznerErrors.UnprocessableEntity, |
| 651 | ); |
| 652 | expect(failure).toBeInstanceOf(HetznerErrors.UnprocessableEntity); |
| 653 | expect(failure?.message).toContain("unsupported tld"); |
| 654 | |
| 655 | yield* stack.destroy(); |
| 656 | }), |
| 657 | { tags: ["provider:dns", "provider:hetzner:dns", "live"] }, |
| 658 | ); |
| 659 | }); |
| 660 | |
| 661 | // --------------------------------------------------------------------------- |
| 662 | // Unregistered DNS hosts |
| 663 | // --------------------------------------------------------------------------- |
| 664 | |
| 665 | describe("DNS.resolve", () => { |
| 666 | cloudflareOnly.test.provider( |
| 667 | "a deploy naming a DNS host whose providers() are not in the stack dies with DnsAdapterNotRegistered", |
| 668 | (stack) => |
| 669 | Effect.gen(function* () { |
| 670 | yield* stack.destroy(); |
| 671 | |
| 672 | const failure = yield* failureOf( |
| 673 | stack.deploy( |
| 674 | Effect.gen(function* () { |
| 675 | const dns = yield* DNS.resolve(Hetzner.DNS.Adapter({ zone: HZ_ZONE })); |
| 676 | yield* dns.records("Records", { |
| 677 | records: [{ name: HZ.txt, type: "TXT", value: TXT_1 }], |
| 678 | }); |
| 679 | }), |
| 680 | ), |
| 681 | DNS.DnsAdapterNotRegistered, |
| 682 | ); |
| 683 | expect(failure).toBeInstanceOf(DNS.DnsAdapterNotRegistered); |
| 684 | expect(failure?.type).toBe("Hetzner.DNS"); |
| 685 | expect(failure?.message).toContain("Hetzner.providers()"); |
| 686 | |
| 687 | yield* stack.destroy(); |
| 688 | }), |
| 689 | { tags: ["provider:dns", "live"] }, |
| 690 | ); |
| 691 | |
| 692 | test.provider( |
| 693 | "a deploy naming an unknown DNS host type dies with DnsAdapterNotRegistered", |
| 694 | (stack) => |
| 695 | Effect.gen(function* () { |
| 696 | yield* stack.destroy(); |
| 697 | |
| 698 | const failure = yield* failureOf( |
| 699 | stack.deploy( |
| 700 | Effect.gen(function* () { |
| 701 | const dns = yield* DNS.resolve({ type: "Nope.DNS" }); |
| 702 | yield* dns.records("Records", { |
| 703 | records: [{ name: CF.txt, type: "TXT", value: TXT_1 }], |
| 704 | }); |
| 705 | }), |
| 706 | ), |
| 707 | DNS.DnsAdapterNotRegistered, |
| 708 | ); |
| 709 | expect(failure?.type).toBe("Nope.DNS"); |
| 710 | expect(failure?.message).toContain("Nope.providers()"); |
| 711 | |
| 712 | yield* stack.destroy(); |
| 713 | }), |
| 714 | { tags: ["provider:dns", "live"] }, |
| 715 | ); |
| 716 | }); |