@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 204 lines 7.98 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as orgpolicy from "@distilled.cloud/gcp/orgpolicy_v2";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const waitUntilGone = (name: string) =>
16 orgpolicy.getOrganizationsCustomConstraints({ name }).pipe(
17 Effect.as("found" as const),
18 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
19 Effect.repeat({
20 schedule: Schedule.spaced("1 second"),
21 until: (status) => status === "gone",
22 times: 10,
23 }),
24 );
25
26 const organizationOf = () =>
27 Effect.gen(function* () {
28 const { project } = yield* GcpEnvironment.current;
29 const fromEnv = process.env.GOOGLE_ORGANIZATION_ID;
30 if (fromEnv && fromEnv.length > 0) {
31 return fromEnv.startsWith("organizations/") ? fromEnv : `organizations/${fromEnv}`;
32 }
33 let current: string | undefined = `projects/${project}`;
34 for (let i = 0; i < 8; i++) {
35 if (current === undefined) return "";
36 if (current.startsWith("organizations/")) return current;
37 current = current.startsWith("projects/")
38 ? yield* resourcemanager.getProjects({ name: current }).pipe(
39 Effect.map((resource) => resource.parent),
40 Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
41 )
42 : current.startsWith("folders/")
43 ? yield* resourcemanager.getFolders({ name: current }).pipe(
44 Effect.map((folder) => folder.parent),
45 Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
46 )
47 : undefined;
48 }
49 return "";
50 });
51
52 const probeCreate = (parent: string) =>
53 orgpolicy.createOrganizationsCustomConstraints({
54 parent,
55 body: {
56 name: `${parent}/customConstraints/custom.alchemyMissingProbe`,
57 resourceTypes: ["compute.googleapis.com/Instance"],
58 methodTypes: ["CREATE"],
59 condition: "resource.name.startsWith('alchemy-probe-')",
60 actionType: "DENY",
61 displayName: "alchemy probe",
62 },
63 });
64
65 test.provider(
66 "getOrganizationsCustomConstraints on a missing constraint fails with a typed tag",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const organization = (yield* organizationOf()) || "organizations/0";
72 const error = yield* Effect.flip(
73 orgpolicy.getOrganizationsCustomConstraints({
74 name: `${organization}/customConstraints/custom.alchemyDoesNotExist`,
75 }),
76 );
77 expect(error._tag).toEqual("Forbidden");
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 { tags: ["provider:gcp", "provider:gcp:orgpolicy", "live"], timeout: 90_000 },
82 );
83
84 // Custom constraints need Organization Policy Administrator on the org; the
85 // testing credentials get Forbidden (probe). Set GOOGLE_ORGANIZATION_ID when
86 // the credentials administer the org to run the lifecycle.
87 const runLifecycle = !!process.env.GOOGLE_ORGANIZATION_ID;
88
89 test.provider.skipIf(runLifecycle)(
90 "createOrganizationsCustomConstraints without org policy admin fails with a typed tag",
91 (stack) =>
92 Effect.gen(function* () {
93 yield* stack.destroy();
94 const organization = (yield* organizationOf()) || "organizations/0";
95 const error = yield* Effect.flip(probeCreate(organization));
96 expect(error._tag).toEqual("Forbidden");
97 yield* stack.destroy();
98 }).pipe(logLevel),
99 { tags: ["provider:gcp", "provider:gcp:orgpolicy", "live"], timeout: 90_000 },
100 );
101
102 test.provider.skipIf(!runLifecycle)(
103 "create, update, replace, and delete an organization custom constraint",
104 (stack) =>
105 Effect.gen(function* () {
106 const { project } = yield* GcpEnvironment.current;
107 yield* stack.destroy();
108
109 const organization = yield* organizationOf();
110
111 const created = yield* stack.deploy(
112 Effect.gen(function* () {
113 return yield* GCP.OrgPolicy.CustomConstraint("NoTestVms", {
114 organization,
115 resourceTypes: ["compute.googleapis.com/Instance"],
116 methodTypes: ["CREATE"],
117 condition: "resource.name.startsWith('test-')",
118 actionType: "DENY",
119 displayName: "Deny test VMs",
120 description: "blocks test-prefixed instances",
121 });
122 }),
123 );
124
125 expect(created.constraintId.startsWith("custom.")).toEqual(true);
126 expect(created.organization).toEqual(organization);
127 expect(created.name).toEqual(`${organization}/customConstraints/${created.constraintId}`);
128 expect(created.project).toEqual(project);
129 expect(created.resourceTypes).toEqual(["compute.googleapis.com/Instance"]);
130 expect(created.methodTypes).toEqual(["CREATE"]);
131 expect(created.condition).toEqual("resource.name.startsWith('test-')");
132 expect(created.actionType).toEqual("DENY");
133 expect(created.displayName).toEqual("Deny test VMs");
134 expect(created.description).toEqual("blocks test-prefixed instances");
135
136 const fetched = yield* orgpolicy.getOrganizationsCustomConstraints({
137 name: created.name,
138 });
139 expect(fetched.name).toEqual(created.name);
140 expect(fetched.condition).toEqual(created.condition);
141 expect(fetched.actionType).toEqual("DENY");
142 expect(fetched.description ?? "").toContain("blocks test-prefixed instances");
143
144 const updated = yield* stack.deploy(
145 Effect.gen(function* () {
146 return yield* GCP.OrgPolicy.CustomConstraint("NoTestVms", {
147 organization,
148 constraintId: created.constraintId,
149 resourceTypes: ["compute.googleapis.com/Instance"],
150 methodTypes: ["CREATE", "UPDATE"],
151 condition: "resource.name.startsWith('tmp-')",
152 actionType: "DENY",
153 displayName: "Deny tmp VMs",
154 description: "blocks tmp-prefixed instances",
155 });
156 }),
157 );
158
159 expect(updated.name).toEqual(created.name);
160 expect(updated.constraintId).toEqual(created.constraintId);
161 expect(updated.methodTypes).toEqual(["CREATE", "UPDATE"]);
162 expect(updated.condition).toEqual("resource.name.startsWith('tmp-')");
163 expect(updated.displayName).toEqual("Deny tmp VMs");
164 expect(updated.description).toEqual("blocks tmp-prefixed instances");
165
166 const fetchedUpdate = yield* orgpolicy.getOrganizationsCustomConstraints({
167 name: updated.name,
168 });
169 expect(fetchedUpdate.condition).toEqual(updated.condition);
170 expect(fetchedUpdate.methodTypes).toEqual(["CREATE", "UPDATE"]);
171
172 const replaced = yield* stack.deploy(
173 Effect.gen(function* () {
174 return yield* GCP.OrgPolicy.CustomConstraint("NoTestVms", {
175 organization,
176 constraintId: `${created.constraintId}.b`,
177 resourceTypes: ["compute.googleapis.com/Instance"],
178 methodTypes: ["CREATE"],
179 condition: "resource.name.startsWith('tmp-')",
180 actionType: "DENY",
181 displayName: "Deny tmp VMs",
182 });
183 }),
184 );
185
186 expect(replaced.constraintId).toEqual(`${created.constraintId}.b`);
187 expect(replaced.name).not.toEqual(created.name);
188 expect(replaced.name).toContain(`/customConstraints/${replaced.constraintId}`);
189
190 const fetchedReplace = yield* orgpolicy.getOrganizationsCustomConstraints({
191 name: replaced.name,
192 });
193 expect(fetchedReplace.name).toEqual(replaced.name);
194
195 const oldGone = yield* waitUntilGone(created.name);
196 expect(oldGone).toEqual("gone");
197
198 yield* stack.destroy();
199
200 const gone = yield* waitUntilGone(replaced.name);
201 expect(gone).toEqual("gone");
202 }).pipe(logLevel),
203 { tags: ["provider:gcp", "provider:gcp:orgpolicy", "live"], timeout: 90_000 },
204 );