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 zones from "@distilled.cloud/cloudflare/zones"; |
| 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 * as Predicate from "effect/Predicate"; |
| 7 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { listAllZones } from "../Zone/lookup.ts"; |
| 10 | |
| 11 | const TypeId = "Cloudflare.Ssl.CertificateTransparencyAlerting" as const; |
| 12 | type TypeId = typeof TypeId; |
| 13 | |
| 14 | export type CertificateTransparencyAlertingProps = { |
| 15 | /** |
| 16 | * Zone whose Certificate Transparency alerting is managed. Stable — |
| 17 | * changing the zone triggers a replacement (the old zone's subscription |
| 18 | * is restored to what it was before Alchemy managed it). |
| 19 | */ |
| 20 | zoneId: string; |
| 21 | /** |
| 22 | * Whether Cloudflare emails an alert when a certificate is issued for |
| 23 | * one of the zone's hostnames. Cloudflare leaves it off for a new zone. |
| 24 | * @default true |
| 25 | */ |
| 26 | enabled?: boolean; |
| 27 | /** |
| 28 | * Addresses that receive the alerts (at most 100). When omitted, the |
| 29 | * zone's stored recipients are left as they are. |
| 30 | */ |
| 31 | emails?: string[]; |
| 32 | }; |
| 33 | |
| 34 | export type CertificateTransparencyAlertingAttributes = { |
| 35 | /** Zone the subscription belongs to. */ |
| 36 | zoneId: string; |
| 37 | /** Whether CT alerting is enabled for the zone. */ |
| 38 | enabled: boolean; |
| 39 | /** The zone's alert recipients. */ |
| 40 | emails: string[]; |
| 41 | /** `enabled` before Alchemy first managed the subscription. */ |
| 42 | initialEnabled: boolean; |
| 43 | /** `emails` before Alchemy first managed the subscription. */ |
| 44 | initialEmails: string[]; |
| 45 | }; |
| 46 | |
| 47 | export type CertificateTransparencyAlerting = Resource< |
| 48 | TypeId, |
| 49 | CertificateTransparencyAlertingProps, |
| 50 | CertificateTransparencyAlertingAttributes, |
| 51 | never, |
| 52 | Providers |
| 53 | >; |
| 54 | |
| 55 | /** |
| 56 | * Certificate Transparency alerting for a Cloudflare zone ("Certificate |
| 57 | * Transparency Monitoring" in the dashboard, `/zones/{zone_id}/ct/alerting` |
| 58 | * in the API): Cloudflare watches the public Certificate Transparency logs |
| 59 | * and emails the listed recipients when a certificate is issued for one of |
| 60 | * the zone's hostnames. Certificates Cloudflare issues itself are filtered |
| 61 | * out. |
| 62 | * |
| 63 | * The subscription is a zone singleton, so this resource never creates or |
| 64 | * deletes anything physical. Reconcile patches it when the observed state |
| 65 | * differs from the props; destroy restores the state observed before |
| 66 | * Alchemy first managed it. |
| 67 | * ### Monitoring certificate issuance |
| 68 | * **Example:** Email the security team on every new certificate |
| 69 | * ```typescript |
| 70 | * yield* Cloudflare.Ssl.CertificateTransparencyAlerting("CtAlerting", { |
| 71 | * zoneId: zone.zoneId, |
| 72 | * emails: ["security@example.com"], |
| 73 | * }); |
| 74 | * ``` |
| 75 | * |
| 76 | * **Example:** Turn alerting off for a zone |
| 77 | * ```typescript |
| 78 | * yield* Cloudflare.Ssl.CertificateTransparencyAlerting("CtAlerting", { |
| 79 | * zoneId: zone.zoneId, |
| 80 | * enabled: false, |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * @see https://blog.cloudflare.com/certificate-transparency-monitoring-ga/ |
| 85 | * |
| 86 | * @resource |
| 87 | * @product SSL/TLS |
| 88 | * @category SSL/TLS & Certificates |
| 89 | */ |
| 90 | export const CertificateTransparencyAlerting = Resource<CertificateTransparencyAlerting>(TypeId); |
| 91 | |
| 92 | /** |
| 93 | * Returns true if the given value is a CertificateTransparencyAlerting resource. |
| 94 | */ |
| 95 | export const isCertificateTransparencyAlerting = ( |
| 96 | value: unknown, |
| 97 | ): value is CertificateTransparencyAlerting => |
| 98 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 99 | |
| 100 | const observe = (zoneId: string) => |
| 101 | zones.getCtAlerting({ zoneId }).pipe(Effect.catchTag("InvalidRoute", () => Effect.undefined)); |
| 102 | |
| 103 | const recipients = ( |
| 104 | observed: zones.GetCtAlertingResponse | zones.PatchCtAlertingResponse, |
| 105 | ): string[] => [...(observed.emails ?? [])]; |
| 106 | |
| 107 | const sameRecipients = (a: readonly string[], b: readonly string[]) => { |
| 108 | const sortedB = [...b].sort(); |
| 109 | return a.length === b.length && [...a].sort().every((email, i) => email === sortedB[i]); |
| 110 | }; |
| 111 | |
| 112 | export const CertificateTransparencyAlertingProvider = () => |
| 113 | Provider.succeed(CertificateTransparencyAlerting, { |
| 114 | nuke: { singleton: true }, |
| 115 | stables: ["zoneId", "initialEnabled", "initialEmails"], |
| 116 | |
| 117 | list: Effect.fn(function* () { |
| 118 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 119 | const allZones = yield* listAllZones(accountId); |
| 120 | const rows = yield* Effect.forEach( |
| 121 | allZones.map((zone) => zone.id), |
| 122 | (zoneId) => |
| 123 | observe(zoneId).pipe( |
| 124 | Effect.map((observed) => { |
| 125 | if (observed === undefined) return undefined; |
| 126 | const emails = recipients(observed); |
| 127 | return { |
| 128 | zoneId, |
| 129 | enabled: observed.enabled, |
| 130 | emails, |
| 131 | initialEnabled: observed.enabled, |
| 132 | initialEmails: emails, |
| 133 | }; |
| 134 | }), |
| 135 | ), |
| 136 | { concurrency: 10 }, |
| 137 | ); |
| 138 | return rows.filter( |
| 139 | (row): row is CertificateTransparencyAlertingAttributes => row !== undefined, |
| 140 | ); |
| 141 | }), |
| 142 | |
| 143 | diff: Effect.fn(function* ({ olds, news, output }) { |
| 144 | if (!isResolved(news)) return undefined; |
| 145 | const oldZoneId = |
| 146 | output?.zoneId ?? (typeof olds?.zoneId === "string" ? olds.zoneId : undefined); |
| 147 | if (oldZoneId !== undefined && typeof news.zoneId === "string" && oldZoneId !== news.zoneId) { |
| 148 | return { action: "replace" } as const; |
| 149 | } |
| 150 | return undefined; |
| 151 | }), |
| 152 | |
| 153 | read: Effect.fn(function* ({ output, olds }) { |
| 154 | const zoneId = output?.zoneId ?? (olds?.zoneId as string | undefined); |
| 155 | if (!zoneId) return undefined; |
| 156 | const observed = yield* observe(zoneId); |
| 157 | if (observed === undefined) return undefined; |
| 158 | const emails = recipients(observed); |
| 159 | return { |
| 160 | zoneId, |
| 161 | enabled: observed.enabled, |
| 162 | emails, |
| 163 | initialEnabled: output?.initialEnabled ?? observed.enabled, |
| 164 | initialEmails: output?.initialEmails ?? emails, |
| 165 | }; |
| 166 | }), |
| 167 | |
| 168 | reconcile: Effect.fn(function* ({ news, output }) { |
| 169 | const zoneId = news.zoneId as string; |
| 170 | const enabled = news.enabled ?? true; |
| 171 | const observed = yield* zones.getCtAlerting({ zoneId }); |
| 172 | const emails = recipients(observed); |
| 173 | const initialEnabled = output?.initialEnabled ?? observed.enabled; |
| 174 | const initialEmails = output?.initialEmails ?? emails; |
| 175 | |
| 176 | if ( |
| 177 | observed.enabled === enabled && |
| 178 | (news.emails === undefined || sameRecipients(news.emails, emails)) |
| 179 | ) { |
| 180 | return { |
| 181 | zoneId, |
| 182 | enabled: observed.enabled, |
| 183 | emails, |
| 184 | initialEnabled, |
| 185 | initialEmails, |
| 186 | }; |
| 187 | } |
| 188 | const patched = yield* zones.patchCtAlerting({ |
| 189 | zoneId, |
| 190 | enabled, |
| 191 | ...(news.emails === undefined ? {} : { emails: news.emails }), |
| 192 | }); |
| 193 | return { |
| 194 | zoneId, |
| 195 | enabled: patched.enabled, |
| 196 | emails: recipients(patched), |
| 197 | initialEnabled, |
| 198 | initialEmails, |
| 199 | }; |
| 200 | }), |
| 201 | |
| 202 | delete: Effect.fn(function* ({ output, olds }) { |
| 203 | const { zoneId, initialEnabled, initialEmails } = output; |
| 204 | const observed = yield* observe(zoneId); |
| 205 | if (observed === undefined) return; |
| 206 | const managedEmails = olds?.emails !== undefined; |
| 207 | if ( |
| 208 | observed.enabled === initialEnabled && |
| 209 | (!managedEmails || sameRecipients(recipients(observed), initialEmails)) |
| 210 | ) { |
| 211 | return; |
| 212 | } |
| 213 | yield* zones |
| 214 | .patchCtAlerting({ |
| 215 | zoneId, |
| 216 | enabled: initialEnabled, |
| 217 | ...(managedEmails ? { emails: initialEmails } : {}), |
| 218 | }) |
| 219 | .pipe(Effect.catchTag("InvalidRoute", () => Effect.void)); |
| 220 | }), |
| 221 | }); |