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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as firebaseappcheck from "@distilled.cloud/gcp/firebaseappcheck_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { expectProbe } from "../bindingHost.ts"; |
| 9 | import { currentProject, runLifecycle } from "./common.ts"; |
| 10 | import AppCheckBindingsHost, { Local } from "./fixtures/bindings-host.ts"; |
| 11 | |
| 12 | const testOptions = { providers: GCP.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "AppCheckBindings"); |
| 15 | |
| 16 | let baseUrl: string; |
| 17 | let hostAccount: string; |
| 18 | let debugTokenName: string; |
| 19 | let appName: string; |
| 20 | |
| 21 | const jwtClaims = (token: string) => |
| 22 | JSON.parse(Buffer.from(token.split(".")[1] ?? "", "base64url").toString("utf8")) as { |
| 23 | sub?: string; |
| 24 | aud?: string[]; |
| 25 | }; |
| 26 | |
| 27 | // App Check needs a Firebase project with the App Check API enabled (see |
| 28 | // ./common.ts); set GCP_TEST_FIREBASE_APP_ID to run. |
| 29 | describe.skipIf(!runLifecycle)( |
| 30 | "FirebaseAppCheck Bindings", |
| 31 | { |
| 32 | tags: ["provider:gcp", "provider:gcp:firebaseappcheck", "provider:gcp:run", "live"], |
| 33 | }, |
| 34 | () => { |
| 35 | beforeAll( |
| 36 | Effect.gen(function* () { |
| 37 | yield* sharedStack.destroy(); |
| 38 | const out = yield* sharedStack.deploy( |
| 39 | Effect.gen(function* () { |
| 40 | const host = yield* AppCheckBindingsHost; |
| 41 | const debug = yield* Local; |
| 42 | return { |
| 43 | uri: host.uri, |
| 44 | serviceAccount: host.serviceAccount, |
| 45 | debugToken: debug.name, |
| 46 | app: debug.app, |
| 47 | }; |
| 48 | }), |
| 49 | ); |
| 50 | baseUrl = out.uri!; |
| 51 | hostAccount = out.serviceAccount!; |
| 52 | debugTokenName = out.debugToken; |
| 53 | appName = out.app; |
| 54 | }), |
| 55 | { timeout: 900_000 }, |
| 56 | ); |
| 57 | |
| 58 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 59 | |
| 60 | describe("ExchangeDebugToken", () => { |
| 61 | test.provider( |
| 62 | "mints an App Check token for the app, with no IAM grant", |
| 63 | (_stack) => |
| 64 | Effect.gen(function* () { |
| 65 | const out = yield* expectProbe<firebaseappcheck.GoogleFirebaseAppcheckV1AppCheckToken>( |
| 66 | baseUrl, |
| 67 | "exchangeDebugToken", |
| 68 | ); |
| 69 | expect(out.ttl).toEqual(expect.any(String)); |
| 70 | const claims = jwtClaims(out.token ?? ""); |
| 71 | // `sub` is the app id: the last segment of `projects/p/apps/{id}`. |
| 72 | expect(claims.sub).toEqual(appName.split("/").pop()); |
| 73 | |
| 74 | // The debug token the exchange used is still registered. |
| 75 | const debug = yield* firebaseappcheck.getProjectsAppsDebugTokens({ |
| 76 | name: debugTokenName, |
| 77 | }); |
| 78 | expect(debug.displayName).toEqual("alchemy-exchange"); |
| 79 | |
| 80 | // exchangeDebugToken is not IAM-gated: the binding grants nothing. |
| 81 | const project = yield* currentProject; |
| 82 | const policy = yield* resourcemanager.getIamPolicyProjects({ |
| 83 | resource: `projects/${project}`, |
| 84 | body: { options: { requestedPolicyVersion: 3 } }, |
| 85 | }); |
| 86 | const roles = (policy.bindings ?? []).filter((binding) => |
| 87 | (binding.members ?? []).includes(`serviceAccount:${hostAccount}`), |
| 88 | ); |
| 89 | expect(roles).toEqual([]); |
| 90 | }), |
| 91 | { |
| 92 | tags: ["provider:gcp", "provider:gcp:firebaseappcheck", "live"], |
| 93 | timeout: 600_000, |
| 94 | }, |
| 95 | ); |
| 96 | }); |
| 97 | }, |
| 98 | ); |