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