@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 143 lines 4.78 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 compute from "@distilled.cloud/gcp/compute_v1";
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 runLifecycle = !!process.env.GCP_TEST_ORG_SECURITY_POLICY && !process.env.FAST;
16
17 const waitUntilGone = (securityPolicy: string) =>
18 compute.getOrganizationSecurityPolicies({ securityPolicy }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28 const ruleAt = (rules: compute.SecurityPolicyRule[] | undefined, priority: number) =>
29 (rules ?? []).find((rule) => rule.priority === priority);
30
31 test.provider(
32 "getOrganizationSecurityPolicies with an invalid id fails with BadRequest",
33 (stack) =>
34 Effect.gen(function* () {
35 yield* stack.destroy();
36
37 const error = yield* Effect.flip(
38 compute.getOrganizationSecurityPolicies({
39 securityPolicy: "0",
40 }),
41 );
42 expect(error._tag).toEqual("BadRequest");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
47 );
48
49 test.provider.skipIf(!!process.env.GCP_TEST_ORG_SECURITY_POLICY)(
50 "insertOrganizationSecurityPolicies without org IAM fails with Forbidden",
51 (stack) =>
52 Effect.gen(function* () {
53 const { project } = yield* GcpEnvironment.current;
54 yield* stack.destroy();
55
56 const resource = yield* resourcemanager.getProjects({
57 name: `projects/${project}`,
58 });
59 expect(resource.parent).toEqual(expect.any(String));
60
61 const error = yield* Effect.flip(
62 compute.insertOrganizationSecurityPolicies({
63 parentId: resource.parent,
64 body: {
65 shortName: "alchemy-orgarmor-probe",
66 description: "probe",
67 type: "CLOUD_ARMOR",
68 },
69 }),
70 );
71 expect(error._tag).toEqual("Forbidden");
72
73 yield* stack.destroy();
74 }).pipe(logLevel),
75 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
76 );
77
78 test.provider.skipIf(!runLifecycle)(
79 "create, update, and delete an organization security policy",
80 (stack) =>
81 Effect.gen(function* () {
82 yield* stack.destroy();
83
84 const created = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* GCP.Compute.OrganizationSecurityPolicy("OrgArmor", {
87 description: "deny scanner",
88 rules: [
89 {
90 action: "deny(403)",
91 priority: 1000,
92 match: {
93 versionedExpr: "SRC_IPS_V1",
94 config: { srcIpRanges: ["9.9.9.0/24"] },
95 },
96 },
97 ],
98 });
99 }),
100 );
101
102 expect(created.shortName).toEqual(expect.any(String));
103 expect(created.securityPolicyId).toEqual(expect.any(String));
104 expect(created.type).toEqual("CLOUD_ARMOR");
105 expect(created.description).toEqual("deny scanner");
106 expect(ruleAt(created.rules, 1000)?.action).toEqual("deny(403)");
107
108 const fetched = yield* compute.getOrganizationSecurityPolicies({
109 securityPolicy: created.securityPolicyId,
110 });
111 expect(fetched.shortName).toEqual(created.shortName);
112 expect(fetched.description).toContain("[alchemy ");
113
114 const updated = yield* stack.deploy(
115 Effect.gen(function* () {
116 return yield* GCP.Compute.OrganizationSecurityPolicy("OrgArmor", {
117 shortName: created.shortName,
118 parent: created.parent,
119 description: "updated armor",
120 rules: [
121 {
122 action: "deny(403)",
123 priority: 1000,
124 match: {
125 versionedExpr: "SRC_IPS_V1",
126 config: { srcIpRanges: ["9.9.9.0/24", "8.8.8.8/32"] },
127 },
128 },
129 ],
130 });
131 }),
132 );
133
134 expect(updated.securityPolicyId).toEqual(created.securityPolicyId);
135 expect(updated.description).toEqual("updated armor");
136
137 yield* stack.destroy();
138
139 const gone = yield* waitUntilGone(created.securityPolicyId);
140 expect(gone).toEqual("gone");
141 }).pipe(logLevel),
142 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
143 );