@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 186 lines 7.09 KB
Raw
1 import * as originTls from "@distilled.cloud/cloudflare/origin-tls-client-auth";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import * as Predicate from "effect/Predicate";
6 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
7 import type { Providers } from "../Providers.ts";
8 import { listAllZones } from "../Zone/lookup.ts";
9
10 const TypeId = "Cloudflare.OriginTlsClientAuth.Setting" as const;
11 type TypeId = typeof TypeId;
12
13 export type SettingProps = {
14 /**
15 * Zone the setting belongs to. Stable — changing the zone triggers a
16 * replacement (the old zone's setting is restored to the value it had
17 * before Alchemy managed it).
18 */
19 zoneId: string;
20 /**
21 * Whether zone-level Authenticated Origin Pulls is enabled. When enabled,
22 * Cloudflare presents the zone's uploaded client certificate
23 * ({@link Certificate}) to your origin on every pull.
24 *
25 * Mutable — updated in place.
26 * @default false (Cloudflare's default)
27 */
28 enabled: boolean;
29 };
30
31 export type SettingAttributes = {
32 /** Zone the setting belongs to. */
33 zoneId: string;
34 /** Whether zone-level Authenticated Origin Pulls is currently enabled. */
35 enabled: boolean;
36 /**
37 * The value the setting had before Alchemy first managed it. Restored on
38 * destroy, so deleting the resource puts the zone back the way it was
39 * found.
40 */
41 initialEnabled: boolean;
42 };
43
44 export type Setting = Resource<TypeId, SettingProps, SettingAttributes, never, Providers>;
45
46 /**
47 * The zone-level Authenticated Origin Pulls (AOP) toggle
48 * (`/zones/{zone_id}/origin_tls_client_auth/settings`).
49 *
50 * The setting is a singleton — it always exists on every zone (Cloudflare
51 * default `false`), so this resource never creates or deletes anything
52 * physical. Reconcile flips the flag when the observed value differs from
53 * the desired one; destroy restores the value the setting had before
54 * Alchemy first managed it (captured as `initialEnabled`).
55 *
56 * Enabling AOP only has effect once a zone client certificate is uploaded
57 * ({@link Certificate}) and your origin is configured to
58 * verify it — enabling the flag alone does not break traffic unless the
59 * origin enforces mTLS.
60 * ### Enabling Authenticated Origin Pulls
61 * **Example:** Enable zone-level AOP
62 * ```typescript
63 * const cert = yield* Cloudflare.OriginTlsClientAuth.Certificate("AopCert", {
64 * zoneId: zone.zoneId,
65 * certificate: clientCertPem,
66 * privateKey: yield* Config.Redacted("AOP_CLIENT_KEY"),
67 * });
68 *
69 * yield* Cloudflare.OriginTlsClientAuth.Setting("Aop", {
70 * zoneId: zone.zoneId,
71 * enabled: true,
72 * });
73 * ```
74 *
75 * **Example:** Pin AOP off
76 * ```typescript
77 * yield* Cloudflare.OriginTlsClientAuth.Setting("Aop", {
78 * zoneId: zone.zoneId,
79 * enabled: false,
80 * });
81 * ```
82 *
83 * @see https://developers.cloudflare.com/ssl/origin-configuration/authenticated-origin-pull/
84 *
85 * @resource
86 * @product Origin TLS Client Auth
87 * @category SSL/TLS & Certificates
88 */
89 export const Setting = Resource<Setting>(TypeId);
90
91 /**
92 * Returns true if the given value is an Setting resource.
93 */
94 export const isSetting = (value: unknown): value is Setting =>
95 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
96
97 export const SettingProvider = () =>
98 Provider.succeed(Setting, {
99 nuke: { singleton: true },
100 stables: ["zoneId", "initialEnabled"],
101
102 list: Effect.fn(function* () {
103 const { accountId } = yield* yield* CloudflareEnvironment;
104 // No account-wide API for this zone singleton — enumerate every
105 // zone in the account and read its setting (every zone has one).
106 const allZones = yield* listAllZones(accountId);
107 const rows = yield* Effect.forEach(
108 allZones.map((zone) => zone.id),
109 (zoneId) =>
110 originTls.getSetting({ zoneId }).pipe(
111 Effect.map((observed): SettingAttributes => {
112 const enabled = observed.enabled ?? false;
113 // Enumeration adopts the live value as the pre-management
114 // baseline, mirroring a cold `read`.
115 return { zoneId, enabled, initialEnabled: enabled };
116 }),
117 // Plan-gated or partial zones reject the route; skip them.
118 Effect.catchTag("InvalidRoute", () => Effect.undefined),
119 ),
120 { concurrency: 10 },
121 );
122 return rows.filter((row): row is SettingAttributes => row !== undefined);
123 }),
124
125 diff: Effect.fn(function* ({ olds = {}, news, output }) {
126 const o = olds as SettingProps;
127 const n = news as SettingProps;
128 // zoneId is Input<string>; compare only once both sides are concrete.
129 const oldZoneId = output?.zoneId ?? (typeof o.zoneId === "string" ? o.zoneId : undefined);
130 if (oldZoneId !== undefined && typeof n.zoneId === "string" && oldZoneId !== n.zoneId) {
131 return { action: "replace" } as const;
132 }
133 return undefined;
134 }),
135
136 read: Effect.fn(function* ({ output, olds }) {
137 const zoneId = output?.zoneId ?? (olds?.zoneId as string | undefined);
138 if (!zoneId) return undefined;
139 const observed = yield* originTls.getSetting({ zoneId });
140 const enabled = observed.enabled ?? false;
141 // The setting is a singleton that always exists with a Cloudflare
142 // default — there is nothing to "own", so a cold read adopts freely
143 // (never `Unowned`). The observed value at adoption time becomes the
144 // `initialEnabled` restored on destroy.
145 const initialEnabled = output !== undefined ? output.initialEnabled : enabled;
146 return { zoneId, enabled, initialEnabled };
147 }),
148
149 reconcile: Effect.fn(function* ({ news, output }) {
150 // Inputs have been resolved to concrete strings by Plan.
151 const zoneId = news.zoneId as string;
152
153 // 1. Observe — the setting always exists; read its live value.
154 const observed = yield* originTls.getSetting({ zoneId });
155 const observedEnabled = observed.enabled ?? false;
156
157 // 2. Capture — the pre-management value, restored on destroy.
158 // `output` (including an adoption read) already carries it;
159 // otherwise this is our first touch and the observed value is the
160 // zone's original.
161 const initialEnabled = output !== undefined ? output.initialEnabled : observedEnabled;
162
163 // 3. Sync — put only when the observed value differs.
164 if (observedEnabled === news.enabled) {
165 return { zoneId, enabled: observedEnabled, initialEnabled };
166 }
167 const updated = yield* originTls.putSetting({
168 zoneId,
169 enabled: news.enabled,
170 });
171 return {
172 zoneId,
173 enabled: updated.enabled ?? news.enabled,
174 initialEnabled,
175 };
176 }),
177
178 delete: Effect.fn(function* ({ output }) {
179 const { zoneId, initialEnabled } = output;
180 // Observe — restore the pre-management value; skip the call when it
181 // already matches (idempotent re-delete after a crashed run).
182 const observed = yield* originTls.getSetting({ zoneId });
183 if ((observed.enabled ?? false) === initialEnabled) return;
184 yield* originTls.putSetting({ zoneId, enabled: initialEnabled });
185 }),
186 });