1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as scc from "@distilled.cloud/gcp/securitycenter_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 scc.getProjectsMuteConfigs({ name }).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// Security Command Center is not activated on the testing project (every
29// call fails with ServiceDisabled). Set GCP_TEST_SECURITY_CENTER=1 on a
30// project with SCC activated.
31const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER;
32
33test.provider(
34 "getProjectsMuteConfigs on a missing config fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 scc.getProjectsMuteConfigs({
43 name: `projects/${project}/muteConfigs/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("ServiceDisabled");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 {
51 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
52 timeout: 90_000,
53 },
54);
55
56test.provider.skipIf(!runLifecycle)(
57 "create, update, and delete a mute config",
58 (stack) =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.SecurityCenter.MuteConfig("Low", {
67 filter: 'severity="LOW"',
68 description: "mute low severity",
69 });
70 }),
71 );
72
73 expect(created.muteConfigId).toEqual(expect.any(String));
74 expect(created.name).toEqual(
75 `projects/${project}/muteConfigs/${created.muteConfigId}`,
76 );
77 expect(created.filter).toEqual('severity="LOW"');
78 expect(created.description).toEqual("mute low severity");
79
80 const fetched = yield* scc.getProjectsMuteConfigs({
81 name: created.name,
82 });
83 expect(fetched.name).toEqual(created.name);
84 expect(fetched.description).toContain("alchemy-id=");
85 expect(fetched.filter).toEqual('severity="LOW"');
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.SecurityCenter.MuteConfig("Low", {
90 muteConfigId: created.muteConfigId,
91 filter: 'severity="LOW" OR severity="MEDIUM"',
92 description: "mute low and medium",
93 });
94 }),
95 );
96
97 expect(updated.name).toEqual(created.name);
98 expect(updated.filter).toEqual('severity="LOW" OR severity="MEDIUM"');
99 expect(updated.description).toEqual("mute low and medium");
100
101 yield* stack.destroy();
102
103 const gone = yield* waitUntilGone(created.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel),
106 {
107 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
108 timeout: 90_000,
109 },
110);