import { Credentials, CredentialsFromEnv, credentials, MachineIdentity, } from "@distilled.cloud/fly-io"; import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient"; import type { RuntimeContext } from "alchemy/RuntimeContext"; import type * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as FetchHttpClient from "effect/http/FetchHttpClient"; import * as HttpClient from "effect/http/HttpClient"; import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; import type { App } from "./App.ts"; import { bindFlyApiToken } from "./Credentials.ts"; import type { Secret } from "./Secret.ts"; import type { SecretKey } from "./SecretKey.ts"; /** * Shared scaffolding for the HTTP-backed Fly Secret bindings. * * Fly has no native Worker-style binding. This layer captures the ambient * org token during stack-eval (so Actions work in-process) and `yield*`s * it plus `appName` / secret `name` so RuntimeContext.set runs. Platform * copies those Outputs into host env; runtime `yield*` gets them back. * {@link CredentialsFromEnv} still reads `FLY_API_TOKEN` after that copy. * * NOT exported from `index.ts`. */ export type AppNamed = Secret | SecretKey; export const makeHttpSecretBinding = (options: { makeClient: ( auth: SecretAuth, appName: Effect.Effect, secretName: Effect.Effect, ) => Client; /** * PetSem encrypt/sign/decrypt/verify only work from a Machine over * `/.fly/api` (implicit machine identity). Org tokens return Forbidden. */ kms?: boolean; }) => Effect.gen(function* () { const context = yield* Effect.context(); const auth = options.kms === true ? makeKmsAuth(context) : makeSecretAuth(context); return Effect.fn(function* (resource: Target) { yield* bindFlyApiToken().pipe(Effect.provideContext(context)); const appName = yield* resource.appName; const secretName = yield* resource.name; return options.makeClient(auth, appName, secretName); }); }); /** * Same scaffolding as {@link makeHttpSecretBinding}, but the target is * an {@link App} (no secret name). Used by ListSecrets. */ export const makeHttpAppBinding = (options: { makeClient: (auth: SecretAuth, appName: Effect.Effect) => Client; }) => Effect.gen(function* () { const context = yield* Effect.context(); return Effect.fn(function* (app: App) { yield* bindFlyApiToken().pipe(Effect.provideContext(context)); const appName = yield* app.appName; return options.makeClient(makeSecretAuth(context), appName); }); }); /** * Injectable auth for the Secret HTTP client builders. Supplies an * `authorize` that provides `Credentials` + `HttpClient` to a raw SDK op. */ export interface SecretAuth { authorize: ( eff: Effect.Effect, ) => Effect.Effect; } /** Build auth that uses ambient stack creds, or env creds inside a host. */ export const makeSecretAuth = ( ambient: Context.Context, ): SecretAuth => ({ authorize: ( eff: Effect.Effect, ): Effect.Effect => { if (globalThis.__ALCHEMY_RUNTIME__) { // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- TimeoutError/env-credential errors surface untyped; widening SecretAuth would ripple through every Fly secret client return eff.pipe( Effect.provide(Layer.mergeAll(CredentialsFromEnv, FetchHttpClient.layer)), Effect.timeout("8 seconds"), ) as Effect.Effect; } return eff.pipe(Effect.provideContext(ambient)) as Effect.Effect; }, }); const FLY_MACHINE_API_SOCKET = "/.fly/api"; /** Distilled machines client over `/.fly/api`. URL host is unused. */ const flyMachineApiHttp: Layer.Layer = NodeHttpClient.layerNodeHttpNoAgent.pipe( Layer.provide( NodeHttpClient.layerAgentOptions({ socketPath: FLY_MACHINE_API_SOCKET, keepAlive: false, }), ), ); /** * PetSem encrypt/sign/decrypt/verify from a Machine. Org API tokens are * Forbidden; the machine identity is the unix socket at `/.fly/api`. * {@link MachineIdentity} is the protocol signal (no Authorization, * Connection: close). GetSecret in the same process keeps Bearer. */ export const makeKmsAuth = ( ambient: Context.Context, ): SecretAuth => ({ authorize: ( eff: Effect.Effect, ): Effect.Effect => { if (globalThis.__ALCHEMY_RUNTIME__) { // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- TimeoutError surfaces untyped; widening SecretAuth would ripple through every Fly secret client return Effect.scoped( eff.pipe( Effect.provide( Layer.mergeAll( Layer.succeed(MachineIdentity, true), flyMachineApiHttp, credentials({ apiKey: Redacted.make("unused"), apiBaseUrl: "http://localhost/v1", }), ), ), ), ).pipe(Effect.timeout("8 seconds")) as Effect.Effect; } return eff.pipe(Effect.provideContext(ambient)) as Effect.Effect; }, }); export const unwrapSecretValue = (value: Redacted.Redacted | string): string => Redacted.isRedacted(value) ? Redacted.value(value) : value;