@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, 06:00 UTC

main
ts 193 lines 7.61 KB
Raw
1 import * as lcc from "@distilled.cloud/cloudflare/leaked-credential-checks";
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.LeakedCredentialCheck.LeakedCredentialCheck" as const;
11 type TypeId = typeof TypeId;
12
13 export interface Props {
14 /**
15 * Zone whose Leaked Credential Checks setting is managed. Stable —
16 * changing the zone triggers a replacement (the old zone's setting is
17 * restored to the value it had before Alchemy managed it).
18 */
19 zoneId: string;
20 /**
21 * Whether Leaked Credential Checks are enabled on the zone. Mutable —
22 * set in place via the API's POST upsert.
23 * @default true
24 */
25 enabled?: boolean;
26 }
27
28 export interface Attributes {
29 /** Zone the setting belongs to. */
30 zoneId: string;
31 /** Whether Leaked Credential Checks are currently enabled. */
32 enabled: boolean;
33 /**
34 * The value the setting had before Alchemy first managed it. Restored
35 * on destroy, so deleting the resource puts the zone back the way it
36 * was found.
37 */
38 initialEnabled: boolean;
39 }
40
41 export type LeakedCredentialCheck = Resource<TypeId, Props, Attributes, never, Providers>;
42
43 /**
44 * The Leaked Credential Checks setting of a Cloudflare zone
45 * (`/zones/{zone_id}/leaked-credential-checks`).
46 *
47 * Leaked credential detection scans incoming requests for authentication
48 * credentials previously seen in known breach compilations, populating the
49 * `cf.waf.credential_check.*` ruleset fields that WAF rules can act on
50 * (e.g. force a password reset on a leaked-credential login). The check is
51 * a zone **singleton** — it always exists (default `enabled: false`), so
52 * this resource never creates or deletes anything physical. Reconcile sets
53 * the flag when the observed value differs from the desired one; destroy
54 * restores the value the setting had before Alchemy first managed it
55 * (captured as `initialEnabled`).
56 *
57 * Leaked-credential detection is available on all plans. Custom detection
58 * locations (see {@link LeakedCredentialDetection}) are plan-gated
59 * separately.
60 *
61 * Only one `LeakedCredentialCheck` resource per zone makes sense — two
62 * instances managing the same zone would fight over the singleton.
63 * ### Managing the check
64 * **Example:** Enable Leaked Credential Checks on a zone
65 * ```typescript
66 * const zone = yield* Cloudflare.Zone.Zone("Site", { name: "example.com" });
67 *
68 * yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", {
69 * zoneId: zone.zoneId,
70 * });
71 * ```
72 *
73 * **Example:** Explicitly pin the check off
74 * ```typescript
75 * yield* Cloudflare.LeakedCredentialCheck.LeakedCredentialCheck("Lcc", {
76 * zoneId: zone.zoneId,
77 * enabled: false,
78 * });
79 * ```
80 *
81 * @see https://developers.cloudflare.com/waf/detections/leaked-credentials/
82 *
83 * @resource
84 * @product Leaked Credential Checks
85 * @category Application Security
86 */
87 export const LeakedCredentialCheck = Resource<LeakedCredentialCheck>(TypeId, {
88 aliases: ["Cloudflare.LeakedCredentialCheck"],
89 });
90
91 /**
92 * Returns true if the given value is a LeakedCredentialCheck resource.
93 */
94 export const isLeakedCredentialCheck = (value: unknown): value is LeakedCredentialCheck =>
95 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
96
97 const desiredEnabled = (props: Props): boolean => props.enabled ?? true;
98
99 export const LeakedCredentialCheckProvider = () =>
100 Provider.succeed(LeakedCredentialCheck, {
101 nuke: { singleton: true },
102 stables: ["zoneId", "initialEnabled"],
103
104 list: Effect.fn(function* () {
105 const { accountId } = yield* yield* CloudflareEnvironment;
106 // No account-wide API for this zone singleton — enumerate every
107 // zone in the account and read its setting (every zone has one).
108 const allZones = yield* listAllZones(accountId);
109 const rows = yield* Effect.forEach(
110 allZones.map((zone) => zone.id),
111 (zoneId) =>
112 lcc.getLeakedCredentialCheck({ zoneId }).pipe(
113 Effect.map((observed) => {
114 const enabled = observed.enabled ?? false;
115 // The singleton always exists; with no managed history its
116 // observed value is also its pre-management value.
117 return {
118 zoneId,
119 enabled,
120 initialEnabled: enabled,
121 } satisfies Attributes;
122 }),
123 // Zone deleted out-of-band or plan-gated route; skip it.
124 Effect.catchTag("InvalidRoute", () => Effect.undefined),
125 ),
126 { concurrency: 10 },
127 );
128 return rows.filter((row): row is Attributes => row !== undefined);
129 }),
130
131 diff: Effect.fn(function* ({ olds = {}, news, output }) {
132 const o = olds as Props;
133 const n = news as Props;
134 // zoneId is Input<string>; compare only once both sides are concrete.
135 const oldZoneId = output?.zoneId ?? (typeof o.zoneId === "string" ? o.zoneId : undefined);
136 if (oldZoneId !== undefined && typeof n.zoneId === "string" && oldZoneId !== n.zoneId) {
137 return { action: "replace" } as const;
138 }
139 return undefined;
140 }),
141
142 read: Effect.fn(function* ({ output, olds }) {
143 const zoneId = output?.zoneId ?? (olds?.zoneId as string | undefined);
144 if (!zoneId) return undefined;
145 const observed = yield* lcc.getLeakedCredentialCheck({ zoneId });
146 // The setting is a singleton that always exists with a Cloudflare
147 // default — there is nothing to "own", so a cold read adopts
148 // freely (never `Unowned`). The observed value at adoption time
149 // becomes the `initialEnabled` restored on destroy.
150 const enabled = observed.enabled ?? false;
151 const initialEnabled = output !== undefined ? output.initialEnabled : enabled;
152 return { zoneId, enabled, initialEnabled };
153 }),
154
155 reconcile: Effect.fn(function* ({ news, output }) {
156 // Inputs have been resolved to concrete strings by Plan.
157 const zoneId = news.zoneId as string;
158
159 // 1. Observe — the setting always exists; read its live value.
160 const observed = yield* lcc.getLeakedCredentialCheck({ zoneId });
161 const observedEnabled = observed.enabled ?? false;
162
163 // 2. Capture — the pre-management value, restored on destroy.
164 // `output` (including an adoption read) already carries it;
165 // otherwise this is our first touch and the observed value is
166 // the zone's original.
167 const initialEnabled = output !== undefined ? output.initialEnabled : observedEnabled;
168
169 // 3. Sync — POST (the API's set/upsert) only when it differs.
170 const desired = desiredEnabled(news);
171 if (observedEnabled === desired) {
172 return { zoneId, enabled: observedEnabled, initialEnabled };
173 }
174 const set = yield* lcc.createLeakedCredentialCheck({
175 zoneId,
176 enabled: desired,
177 });
178 return { zoneId, enabled: set.enabled ?? desired, initialEnabled };
179 }),
180
181 delete: Effect.fn(function* ({ output }) {
182 const { zoneId, initialEnabled } = output;
183 // Observe, then restore the pre-management value; skip the call
184 // when it already matches (idempotent re-delete after a crashed
185 // run). The singleton always exists, so nothing here can 404.
186 const observed = yield* lcc.getLeakedCredentialCheck({ zoneId });
187 if ((observed.enabled ?? false) === initialEnabled) return;
188 yield* lcc.createLeakedCredentialCheck({
189 zoneId,
190 enabled: initialEnabled,
191 });
192 }),
193 });