@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 183 lines 7.27 KB
Raw
1 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
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
9 const TypeId = "Cloudflare.Access.KeyConfiguration" as const;
10 type TypeId = typeof TypeId;
11
12 export type KeyConfigurationProps = {
13 /**
14 * The number of days between automatic Access service key rotations.
15 * Cloudflare accepts values between 21 and 372 days.
16 *
17 * Mutable — converged in place via PUT.
18 */
19 keyRotationIntervalDays: number;
20 };
21
22 export type KeyConfigurationAttributes = {
23 /** Cloudflare account the key configuration belongs to. */
24 accountId: string;
25 /** The number of days between key rotations. */
26 keyRotationIntervalDays: number | undefined;
27 /** The number of days until the next key rotation. */
28 daysUntilNextRotation: number | undefined;
29 /** The timestamp of the previous key rotation, if one has happened. */
30 lastKeyRotationAt: string | undefined;
31 /**
32 * The rotation interval the account had before Alchemy first managed it.
33 * Restored on destroy, so deleting the resource puts the account back
34 * the way it was found. `undefined` when Cloudflare reported no interval
35 * at adoption time — destroy then leaves the current interval in place.
36 */
37 initialKeyRotationIntervalDays: number | undefined;
38 };
39
40 export type KeyConfiguration = Resource<
41 TypeId,
42 KeyConfigurationProps,
43 KeyConfigurationAttributes,
44 never,
45 Providers
46 >;
47
48 /**
49 * The Cloudflare Zero Trust Access service-key rotation configuration for an
50 * account (`/accounts/{account_id}/access/keys`).
51 *
52 * The key configuration is an account singleton — it always exists, so this
53 * resource never creates or deletes anything physical. Reconcile PUTs the
54 * rotation interval when the observed value differs from the desired one;
55 * destroy restores the interval the account had before Alchemy first managed
56 * it (captured as `initialKeyRotationIntervalDays`).
57 * ### Managing the rotation interval
58 * **Example:** Rotate Access service keys every 30 days
59 * ```typescript
60 * const keys = yield* Cloudflare.Access.KeyConfiguration("Keys", {
61 * keyRotationIntervalDays: 30,
62 * });
63 * ```
64 *
65 * **Example:** Inspect rotation status
66 * ```typescript
67 * const keys = yield* Cloudflare.Access.KeyConfiguration("Keys", {
68 * keyRotationIntervalDays: 90,
69 * });
70 * // keys.daysUntilNextRotation, keys.lastKeyRotationAt
71 * ```
72 *
73 * @see https://developers.cloudflare.com/api/resources/zero_trust/subresources/access/subresources/keys/
74 *
75 * @resource
76 * @product Access
77 * @category Cloudflare One (Zero Trust)
78 */
79 export const KeyConfiguration = Resource<KeyConfiguration>(TypeId);
80
81 /**
82 * Returns true if the given value is an KeyConfiguration resource.
83 */
84 export const isKeyConfiguration = (value: unknown): value is KeyConfiguration =>
85 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
86
87 export const KeyConfigurationProvider = () =>
88 Provider.succeed(KeyConfiguration, {
89 nuke: { singleton: true },
90 stables: ["accountId", "initialKeyRotationIntervalDays"],
91
92 list: Effect.fn(function* () {
93 const { accountId } = yield* yield* CloudflareEnvironment;
94 // Per-account singleton: the key configuration always exists with a
95 // Cloudflare default and there is no enumeration API — read the one
96 // instance and return it as a one-element array. The observed
97 // interval becomes the value restored on destroy (adoption read).
98 const observed = yield* zeroTrust.getAccessKey({ accountId });
99 return [toAttributes(accountId, observed, observed.keyRotationIntervalDays ?? undefined)];
100 }),
101
102 diff: Effect.fn(function* ({ output }) {
103 // The configuration is an account singleton — its identity is the
104 // account. Moving accounts replaces (old account's interval is
105 // restored as the old instance deletes).
106 const { accountId } = yield* yield* CloudflareEnvironment;
107 if ((output?.accountId ?? accountId) !== accountId) {
108 return { action: "replace" } as const;
109 }
110 return undefined;
111 }),
112
113 read: Effect.fn(function* ({ output }) {
114 const { accountId } = yield* yield* CloudflareEnvironment;
115 const acct = output?.accountId ?? accountId;
116 const observed = yield* zeroTrust.getAccessKey({ accountId: acct });
117 // The configuration is a singleton that always exists with a
118 // Cloudflare default — there is nothing to "own", so a cold read
119 // adopts freely (never `Unowned`). The observed interval at adoption
120 // time becomes the value restored on destroy.
121 const initial =
122 output !== undefined
123 ? output.initialKeyRotationIntervalDays
124 : (observed.keyRotationIntervalDays ?? undefined);
125 return toAttributes(acct, observed, initial);
126 }),
127
128 reconcile: Effect.fn(function* ({ news, output }) {
129 const { accountId } = yield* yield* CloudflareEnvironment;
130
131 // 1. Observe — the configuration always exists; read the live value.
132 const observed = yield* zeroTrust.getAccessKey({ accountId });
133
134 // 2. Capture — the pre-management interval, restored on destroy.
135 // `output` (including an adoption read) already carries it;
136 // otherwise this is our first touch and the observed interval is
137 // the account's original.
138 const initial =
139 output !== undefined
140 ? output.initialKeyRotationIntervalDays
141 : (observed.keyRotationIntervalDays ?? undefined);
142
143 // 3. Sync — PUT only when the observed interval differs.
144 if (observed.keyRotationIntervalDays === news.keyRotationIntervalDays) {
145 return toAttributes(accountId, observed, initial);
146 }
147 const updated = yield* zeroTrust.putAccessKey({
148 accountId,
149 keyRotationIntervalDays: news.keyRotationIntervalDays,
150 });
151 return toAttributes(accountId, updated, initial);
152 }),
153
154 delete: Effect.fn(function* ({ output }) {
155 const { accountId, initialKeyRotationIntervalDays } = output;
156 // Nothing physical to delete — restore the pre-management interval.
157 // If Cloudflare reported no interval at adoption time there is
158 // nothing to restore to; leave the current value in place.
159 if (initialKeyRotationIntervalDays === undefined) return;
160 const observed = yield* zeroTrust.getAccessKey({ accountId });
161 // Skip the PUT when the live value already matches (idempotent
162 // re-delete after a crashed run).
163 if (observed.keyRotationIntervalDays === initialKeyRotationIntervalDays) {
164 return;
165 }
166 yield* zeroTrust.putAccessKey({
167 accountId,
168 keyRotationIntervalDays: initialKeyRotationIntervalDays,
169 });
170 }),
171 });
172
173 const toAttributes = (
174 accountId: string,
175 observed: zeroTrust.GetAccessKeyResponse | zeroTrust.PutAccessKeyResponse,
176 initialKeyRotationIntervalDays: number | undefined,
177 ): KeyConfigurationAttributes => ({
178 accountId,
179 keyRotationIntervalDays: observed.keyRotationIntervalDays ?? undefined,
180 daysUntilNextRotation: observed.daysUntilNextRotation ?? undefined,
181 lastKeyRotationAt: observed.lastKeyRotationAt ?? undefined,
182 initialKeyRotationIntervalDays,
183 });