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

main
ts 93 lines 3.11 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as apikeys from "@distilled.cloud/gcp/apikeys_v2";
7 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
8 import * as Effect from "effect/Effect";
9 import { expectProbe } from "../bindingHost.ts";
10 import ApiKeysBindingsHost, { Maps } from "./fixtures/bindings-host.ts";
11
12 const testOptions = { providers: GCP.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "ApiKeysBindings");
15
16 let baseUrl: string;
17 let keyName: string;
18 let hostAccount: string;
19
20 /** Every project-level role (and its IAM Condition) `account` holds. */
21 const projectGrantsOf = (account: string) =>
22 Effect.gen(function* () {
23 const { project } = yield* GcpEnvironment.current;
24 const policy = yield* resourcemanager.getIamPolicyProjects({
25 resource: `projects/${project}`,
26 body: { options: { requestedPolicyVersion: 3 } },
27 });
28 return (policy.bindings ?? [])
29 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
30 .map((binding) => ({
31 role: binding.role,
32 condition: binding.condition?.expression,
33 }));
34 });
35
36 describe(
37 "ApiKeys Bindings",
38 {
39 tags: ["provider:gcp", "provider:gcp:apikeys", "provider:gcp:run", "live"],
40 },
41 () => {
42 beforeAll(
43 Effect.gen(function* () {
44 yield* sharedStack.destroy();
45 const out = yield* sharedStack.deploy(
46 Effect.gen(function* () {
47 const host = yield* ApiKeysBindingsHost;
48 const key = yield* Maps;
49 return {
50 uri: host.uri,
51 serviceAccount: host.serviceAccount,
52 key: key.name,
53 };
54 }),
55 );
56 baseUrl = out.uri!;
57 keyName = out.key;
58 hostAccount = out.serviceAccount!;
59 }),
60 { timeout: 900_000 },
61 );
62
63 afterAll(sharedStack.destroy(), { timeout: 600_000 });
64
65 describe("GetKeyString", () => {
66 test.provider(
67 "reads the key string as the host's service account",
68 (_stack) =>
69 Effect.gen(function* () {
70 const out = yield* expectProbe<{ keyString?: string }>(baseUrl, "getKeyString");
71 const expected = yield* apikeys.getKeyStringProjectsLocationsKeys({
72 name: keyName,
73 });
74 expect(out.keyString).toEqual(expected.keyString);
75 expect(out.keyString?.length ?? 0).toBeGreaterThan(8);
76
77 // API keys have no resource-level IAM policy.
78 const grants = yield* projectGrantsOf(hostAccount);
79 expect(grants).toEqual([
80 {
81 role: "roles/serviceusage.apiKeysViewer",
82 condition: undefined,
83 },
84 ]);
85 }),
86 {
87 tags: ["provider:gcp", "provider:gcp:apikeys", "live"],
88 timeout: 600_000,
89 },
90 );
91 });
92 },
93 );