The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ConfigError } from "@distilled.cloud/core/errors"; |
| 2 | import { Credentials, type Config as CredentialsConfig } from "@distilled.cloud/gcp/Credentials"; |
| 3 | import { GCPCredentialsError } from "@distilled.cloud/gcp/Errors"; |
| 4 | import { lazyCredentials } from "alchemy/Auth/Resolve"; |
| 5 | import * as Config from "effect/Config"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import { GCP_AUTH_PROVIDER_NAME, type GcpResolvedCredentials } from "./AuthProvider.ts"; |
| 13 | import { |
| 14 | cacheCredentials, |
| 15 | fetchMetadataProject, |
| 16 | fetchMetadataToken, |
| 17 | } from "./MetadataCredentials.ts"; |
| 18 | import { mintAccessToken, parseServiceAccountKey } from "./Token.ts"; |
| 19 | |
| 20 | export { |
| 21 | Credentials, |
| 22 | CredentialsFromEnv, |
| 23 | fromAccessToken, |
| 24 | type Config as CredentialsConfig, |
| 25 | } from "@distilled.cloud/gcp/Credentials"; |
| 26 | |
| 27 | /** |
| 28 | * Build a `Credentials` layer that resolves GCP credentials via the |
| 29 | * Alchemy AuthProvider using the configured profile (defaults to "default", |
| 30 | * overridable with the `ALCHEMY_PROFILE` env/config value). |
| 31 | * |
| 32 | * Maps onto `@distilled.cloud/gcp`'s `{ accessToken, project }` shape. |
| 33 | * Access tokens are minted from a service-account key when |
| 34 | * `GOOGLE_APPLICATION_CREDENTIALS` is set. |
| 35 | */ |
| 36 | export const fromAuthProvider = () => |
| 37 | Layer.effect( |
| 38 | Credentials, |
| 39 | // Minted service-account tokens are re-resolved shortly before they |
| 40 | // expire; supplied access tokens are cached for the process. |
| 41 | lazyCredentials<GcpResolvedCredentials, GCPCredentialsError>()( |
| 42 | GCP_AUTH_PROVIDER_NAME, |
| 43 | (creds) => ({ |
| 44 | accessToken: creds.accessToken, |
| 45 | project: creds.project, |
| 46 | region: creds.region, |
| 47 | }), |
| 48 | { expiresAt: (creds) => creds.expiresAt }, |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | /** |
| 53 | * GCP credential chain for Effect-native GKE workload pods (and local |
| 54 | * processes): `GOOGLE_ACCESS_TOKEN`, then |
| 55 | * `GOOGLE_APPLICATION_CREDENTIALS`, then the GCE metadata server |
| 56 | * (Workload Identity). Minted tokens are cached until shortly before they |
| 57 | * expire. |
| 58 | */ |
| 59 | export const fromChain = () => |
| 60 | Layer.effect( |
| 61 | Credentials, |
| 62 | Effect.gen(function* () { |
| 63 | const fs = yield* FileSystem.FileSystem; |
| 64 | const http = yield* HttpClient.HttpClient; |
| 65 | const cached = yield* cacheCredentials( |
| 66 | Effect.gen(function* () { |
| 67 | const envToken = yield* Config.option(Config.String("GOOGLE_ACCESS_TOKEN")); |
| 68 | const envProject = yield* Config.option( |
| 69 | Config.String("GOOGLE_PROJECT_ID").pipe( |
| 70 | Config.orElse(() => Config.String("GOOGLE_CLOUD_PROJECT")), |
| 71 | ), |
| 72 | ); |
| 73 | if (Option.isSome(envToken)) { |
| 74 | return { |
| 75 | config: { |
| 76 | accessToken: Redacted.make(envToken.value), |
| 77 | project: Option.getOrUndefined(envProject), |
| 78 | }, |
| 79 | expiresAt: undefined, |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | const keyFile = yield* Config.option(Config.String("GOOGLE_APPLICATION_CREDENTIALS")); |
| 84 | if (Option.isSome(keyFile)) { |
| 85 | const raw = yield* fs.readFileString(keyFile.value).pipe( |
| 86 | Effect.mapError((cause) => |
| 87 | ConfigError.make({ |
| 88 | message: `Failed to read GOOGLE_APPLICATION_CREDENTIALS at ${keyFile.value}: ${cause.message}`, |
| 89 | }), |
| 90 | ), |
| 91 | ); |
| 92 | const sa = yield* parseServiceAccountKey(raw).pipe( |
| 93 | Effect.mapError((cause) => |
| 94 | ConfigError.make({ |
| 95 | message: `Invalid GOOGLE_APPLICATION_CREDENTIALS JSON: ${cause.message}`, |
| 96 | }), |
| 97 | ), |
| 98 | ); |
| 99 | const minted = yield* mintAccessToken(sa).pipe( |
| 100 | Effect.mapError((cause) => |
| 101 | ConfigError.make({ |
| 102 | message: `Failed to mint a Google access token from the service-account key: ${cause.message}`, |
| 103 | }), |
| 104 | ), |
| 105 | ); |
| 106 | return { |
| 107 | config: { |
| 108 | accessToken: minted.accessToken, |
| 109 | project: Option.getOrUndefined(envProject) ?? sa.project_id ?? minted.project, |
| 110 | }, |
| 111 | expiresAt: minted.expirationMs, |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | const token = yield* fetchMetadataToken(http); |
| 116 | const project = |
| 117 | Option.getOrUndefined(envProject) ?? |
| 118 | (yield* fetchMetadataProject(http).pipe(Effect.orElseSucceed(() => undefined))); |
| 119 | return { |
| 120 | config: { accessToken: token.accessToken, project }, |
| 121 | expiresAt: token.expiresAt, |
| 122 | }; |
| 123 | }), |
| 124 | ); |
| 125 | // Distilled `Credentials` is `Effect<Config>` (error `never`). |
| 126 | return cached.pipe(Effect.orDie) as Effect.Effect<CredentialsConfig>; |
| 127 | }), |
| 128 | ); |