The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 5 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 6 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 7 | import * as iam from "@distilled.cloud/gcp/unstable/iam_v1"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | |
| 11 | /** |
| 12 | * Host IAM plumbing without a container build: image-only hosts whose |
| 13 | * Effect declares bindings. The bindings' IAM and env land on the host's |
| 14 | * runtime service account even though the image is not Alchemy-built, |
| 15 | * so this pins grant scoping, concurrent policy writes, revocation, and |
| 16 | * account cleanup on any machine with GCP credentials. |
| 17 | */ |
| 18 | |
| 19 | const { test } = Test.make({ providers: GCP.providers() }); |
| 20 | |
| 21 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 22 | |
| 23 | const HELLO_IMAGE = "us-docker.pkg.dev/cloudrun/container/hello"; |
| 24 | |
| 25 | const Events = GCP.PubSub.Topic("IamEvents", {}); |
| 26 | const Blobs = GCP.Storage.Bucket("IamBlobs", { forceDestroy: true }); |
| 27 | |
| 28 | class IamService extends GCP.Function<IamService>()( |
| 29 | "IamService", |
| 30 | { |
| 31 | location: "us-central1", |
| 32 | template: { containers: [{ image: HELLO_IMAGE }] }, |
| 33 | }, |
| 34 | Effect.gen(function* () { |
| 35 | yield* GCP.PubSub.Publish(Events); |
| 36 | yield* GCP.Storage.GetObject(Blobs); |
| 37 | return {}; |
| 38 | }).pipe(Effect.provide(GCP.PubSub.PublishHttp), Effect.provide(GCP.Storage.GetObjectHttp)), |
| 39 | ) {} |
| 40 | |
| 41 | class IamServicePublishOnly extends GCP.Function<IamServicePublishOnly>()( |
| 42 | "IamService", |
| 43 | { |
| 44 | location: "us-central1", |
| 45 | template: { containers: [{ image: HELLO_IMAGE }] }, |
| 46 | }, |
| 47 | Effect.gen(function* () { |
| 48 | yield* GCP.PubSub.Publish(Events); |
| 49 | yield* Blobs; |
| 50 | return {}; |
| 51 | }).pipe(Effect.provide(GCP.PubSub.PublishHttp)), |
| 52 | ) {} |
| 53 | |
| 54 | // A second host granting on the same topic in the same deploy: both write |
| 55 | // the topic's IAM policy concurrently and must both land. |
| 56 | class IamJob extends GCP.Run.Job<IamJob>()( |
| 57 | "IamJob", |
| 58 | { |
| 59 | location: "us-central1", |
| 60 | containers: [{ image: HELLO_IMAGE }], |
| 61 | }, |
| 62 | Effect.gen(function* () { |
| 63 | yield* GCP.PubSub.Publish(Events); |
| 64 | return {}; |
| 65 | }).pipe(Effect.provide(GCP.PubSub.PublishHttp)), |
| 66 | ) {} |
| 67 | |
| 68 | const membersOf = ( |
| 69 | policy: { |
| 70 | bindings?: ReadonlyArray<{ |
| 71 | role?: string; |
| 72 | members?: ReadonlyArray<string>; |
| 73 | }>; |
| 74 | }, |
| 75 | member: string, |
| 76 | ) => |
| 77 | [ |
| 78 | ...new Set( |
| 79 | (policy.bindings ?? []) |
| 80 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 81 | .flatMap((binding) => (binding.role ? [binding.role] : [])), |
| 82 | ), |
| 83 | ].sort(); |
| 84 | |
| 85 | test.provider( |
| 86 | "bindings grant on the bound resource, converge concurrently, and revoke on removal", |
| 87 | (stack) => |
| 88 | Effect.gen(function* () { |
| 89 | yield* stack.destroy(); |
| 90 | |
| 91 | const deploy = (host: typeof IamService | typeof IamServicePublishOnly) => |
| 92 | stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | const service = yield* host; |
| 95 | const job = yield* IamJob; |
| 96 | const topic = yield* Events; |
| 97 | const bucket = yield* Blobs; |
| 98 | return { |
| 99 | project: service.project, |
| 100 | serviceAccount: service.serviceAccount, |
| 101 | managed: service.managedServiceAccount, |
| 102 | grants: service.iamGrants, |
| 103 | jobAccount: job.serviceAccount, |
| 104 | topic: topic.name, |
| 105 | bucket: bucket.bucketName, |
| 106 | }; |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | const out = yield* deploy(IamService); |
| 111 | expect(out.managed).toEqual(true); |
| 112 | expect(out.serviceAccount).toMatch(/^alch-/); |
| 113 | expect(out.jobAccount).toMatch(/^alch-/); |
| 114 | expect(out.jobAccount).not.toEqual(out.serviceAccount); |
| 115 | const service = `serviceAccount:${out.serviceAccount}`; |
| 116 | const job = `serviceAccount:${out.jobAccount}`; |
| 117 | |
| 118 | expect( |
| 119 | membersOf( |
| 120 | yield* resourcemanager.getIamPolicyProjects({ |
| 121 | resource: `projects/${out.project}`, |
| 122 | }), |
| 123 | service, |
| 124 | ), |
| 125 | ).toEqual([]); |
| 126 | const topicPolicy = yield* pubsub.getIamPolicyProjectsTopics({ |
| 127 | resource: out.topic, |
| 128 | }); |
| 129 | expect(membersOf(topicPolicy, service)).toEqual(["roles/pubsub.publisher"]); |
| 130 | expect(membersOf(topicPolicy, job)).toEqual(["roles/pubsub.publisher"]); |
| 131 | expect( |
| 132 | membersOf(yield* storage.getIamPolicyBuckets({ bucket: out.bucket }), service), |
| 133 | ).toEqual(["roles/storage.objectViewer"]); |
| 134 | expect(out.grants.map((grant) => grant.kind).sort()).toEqual([ |
| 135 | "pubsub.topic", |
| 136 | "storage.bucket", |
| 137 | ]); |
| 138 | |
| 139 | const next = yield* deploy(IamServicePublishOnly); |
| 140 | expect(next.serviceAccount).toEqual(out.serviceAccount); |
| 141 | expect(next.grants.map((grant) => grant.kind)).toEqual(["pubsub.topic"]); |
| 142 | expect( |
| 143 | membersOf(yield* storage.getIamPolicyBuckets({ bucket: out.bucket }), service), |
| 144 | ).toEqual([]); |
| 145 | expect( |
| 146 | membersOf(yield* pubsub.getIamPolicyProjectsTopics({ resource: out.topic }), service), |
| 147 | ).toEqual(["roles/pubsub.publisher"]); |
| 148 | |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | for (const email of [out.serviceAccount, out.jobAccount]) { |
| 152 | const gone = yield* iam |
| 153 | .getProjectsServiceAccounts({ |
| 154 | name: `projects/${out.project}/serviceAccounts/${email}`, |
| 155 | }) |
| 156 | .pipe( |
| 157 | Effect.as("found" as const), |
| 158 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 159 | ); |
| 160 | expect(gone).toEqual("gone"); |
| 161 | } |
| 162 | }).pipe(logLevel), |
| 163 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 240_000 }, |
| 164 | ); |