@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 290 lines 10.2 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import { testName } from "../../names.ts";
10
11 const { test } = Test.make({
12 providers: Cloudflare.providers(),
13 state: Cloudflare.state(),
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 test.provider(
19 "create and delete basic allow policy",
20 (stack) =>
21 Effect.gen(function* () {
22 const { accountId } = yield* yield* CloudflareEnvironment;
23
24 yield* stack.destroy();
25
26 const policy = yield* stack.deploy(
27 Effect.gen(function* () {
28 return yield* Cloudflare.Access.Policy("BasicAllowPolicy", {
29 decision: "allow",
30 include: [{ emailDomain: { domain: "example.com" } }],
31 });
32 }),
33 );
34
35 expect(policy.policyId).toBeDefined();
36 expect(policy.decision).toEqual("allow");
37 expect(policy.accountId).toEqual(accountId);
38
39 const actual = yield* zeroTrust.getAccessPolicy({
40 accountId,
41 policyId: policy.policyId,
42 });
43 expect(actual.id).toEqual(policy.policyId);
44 expect(actual.decision).toEqual("allow");
45 expect(actual.include?.length).toEqual(1);
46
47 yield* stack.destroy();
48
49 const afterDestroy = yield* zeroTrust
50 .getAccessPolicy({ accountId, policyId: policy.policyId })
51 .pipe(Effect.catch(() => Effect.succeed(undefined)));
52 expect(afterDestroy).toBeUndefined();
53 }).pipe(logLevel),
54 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
55 );
56
57 test.provider(
58 "update mutates includes without replacing",
59 (stack) =>
60 Effect.gen(function* () {
61 const { accountId } = yield* yield* CloudflareEnvironment;
62
63 yield* stack.destroy();
64
65 const initial = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* Cloudflare.Access.Policy("UpdatePolicy", {
68 decision: "allow",
69 include: [{ emailDomain: { domain: "example.com" } }],
70 adopt: true,
71 });
72 }),
73 );
74
75 expect(initial.policyId).toBeDefined();
76
77 const updated = yield* stack.deploy(
78 Effect.gen(function* () {
79 return yield* Cloudflare.Access.Policy("UpdatePolicy", {
80 decision: "allow",
81 include: [
82 { emailDomain: { domain: "example.com" } },
83 { emailDomain: { domain: "test.example.com" } },
84 ],
85 adopt: true,
86 });
87 }),
88 );
89
90 expect(updated.policyId).toEqual(initial.policyId);
91
92 const actual = yield* zeroTrust.getAccessPolicy({
93 accountId,
94 policyId: updated.policyId,
95 });
96 expect(actual.include?.length).toEqual(2);
97
98 yield* stack.destroy();
99 }).pipe(logLevel),
100 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
101 );
102
103 test.provider(
104 "adopts an out-of-band reusable policy",
105 (stack) =>
106 Effect.gen(function* () {
107 const { accountId } = yield* yield* CloudflareEnvironment;
108 const name = testName("alchemy-access-policy-adopt-test");
109
110 yield* stack.destroy();
111
112 // Pre-create the policy out of band so adoption has something to find.
113 const preExisting = yield* zeroTrust.createAccessPolicy({
114 accountId,
115 name,
116 decision: "allow",
117 include: [{ emailDomain: { domain: "example.com" } }],
118 });
119 expect(preExisting.id).toBeDefined();
120
121 const adopted = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* Cloudflare.Access.Policy("AdoptPolicy", {
124 name,
125 decision: "allow",
126 include: [{ emailDomain: { domain: "example.com" } }],
127 adopt: true,
128 });
129 }),
130 );
131
132 expect(adopted.policyId).toEqual(preExisting.id);
133 expect(adopted.accountId).toEqual(accountId);
134
135 yield* stack.destroy();
136
137 const afterDestroy = yield* zeroTrust
138 .getAccessPolicy({ accountId, policyId: preExisting.id! })
139 .pipe(Effect.catch(() => Effect.succeed(undefined)));
140 expect(afterDestroy).toBeUndefined();
141 }).pipe(logLevel),
142 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
143 );
144
145 test.provider(
146 "list enumerates the deployed reusable policy",
147 (stack) =>
148 Effect.gen(function* () {
149 const { accountId } = yield* yield* CloudflareEnvironment;
150
151 yield* stack.destroy();
152
153 const policy = yield* stack.deploy(
154 Effect.gen(function* () {
155 return yield* Cloudflare.Access.Policy("ListPolicy", {
156 decision: "allow",
157 include: [{ emailDomain: { domain: "example.com" } }],
158 });
159 }),
160 );
161
162 const provider = yield* Provider.findProvider(Cloudflare.Access.Policy);
163 const all = yield* provider.list();
164
165 const match = all.find((p) => p.policyId === policy.policyId);
166 expect(match).toBeDefined();
167 expect(match?.accountId).toEqual(accountId);
168 expect(match?.decision).toEqual("allow");
169
170 yield* stack.destroy();
171 }).pipe(logLevel),
172 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
173 );
174
175 test.provider(
176 "connection rules and approval settings round-trip through create and update",
177 (stack) =>
178 Effect.gen(function* () {
179 const { accountId } = yield* yield* CloudflareEnvironment;
180
181 yield* stack.destroy();
182
183 const deploy = (usernames: string[]) =>
184 stack.deploy(
185 Effect.gen(function* () {
186 return yield* Cloudflare.Access.Policy("SshPolicy", {
187 decision: "allow",
188 include: [{ emailDomain: "example.com" }],
189 connectionRules: {
190 ssh: { usernames, allowEmailAlias: true },
191 rdp: { allowedClipboardLocalToRemoteFormats: ["text"] },
192 },
193 isolationRequired: false,
194 purposeJustificationRequired: true,
195 purposeJustificationPrompt: "Why do you need access?",
196 });
197 }),
198 );
199
200 const created = yield* deploy(["root"]);
201 const live1 = yield* zeroTrust.getAccessPolicy({
202 accountId,
203 policyId: created.policyId,
204 });
205 expect(live1.connectionRules?.ssh?.usernames).toEqual(["root"]);
206 expect(live1.connectionRules?.ssh?.allowEmailAlias).toBe(true);
207 expect(live1.connectionRules?.rdp?.allowedClipboardLocalToRemoteFormats).toEqual(["text"]);
208 expect(live1.purposeJustificationPrompt).toEqual("Why do you need access?");
209
210 const updated = yield* deploy(["root", "ubuntu"]);
211 expect(updated.policyId).toEqual(created.policyId);
212 const live2 = yield* zeroTrust.getAccessPolicy({
213 accountId,
214 policyId: updated.policyId,
215 });
216 expect(live2.connectionRules?.ssh?.usernames).toEqual(["root", "ubuntu"]);
217 expect(live2.purposeJustificationRequired).toBe(true);
218
219 yield* stack.destroy();
220 }).pipe(logLevel),
221 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
222 );
223
224 test.provider(
225 "adopted policy keeps the live fields it does not declare",
226 (stack) =>
227 Effect.gen(function* () {
228 const { accountId } = yield* yield* CloudflareEnvironment;
229 const name = `alchemy-access-policy-preserve-test-${stack.stage}`;
230
231 yield* stack.destroy();
232
233 const existing = yield* zeroTrust.createAccessPolicy({
234 accountId,
235 name,
236 decision: "allow",
237 include: [{ emailDomain: { domain: "example.com" } }],
238 exclude: [{ email: { email: "intern@example.com" } }],
239 require: [{ geo: { countryCode: "US" } }],
240 approvalRequired: true,
241 approvalGroups: [{ approvalsNeeded: 1, emailAddresses: ["lead@example.com"] }],
242 purposeJustificationRequired: true,
243 purposeJustificationPrompt: "Ticket number?",
244 connectionRules: {
245 ssh: { usernames: ["root", "ubuntu"], allowEmailAlias: true },
246 rdp: { allowedClipboardLocalToRemoteFormats: ["text"] },
247 },
248 });
249
250 const deploy = (props: Partial<Cloudflare.Access.PolicyProps>) =>
251 stack.deploy(
252 Cloudflare.Access.Policy("PreservePolicy", {
253 name,
254 decision: "allow",
255 include: [{ emailDomain: "example.com" }],
256 adopt: true,
257 ...props,
258 }),
259 );
260 const live = () => zeroTrust.getAccessPolicy({ accountId, policyId: existing.id! });
261
262 // Only sessionDuration is declared; every other live field survives the PUT.
263 const adopted = yield* deploy({ sessionDuration: "12h" });
264 expect(adopted.policyId).toEqual(existing.id);
265 const kept = yield* live();
266 expect(kept.sessionDuration).toEqual("12h");
267 expect(kept.exclude).toEqual([{ email: { email: "intern@example.com" } }]);
268 expect(kept.require).toEqual([{ geo: { countryCode: "US" } }]);
269 expect(kept.approvalRequired).toBe(true);
270 expect(kept.approvalGroups?.[0]?.emailAddresses).toEqual(["lead@example.com"]);
271 expect(kept.purposeJustificationRequired).toBe(true);
272 expect(kept.purposeJustificationPrompt).toEqual("Ticket number?");
273 expect(kept.connectionRules?.ssh?.usernames).toEqual(["root", "ubuntu"]);
274 expect(kept.connectionRules?.ssh?.allowEmailAlias).toBe(true);
275 expect(kept.connectionRules?.rdp?.allowedClipboardLocalToRemoteFormats).toEqual(["text"]);
276
277 // Explicit `[]` / `false` still clear.
278 yield* deploy({ sessionDuration: "12h", require: [], approvalRequired: false });
279 const cleared = yield* live();
280 expect(cleared.require ?? []).toEqual([]);
281 expect(cleared.approvalRequired ?? false).toBe(false);
282 expect(cleared.exclude).toEqual([{ email: { email: "intern@example.com" } }]);
283
284 yield* stack.destroy();
285
286 const afterDestroy = yield* live().pipe(Effect.catch(() => Effect.succeed(undefined)));
287 expect(afterDestroy).toBeUndefined();
288 }).pipe(logLevel),
289 { tags: ["provider:cloudflare", "provider:cloudflare:access", "live"] },
290 );