The best way to control Claude Code, Codex and more from any computer or phone
| 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 waitUntilGone = (name: string) => |
| 15 | scc.getFoldersNotificationConfigs({ name }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("1 second"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | // Folder-level Security Command Center needs a folder under an organization |
| 26 | // with SCC activated and folder-level SCC roles for the test identity. Set |
| 27 | // GCP_TEST_SECURITY_CENTER=1 and GOOGLE_FOLDER_ID when both hold. |
| 28 | const folderId = process.env.GOOGLE_FOLDER_ID; |
| 29 | const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER && !!folderId; |
| 30 | |
| 31 | const folderOf = () => |
| 32 | Effect.succeed( |
| 33 | folderId === undefined |
| 34 | ? "" |
| 35 | : folderId.startsWith("folders/") |
| 36 | ? folderId |
| 37 | : `folders/${folderId}`, |
| 38 | ); |
| 39 | |
| 40 | test.provider( |
| 41 | "getFoldersNotificationConfigs on a missing config fails with a typed tag", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const folder = (yield* folderOf()) || "folders/0"; |
| 47 | const error = yield* Effect.flip( |
| 48 | scc.getFoldersNotificationConfigs({ |
| 49 | name: `${folder}/notificationConfigs/alchemy-missing`, |
| 50 | }), |
| 51 | ); |
| 52 | expect(["ServiceDisabled", "Forbidden"]).toContain(error._tag); |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | }).pipe(logLevel), |
| 56 | { |
| 57 | tags: ["provider:gcp", "provider:gcp:securitycenter", "live"], |
| 58 | timeout: 90_000, |
| 59 | }, |
| 60 | ); |
| 61 | |
| 62 | test.provider.skipIf(!runLifecycle)( |
| 63 | "create, update, and delete a folder notification config", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | const { project } = yield* GcpEnvironment.current; |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | const folder = yield* folderOf(); |
| 71 | |
| 72 | const created = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | const topic = yield* GCP.PubSub.Topic("SccFindings", {}); |
| 75 | const config = yield* GCP.SecurityCenter.FolderNotificationConfig("Findings", { |
| 76 | folder, |
| 77 | pubsubTopic: topic.name, |
| 78 | filter: 'state="ACTIVE"', |
| 79 | description: "active findings", |
| 80 | }); |
| 81 | return { config, topicName: topic.name }; |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(created.config.configId).toEqual(expect.any(String)); |
| 86 | expect(created.config.folder).toEqual(folder); |
| 87 | expect(created.config.name).toEqual( |
| 88 | `${folder}/notificationConfigs/${created.config.configId}`, |
| 89 | ); |
| 90 | expect(created.config.filter).toEqual('state="ACTIVE"'); |
| 91 | expect(created.config.description).toEqual("active findings"); |
| 92 | |
| 93 | const fetched = yield* scc.getFoldersNotificationConfigs({ |
| 94 | name: created.config.name, |
| 95 | }); |
| 96 | expect(fetched.name).toEqual(created.config.name); |
| 97 | expect(fetched.description).toContain("alchemy-id="); |
| 98 | |
| 99 | const updated = yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | const topic = yield* GCP.PubSub.Topic("SccFindings", {}); |
| 102 | return yield* GCP.SecurityCenter.FolderNotificationConfig("Findings", { |
| 103 | folder, |
| 104 | configId: created.config.configId, |
| 105 | pubsubTopic: topic.name, |
| 106 | filter: 'state="INACTIVE"', |
| 107 | description: "inactive findings", |
| 108 | }); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | expect(updated.name).toEqual(created.config.name); |
| 113 | expect(updated.filter).toEqual('state="INACTIVE"'); |
| 114 | expect(updated.description).toEqual("inactive findings"); |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const gone = yield* waitUntilGone(created.config.name); |
| 119 | expect(gone).toEqual("gone"); |
| 120 | }).pipe(logLevel), |
| 121 | { |
| 122 | tags: ["provider:gcp", "provider:gcp:securitycenter", "live"], |
| 123 | timeout: 90_000, |
| 124 | }, |
| 125 | ); |