@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 138 lines 4.67 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as scc from "@distilled.cloud/gcp/securitycenter_v1";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const alwaysTrue = {
14 predicate: { expression: 'resource.name == "alchemy-nonexistent"' },
15 resourceSelector: {
16 resourceTypes: ["compute.googleapis.com/Instance"],
17 },
18 severity: "LOW" as const,
19 description: "always true",
20 recommendation: "n/a",
21 };
22
23 const waitUntilGone = (name: string) =>
24 scc.getOrganizationsSecurityHealthAnalyticsSettingsCustomModules({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 // Organization-level Security Command Center needs SCC activated on the
35 // organization and org-level SCC roles for the test identity. Set
36 // GCP_TEST_SECURITY_CENTER=1 and GOOGLE_ORGANIZATION_ID when both hold.
37 const organizationId = process.env.GOOGLE_ORGANIZATION_ID;
38 const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER && !!organizationId;
39
40 const organizationOf = () =>
41 Effect.succeed(
42 organizationId === undefined
43 ? ""
44 : organizationId.startsWith("organizations/")
45 ? organizationId
46 : `organizations/${organizationId}`,
47 );
48
49 test.provider(
50 "getOrganizationsSecurityHealthAnalyticsSettingsCustomModules on a missing module fails with a typed tag",
51 (stack) =>
52 Effect.gen(function* () {
53 yield* stack.destroy();
54
55 const organization = (yield* organizationOf()) || "organizations/0";
56 const error = yield* Effect.flip(
57 scc.getOrganizationsSecurityHealthAnalyticsSettingsCustomModules({
58 name: `${organization}/securityHealthAnalyticsSettings/customModules/alchemy-missing`,
59 }),
60 );
61 expect(["ServiceDisabled", "Forbidden"]).toContain(error._tag);
62
63 yield* stack.destroy();
64 }).pipe(logLevel),
65 {
66 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
67 timeout: 90_000,
68 },
69 );
70
71 test.provider.skipIf(!runLifecycle)(
72 "create, update, and delete an organization security health analytics custom module",
73 (stack) =>
74 Effect.gen(function* () {
75 yield* stack.destroy();
76
77 const organization = yield* organizationOf();
78
79 const parent = `${organization}/securityHealthAnalyticsSettings`;
80
81 const created = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModule(
84 "AlwaysTrue",
85 {
86 organization,
87 displayName: "AlchemyOrgAlwaysTrue",
88 customConfig: alwaysTrue,
89 },
90 );
91 }),
92 );
93
94 expect(created.moduleId).toEqual(expect.any(String));
95 expect(created.organization).toEqual(organization);
96 expect(created.name).toEqual(`${parent}/customModules/${created.moduleId}`);
97 expect(created.displayName).toEqual("AlchemyOrgAlwaysTrue");
98 expect(created.customConfig?.description).toEqual("always true");
99
100 const fetched = yield* scc.getOrganizationsSecurityHealthAnalyticsSettingsCustomModules({
101 name: created.name,
102 });
103 expect(fetched.name).toEqual(created.name);
104 expect(fetched.customConfig?.description).toContain("alchemy-id=");
105
106 const updated = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModule(
109 "AlwaysTrue",
110 {
111 organization,
112 moduleId: created.moduleId,
113 displayName: "AlchemyOrgAlwaysTrue",
114 enablementState: "DISABLED",
115 customConfig: {
116 ...alwaysTrue,
117 description: "always true disabled",
118 severity: "MEDIUM",
119 },
120 },
121 );
122 }),
123 );
124
125 expect(updated.name).toEqual(created.name);
126 expect(updated.enablementState).toEqual("DISABLED");
127 expect(updated.customConfig?.description).toEqual("always true disabled");
128
129 yield* stack.destroy();
130
131 const gone = yield* waitUntilGone(created.name);
132 expect(gone).toEqual("gone");
133 }).pipe(logLevel),
134 {
135 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
136 timeout: 90_000,
137 },
138 );