The best way to control Claude Code, Codex and more from any computer or phone

main
ts 124 lines 4.15 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 scc from "@distilled.cloud/gcp/securitycenter_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const badIpConfig = {
15 metadata: {
16 severity: "LOW",
17 description: "test",
18 recommendation: "investigate",
19 },
20 ips: ["192.0.2.1"],
21 };
22
23 const waitUntilGone = (name: string) =>
24 scc.getProjectsEventThreatDetectionSettingsCustomModules({ 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 // Security Command Center is not activated on the testing project (every
35 // call fails with ServiceDisabled). Set GCP_TEST_SECURITY_CENTER=1 on a
36 // project with SCC activated.
37 const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER;
38
39 test.provider(
40 "getProjectsEventThreatDetectionSettingsCustomModules on a missing module fails with a typed tag",
41 (stack) =>
42 Effect.gen(function* () {
43 const { project } = yield* GcpEnvironment.current;
44 const parent = `projects/${project}/eventThreatDetectionSettings`;
45
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 scc.getProjectsEventThreatDetectionSettingsCustomModules({
50 name: `${parent}/customModules/alchemy-missing`,
51 }),
52 );
53 expect(["ServiceDisabled", "SecurityCommandCenterLegacyDisabled"]).toContain(error._tag);
54
55 yield* stack.destroy();
56 }).pipe(logLevel),
57 {
58 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
59 timeout: 90_000,
60 },
61 );
62
63 test.provider.skipIf(!runLifecycle)(
64 "create, update, and delete an event threat detection custom module",
65 (stack) =>
66 Effect.gen(function* () {
67 const { project } = yield* GcpEnvironment.current;
68 const parent = `projects/${project}/eventThreatDetectionSettings`;
69
70 yield* stack.destroy();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* GCP.SecurityCenter.EventThreatDetectionSettingsCustomModule("BadIp", {
75 type: "CONFIGURABLE_BAD_IP",
76 displayName: "alchemy_bad_ip",
77 description: "test bad ip",
78 config: badIpConfig,
79 });
80 }),
81 );
82
83 expect(created.moduleId).toEqual(expect.any(String));
84 expect(created.name).toEqual(`${parent}/customModules/${created.moduleId}`);
85 expect(created.type).toEqual("CONFIGURABLE_BAD_IP");
86 expect(created.description).toEqual("test bad ip");
87 expect(created.enablementState).toEqual("ENABLED");
88
89 const fetched = yield* scc.getProjectsEventThreatDetectionSettingsCustomModules({
90 name: created.name,
91 });
92 expect(fetched.name).toEqual(created.name);
93 expect(fetched.description).toContain("alchemy-id=");
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.SecurityCenter.EventThreatDetectionSettingsCustomModule("BadIp", {
98 moduleId: created.moduleId,
99 type: "CONFIGURABLE_BAD_IP",
100 displayName: "alchemy_bad_ip",
101 description: "updated bad ip",
102 enablementState: "DISABLED",
103 config: {
104 ...badIpConfig,
105 ips: ["192.0.2.1", "192.0.2.0/24"],
106 },
107 });
108 }),
109 );
110
111 expect(updated.name).toEqual(created.name);
112 expect(updated.description).toEqual("updated bad ip");
113 expect(updated.enablementState).toEqual("DISABLED");
114
115 yield* stack.destroy();
116
117 const gone = yield* waitUntilGone(created.name);
118 expect(gone).toEqual("gone");
119 }).pipe(logLevel),
120 {
121 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
122 timeout: 90_000,
123 },
124 );