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 { runsAsGoogleServiceAccount, runsAsRole } from "@alchemy.run/aws/AWSBindings"; |
| 3 | import * as GCP from "@alchemy.run/gcp"; |
| 4 | import { describe, expect, it } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as iam from "@distilled.cloud/aws/iam"; |
| 7 | import * as sqs from "@distilled.cloud/aws/sqs"; |
| 8 | import * as cloudrun from "@distilled.cloud/gcp/run_v2"; |
| 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 Schedule from "effect/Schedule"; |
| 15 | import type { ProbeOutcome } from "../GCP/bindingHost.ts"; |
| 16 | import FederatedHost, { Jobs } from "./fixtures/gcp-host.ts"; |
| 17 | |
| 18 | const host = (Type: string, Props?: object) => |
| 19 | ({ Type, LogicalId: "Host", FQN: "Host", Props }) as ResourceLike; |
| 20 | |
| 21 | describe("AWSBindings.runsAsRole", { tags: ["unit", "provider:aws", "local"] }, () => { |
| 22 | it("accepts Lambda, ECS and EC2", () => { |
| 23 | for (const Type of ["AWS.Lambda.Function", "AWS.ECS.Task", "AWS.EC2.Instance"]) { |
| 24 | expect(runsAsRole(host(Type))).toBe(true); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | it("accepts Kubernetes workloads on EKS (Pod Identity)", () => { |
| 29 | const eks = { Type: "AWS.EKS.Cluster", LogicalId: "Cluster" }; |
| 30 | expect(runsAsRole(host("Kubernetes.Deployment", { cluster: eks }))).toBe(true); |
| 31 | expect( |
| 32 | runsAsRole( |
| 33 | host("Kubernetes.Job", { cluster: { auth: { kind: "aws-eks", clusterName: "c" } } }), |
| 34 | ), |
| 35 | ).toBe(true); |
| 36 | expect( |
| 37 | runsAsRole( |
| 38 | host("Kubernetes.Job", { |
| 39 | cluster: { connection: { auth: { kind: "aws-eks", clusterName: "c" } } }, |
| 40 | }), |
| 41 | ), |
| 42 | ).toBe(true); |
| 43 | }); |
| 44 | |
| 45 | it("accepts a Kubernetes workload on an EKS cluster ref", () => |
| 46 | Effect.gen(function* () { |
| 47 | const cluster = yield* AWS.EKS.Cluster.ref("Cluster"); |
| 48 | expect(runsAsRole(host("Kubernetes.Deployment", { cluster }))).toBe(true); |
| 49 | }).pipe(Effect.runSync)); |
| 50 | |
| 51 | it("rejects Kubernetes workloads on other clusters", () => { |
| 52 | const gke = { Type: "GCP.Container.Cluster", LogicalId: "Cluster" }; |
| 53 | const local = { Type: "Kubernetes.LocalCluster", LogicalId: "Cluster" }; |
| 54 | const kubeconfig = { auth: { kind: "kubeconfig", context: "prod" } }; |
| 55 | for (const cluster of [gke, local, kubeconfig, undefined]) { |
| 56 | expect(runsAsRole(host("Kubernetes.Deployment", { cluster }))).toBe(false); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | it("rejects AWS resources that run no code as a role", () => { |
| 61 | expect(runsAsRole(host("AWS.S3.Bucket"))).toBe(false); |
| 62 | // A MicroVM image has no role of its own; RunMicrovm takes one per launch. |
| 63 | expect(runsAsRole(host("AWS.Lambda.MicrovmImage"))).toBe(false); |
| 64 | }); |
| 65 | |
| 66 | it("rejects hosts outside AWS", () => { |
| 67 | expect(runsAsRole(host("Neon.Function"))).toBe(false); |
| 68 | expect(runsAsRole(host("Cloudflare.Worker"))).toBe(false); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe( |
| 73 | "AWSBindings.runsAsGoogleServiceAccount", |
| 74 | { tags: ["unit", "provider:aws", "local"] }, |
| 75 | () => { |
| 76 | it("accepts Cloud Run and Cloud Functions", () => { |
| 77 | for (const Type of [ |
| 78 | "GCP.Run.Service", |
| 79 | "GCP.Run.Job", |
| 80 | "GCP.Run.WorkerPool", |
| 81 | "GCP.CloudFunctions.Function", |
| 82 | ]) { |
| 83 | expect(runsAsGoogleServiceAccount(host(Type))).toBe(true); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | it("rejects GKE pods, which have no Google ID tokens", () => { |
| 88 | const gke = { Type: "GCP.Container.Cluster", LogicalId: "Cluster" }; |
| 89 | expect(runsAsGoogleServiceAccount(host("Kubernetes.Deployment", { cluster: gke }))).toBe( |
| 90 | false, |
| 91 | ); |
| 92 | }); |
| 93 | |
| 94 | it("rejects AWS and other hosts", () => { |
| 95 | expect(runsAsGoogleServiceAccount(host("AWS.Lambda.Function"))).toBe(false); |
| 96 | expect(runsAsGoogleServiceAccount(host("Cloudflare.Worker"))).toBe(false); |
| 97 | }); |
| 98 | }, |
| 99 | ); |
| 100 | |
| 101 | const { test: liveTest } = Test.make({ |
| 102 | providers: Layer.mergeAll(AWS.providers(), GCP.providers()), |
| 103 | }); |
| 104 | |
| 105 | class SendNotReady extends Data.TaggedError("SendNotReady")<{ reason: string }> {} |
| 106 | |
| 107 | describe( |
| 108 | "AWS bindings on a Google service account host", |
| 109 | { tags: ["provider:aws", "provider:gcp", "provider:gcp:run", "live"] }, |
| 110 | () => { |
| 111 | liveTest.provider( |
| 112 | "Cloud Run reaches SQS by assuming a Role with its Google ID token", |
| 113 | (stack) => |
| 114 | Effect.gen(function* () { |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const out = yield* stack.deploy( |
| 118 | Effect.gen(function* () { |
| 119 | const host = yield* FederatedHost; |
| 120 | const jobs = yield* Jobs; |
| 121 | return { |
| 122 | uri: host.uri, |
| 123 | name: host.name, |
| 124 | uniqueId: host.serviceAccountUniqueId, |
| 125 | queueUrl: jobs.queueUrl, |
| 126 | }; |
| 127 | }), |
| 128 | ); |
| 129 | expect(out.uniqueId).toMatch(/^\d+$/); |
| 130 | |
| 131 | // The host has no AWS key, only the Role it assumes. |
| 132 | const service = yield* cloudrun.getProjectsLocationsServices({ name: out.name }); |
| 133 | const env = Object.fromEntries( |
| 134 | (service.template?.containers?.[0]?.env ?? []).map((entry) => [ |
| 135 | entry.name, |
| 136 | entry.value, |
| 137 | ]), |
| 138 | ); |
| 139 | expect(env.ALCHEMY_AWS_ACCESS_KEY_ID).toBeUndefined(); |
| 140 | expect(env.ALCHEMY_AWS_SECRET_ACCESS_KEY).toBeUndefined(); |
| 141 | const roleArn = env.ALCHEMY_AWS_WEB_IDENTITY_ROLE_ARN; |
| 142 | expect(roleArn).toMatch(/^arn:aws:iam::\d{12}:role\//); |
| 143 | const roleName = roleArn!.split("/").pop()!; |
| 144 | |
| 145 | const role = yield* iam.getRole({ RoleName: roleName }); |
| 146 | const trust = JSON.parse(decodeURIComponent(role.Role.AssumeRolePolicyDocument!)) as { |
| 147 | Statement: Array<Record<string, any>>; |
| 148 | }; |
| 149 | expect(trust.Statement).toEqual([ |
| 150 | expect.objectContaining({ |
| 151 | Effect: "Allow", |
| 152 | Principal: { Federated: "accounts.google.com" }, |
| 153 | Action: "sts:AssumeRoleWithWebIdentity", |
| 154 | Condition: { |
| 155 | StringEquals: { |
| 156 | "accounts.google.com:sub": out.uniqueId, |
| 157 | "accounts.google.com:aud": out.uniqueId, |
| 158 | "accounts.google.com:oaud": "sts.amazonaws.com", |
| 159 | }, |
| 160 | }, |
| 161 | }), |
| 162 | ]); |
| 163 | |
| 164 | // A new role's trust takes a while to propagate to STS. |
| 165 | const sent = yield* HttpClient.get(`${out.uri!.replace(/\/+$/, "")}/probe/send`).pipe( |
| 166 | Effect.flatMap((response) => |
| 167 | response.status === 200 |
| 168 | ? (response.json as Effect.Effect<ProbeOutcome<{ MessageId?: string }>>) |
| 169 | : Effect.fail(new SendNotReady({ reason: `HTTP ${response.status}` })), |
| 170 | ), |
| 171 | Effect.flatMap((outcome) => |
| 172 | outcome.ok |
| 173 | ? Effect.succeed(outcome.value) |
| 174 | : Effect.fail( |
| 175 | new SendNotReady({ |
| 176 | reason: `${outcome.error._tag}: ${outcome.error.message ?? ""}`, |
| 177 | }), |
| 178 | ), |
| 179 | ), |
| 180 | Effect.mapError((error) => |
| 181 | error._tag === "SendNotReady" ? error : new SendNotReady({ reason: String(error) }), |
| 182 | ), |
| 183 | Effect.tapError((error) => Effect.logInfo(`send not ready: ${error.reason}`)), |
| 184 | Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 24 }), |
| 185 | ); |
| 186 | expect(sent.MessageId).toBeDefined(); |
| 187 | |
| 188 | const received = yield* sqs |
| 189 | .receiveMessage({ |
| 190 | QueueUrl: out.queueUrl, |
| 191 | MaxNumberOfMessages: 1, |
| 192 | WaitTimeSeconds: 10, |
| 193 | }) |
| 194 | .pipe( |
| 195 | Effect.repeat({ |
| 196 | until: (response) => (response.Messages ?? []).length > 0, |
| 197 | times: 6, |
| 198 | }), |
| 199 | ); |
| 200 | expect(received.Messages?.[0]?.Body).toBe("hello from cloud run"); |
| 201 | |
| 202 | yield* stack.destroy(); |
| 203 | |
| 204 | const gone = yield* iam.getRole({ RoleName: roleName }).pipe( |
| 205 | Effect.as(false), |
| 206 | Effect.catchTag("NoSuchEntityException", () => Effect.succeed(true)), |
| 207 | ); |
| 208 | expect(gone).toBe(true); |
| 209 | }), |
| 210 | { timeout: 900_000 }, |
| 211 | ); |
| 212 | }, |
| 213 | ); |