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 compute from "@distilled.cloud/gcp/compute_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 waitUntilGone = (project: string, securityPolicy: string) => |
| 14 | compute.getSecurityPolicies({ project, securityPolicy }).pipe( |
| 15 | Effect.as("found" as const), |
| 16 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 17 | Effect.repeat({ |
| 18 | schedule: Schedule.spaced("1 second"), |
| 19 | until: (status) => status === "gone", |
| 20 | times: 10, |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | const ruleAt = (rules: compute.SecurityPolicyRule[] | undefined, priority: number) => |
| 25 | (rules ?? []).find((rule) => rule.priority === priority); |
| 26 | |
| 27 | test.provider( |
| 28 | "create, update, replace, and delete a security policy", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | return yield* GCP.Compute.SecurityPolicy("Armor", { |
| 36 | description: "deny scanner", |
| 37 | labels: { env: "test" }, |
| 38 | rules: [ |
| 39 | { |
| 40 | action: "deny(403)", |
| 41 | priority: 1000, |
| 42 | description: "block scanner", |
| 43 | match: { |
| 44 | versionedExpr: "SRC_IPS_V1", |
| 45 | config: { srcIpRanges: ["9.9.9.0/24"] }, |
| 46 | }, |
| 47 | }, |
| 48 | ], |
| 49 | }); |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | expect(created.securityPolicyName).toEqual(expect.any(String)); |
| 54 | expect(created.type).toEqual("CLOUD_ARMOR"); |
| 55 | expect(created.description).toEqual("deny scanner"); |
| 56 | expect(created.labels).toMatchObject({ env: "test" }); |
| 57 | expect(ruleAt(created.rules, 1000)?.action).toEqual("deny(403)"); |
| 58 | expect(ruleAt(created.rules, 2147483647)?.action).toEqual("allow"); |
| 59 | |
| 60 | const fetched = yield* compute.getSecurityPolicies({ |
| 61 | project: created.project, |
| 62 | securityPolicy: created.securityPolicyName, |
| 63 | }); |
| 64 | expect(fetched.name).toEqual(created.securityPolicyName); |
| 65 | expect(fetched.type).toEqual("CLOUD_ARMOR"); |
| 66 | expect(fetched.description).toEqual("deny scanner"); |
| 67 | expect(fetched.labels?.env).toEqual("test"); |
| 68 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 69 | expect(ruleAt(fetched.rules, 1000)?.match?.config?.srcIpRanges).toEqual( |
| 70 | expect.arrayContaining(["9.9.9.0/24"]), |
| 71 | ); |
| 72 | |
| 73 | const listed = yield* compute.listSecurityPolicies({ |
| 74 | project: created.project, |
| 75 | filter: "labels.alchemy-id:*", |
| 76 | maxResults: 500, |
| 77 | }); |
| 78 | expect( |
| 79 | (listed.items ?? []).some((policy) => policy.name === created.securityPolicyName), |
| 80 | ).toEqual(true); |
| 81 | |
| 82 | const updated = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* GCP.Compute.SecurityPolicy("Armor", { |
| 85 | securityPolicyName: created.securityPolicyName, |
| 86 | description: "updated armor", |
| 87 | labels: { env: "prod", role: "waf" }, |
| 88 | advancedOptionsConfig: { |
| 89 | jsonParsing: "STANDARD", |
| 90 | logLevel: "VERBOSE", |
| 91 | }, |
| 92 | rules: [ |
| 93 | { |
| 94 | action: "deny(403)", |
| 95 | priority: 1000, |
| 96 | description: "block scanners", |
| 97 | match: { |
| 98 | versionedExpr: "SRC_IPS_V1", |
| 99 | config: { srcIpRanges: ["9.9.9.0/24", "8.8.8.8/32"] }, |
| 100 | }, |
| 101 | }, |
| 102 | { |
| 103 | action: "allow", |
| 104 | priority: 2147483647, |
| 105 | match: { |
| 106 | versionedExpr: "SRC_IPS_V1", |
| 107 | config: { srcIpRanges: ["*"] }, |
| 108 | }, |
| 109 | }, |
| 110 | ], |
| 111 | }); |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | expect(updated.securityPolicyName).toEqual(created.securityPolicyName); |
| 116 | expect(updated.securityPolicyId).toEqual(created.securityPolicyId); |
| 117 | expect(updated.description).toEqual("updated armor"); |
| 118 | expect(updated.labels).toMatchObject({ env: "prod", role: "waf" }); |
| 119 | expect(updated.advancedOptionsConfig?.jsonParsing).toEqual("STANDARD"); |
| 120 | expect(updated.advancedOptionsConfig?.logLevel).toEqual("VERBOSE"); |
| 121 | expect(ruleAt(updated.rules, 1000)?.match?.config?.srcIpRanges?.sort()).toEqual([ |
| 122 | "8.8.8.8/32", |
| 123 | "9.9.9.0/24", |
| 124 | ]); |
| 125 | |
| 126 | const fetchedUpdate = yield* compute.getSecurityPolicies({ |
| 127 | project: updated.project, |
| 128 | securityPolicy: updated.securityPolicyName, |
| 129 | }); |
| 130 | expect(fetchedUpdate.description).toEqual("updated armor"); |
| 131 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 132 | expect(fetchedUpdate.labels?.role).toEqual("waf"); |
| 133 | expect(fetchedUpdate.advancedOptionsConfig?.jsonParsing).toEqual("STANDARD"); |
| 134 | |
| 135 | const replaced = yield* stack.deploy( |
| 136 | Effect.gen(function* () { |
| 137 | return yield* GCP.Compute.SecurityPolicy("Armor", { |
| 138 | securityPolicyName: created.securityPolicyName, |
| 139 | type: "CLOUD_ARMOR_EDGE", |
| 140 | description: "edge policy", |
| 141 | labels: { env: "prod" }, |
| 142 | rules: [ |
| 143 | { |
| 144 | action: "deny(403)", |
| 145 | priority: 1000, |
| 146 | match: { |
| 147 | versionedExpr: "SRC_IPS_V1", |
| 148 | config: { srcIpRanges: ["9.9.9.0/24"] }, |
| 149 | }, |
| 150 | }, |
| 151 | ], |
| 152 | }); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | expect(replaced.securityPolicyName).toEqual(created.securityPolicyName); |
| 157 | expect(replaced.type).toEqual("CLOUD_ARMOR_EDGE"); |
| 158 | expect(replaced.description).toEqual("edge policy"); |
| 159 | expect(replaced.securityPolicyId).not.toEqual(created.securityPolicyId); |
| 160 | |
| 161 | const fetchedReplace = yield* compute.getSecurityPolicies({ |
| 162 | project: replaced.project, |
| 163 | securityPolicy: replaced.securityPolicyName, |
| 164 | }); |
| 165 | expect(fetchedReplace.type).toEqual("CLOUD_ARMOR_EDGE"); |
| 166 | expect(fetchedReplace.description).toEqual("edge policy"); |
| 167 | |
| 168 | yield* stack.destroy(); |
| 169 | |
| 170 | const gone = yield* waitUntilGone(created.project, created.securityPolicyName); |
| 171 | expect(gone).toEqual("gone"); |
| 172 | }).pipe(logLevel), |
| 173 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 174 | ); |