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 { 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.getOrganizationsEventThreatDetectionSettingsCustomModules({ 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 | // Organization-level Security Command Center needs SCC activated on the |
| 39 | // organization and org-level SCC roles for the test identity. Set |
| 40 | // GCP_TEST_SECURITY_CENTER=1 and GOOGLE_ORGANIZATION_ID when both hold. |
| 41 | const organizationId = process.env.GOOGLE_ORGANIZATION_ID; |
| 42 | const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER && !!organizationId; |
| 43 | |
| 44 | const organizationOf = () => |
| 45 | Effect.succeed( |
| 46 | organizationId === undefined |
| 47 | ? "" |
| 48 | : organizationId.startsWith("organizations/") |
| 49 | ? organizationId |
| 50 | : `organizations/${organizationId}`, |
| 51 | ); |
| 52 | |
| 53 | test.provider( |
| 54 | "getOrganizationsEventThreatDetectionSettingsCustomModules on a missing module fails with a typed tag", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const organization = (yield* organizationOf()) || "organizations/0"; |
| 60 | const error = yield* Effect.flip( |
| 61 | scc.getOrganizationsEventThreatDetectionSettingsCustomModules({ |
| 62 | name: `${organization}/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 an organization Event Threat Detection custom module", |
| 77 | (stack) => |
| 78 | Effect.gen(function* () { |
| 79 | yield* stack.destroy(); |
| 80 | |
| 81 | const organization = yield* organizationOf(); |
| 82 | const parent = organization |
| 83 | ? `${organization}/eventThreatDetectionSettings` |
| 84 | : "organizations/0/eventThreatDetectionSettings"; |
| 85 | |
| 86 | const created = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* GCP.SecurityCenter.OrganizationEventThreatDetectionSettingsCustomModule( |
| 89 | "BadIp", |
| 90 | { |
| 91 | organization, |
| 92 | type: "CONFIGURABLE_BAD_IP", |
| 93 | config: configV1, |
| 94 | description: "test bad ip", |
| 95 | }, |
| 96 | ); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | expect(created.moduleId).toEqual(expect.any(String)); |
| 101 | expect(created.organization).toEqual(organization); |
| 102 | expect(created.name).toContain(`${organization}/eventThreatDetectionSettings/customModules/`); |
| 103 | expect(created.description).toEqual("test bad ip"); |
| 104 | |
| 105 | const fetched = yield* scc.getOrganizationsEventThreatDetectionSettingsCustomModules({ |
| 106 | name: created.name, |
| 107 | }); |
| 108 | expect(fetched.name).toEqual(created.name); |
| 109 | expect(fetched.description).toContain("alchemy-id="); |
| 110 | |
| 111 | const updated = yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | return yield* GCP.SecurityCenter.OrganizationEventThreatDetectionSettingsCustomModule( |
| 114 | "BadIp", |
| 115 | { |
| 116 | organization, |
| 117 | type: "CONFIGURABLE_BAD_IP", |
| 118 | config: configV2, |
| 119 | description: "updated bad ip", |
| 120 | }, |
| 121 | ); |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | expect(updated.name).toEqual(created.name); |
| 126 | expect(updated.description).toEqual("updated bad ip"); |
| 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 | ); |