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

main
ts 134 lines 4.81 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 crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as containeranalysis from "@distilled.cloud/gcp/containeranalysis_v1";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import { currentProject, TEST_RESOURCE_URI } from "./common.ts";
10 import ContainerAnalysisBindingsHost, { Authority, Signed } 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, "ContainerAnalysisBindings");
15
16 let baseUrl: string;
17 let noteName: string;
18 let occurrenceName: string;
19 let hostAccount: string;
20
21 /** Roles the host's service account holds on the project policy. */
22 const projectRoles = Effect.gen(function* () {
23 const project = yield* currentProject;
24 const policy = yield* crm.getIamPolicyProjects({
25 resource: `projects/${project}`,
26 body: { options: { requestedPolicyVersion: 3 } },
27 });
28 return (policy.bindings ?? [])
29 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
30 .map((binding) => ({
31 role: binding.role,
32 condition: binding.condition?.expression,
33 }));
34 });
35
36 describe(
37 "ContainerAnalysis Bindings",
38 {
39 tags: ["provider:gcp", "provider:gcp:containeranalysis", "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* ContainerAnalysisBindingsHost;
48 const note = yield* Authority;
49 const occurrence = yield* Signed;
50 return {
51 uri: host.uri,
52 serviceAccount: host.serviceAccount,
53 note: note.name,
54 occurrence: occurrence.name,
55 };
56 }),
57 );
58 baseUrl = out.uri!;
59 noteName = out.note;
60 occurrenceName = out.occurrence;
61 hostAccount = out.serviceAccount!;
62 }),
63 { timeout: 900_000 },
64 );
65
66 afterAll(sharedStack.destroy(), { timeout: 600_000 });
67
68 describe("GetNote", () => {
69 test.provider(
70 "reads the note as the host's service account, granted on the note only",
71 (_stack) =>
72 Effect.gen(function* () {
73 const note = yield* expectProbe<containeranalysis.Note>(baseUrl, "getNote");
74 const live = yield* containeranalysis.getProjectsNotes({
75 name: noteName,
76 });
77 expect(note.name).toEqual(noteName);
78 expect(note.kind).toEqual("ATTESTATION");
79 expect(note.shortDescription).toEqual(live.shortDescription);
80
81 const policy = yield* containeranalysis.getIamPolicyProjectsNotes({
82 resource: noteName,
83 });
84 const roles = (policy.bindings ?? [])
85 .filter((binding) =>
86 (binding.members ?? []).includes(`serviceAccount:${hostAccount}`),
87 )
88 .map((binding) => binding.role);
89 expect(roles).toEqual(["roles/containeranalysis.notes.viewer"]);
90 expect((yield* projectRoles).map((grant) => grant.role)).not.toContain(
91 "roles/containeranalysis.notes.viewer",
92 );
93 }),
94 {
95 tags: ["provider:gcp", "provider:gcp:containeranalysis", "live"],
96 timeout: 600_000,
97 },
98 );
99 });
100
101 describe("GetOccurrence", () => {
102 test.provider(
103 "reads the occurrence as the host's service account, granted on the project",
104 (_stack) =>
105 Effect.gen(function* () {
106 const occurrence = yield* expectProbe<containeranalysis.Occurrence>(
107 baseUrl,
108 "getOccurrence",
109 );
110 const live = yield* containeranalysis.getProjectsOccurrences({
111 name: occurrenceName,
112 });
113 expect(occurrence.name).toEqual(occurrenceName);
114 expect(occurrence.noteName).toEqual(noteName);
115 expect(occurrence.resourceUri).toEqual(TEST_RESOURCE_URI);
116 expect(occurrence.createTime).toEqual(live.createTime);
117
118 // Occurrence IAM is not modelled as a grant target, so the role is
119 // granted on the project (no condition).
120 expect(yield* projectRoles).toEqual([
121 {
122 role: "roles/containeranalysis.occurrences.viewer",
123 condition: undefined,
124 },
125 ]);
126 }),
127 {
128 tags: ["provider:gcp", "provider:gcp:containeranalysis", "live"],
129 timeout: 600_000,
130 },
131 );
132 });
133 },
134 );