The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 region = "us-central1"; |
| 14 | |
| 15 | const waitUntilGone = (project: string, securityPolicy: string) => |
| 16 | compute.getRegionSecurityPolicies({ project, region, securityPolicy }).pipe( |
| 17 | Effect.as("found" as const), |
| 18 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 19 | Effect.repeat({ |
| 20 | schedule: Schedule.spaced("1 second"), |
| 21 | until: (status) => status === "gone", |
| 22 | times: 10, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | const ruleAt = (rules: compute.SecurityPolicyRule[] | undefined, priority: number) => |
| 27 | (rules ?? []).find((rule) => rule.priority === priority); |
| 28 | |
| 29 | test.provider( |
| 30 | "create, update, and delete a regional security policy", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const created = yield* stack.deploy( |
| 36 | Effect.gen(function* () { |
| 37 | return yield* GCP.Compute.RegionSecurityPolicy("Armor", { |
| 38 | region, |
| 39 | description: "deny scanner", |
| 40 | labels: { env: "test" }, |
| 41 | rules: [ |
| 42 | { |
| 43 | action: "deny(403)", |
| 44 | priority: 1000, |
| 45 | description: "block scanner", |
| 46 | match: { |
| 47 | versionedExpr: "SRC_IPS_V1", |
| 48 | config: { srcIpRanges: ["9.9.9.0/24"] }, |
| 49 | }, |
| 50 | }, |
| 51 | ], |
| 52 | }); |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | expect(created.securityPolicyName).toEqual(expect.any(String)); |
| 57 | expect(created.region).toEqual(region); |
| 58 | expect(created.type).toEqual("CLOUD_ARMOR"); |
| 59 | expect(created.description).toEqual("deny scanner"); |
| 60 | expect(created.labels).toMatchObject({ env: "test" }); |
| 61 | expect(ruleAt(created.rules, 1000)?.action).toEqual("deny(403)"); |
| 62 | expect(ruleAt(created.rules, 2147483647)?.action).toEqual("allow"); |
| 63 | |
| 64 | const fetched = yield* compute.getRegionSecurityPolicies({ |
| 65 | project: created.project, |
| 66 | region, |
| 67 | securityPolicy: created.securityPolicyName, |
| 68 | }); |
| 69 | expect(fetched.name).toEqual(created.securityPolicyName); |
| 70 | expect(fetched.type).toEqual("CLOUD_ARMOR"); |
| 71 | expect(fetched.description).toEqual("deny scanner"); |
| 72 | expect(fetched.labels?.env).toEqual("test"); |
| 73 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 74 | expect(ruleAt(fetched.rules, 1000)?.match?.config?.srcIpRanges).toEqual( |
| 75 | expect.arrayContaining(["9.9.9.0/24"]), |
| 76 | ); |
| 77 | |
| 78 | const listed = yield* compute.listRegionSecurityPolicies({ |
| 79 | project: created.project, |
| 80 | region, |
| 81 | filter: "labels.alchemy-id:*", |
| 82 | maxResults: 500, |
| 83 | }); |
| 84 | expect( |
| 85 | (listed.items ?? []).some((policy) => policy.name === created.securityPolicyName), |
| 86 | ).toEqual(true); |
| 87 | |
| 88 | const updated = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* GCP.Compute.RegionSecurityPolicy("Armor", { |
| 91 | securityPolicyName: created.securityPolicyName, |
| 92 | region, |
| 93 | description: "updated armor", |
| 94 | labels: { env: "prod", role: "waf" }, |
| 95 | advancedOptionsConfig: { |
| 96 | jsonParsing: "STANDARD", |
| 97 | logLevel: "VERBOSE", |
| 98 | }, |
| 99 | rules: [ |
| 100 | { |
| 101 | action: "deny(403)", |
| 102 | priority: 1000, |
| 103 | description: "block scanners", |
| 104 | match: { |
| 105 | versionedExpr: "SRC_IPS_V1", |
| 106 | config: { srcIpRanges: ["9.9.9.0/24", "8.8.8.8/32"] }, |
| 107 | }, |
| 108 | }, |
| 109 | { |
| 110 | action: "allow", |
| 111 | priority: 2147483647, |
| 112 | match: { |
| 113 | versionedExpr: "SRC_IPS_V1", |
| 114 | config: { srcIpRanges: ["*"] }, |
| 115 | }, |
| 116 | }, |
| 117 | ], |
| 118 | }); |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | expect(updated.securityPolicyName).toEqual(created.securityPolicyName); |
| 123 | expect(updated.securityPolicyId).toEqual(created.securityPolicyId); |
| 124 | expect(updated.description).toEqual("updated armor"); |
| 125 | expect(updated.labels).toMatchObject({ env: "prod", role: "waf" }); |
| 126 | expect(updated.advancedOptionsConfig?.jsonParsing).toEqual("STANDARD"); |
| 127 | expect(updated.advancedOptionsConfig?.logLevel).toEqual("VERBOSE"); |
| 128 | expect(ruleAt(updated.rules, 1000)?.match?.config?.srcIpRanges?.sort()).toEqual([ |
| 129 | "8.8.8.8/32", |
| 130 | "9.9.9.0/24", |
| 131 | ]); |
| 132 | |
| 133 | const fetchedUpdate = yield* compute.getRegionSecurityPolicies({ |
| 134 | project: updated.project, |
| 135 | region, |
| 136 | securityPolicy: updated.securityPolicyName, |
| 137 | }); |
| 138 | expect(fetchedUpdate.description).toEqual("updated armor"); |
| 139 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 140 | expect(fetchedUpdate.labels?.role).toEqual("waf"); |
| 141 | expect(fetchedUpdate.advancedOptionsConfig?.jsonParsing).toEqual("STANDARD"); |
| 142 | |
| 143 | yield* stack.destroy(); |
| 144 | |
| 145 | const gone = yield* waitUntilGone(created.project, created.securityPolicyName); |
| 146 | expect(gone).toEqual("gone"); |
| 147 | }).pipe(logLevel), |
| 148 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 149 | ); |