The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Predicate from "effect/Predicate"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | |
| 13 | const TypeId = "Cloudflare.DNS.ZoneTransferTsig" as const; |
| 14 | type TypeId = typeof TypeId; |
| 15 | |
| 16 | export interface ZoneTransferTsigProps { |
| 17 | /** |
| 18 | * TSIG key name. If omitted, a unique name is generated from the app, |
| 19 | * stage, and logical ID. |
| 20 | * |
| 21 | * Mutable — updated in place (PUT). |
| 22 | * @default ${app}-${stage}-${id} |
| 23 | */ |
| 24 | name?: string; |
| 25 | /** |
| 26 | * TSIG algorithm (e.g. `hmac-sha512.`). |
| 27 | * |
| 28 | * Mutable — updated in place (PUT). |
| 29 | */ |
| 30 | algo: string; |
| 31 | /** |
| 32 | * TSIG secret (base64-encoded key material). Kept redacted — it is |
| 33 | * never stored in the resource's attributes. |
| 34 | * |
| 35 | * Mutable — updated in place (PUT). |
| 36 | */ |
| 37 | secret: Redacted.Redacted<string>; |
| 38 | } |
| 39 | |
| 40 | export interface ZoneTransferTsigAttributes { |
| 41 | /** Identifier of the TSIG. */ |
| 42 | tsigId: string; |
| 43 | /** The Cloudflare account the TSIG belongs to. */ |
| 44 | accountId: string; |
| 45 | /** TSIG key name. */ |
| 46 | name: string; |
| 47 | /** TSIG algorithm. */ |
| 48 | algo: string; |
| 49 | } |
| 50 | |
| 51 | export type ZoneTransferTsig = Resource< |
| 52 | TypeId, |
| 53 | ZoneTransferTsigProps, |
| 54 | ZoneTransferTsigAttributes, |
| 55 | never, |
| 56 | Providers |
| 57 | >; |
| 58 | |
| 59 | /** |
| 60 | * A Secondary DNS TSIG key |
| 61 | * (`/accounts/{account_id}/secondary_dns/tsigs`) — shared-secret |
| 62 | * authentication for zone transfers between Cloudflare and external |
| 63 | * nameservers. Reference it from a {@link ZoneTransferPeer} via |
| 64 | * `tsigId`. |
| 65 | * |
| 66 | * Requires the Secondary DNS (zone transfer) entitlement on the |
| 67 | * account. All fields are mutable in place; the secret is redacted and |
| 68 | * never persisted in attributes. |
| 69 | * ### Creating a TSIG |
| 70 | * **Example:** HMAC-SHA512 key |
| 71 | * ```typescript |
| 72 | * const tsig = yield* Cloudflare.DNS.ZoneTransferTsig("TransferKey", { |
| 73 | * algo: "hmac-sha512.", |
| 74 | * secret: Redacted.make(process.env.TSIG_SECRET!), |
| 75 | * }); |
| 76 | * ``` |
| 77 | * |
| 78 | * ### Using with a Peer |
| 79 | * **Example:** Authenticate transfers from a primary nameserver |
| 80 | * ```typescript |
| 81 | * const peer = yield* Cloudflare.DNS.ZoneTransferPeer("Primary", { |
| 82 | * ip: "192.0.2.53", |
| 83 | * port: 53, |
| 84 | * tsigId: tsig.tsigId, |
| 85 | * }); |
| 86 | * ``` |
| 87 | * |
| 88 | * @see https://developers.cloudflare.com/dns/zone-setups/zone-transfers/ |
| 89 | * |
| 90 | * @resource |
| 91 | * @product DNS |
| 92 | * @category Domains & DNS |
| 93 | */ |
| 94 | export const ZoneTransferTsig = Resource<ZoneTransferTsig>(TypeId, { |
| 95 | aliases: ["Cloudflare.Dns.ZoneTransferTsig"], |
| 96 | }); |
| 97 | |
| 98 | /** |
| 99 | * Returns true if the given value is a ZoneTransferTsig resource. |
| 100 | */ |
| 101 | export const isZoneTransferTsig = (value: unknown): value is ZoneTransferTsig => |
| 102 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 103 | |
| 104 | export const ZoneTransferTsigProvider = () => |
| 105 | Provider.succeed(ZoneTransferTsig, { |
| 106 | stables: ["tsigId", "accountId"], |
| 107 | |
| 108 | diff: Effect.fn(function* ({ output }) { |
| 109 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 110 | if (output !== undefined && output.accountId !== accountId) { |
| 111 | return { action: "replace" } as const; |
| 112 | } |
| 113 | return undefined; |
| 114 | }), |
| 115 | |
| 116 | read: Effect.fn(function* ({ id, output, olds }) { |
| 117 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 118 | const acct = output?.accountId ?? accountId; |
| 119 | |
| 120 | if (output?.tsigId) { |
| 121 | const observed = yield* getTsig(acct, output.tsigId); |
| 122 | return observed ? toAttributes(observed, acct) : undefined; |
| 123 | } |
| 124 | // Cold read — recover from lost state by matching the |
| 125 | // deterministic physical name. TSIGs carry no ownership markers, |
| 126 | // so gate takeover behind adoption. |
| 127 | const name = yield* createTsigName(id, olds?.name); |
| 128 | const match = yield* findByName(acct, name); |
| 129 | return match ? Unowned(toAttributes(match, acct)) : undefined; |
| 130 | }), |
| 131 | |
| 132 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 133 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 134 | const name = yield* createTsigName(id, news.name); |
| 135 | const secret = Redacted.value(news.secret); |
| 136 | |
| 137 | // Observe — the id cached on `output` is a hint, not a guarantee. |
| 138 | const observed = output?.tsigId |
| 139 | ? yield* getTsig(output.accountId ?? accountId, output.tsigId) |
| 140 | : undefined; |
| 141 | |
| 142 | if (!observed) { |
| 143 | // Ensure — greenfield (or out-of-band delete). Names are not |
| 144 | // unique, so there is no AlreadyExists race to tolerate. |
| 145 | const created = yield* dns.createZoneTransferTsig({ |
| 146 | accountId, |
| 147 | name, |
| 148 | algo: news.algo, |
| 149 | secret, |
| 150 | }); |
| 151 | return toAttributes(created, accountId); |
| 152 | } |
| 153 | |
| 154 | // Sync — PUT with the full body; skip the call on no delta. The |
| 155 | // observed secret comes back from the GET, so the comparison is |
| 156 | // exact (not a guess from `olds`). |
| 157 | if (observed.name === name && observed.algo === news.algo && observed.secret === secret) { |
| 158 | return toAttributes(observed, output?.accountId ?? accountId); |
| 159 | } |
| 160 | const updated = yield* dns.updateZoneTransferTsig({ |
| 161 | accountId: output?.accountId ?? accountId, |
| 162 | tsigId: observed.id, |
| 163 | name, |
| 164 | algo: news.algo, |
| 165 | secret, |
| 166 | }); |
| 167 | return toAttributes(updated, output?.accountId ?? accountId); |
| 168 | }), |
| 169 | |
| 170 | delete: Effect.fn(function* ({ output }) { |
| 171 | yield* dns |
| 172 | .deleteZoneTransferTsig({ |
| 173 | accountId: output.accountId, |
| 174 | tsigId: output.tsigId, |
| 175 | }) |
| 176 | .pipe(Effect.catchTag("TsigNotFound", () => Effect.void)); |
| 177 | }), |
| 178 | |
| 179 | // Account collection — enumerate every TSIG in the account via the |
| 180 | // account-scoped list API, paginate exhaustively, and hydrate each into |
| 181 | // the same Attributes shape `read` returns (the secret is write-only and |
| 182 | // never persisted). |
| 183 | list: Effect.fn(function* () { |
| 184 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 185 | return yield* dns.listZoneTransferTsigs.pages({ accountId }).pipe( |
| 186 | Stream.runCollect, |
| 187 | Effect.map((chunk) => |
| 188 | Array.from(chunk).flatMap((page) => |
| 189 | (page.result ?? []).map((tsig) => toAttributes(tsig, accountId)), |
| 190 | ), |
| 191 | ), |
| 192 | ); |
| 193 | }), |
| 194 | }); |
| 195 | |
| 196 | type ObservedTsig = |
| 197 | | dns.GetZoneTransferTsigResponse |
| 198 | | dns.CreateZoneTransferTsigResponse |
| 199 | | dns.UpdateZoneTransferTsigResponse; |
| 200 | |
| 201 | /** Read a TSIG by id, mapping "gone" (404) to `undefined`. */ |
| 202 | const getTsig = (accountId: string, tsigId: string) => |
| 203 | dns |
| 204 | .getZoneTransferTsig({ accountId, tsigId }) |
| 205 | .pipe(Effect.catchTag("TsigNotFound", () => Effect.undefined)); |
| 206 | |
| 207 | /** |
| 208 | * Find a TSIG by exact name. Names are not unique on Cloudflare's side; |
| 209 | * pick the lexicographically-first id for determinism. |
| 210 | */ |
| 211 | const findByName = (accountId: string, name: string) => |
| 212 | dns.listZoneTransferTsigs.items({ accountId }).pipe( |
| 213 | Stream.filter((t) => t.name === name), |
| 214 | Stream.runCollect, |
| 215 | Effect.map((chunk) => |
| 216 | Array.from(chunk) |
| 217 | .sort((a, b) => a.id.localeCompare(b.id)) |
| 218 | .at(0), |
| 219 | ), |
| 220 | ); |
| 221 | |
| 222 | const createTsigName = (id: string, name: string | undefined) => |
| 223 | Effect.gen(function* () { |
| 224 | // A TSIG name is a domain name, so a generated one is a single label of at |
| 225 | // most 63 characters. |
| 226 | return name ?? (yield* createPhysicalName({ id, lowercase: true, maxLength: 63 })); |
| 227 | }); |
| 228 | |
| 229 | const toAttributes = ( |
| 230 | tsig: Pick<ObservedTsig, "id" | "name" | "algo">, |
| 231 | accountId: string, |
| 232 | ): ZoneTransferTsigAttributes => ({ |
| 233 | tsigId: tsig.id, |
| 234 | accountId, |
| 235 | name: tsig.name, |
| 236 | algo: tsig.algo, |
| 237 | }); |