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 { 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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const runLifecycle = !!process.env.GCP_TEST_FIREWALL_POLICY && !process.env.FAST; |
| 16 | |
| 17 | const waitUntilGone = (firewallPolicy: string) => |
| 18 | compute.getFirewallPolicies({ firewallPolicy }).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 | const ruleAt = (rules: compute.FirewallPolicyRule[] | undefined, priority: number) => |
| 29 | (rules ?? []).find((rule) => rule.priority === priority); |
| 30 | |
| 31 | const nextShortName = (name: string) => (name.length < 63 ? `${name}x` : `${name.slice(0, 62)}x`); |
| 32 | |
| 33 | test.provider( |
| 34 | "getFirewallPolicies on a missing policy fails with NotFound", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | compute.getFirewallPolicies({ |
| 41 | firewallPolicy: "0", |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toBe("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 49 | ); |
| 50 | |
| 51 | test.provider.skipIf(!!process.env.GCP_TEST_FIREWALL_POLICY)( |
| 52 | "insertFirewallPolicies without org IAM fails with Forbidden", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | const { project } = yield* GcpEnvironment.current; |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const resource = yield* resourcemanager.getProjects({ |
| 59 | name: `projects/${project}`, |
| 60 | }); |
| 61 | expect(resource.parent).toEqual(expect.any(String)); |
| 62 | |
| 63 | const error = yield* Effect.flip( |
| 64 | compute.insertFirewallPolicies({ |
| 65 | parentId: resource.parent, |
| 66 | body: { |
| 67 | shortName: "alchemy-fwpol-probe", |
| 68 | description: "probe", |
| 69 | }, |
| 70 | }), |
| 71 | ); |
| 72 | expect(error._tag).toBe("Forbidden"); |
| 73 | expect(error.message).toContain("compute.firewallPolicies.create"); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | }).pipe(logLevel), |
| 77 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 78 | ); |
| 79 | |
| 80 | test.provider.skipIf(!runLifecycle)( |
| 81 | "create, update, replace, and delete a firewall policy", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | yield* stack.destroy(); |
| 85 | |
| 86 | const created = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* GCP.Compute.FirewallPolicy("OrgFw", { |
| 89 | description: "allow internal http", |
| 90 | rules: [ |
| 91 | { |
| 92 | action: "allow", |
| 93 | priority: 1000, |
| 94 | direction: "INGRESS", |
| 95 | match: { |
| 96 | srcIpRanges: ["10.0.0.0/8"], |
| 97 | layer4Configs: [{ ipProtocol: "tcp", ports: ["80"] }], |
| 98 | }, |
| 99 | }, |
| 100 | ], |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(created.shortName).toEqual(expect.any(String)); |
| 106 | expect(created.firewallPolicyId).toEqual(expect.any(String)); |
| 107 | expect(created.parent).toEqual(expect.stringMatching(/^(folders|organizations)\//)); |
| 108 | expect(created.policyType).toEqual("VPC_POLICY"); |
| 109 | expect(created.description).toEqual("allow internal http"); |
| 110 | expect(ruleAt(created.rules, 1000)?.action).toEqual("allow"); |
| 111 | expect(ruleAt(created.rules, 2147483647)?.action).toEqual("goto_next"); |
| 112 | |
| 113 | const fetched = yield* compute.getFirewallPolicies({ |
| 114 | firewallPolicy: created.firewallPolicyId, |
| 115 | }); |
| 116 | expect(fetched.shortName).toEqual(created.shortName); |
| 117 | expect(fetched.name).toEqual(created.firewallPolicyId); |
| 118 | expect(fetched.description).toContain("[alchemy "); |
| 119 | expect(fetched.description).toContain("allow internal http"); |
| 120 | expect(ruleAt(fetched.rules, 1000)?.match?.srcIpRanges).toEqual( |
| 121 | expect.arrayContaining(["10.0.0.0/8"]), |
| 122 | ); |
| 123 | |
| 124 | const updated = yield* stack.deploy( |
| 125 | Effect.gen(function* () { |
| 126 | return yield* GCP.Compute.FirewallPolicy("OrgFw", { |
| 127 | shortName: created.shortName, |
| 128 | parent: created.parent, |
| 129 | description: "updated org fw", |
| 130 | rules: [ |
| 131 | { |
| 132 | action: "allow", |
| 133 | priority: 1000, |
| 134 | direction: "INGRESS", |
| 135 | description: "http and https", |
| 136 | match: { |
| 137 | srcIpRanges: ["10.0.0.0/8", "192.168.0.0/16"], |
| 138 | layer4Configs: [{ ipProtocol: "tcp", ports: ["80", "443"] }], |
| 139 | }, |
| 140 | }, |
| 141 | ], |
| 142 | }); |
| 143 | }), |
| 144 | ); |
| 145 | |
| 146 | expect(updated.shortName).toEqual(created.shortName); |
| 147 | expect(updated.firewallPolicyId).toEqual(created.firewallPolicyId); |
| 148 | expect(updated.description).toEqual("updated org fw"); |
| 149 | expect(ruleAt(updated.rules, 1000)?.match?.srcIpRanges?.slice().sort()).toEqual([ |
| 150 | "10.0.0.0/8", |
| 151 | "192.168.0.0/16", |
| 152 | ]); |
| 153 | |
| 154 | const fetchedUpdate = yield* compute.getFirewallPolicies({ |
| 155 | firewallPolicy: updated.firewallPolicyId, |
| 156 | }); |
| 157 | expect(fetchedUpdate.description).toContain("updated org fw"); |
| 158 | expect(ruleAt(fetchedUpdate.rules, 1000)?.description).toEqual("http and https"); |
| 159 | |
| 160 | const replaced = yield* stack.deploy( |
| 161 | Effect.gen(function* () { |
| 162 | return yield* GCP.Compute.FirewallPolicy("OrgFw", { |
| 163 | shortName: nextShortName(created.shortName), |
| 164 | parent: created.parent, |
| 165 | description: "replaced org fw", |
| 166 | rules: [ |
| 167 | { |
| 168 | action: "deny", |
| 169 | priority: 1000, |
| 170 | direction: "INGRESS", |
| 171 | match: { |
| 172 | srcIpRanges: ["0.0.0.0/0"], |
| 173 | layer4Configs: [{ ipProtocol: "tcp", ports: ["23"] }], |
| 174 | }, |
| 175 | }, |
| 176 | ], |
| 177 | }); |
| 178 | }), |
| 179 | ); |
| 180 | |
| 181 | expect(replaced.shortName).toEqual(nextShortName(created.shortName)); |
| 182 | expect(replaced.firewallPolicyId).not.toEqual(created.firewallPolicyId); |
| 183 | expect(replaced.description).toEqual("replaced org fw"); |
| 184 | expect(ruleAt(replaced.rules, 1000)?.action).toEqual("deny"); |
| 185 | |
| 186 | const fetchedReplace = yield* compute.getFirewallPolicies({ |
| 187 | firewallPolicy: replaced.firewallPolicyId, |
| 188 | }); |
| 189 | expect(fetchedReplace.shortName).toEqual(replaced.shortName); |
| 190 | expect(fetchedReplace.description).toContain("replaced org fw"); |
| 191 | |
| 192 | yield* stack.destroy(); |
| 193 | |
| 194 | const gone = yield* waitUntilGone(replaced.firewallPolicyId); |
| 195 | expect(gone).toEqual("gone"); |
| 196 | }).pipe(logLevel), |
| 197 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 198 | ); |