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 { 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 storage from "@distilled.cloud/gcp/storage_v1"; |
| 6 | import * as iam from "@distilled.cloud/gcp/unstable/iam_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { callProbe, expectProbe } from "../bindingHost.ts"; |
| 11 | import StorageBindingsHost, { |
| 12 | DeleteAssets, |
| 13 | GetAssets, |
| 14 | PutAssets, |
| 15 | ReadAssets, |
| 16 | ReadWriteAssets, |
| 17 | SEED_KEY, |
| 18 | SEED_TEXT, |
| 19 | SignAssets, |
| 20 | WriteAssets, |
| 21 | } from "./fixtures/bindings-host.ts"; |
| 22 | |
| 23 | const testOptions = { providers: GCP.providers() }; |
| 24 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 25 | const sharedStack = Core.scratchStack(testOptions, "StorageBindings"); |
| 26 | |
| 27 | let baseUrl: string; |
| 28 | let hostAccount: string; |
| 29 | let project: string; |
| 30 | let buckets: { |
| 31 | read: string; |
| 32 | write: string; |
| 33 | readWrite: string; |
| 34 | get: string; |
| 35 | put: string; |
| 36 | delete: string; |
| 37 | sign: string; |
| 38 | }; |
| 39 | |
| 40 | /** Roles the host's service account holds on one bucket's IAM policy. */ |
| 41 | const bucketRoles = (bucket: string) => |
| 42 | storage.getIamPolicyBuckets({ bucket, optionsRequestedPolicyVersion: 3 }).pipe( |
| 43 | Effect.map((policy) => |
| 44 | (policy.bindings ?? []) |
| 45 | .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`)) |
| 46 | .map((binding) => binding.role) |
| 47 | .sort(), |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | const objectOf = (bucket: string, object: string) => |
| 52 | storage |
| 53 | .getObjects({ bucket, object }) |
| 54 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 55 | |
| 56 | describe( |
| 57 | "Storage Bindings", |
| 58 | { |
| 59 | tags: ["provider:gcp", "provider:gcp:storage", "provider:gcp:run", "live"], |
| 60 | }, |
| 61 | () => { |
| 62 | beforeAll( |
| 63 | Effect.gen(function* () { |
| 64 | yield* sharedStack.destroy(); |
| 65 | const out = yield* sharedStack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | const host = yield* StorageBindingsHost; |
| 68 | return { |
| 69 | uri: host.uri, |
| 70 | serviceAccount: host.serviceAccount, |
| 71 | project: host.project, |
| 72 | read: (yield* ReadAssets).bucketName, |
| 73 | write: (yield* WriteAssets).bucketName, |
| 74 | readWrite: (yield* ReadWriteAssets).bucketName, |
| 75 | get: (yield* GetAssets).bucketName, |
| 76 | put: (yield* PutAssets).bucketName, |
| 77 | delete: (yield* DeleteAssets).bucketName, |
| 78 | sign: (yield* SignAssets).bucketName, |
| 79 | }; |
| 80 | }), |
| 81 | ); |
| 82 | baseUrl = out.uri!; |
| 83 | hostAccount = out.serviceAccount!; |
| 84 | project = out.project; |
| 85 | buckets = { |
| 86 | read: out.read, |
| 87 | write: out.write, |
| 88 | readWrite: out.readWrite, |
| 89 | get: out.get, |
| 90 | put: out.put, |
| 91 | delete: out.delete, |
| 92 | sign: out.sign, |
| 93 | }; |
| 94 | }), |
| 95 | { timeout: 900_000 }, |
| 96 | ); |
| 97 | |
| 98 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 99 | |
| 100 | describe("ReadBucket", () => { |
| 101 | test.provider( |
| 102 | "head, get and list as the host, with objectViewer on the bucket only", |
| 103 | (_stack) => |
| 104 | Effect.gen(function* () { |
| 105 | const out = yield* expectProbe<{ |
| 106 | headName: string; |
| 107 | headSize: string; |
| 108 | text: string; |
| 109 | contentType: string; |
| 110 | listed: string[]; |
| 111 | missingHead: boolean; |
| 112 | missingGet: boolean; |
| 113 | }>(baseUrl, "readBucket"); |
| 114 | expect(out.headName).toEqual(SEED_KEY); |
| 115 | expect(out.headSize).toEqual(String(SEED_TEXT.length)); |
| 116 | expect(out.text).toEqual(SEED_TEXT); |
| 117 | expect(out.contentType).toEqual("text/plain"); |
| 118 | expect(out.listed).toEqual([SEED_KEY]); |
| 119 | expect(out.missingHead).toEqual(true); |
| 120 | expect(out.missingGet).toEqual(true); |
| 121 | |
| 122 | expect(yield* bucketRoles(buckets.read)).toEqual(["roles/storage.objectViewer"]); |
| 123 | }), |
| 124 | { |
| 125 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 126 | timeout: 600_000, |
| 127 | }, |
| 128 | ); |
| 129 | }); |
| 130 | |
| 131 | describe("WriteBucket", () => { |
| 132 | test.provider( |
| 133 | "put and delete as the host, with objectUser on the bucket only", |
| 134 | (_stack) => |
| 135 | Effect.gen(function* () { |
| 136 | const put = yield* expectProbe<{ name: string }>(baseUrl, "writeBucketPut"); |
| 137 | expect(put.name).toEqual("written.txt"); |
| 138 | const written = yield* objectOf(buckets.write, "written.txt"); |
| 139 | expect(written?.size).toEqual(String("hello from WriteBucket".length)); |
| 140 | expect(written?.contentType).toEqual("text/plain"); |
| 141 | expect(written?.metadata).toEqual({ source: "write-bucket" }); |
| 142 | |
| 143 | const deleted = yield* expectProbe<{ deleted: string }>(baseUrl, "writeBucketDelete"); |
| 144 | expect(deleted.deleted).toEqual("written.txt"); |
| 145 | expect(yield* objectOf(buckets.write, "written.txt")).toEqual(undefined); |
| 146 | |
| 147 | expect(yield* bucketRoles(buckets.write)).toEqual(["roles/storage.objectUser"]); |
| 148 | }), |
| 149 | { |
| 150 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 151 | timeout: 600_000, |
| 152 | }, |
| 153 | ); |
| 154 | }); |
| 155 | |
| 156 | describe("ReadWriteBucket", () => { |
| 157 | test.provider( |
| 158 | "put, get, list and delete as the host, with objectUser on the bucket only", |
| 159 | (_stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const out = yield* expectProbe<{ |
| 162 | putName: string; |
| 163 | text: string; |
| 164 | listed: string[]; |
| 165 | }>(baseUrl, "readWriteBucket"); |
| 166 | expect(out.putName).toEqual("round-trip.txt"); |
| 167 | expect(out.text).toEqual("hello from ReadWriteBucket"); |
| 168 | expect(out.listed).toEqual(["round-trip.txt"]); |
| 169 | const written = yield* objectOf(buckets.readWrite, "round-trip.txt"); |
| 170 | expect(written?.size).toEqual(String("hello from ReadWriteBucket".length)); |
| 171 | |
| 172 | const deleted = yield* expectProbe<{ gone: boolean }>(baseUrl, "readWriteBucketDelete"); |
| 173 | expect(deleted.gone).toEqual(true); |
| 174 | expect(yield* objectOf(buckets.readWrite, "round-trip.txt")).toEqual(undefined); |
| 175 | |
| 176 | expect(yield* bucketRoles(buckets.readWrite)).toEqual(["roles/storage.objectUser"]); |
| 177 | }), |
| 178 | { |
| 179 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 180 | timeout: 600_000, |
| 181 | }, |
| 182 | ); |
| 183 | }); |
| 184 | |
| 185 | describe("GetObject", () => { |
| 186 | test.provider( |
| 187 | "downloads the seeded object as the host, with objectViewer on the bucket only", |
| 188 | (_stack) => |
| 189 | Effect.gen(function* () { |
| 190 | const out = yield* expectProbe<{ |
| 191 | text: string; |
| 192 | contentType: string; |
| 193 | }>(baseUrl, "getObject"); |
| 194 | expect(out.text).toEqual(SEED_TEXT); |
| 195 | expect(out.contentType).toEqual("text/plain"); |
| 196 | |
| 197 | const missing = yield* callProbe(baseUrl, "getObjectMissing"); |
| 198 | expect(missing.ok ? undefined : missing.error._tag).toEqual( |
| 199 | "GCP.Storage.ObjectNotFound", |
| 200 | ); |
| 201 | |
| 202 | expect(yield* bucketRoles(buckets.get)).toEqual(["roles/storage.objectViewer"]); |
| 203 | }), |
| 204 | { |
| 205 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 206 | timeout: 600_000, |
| 207 | }, |
| 208 | ); |
| 209 | }); |
| 210 | |
| 211 | describe("PutObject", () => { |
| 212 | test.provider( |
| 213 | "uploads bytes with metadata as the host, with objectUser on the bucket only", |
| 214 | (_stack) => |
| 215 | Effect.gen(function* () { |
| 216 | const out = yield* expectProbe<{ |
| 217 | name: string; |
| 218 | metadata: Record<string, string>; |
| 219 | }>(baseUrl, "putObject"); |
| 220 | expect(out.name).toEqual("put.bin"); |
| 221 | expect(out.metadata).toEqual({ source: "put-object" }); |
| 222 | |
| 223 | const written = yield* objectOf(buckets.put, "put.bin"); |
| 224 | expect(written?.size).toEqual(String("hello from PutObject".length)); |
| 225 | expect(written?.contentType).toEqual("application/octet-stream"); |
| 226 | expect(written?.metadata).toEqual({ source: "put-object" }); |
| 227 | |
| 228 | expect(yield* bucketRoles(buckets.put)).toEqual(["roles/storage.objectUser"]); |
| 229 | }), |
| 230 | { |
| 231 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 232 | timeout: 600_000, |
| 233 | }, |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | describe("DeleteObject", () => { |
| 238 | test.provider( |
| 239 | "deletes the seeded object as the host, with objectUser on the bucket only", |
| 240 | (_stack) => |
| 241 | Effect.gen(function* () { |
| 242 | const seed = yield* objectOf(buckets.delete, SEED_KEY); |
| 243 | expect(seed?.name).toEqual(SEED_KEY); |
| 244 | |
| 245 | const out = yield* expectProbe<{ deleted: string }>(baseUrl, "deleteObject"); |
| 246 | expect(out.deleted).toEqual(SEED_KEY); |
| 247 | expect(yield* objectOf(buckets.delete, SEED_KEY)).toEqual(undefined); |
| 248 | |
| 249 | const missing = yield* callProbe(baseUrl, "deleteObjectMissing"); |
| 250 | expect(missing.ok ? undefined : missing.error._tag).toEqual("NotFound"); |
| 251 | |
| 252 | expect(yield* bucketRoles(buckets.delete)).toEqual(["roles/storage.objectUser"]); |
| 253 | }), |
| 254 | { |
| 255 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 256 | timeout: 600_000, |
| 257 | }, |
| 258 | ); |
| 259 | }); |
| 260 | |
| 261 | describe("SignGetObjectUrl", () => { |
| 262 | test.provider( |
| 263 | "mints a V4 URL that downloads without credentials; grants objectViewer on the bucket and tokenCreator on the host's own account", |
| 264 | (_stack) => |
| 265 | Effect.gen(function* () { |
| 266 | const { url } = yield* expectProbe<{ url: string }>(baseUrl, "signGetObjectUrl"); |
| 267 | expect(url).toContain(`https://storage.googleapis.com/${buckets.sign}/seed/hello.txt?`); |
| 268 | expect(url).toContain(`X-Goog-Credential=${encodeURIComponent(hostAccount)}`); |
| 269 | |
| 270 | // No credentials: the signature alone authorizes the download. |
| 271 | // The signer's bucket grant may still be propagating. |
| 272 | const body = yield* HttpClient.get(url).pipe( |
| 273 | Effect.flatMap((response) => |
| 274 | response.status === 200 |
| 275 | ? response.text |
| 276 | : Effect.fail(new Error(`HTTP ${response.status}`)), |
| 277 | ), |
| 278 | Effect.retry({ |
| 279 | schedule: Schedule.spaced("5 seconds"), |
| 280 | times: 24, |
| 281 | }), |
| 282 | ); |
| 283 | expect(body).toEqual(SEED_TEXT); |
| 284 | |
| 285 | expect(yield* bucketRoles(buckets.sign)).toEqual(["roles/storage.objectViewer"]); |
| 286 | const accountPolicy = yield* iam.getIamPolicyProjectsServiceAccounts({ |
| 287 | resource: `projects/${project}/serviceAccounts/${hostAccount}`, |
| 288 | }); |
| 289 | expect( |
| 290 | (accountPolicy.bindings ?? []) |
| 291 | .filter((binding) => |
| 292 | (binding.members ?? []).includes(`serviceAccount:${hostAccount}`), |
| 293 | ) |
| 294 | .map((binding) => binding.role), |
| 295 | ).toEqual(["roles/iam.serviceAccountTokenCreator"]); |
| 296 | }), |
| 297 | { |
| 298 | tags: ["provider:gcp", "provider:gcp:storage", "live"], |
| 299 | timeout: 600_000, |
| 300 | }, |
| 301 | ); |
| 302 | }); |
| 303 | }, |
| 304 | ); |