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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as GCP from "@alchemy.run/gcp"; |
| 3 | import { runsAsAwsRole, runsAsServiceAccount } from "@alchemy.run/gcp/GCPBindings"; |
| 4 | import { describe, expect, it } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as lambda from "@distilled.cloud/aws/lambda"; |
| 7 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 8 | import * as iam from "@distilled.cloud/gcp/unstable/iam_v1"; |
| 9 | import type { ResourceLike } from "alchemy/Resource"; |
| 10 | import * as Data from "effect/Data"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as HttpClient from "effect/http/HttpClient"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | import * as Schedule from "effect/Schedule"; |
| 16 | import FederatedLambdaLive, { Events, FederatedLambda } from "./fixtures/aws-host.ts"; |
| 17 | |
| 18 | const host = (Type: string, Props?: object) => |
| 19 | ({ Type, LogicalId: "Host", FQN: "Host", Props }) as ResourceLike; |
| 20 | |
| 21 | describe("GCPBindings.runsAsServiceAccount", { tags: ["unit", "provider:gcp", "local"] }, () => { |
| 22 | it("accepts Cloud Run and Cloud Functions", () => { |
| 23 | for (const Type of [ |
| 24 | "GCP.Run.Service", |
| 25 | "GCP.Run.Job", |
| 26 | "GCP.Run.WorkerPool", |
| 27 | "GCP.CloudFunctions.Function", |
| 28 | ]) { |
| 29 | expect(runsAsServiceAccount(host(Type))).toBe(true); |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | it("accepts Kubernetes workloads on GKE (Workload Identity)", () => { |
| 34 | const gke = { Type: "GCP.Container.Cluster", LogicalId: "Cluster" }; |
| 35 | expect(runsAsServiceAccount(host("Kubernetes.Deployment", { cluster: gke }))).toBe(true); |
| 36 | expect( |
| 37 | runsAsServiceAccount( |
| 38 | host("Kubernetes.Job", { cluster: { auth: { kind: "gcp-gke", cluster: "c" } } }), |
| 39 | ), |
| 40 | ).toBe(true); |
| 41 | expect( |
| 42 | runsAsServiceAccount( |
| 43 | host("Kubernetes.Job", { |
| 44 | cluster: { connection: { auth: { kind: "gcp-gke", cluster: "c" } } }, |
| 45 | }), |
| 46 | ), |
| 47 | ).toBe(true); |
| 48 | }); |
| 49 | |
| 50 | it("accepts a Kubernetes workload on a GKE cluster ref", () => |
| 51 | Effect.gen(function* () { |
| 52 | const cluster = yield* GCP.Container.Cluster.ref("Cluster"); |
| 53 | expect(runsAsServiceAccount(host("Kubernetes.Deployment", { cluster }))).toBe(true); |
| 54 | }).pipe(Effect.runSync)); |
| 55 | |
| 56 | it("rejects Kubernetes workloads on other clusters", () => { |
| 57 | const eks = { Type: "AWS.EKS.Cluster", LogicalId: "Cluster" }; |
| 58 | const local = { Type: "Kubernetes.LocalCluster", LogicalId: "Cluster" }; |
| 59 | const kubeconfig = { auth: { kind: "kubeconfig", context: "prod" } }; |
| 60 | for (const cluster of [eks, local, kubeconfig, undefined]) { |
| 61 | expect(runsAsServiceAccount(host("Kubernetes.Deployment", { cluster }))).toBe(false); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | it("rejects hosts outside GCP and GCP resources that run no code", () => { |
| 66 | expect(runsAsServiceAccount(host("Cloudflare.Worker"))).toBe(false); |
| 67 | expect(runsAsServiceAccount(host("AWS.Lambda.Function"))).toBe(false); |
| 68 | expect(runsAsServiceAccount(host("GCP.Storage.Bucket"))).toBe(false); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe("GCPBindings.runsAsAwsRole", { tags: ["unit", "provider:gcp", "local"] }, () => { |
| 73 | it("accepts Lambda, ECS, EC2 and App Runner", () => { |
| 74 | for (const Type of [ |
| 75 | "AWS.Lambda.Function", |
| 76 | "AWS.Lambda.WebFunction", |
| 77 | "AWS.ECS.Task", |
| 78 | "AWS.ECS.Service", |
| 79 | "AWS.EC2.Instance", |
| 80 | "AWS.AutoScaling.LaunchTemplate", |
| 81 | "AWS.AppRunner.Service", |
| 82 | ]) { |
| 83 | expect(runsAsAwsRole(host(Type))).toBe(true); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | it("rejects EKS pods and Batch jobs, which keep a key", () => { |
| 88 | const eks = { Type: "AWS.EKS.Cluster", LogicalId: "Cluster" }; |
| 89 | expect(runsAsAwsRole(host("Kubernetes.Deployment", { cluster: eks }))).toBe(false); |
| 90 | expect(runsAsAwsRole(host("AWS.Batch.JobDefinition"))).toBe(false); |
| 91 | }); |
| 92 | |
| 93 | it("rejects hosts outside AWS", () => { |
| 94 | expect(runsAsAwsRole(host("Cloudflare.Worker"))).toBe(false); |
| 95 | expect(runsAsAwsRole(host("GCP.Run.Service"))).toBe(false); |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | const { test: liveTest } = Test.make({ |
| 100 | providers: Layer.mergeAll(AWS.providers(), GCP.providers()), |
| 101 | }); |
| 102 | |
| 103 | class PublishNotReady extends Data.TaggedError("PublishNotReady")<{ reason: string }> {} |
| 104 | |
| 105 | describe( |
| 106 | "GCP bindings on an AWS IAM role host", |
| 107 | { tags: ["provider:aws", "provider:gcp", "provider:aws:lambda", "live"] }, |
| 108 | () => { |
| 109 | liveTest.provider( |
| 110 | "Lambda reaches Pub/Sub through workload identity federation", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | const out = yield* stack.deploy( |
| 116 | Effect.gen(function* () { |
| 117 | const fn = yield* FederatedLambda; |
| 118 | const events = yield* Events; |
| 119 | const inbox = yield* GCP.PubSub.Subscription("FederatedInbox", { |
| 120 | topic: events.name, |
| 121 | }); |
| 122 | return { |
| 123 | url: fn.functionUrl.as<string>(), |
| 124 | functionName: fn.functionName, |
| 125 | roleArn: fn.roleArn, |
| 126 | subscription: inbox.name, |
| 127 | }; |
| 128 | }).pipe(Effect.provide(FederatedLambdaLive)), |
| 129 | ); |
| 130 | |
| 131 | // The Lambda has no GCP key, only the provider and account it |
| 132 | // federates through. |
| 133 | const config = yield* lambda.getFunctionConfiguration({ |
| 134 | FunctionName: out.functionName, |
| 135 | }); |
| 136 | const env = Object.fromEntries( |
| 137 | Object.entries(config.Environment?.Variables ?? {}).map(([name, value]) => [ |
| 138 | name, |
| 139 | typeof value === "string" || value === undefined ? value : Redacted.value(value), |
| 140 | ]), |
| 141 | ); |
| 142 | expect(env.ALCHEMY_GCP_SERVICE_ACCOUNT_KEY).toBeUndefined(); |
| 143 | const audience = env.ALCHEMY_GCP_WORKLOAD_IDENTITY_AUDIENCE; |
| 144 | expect(audience).toMatch( |
| 145 | /^\/\/iam\.googleapis\.com\/projects\/\d+\/locations\/global\/workloadIdentityPools\/[^/]+\/providers\/[^/]+$/, |
| 146 | ); |
| 147 | const email = env.ALCHEMY_GCP_WORKLOAD_IDENTITY_SERVICE_ACCOUNT!; |
| 148 | expect(email).toMatch(/\.iam\.gserviceaccount\.com$/); |
| 149 | |
| 150 | const keys = yield* iam.listProjectsServiceAccountsKeys({ |
| 151 | name: `projects/-/serviceAccounts/${email}`, |
| 152 | keyTypes: ["USER_MANAGED"], |
| 153 | }); |
| 154 | expect(keys.keys ?? []).toEqual([]); |
| 155 | |
| 156 | const pool = audience!.replace("//iam.googleapis.com/", "").split("/providers/")[0]; |
| 157 | const [, partition, , , account, resource] = out.roleArn.split(":"); |
| 158 | const principal = `principalSet://iam.googleapis.com/${pool}/attribute.aws_role/arn:${partition}:sts::${account}:assumed-role/${resource!.split("/").pop()}`; |
| 159 | const policy = yield* iam.getIamPolicyProjectsServiceAccounts({ |
| 160 | resource: `projects/-/serviceAccounts/${email}`, |
| 161 | }); |
| 162 | expect( |
| 163 | (policy.bindings ?? []).find( |
| 164 | (binding) => binding.role === "roles/iam.workloadIdentityUser", |
| 165 | )?.members, |
| 166 | ).toEqual([principal]); |
| 167 | |
| 168 | // A grant to a federated principal takes minutes to propagate |
| 169 | // (IAM documents up to 7), as does the account's topic grant. |
| 170 | const published = yield* HttpClient.get(out.url).pipe( |
| 171 | Effect.flatMap((response) => |
| 172 | response.text.pipe( |
| 173 | Effect.flatMap((body) => |
| 174 | response.status === 200 |
| 175 | ? Effect.succeed(JSON.parse(body) as { messageIds?: string[] }) |
| 176 | : Effect.fail(new PublishNotReady({ reason: `${response.status} ${body}` })), |
| 177 | ), |
| 178 | ), |
| 179 | ), |
| 180 | Effect.mapError((error) => |
| 181 | error._tag === "PublishNotReady" |
| 182 | ? error |
| 183 | : new PublishNotReady({ reason: String(error) }), |
| 184 | ), |
| 185 | Effect.tapError((error) => Effect.logInfo(`publish not ready: ${error.reason}`)), |
| 186 | Effect.retry({ schedule: Schedule.spaced("10 seconds"), times: 42 }), |
| 187 | ); |
| 188 | expect(published.messageIds?.length).toBe(1); |
| 189 | |
| 190 | const pulled = yield* pubsub |
| 191 | .pullProjectsSubscriptions({ |
| 192 | subscription: out.subscription, |
| 193 | body: { maxMessages: 1 }, |
| 194 | }) |
| 195 | .pipe( |
| 196 | Effect.repeat({ |
| 197 | until: (response) => (response.receivedMessages ?? []).length > 0, |
| 198 | schedule: Schedule.spaced("2 seconds"), |
| 199 | times: 10, |
| 200 | }), |
| 201 | ); |
| 202 | expect(atob(pulled.receivedMessages?.[0]?.message?.data ?? "")).toBe("hello from lambda"); |
| 203 | |
| 204 | yield* stack.destroy(); |
| 205 | |
| 206 | const gone = yield* iam |
| 207 | .getProjectsServiceAccounts({ |
| 208 | name: `projects/${env.ALCHEMY_GCP_WORKLOAD_IDENTITY_PROJECT}/serviceAccounts/${email}`, |
| 209 | }) |
| 210 | .pipe( |
| 211 | Effect.as(false), |
| 212 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 213 | ); |
| 214 | expect(gone).toBe(true); |
| 215 | }), |
| 216 | { timeout: 900_000 }, |
| 217 | ); |
| 218 | }, |
| 219 | ); |