The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as emailAuth from "@distilled.cloud/cloudflare/email-auth"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { resolveZoneId, type Reference } from "../Zone/index.ts"; |
| 9 | import { listAllZones } from "../Zone/lookup.ts"; |
| 10 | |
| 11 | export type DmarcReportsStatus = |
| 12 | | "missing-dmarc-report" |
| 13 | | "multiple-dmarc-reports" |
| 14 | | "missing-dmarc-rua" |
| 15 | | "cname-on-dmarc-record" |
| 16 | | "unauthorized-reporting-domain"; |
| 17 | |
| 18 | export type DmarcReportsProps = { |
| 19 | /** |
| 20 | * Apex zone to manage DMARC reports for. Accepts a zone id, a zone name |
| 21 | * (`example.com`), or a `{ zoneId, name? }` object. The zone's DNS must be |
| 22 | * hosted on Cloudflare. Changing the zone replaces the resource. |
| 23 | */ |
| 24 | zone: Reference; |
| 25 | /** |
| 26 | * Whether DMARC reports are enabled on the zone. Enabling makes |
| 27 | * Cloudflare add its RUA address to the zone's `_dmarc` record. |
| 28 | * |
| 29 | * @default true |
| 30 | */ |
| 31 | enabled?: boolean; |
| 32 | }; |
| 33 | |
| 34 | export type DmarcReports = Resource< |
| 35 | "Cloudflare.Email.DmarcReports", |
| 36 | DmarcReportsProps, |
| 37 | { |
| 38 | /** Zone identifier. */ |
| 39 | zoneId: string; |
| 40 | /** Whether DMARC reports are enabled on the zone. */ |
| 41 | enabled: boolean; |
| 42 | /** |
| 43 | * Prefix of the Cloudflare RUA address that receives aggregate reports |
| 44 | * (32-char hex string). |
| 45 | */ |
| 46 | ruaPrefix: string | undefined; |
| 47 | /** |
| 48 | * DMARC configuration problem, if any. `undefined` when DMARC is |
| 49 | * correctly configured. |
| 50 | */ |
| 51 | status: DmarcReportsStatus | undefined; |
| 52 | }, |
| 53 | never, |
| 54 | Providers |
| 55 | >; |
| 56 | |
| 57 | type DmarcReportsAttributes = DmarcReports["Attributes"]; |
| 58 | |
| 59 | const toAttributes = ( |
| 60 | zoneId: string, |
| 61 | result: emailAuth.GetDmarcReportResponse, |
| 62 | ): DmarcReportsAttributes => ({ |
| 63 | zoneId, |
| 64 | enabled: result.enabled ?? false, |
| 65 | ruaPrefix: result.ruaPrefix ?? undefined, |
| 66 | status: result.status ?? undefined, |
| 67 | }); |
| 68 | |
| 69 | /** |
| 70 | * Enables Cloudflare [DMARC Management](https://developers.cloudflare.com/dmarc-management/) |
| 71 | * on an apex zone. Cloudflare receives the zone's aggregate DMARC reports |
| 72 | * and shows which sources send mail as your domain. |
| 73 | * |
| 74 | * Enabling adds Cloudflare's RUA address to the zone's `_dmarc` TXT record |
| 75 | * (creating the record when it is missing). Disabling or destroying the |
| 76 | * resource only turns reporting off; it never deletes the zone or any DNS |
| 77 | * record. |
| 78 | * |
| 79 | * ### Enabling DMARC reports |
| 80 | * **Example:** Enable on a zone you own |
| 81 | * ```typescript |
| 82 | * const dmarc = yield* Cloudflare.Email.DmarcReports("Dmarc", { |
| 83 | * zone: "example.com", |
| 84 | * }); |
| 85 | * ``` |
| 86 | * |
| 87 | * **Example:** Keep the resource but turn reporting off |
| 88 | * ```typescript |
| 89 | * const dmarc = yield* Cloudflare.Email.DmarcReports("Dmarc", { |
| 90 | * zone: "example.com", |
| 91 | * enabled: false, |
| 92 | * }); |
| 93 | * ``` |
| 94 | * |
| 95 | * @resource |
| 96 | * @product Email |
| 97 | * @category Email |
| 98 | */ |
| 99 | export const DmarcReports = Resource<DmarcReports>("Cloudflare.Email.DmarcReports"); |
| 100 | |
| 101 | const resolve = Effect.fn(function* (zone: Reference) { |
| 102 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 103 | return yield* resolveZoneId({ |
| 104 | accountId, |
| 105 | zone, |
| 106 | hostname: typeof zone === "string" ? zone : (zone.name ?? ""), |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | export const DmarcReportsProvider = () => |
| 111 | Provider.succeed(DmarcReports, { |
| 112 | nuke: { singleton: true }, |
| 113 | stables: ["zoneId"], |
| 114 | list: Effect.fn(function* () { |
| 115 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 116 | // DMARC report settings are a per-zone singleton with no account-wide |
| 117 | // enumeration API: read the settings in every zone. |
| 118 | const allZones = yield* listAllZones(accountId); |
| 119 | return yield* Effect.forEach( |
| 120 | allZones, |
| 121 | (zone) => |
| 122 | emailAuth |
| 123 | .getDmarcReport({ zoneId: zone.id }) |
| 124 | .pipe(Effect.map((result) => toAttributes(zone.id, result))), |
| 125 | { concurrency: 10 }, |
| 126 | ); |
| 127 | }), |
| 128 | diff: Effect.fn(function* ({ news, output }) { |
| 129 | if (!output) return undefined; |
| 130 | if (!isResolved(news)) return undefined; |
| 131 | const zoneId = yield* resolve(news.zone); |
| 132 | if (zoneId !== output.zoneId) { |
| 133 | return { action: "replace" } as const; |
| 134 | } |
| 135 | return undefined; |
| 136 | }), |
| 137 | read: Effect.fn(function* ({ output }) { |
| 138 | if (!output?.zoneId) return undefined; |
| 139 | const result = yield* emailAuth.getDmarcReport({ |
| 140 | zoneId: output.zoneId, |
| 141 | }); |
| 142 | return toAttributes(output.zoneId, result); |
| 143 | }), |
| 144 | reconcile: Effect.fn(function* ({ news, output }) { |
| 145 | const zoneId = output?.zoneId ?? (yield* resolve(news.zone)); |
| 146 | const desired = news.enabled ?? true; |
| 147 | |
| 148 | // Observe — the zone's settings always exist; only `enabled` is ours. |
| 149 | const observed = yield* emailAuth.getDmarcReport({ zoneId }); |
| 150 | if ((observed.enabled ?? false) === desired) { |
| 151 | return toAttributes(zoneId, observed); |
| 152 | } |
| 153 | |
| 154 | // Sync — enabling also makes Cloudflare upsert the RUA address into |
| 155 | // the `_dmarc` record; that DNS side effect is expected. |
| 156 | const result = yield* emailAuth.patchDmarcReport({ |
| 157 | zoneId, |
| 158 | enabled: desired, |
| 159 | }); |
| 160 | return toAttributes(zoneId, result); |
| 161 | }), |
| 162 | delete: Effect.fn(function* ({ output }) { |
| 163 | // Only turn reporting off. Never touch the zone or its DNS records. |
| 164 | const observed = yield* emailAuth.getDmarcReport({ |
| 165 | zoneId: output.zoneId, |
| 166 | }); |
| 167 | if (!observed.enabled) return; |
| 168 | yield* emailAuth.patchDmarcReport({ |
| 169 | zoneId: output.zoneId, |
| 170 | enabled: false, |
| 171 | }); |
| 172 | }), |
| 173 | }); |