The best way to control Claude Code, Codex and more from any computer or phone

main
ts 124 lines 4.52 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2";
6 import * as crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import { expectProbe } from "../bindingHost.ts";
10 import CloudFunctionsBindingsHost from "./fixtures/bindings-host.ts";
11 import TargetFunction from "./fixtures/target-function.ts";
12
13 const testOptions = { providers: GCP.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "CloudFunctionsBindings");
16
17 let baseUrl: string;
18 let hostAccount: string;
19 let functionName: string;
20 let project: string;
21
22 /**
23 * 2nd gen functions accept only invoker roles on their own IAM policy, so
24 * both bindings grant on the project under an IAM Condition naming only the
25 * function: GetFunction `roles/cloudfunctions.viewer`, GenerateDownloadUrl
26 * `roles/cloudfunctions.developer` (narrowest role with
27 * `functions.sourceCodeGet`).
28 */
29 const expectFunctionGrants = Effect.gen(function* () {
30 const policy = yield* crm.getIamPolicyProjects({
31 resource: `projects/${project}`,
32 body: { options: { requestedPolicyVersion: 3 } },
33 });
34 const condition = `resource.name == "${functionName}" || resource.name.startsWith("${functionName}/")`;
35 const grants = (policy.bindings ?? [])
36 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
37 .map((binding) => ({
38 role: binding.role,
39 condition: binding.condition?.expression,
40 }))
41 .sort((a, b) => (a.role ?? "").localeCompare(b.role ?? ""));
42 expect(grants).toEqual([
43 { role: "roles/cloudfunctions.developer", condition },
44 { role: "roles/cloudfunctions.viewer", condition },
45 ]);
46 });
47
48 // The target function's gen2 build takes 2-4 minutes.
49 describe.skipIf(!!process.env.FAST)(
50 "CloudFunctions Bindings",
51 {
52 tags: ["provider:gcp", "provider:gcp:cloudfunctions", "provider:gcp:run", "live"],
53 },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* sharedStack.destroy();
58 const out = yield* sharedStack.deploy(
59 Effect.gen(function* () {
60 const host = yield* CloudFunctionsBindingsHost;
61 const fn = yield* TargetFunction;
62 return {
63 uri: host.uri,
64 serviceAccount: host.serviceAccount,
65 name: fn.name,
66 project: fn.project,
67 };
68 }),
69 );
70 baseUrl = out.uri!;
71 hostAccount = out.serviceAccount!;
72 functionName = out.name;
73 project = out.project;
74 }),
75 { timeout: 1_200_000 },
76 );
77
78 afterAll(sharedStack.destroy(), { timeout: 600_000 });
79
80 describe("GetFunction", () => {
81 test.provider(
82 "reads the function as the host's service account, scoped to the function by condition",
83 (_stack) =>
84 Effect.gen(function* () {
85 const live = yield* expectProbe<cloudfunctions.Cloudfunctions_Function>(
86 baseUrl,
87 "getFunction",
88 );
89 expect(live.name).toEqual(functionName);
90 expect(live.state).toEqual("ACTIVE");
91 expect(live.buildConfig?.entryPoint).toEqual("handler");
92 yield* expectFunctionGrants;
93 }),
94 {
95 tags: ["provider:gcp", "provider:gcp:cloudfunctions", "live"],
96 timeout: 600_000,
97 },
98 );
99 });
100
101 describe("GenerateDownloadUrl", () => {
102 test.provider(
103 "signs a source download URL as the host's service account, scoped to the function by condition",
104 (_stack) =>
105 Effect.gen(function* () {
106 const { downloadUrl } = yield* expectProbe<cloudfunctions.GenerateDownloadUrlResponse>(
107 baseUrl,
108 "generateDownloadUrl",
109 );
110 // The signed URL serves the function's source archive (a zip).
111 const response = yield* HttpClient.get(downloadUrl ?? "");
112 expect(response.status).toEqual(200);
113 const bytes = new Uint8Array(yield* response.arrayBuffer);
114 expect(String.fromCharCode(bytes[0] ?? 0, bytes[1] ?? 0)).toEqual("PK");
115 yield* expectFunctionGrants;
116 }),
117 {
118 tags: ["provider:gcp", "provider:gcp:cloudfunctions", "live"],
119 timeout: 600_000,
120 },
121 );
122 });
123 },
124 );