@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 207 lines 7.5 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 binaryauthorization from "@distilled.cloud/gcp/binaryauthorization_v1";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import { currentProject, TEST_ATTESTATION, TEST_POD, TEST_RESOURCE_URI } from "./common.ts";
10 import BinaryAuthorizationBindingsHost, {
11 GkePolicy,
12 Verifier,
13 Viewed,
14 } from "./fixtures/bindings-host.ts";
15
16 const testOptions = { providers: GCP.providers() };
17 const { test, beforeAll, afterAll } = Test.make(testOptions);
18 const sharedStack = Core.scratchStack(testOptions, "BinaryAuthorizationBindings");
19
20 let baseUrl: string;
21 let hostAccount: string;
22 let viewed: { name: string; noteReference: string };
23 let verifier: { name: string; noteReference: string };
24 let policyName: string;
25
26 /** Every project-level role (and its IAM Condition) `account` holds. */
27 const projectGrantsOf = (account: string) =>
28 Effect.gen(function* () {
29 const project = yield* currentProject;
30 const policy = yield* resourcemanager.getIamPolicyProjects({
31 resource: `projects/${project}`,
32 body: { options: { requestedPolicyVersion: 3 } },
33 });
34 return (policy.bindings ?? [])
35 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
36 .map((binding) => ({
37 role: binding.role,
38 condition: binding.condition?.expression,
39 }))
40 .sort((left, right) => (left.role ?? "").localeCompare(right.role ?? ""));
41 });
42
43 /** Platform policies have no resource-level IAM: both roles sit on the project. */
44 const PROJECT_GRANTS = [
45 {
46 role: "roles/binaryauthorization.policyEvaluator",
47 condition: undefined,
48 },
49 { role: "roles/binaryauthorization.policyViewer", condition: undefined },
50 ];
51
52 /** Roles `account` holds on an attestor's own IAM policy. */
53 const attestorRolesOf = (attestor: string, account: string) =>
54 binaryauthorization
55 .getIamPolicyProjectsAttestors({
56 resource: attestor,
57 "options.requestedPolicyVersion": 3,
58 })
59 .pipe(
60 Effect.map((policy) =>
61 (policy.bindings ?? [])
62 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
63 .map((binding) => binding.role),
64 ),
65 );
66
67 describe(
68 "BinaryAuthorization Bindings",
69 {
70 tags: ["provider:gcp", "provider:gcp:binaryauthorization", "provider:gcp:run", "live"],
71 },
72 () => {
73 beforeAll(
74 Effect.gen(function* () {
75 yield* sharedStack.destroy();
76 const out = yield* sharedStack.deploy(
77 Effect.gen(function* () {
78 const host = yield* BinaryAuthorizationBindingsHost;
79 const viewedAttestor = yield* Viewed;
80 const verifierAttestor = yield* Verifier;
81 const policy = yield* GkePolicy;
82 return {
83 uri: host.uri,
84 serviceAccount: host.serviceAccount,
85 viewed: {
86 name: viewedAttestor.name,
87 noteReference: viewedAttestor.noteReference,
88 },
89 verifier: {
90 name: verifierAttestor.name,
91 noteReference: verifierAttestor.noteReference,
92 },
93 policy: policy.name,
94 };
95 }),
96 );
97 baseUrl = out.uri!;
98 hostAccount = out.serviceAccount!;
99 viewed = out.viewed;
100 verifier = out.verifier;
101 policyName = out.policy;
102 }),
103 { timeout: 900_000 },
104 );
105
106 afterAll(sharedStack.destroy(), { timeout: 600_000 });
107
108 describe("GetAttestor", () => {
109 test.provider(
110 "reads the attestor as the host's service account, granted on the attestor only",
111 (_stack) =>
112 Effect.gen(function* () {
113 const live = yield* expectProbe<{
114 name?: string;
115 userOwnedGrafeasNote?: { noteReference?: string };
116 }>(baseUrl, "getAttestor");
117 expect(live.name).toEqual(viewed.name);
118 expect(live.userOwnedGrafeasNote?.noteReference).toEqual(viewed.noteReference);
119
120 expect(yield* attestorRolesOf(viewed.name, hostAccount)).toEqual([
121 "roles/binaryauthorization.attestorsViewer",
122 ]);
123 expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS);
124 }),
125 {
126 tags: ["provider:gcp", "provider:gcp:binaryauthorization", "live"],
127 timeout: 600_000,
128 },
129 );
130 });
131
132 describe("ValidateAttestation", () => {
133 test.provider(
134 "validates against the attestor as the host's service account, granted on the attestor only",
135 (_stack) =>
136 Effect.gen(function* () {
137 const result = yield* expectProbe<{ result?: string }>(baseUrl, "validateAttestation");
138 // The attestor has no public keys, so nothing can verify.
139 expect(result.result).toEqual("ATTESTATION_NOT_VERIFIABLE");
140 const expected =
141 yield* binaryauthorization.validateAttestationOccurrenceProjectsAttestors({
142 attestor: verifier.name,
143 body: {
144 occurrenceResourceUri: TEST_RESOURCE_URI,
145 occurrenceNote: verifier.noteReference,
146 attestation: TEST_ATTESTATION,
147 },
148 });
149 expect(result.result).toEqual(expected.result);
150
151 expect(yield* attestorRolesOf(verifier.name, hostAccount)).toEqual([
152 "roles/binaryauthorization.attestorsVerifier",
153 ]);
154 expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS);
155 }),
156 {
157 tags: ["provider:gcp", "provider:gcp:binaryauthorization", "live"],
158 timeout: 600_000,
159 },
160 );
161 });
162
163 describe("GetPlatformsPolicy", () => {
164 test.provider(
165 "reads the platform policy as the host's service account",
166 (_stack) =>
167 Effect.gen(function* () {
168 const live = yield* expectProbe<{ name?: string; etag?: string }>(
169 baseUrl,
170 "getPlatformsPolicy",
171 );
172 const expected = yield* binaryauthorization.getProjectsPlatformsPolicies({
173 name: policyName,
174 });
175 expect(live.name).toEqual(policyName);
176 expect(live.etag).toEqual(expected.etag);
177 expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS);
178 }),
179 {
180 tags: ["provider:gcp", "provider:gcp:binaryauthorization", "live"],
181 timeout: 600_000,
182 },
183 );
184 });
185
186 describe("EvaluateGkePolicy", () => {
187 test.provider(
188 "evaluates a pod against the platform policy as the host's service account",
189 (_stack) =>
190 Effect.gen(function* () {
191 const live = yield* expectProbe<{ verdict?: string }>(baseUrl, "evaluateGkePolicy");
192 const expected = yield* binaryauthorization.evaluateProjectsPlatformsGkePolicies({
193 name: policyName,
194 body: { resource: TEST_POD },
195 });
196 expect(live.verdict).toEqual(expected.verdict);
197 expect(live.verdict).toEqual(expect.any(String));
198 expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS);
199 }),
200 {
201 tags: ["provider:gcp", "provider:gcp:binaryauthorization", "live"],
202 timeout: 600_000,
203 },
204 );
205 });
206 },
207 );