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