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 cloudrun from "@distilled.cloud/gcp/run_v2"; |
| 5 | import * as Cause from "effect/Cause"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Exit from "effect/Exit"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import InvokeCallee from "./fixtures/invoke-callee.ts"; |
| 12 | import InvokeCaller from "./fixtures/invoke-caller.ts"; |
| 13 | import PublicRpc from "./fixtures/invoke-public-rpc.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: GCP.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | interface CallerBody { |
| 20 | status: number; |
| 21 | body?: { from: string; method: string; path: string; body: string }; |
| 22 | } |
| 23 | |
| 24 | /** GET `url`, repeating while the result does not satisfy `until`. */ |
| 25 | const getUntil = <A>( |
| 26 | url: string, |
| 27 | decode: (status: number, body: string) => A, |
| 28 | until: (value: A) => boolean, |
| 29 | ) => |
| 30 | Effect.gen(function* () { |
| 31 | const client = yield* HttpClient.HttpClient; |
| 32 | const response = yield* client.get(url); |
| 33 | return decode(response.status, yield* response.text); |
| 34 | }).pipe( |
| 35 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }), |
| 36 | // A fresh revision and a fresh run.invoker grant both take a moment |
| 37 | // to serve; bounded at ~2 minutes. |
| 38 | Effect.repeat({ |
| 39 | schedule: Schedule.spaced("5 seconds"), |
| 40 | until, |
| 41 | times: 24, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | test.provider( |
| 46 | "InvokeService calls a private service with a Google-signed ID token", |
| 47 | (stack) => |
| 48 | Effect.gen(function* () { |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | const out = yield* stack.deploy( |
| 52 | Effect.gen(function* () { |
| 53 | const callee = yield* InvokeCallee; |
| 54 | const caller = yield* InvokeCaller; |
| 55 | return { |
| 56 | callee: { |
| 57 | name: callee.name, |
| 58 | uri: callee.uri, |
| 59 | invokerIamDisabled: callee.invokerIamDisabled, |
| 60 | }, |
| 61 | caller: { |
| 62 | name: caller.name, |
| 63 | uri: caller.uri, |
| 64 | serviceAccount: caller.serviceAccount, |
| 65 | }, |
| 66 | }; |
| 67 | }), |
| 68 | ); |
| 69 | expect(out.callee.invokerIamDisabled).toEqual(false); |
| 70 | |
| 71 | // run.invoker is granted on the callee's own policy, not the project. |
| 72 | const policy = yield* cloudrun.getIamPolicyProjectsLocationsServices({ |
| 73 | resource: out.callee.name, |
| 74 | }); |
| 75 | const invokers = |
| 76 | policy.bindings?.find((binding) => binding.role === "roles/run.invoker")?.members ?? []; |
| 77 | expect(invokers).toContain(`serviceAccount:${out.caller.serviceAccount}`); |
| 78 | |
| 79 | // Without a token Cloud Run rejects the call at its front end. |
| 80 | const direct = yield* getUntil( |
| 81 | `${out.callee.uri}/hello`, |
| 82 | (status) => status, |
| 83 | (status) => status === 403, |
| 84 | ); |
| 85 | expect(direct).toEqual(403); |
| 86 | |
| 87 | const viaCaller = yield* getUntil( |
| 88 | `${out.caller.uri}/`, |
| 89 | (status, body) => (status === 200 ? (JSON.parse(body) as CallerBody) : { status }), |
| 90 | (value) => value.status === 200, |
| 91 | ); |
| 92 | expect(viaCaller).toEqual({ |
| 93 | status: 200, |
| 94 | body: { from: "callee", method: "GET", path: "/hello", body: "" }, |
| 95 | }); |
| 96 | |
| 97 | const posted = yield* getUntil( |
| 98 | `${out.caller.uri}/post`, |
| 99 | (status, body) => (status === 200 ? (JSON.parse(body) as CallerBody) : { status }), |
| 100 | (value) => value.status === 200, |
| 101 | ); |
| 102 | expect(posted.body).toEqual({ |
| 103 | from: "callee", |
| 104 | method: "POST", |
| 105 | path: "/echo", |
| 106 | body: "ping", |
| 107 | }); |
| 108 | |
| 109 | // RPC methods ride the same ID token. |
| 110 | const rpc = yield* getUntil( |
| 111 | `${out.caller.uri}/rpc`, |
| 112 | (status, body) => |
| 113 | status === 200 |
| 114 | ? (JSON.parse(body) as { sum: number; countdown: number[]; viaClient: unknown }) |
| 115 | : { status }, |
| 116 | (value) => "sum" in value, |
| 117 | ); |
| 118 | expect(rpc).toEqual({ |
| 119 | sum: 5, |
| 120 | countdown: [3, 2, 1], |
| 121 | viaClient: { from: "callee", method: "GET", path: "/hello", body: "" }, |
| 122 | }); |
| 123 | |
| 124 | // The RPC route is behind the invoker check like every other path. |
| 125 | const client = yield* HttpClient.HttpClient; |
| 126 | const unsigned = yield* client.post(`${out.callee.uri}/__rpc__/add`); |
| 127 | expect(unsigned.status).toEqual(403); |
| 128 | |
| 129 | yield* stack.destroy(); |
| 130 | |
| 131 | for (const name of [out.callee.name, out.caller.name]) { |
| 132 | const gone = yield* cloudrun.getProjectsLocationsServices({ name }).pipe( |
| 133 | Effect.as("found" as const), |
| 134 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 135 | ); |
| 136 | expect(gone).toEqual("gone"); |
| 137 | } |
| 138 | }).pipe(logLevel), |
| 139 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 600_000 }, |
| 140 | ); |
| 141 | |
| 142 | test.provider( |
| 143 | "a public service cannot serve RPC methods", |
| 144 | (stack) => |
| 145 | Effect.gen(function* () { |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | const exit = yield* stack |
| 149 | .deploy( |
| 150 | Effect.gen(function* () { |
| 151 | return yield* PublicRpc; |
| 152 | }), |
| 153 | ) |
| 154 | .pipe(Effect.exit); |
| 155 | expect(Exit.isFailure(exit)).toEqual(true); |
| 156 | expect(String(Exit.isFailure(exit) ? Cause.pretty(exit.cause) : "")).toContain( |
| 157 | "GCP.Run.ServiceRpcPublic", |
| 158 | ); |
| 159 | |
| 160 | yield* stack.destroy(); |
| 161 | }).pipe(logLevel), |
| 162 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 180_000 }, |
| 163 | ); |