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

main
ts 112 lines 3.73 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 privateca from "@distilled.cloud/gcp/privateca_v1";
6 import * as Effect from "effect/Effect";
7 import { expectProbe } from "../bindingHost.ts";
8 import PrivateCaBindingsHost, { Pool, Root } from "./fixtures/bindings-host.ts";
9
10 const testOptions = { providers: GCP.providers() };
11 const { test, beforeAll, afterAll } = Test.make(testOptions);
12 const sharedStack = Core.scratchStack(testOptions, "PrivateCABindings");
13
14 let baseUrl: string;
15 let hostAccount: string;
16 let poolName: string;
17 let caName: string;
18
19 /**
20 * Roles the host holds on the CA pool (CAs have no IAM policy of their
21 * own, so both bindings grant on the parent pool).
22 */
23 const expectPoolGrants = Effect.gen(function* () {
24 const policy = yield* privateca.getIamPolicyProjectsLocationsCaPools({
25 resource: poolName,
26 });
27 const roles = (policy.bindings ?? [])
28 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
29 .map((binding) => binding.role)
30 .sort();
31 expect(roles).toEqual(["roles/privateca.auditor", "roles/privateca.poolReader"]);
32 });
33
34 describe(
35 "PrivateCA Bindings",
36 {
37 tags: ["provider:gcp", "provider:gcp:privateca", "provider:gcp:run", "live"],
38 },
39 () => {
40 beforeAll(
41 Effect.gen(function* () {
42 yield* sharedStack.destroy();
43 const out = yield* sharedStack.deploy(
44 Effect.gen(function* () {
45 const host = yield* PrivateCaBindingsHost;
46 return {
47 uri: host.uri,
48 serviceAccount: host.serviceAccount,
49 pool: (yield* Pool).name,
50 ca: (yield* Root).name,
51 };
52 }),
53 );
54 baseUrl = out.uri!;
55 hostAccount = out.serviceAccount!;
56 poolName = out.pool;
57 caName = out.ca;
58 }),
59 { timeout: 900_000 },
60 );
61
62 afterAll(sharedStack.destroy(), { timeout: 600_000 });
63
64 describe("FetchCaCerts", () => {
65 test.provider(
66 "fetches the pool's trust anchors as the host's service account, granted on the pool only",
67 (_stack) =>
68 Effect.gen(function* () {
69 const certs = yield* expectProbe<privateca.FetchCaCertsResponse>(
70 baseUrl,
71 "fetchCaCerts",
72 );
73 const pems = (certs.caCerts ?? []).flatMap((chain) => chain.certificates ?? []);
74 // The enabled root CA is the pool's only trust anchor.
75 const ca = yield* privateca.getProjectsLocationsCaPoolsCertificateAuthorities({
76 name: caName,
77 });
78 expect(pems.map((pem) => pem.trim())).toEqual(
79 (ca.pemCaCertificates ?? []).map((pem) => pem.trim()),
80 );
81 expect(pems[0]).toContain("BEGIN CERTIFICATE");
82 yield* expectPoolGrants;
83 }),
84 {
85 tags: ["provider:gcp", "provider:gcp:privateca", "live"],
86 timeout: 600_000,
87 },
88 );
89 });
90
91 describe("GetCertificateAuthority", () => {
92 test.provider(
93 "reads the CA as the host's service account, granted on the pool only",
94 (_stack) =>
95 Effect.gen(function* () {
96 const live = yield* expectProbe<privateca.CertificateAuthority>(
97 baseUrl,
98 "getCertificateAuthority",
99 );
100 expect(live.name).toEqual(caName);
101 expect(live.type).toEqual("SELF_SIGNED");
102 expect(live.state).toEqual("ENABLED");
103 yield* expectPoolGrants;
104 }),
105 {
106 tags: ["provider:gcp", "provider:gcp:privateca", "live"],
107 timeout: 600_000,
108 },
109 );
110 });
111 },
112 );