sandbox-credentials.ts
This file
Path
apps/platform/src/adapters/sandbox-credentials.ts
165 lines · 4.7 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–8why, one step back1import { RepoHost } from "@repo/artifacts/repo-host";2import type {3 RepoName,4 RepoNotFound,5 TokenScope,6} from "@repo/artifacts/repo-host";7import type { Remote } from "@repo/checks/check-runner/wire";8import { VerifierBlocked } from "@repo/protocol/refusal";Merge observability into mainoscabrielbefore adoption1985111
line 9why, one step back9import { unquoted } from "@repo/telemetry/defects";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 10–20why, one step back10import {11 Cause,12 Data,13 Duration,14 Effect,15 Exit,16 Option,17 Redacted,18 Schema,19} from "effect";20Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 21why, one step back21// Each token is revoked when its job ends, so this bounds only a revoke that failed.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 22–37why, one step back22export const jobTokenTtl = Duration.minutes(5);2324export type SandboxMethod =25 | "bundleRules"26 | "check"27 | "compose"28 | "land"29 | "merge";3031export interface RemoteNeed {32 readonly repo: RepoName;33 readonly scope: TokenScope;34}3536const DETAIL_LIMIT = 2000;37Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 38why, one step back38// The far side holds the tokens and may echo one in an error.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 39–66why, one step back39const scrub = (text: string, remotes: readonly Remote[]) => {40 let scrubbed = text;4142 for (const { token } of remotes) {43 const secret = Redacted.value(token);4445 if (secret.length > 0) {46 scrubbed = scrubbed.replaceAll(secret, "<token>");47 }48 }4950 return scrubbed.slice(0, DETAIL_LIMIT);51};5253const blocked = (54 method: SandboxMethod,55 annotations: Readonly<Record<string, string>>56) =>57 Effect.logError("sandbox job blocked").pipe(58 Effect.annotateLogs({ ...annotations, method }),59 Effect.andThen(Effect.fail(new VerifierBlocked({})))60 );6162const unknownRepo =63 (method: SandboxMethod) =>64 ({ name }: RepoNotFound) =>65 blocked(method, { failure: "repo not found", repo: name });66Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
line 67why, one step back67// A failed revoke is logged with the token scrubbed, never raised: the token dies with its lifetime.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 68–118why, one step back68export const withRemote = <A, E, R>(69 method: SandboxMethod,70 need: RemoteNeed,71 use: (remote: Remote) => Effect.Effect<A, E, R>72): Effect.Effect<A, E | VerifierBlocked, R | RepoHost> =>73 Effect.gen(function* () {74 const host = yield* RepoHost;7576 const url = yield* host77 .remote(need.repo)78 .pipe(Effect.catchTag("RepoNotFound", unknownRepo(method)));7980 return yield* Effect.acquireUseRelease(81 host.mintToken(need.repo, need.scope, jobTokenTtl).pipe(82 Effect.map(({ id, secret }) => ({83 id,84 remote: { token: secret, url } satisfies Remote,85 })),86 Effect.catchTag("RepoNotFound", unknownRepo(method))87 ),88 ({ remote }) => use(remote),89 ({ id, remote }) =>90 host.revokeToken(need.repo, id).pipe(91 Effect.catchCause((cause) =>92 Effect.logWarning("sandbox job token not revoked").pipe(93 Effect.annotateLogs({94 cause: scrub(Cause.pretty(cause), [remote]),95 method,96 repo: need.repo,97 tokenId: id,98 })99 )100 )101 )102 );103 });104105class Unreached extends Data.TaggedError("Unreached")<{106 readonly failure: "rejected" | "undecodable" | "unencodable";107 readonly detail: string;108}> {}109110export interface SandboxJob<W, WI, A, E, X> {111 readonly method: SandboxMethod;112 readonly wire: Schema.Codec<W, WI>;113 readonly exit: Schema.Codec<Exit.Exit<A, E>, X>;114 readonly job: W;115 readonly remotes: readonly Remote[];116 readonly send: (encoded: WI) => Promise<X>;117}118Merge observability into mainoscabrielbefore adoption1985111
lines 119–120why, one step back119// A job's defect is logged by its name and frames alone: a Checker's may120// quote a sealed file's path.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 121–130why, one step back121const settle = <A, E>(122 method: SandboxMethod,123 remotes: readonly Remote[],124 exit: Exit.Exit<A, E>125): Effect.Effect<A, E | VerifierBlocked> =>126 Exit.isSuccess(exit)127 ? Effect.succeed(exit.value)128 : Option.match(Cause.findErrorOption(exit.cause), {129 onNone: () =>130 blocked(method, {Merge observability into mainoscabrielbefore adoption1985111
line 131why, one step back131 detail: scrub(Cause.pretty(unquoted(exit.cause)), remotes),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 132–165why, one step back132 failure: "defect",133 }),134 onSome: (refusal): Effect.Effect<never, E> => Effect.fail(refusal),135 });136137export const sandboxJob = <W, WI, A, E, X>(138 call: SandboxJob<W, WI, A, E, X>139): Effect.Effect<A, E | VerifierBlocked> =>140 Schema.encodeEffect(call.wire)(call.job).pipe(141 Effect.mapError(142 ({ message }) =>143 new Unreached({ detail: message, failure: "unencodable" })144 ),145 Effect.flatMap((encoded) =>146 Effect.tryPromise({147 catch: (cause) =>148 new Unreached({ detail: String(cause), failure: "rejected" }),149 try: () => call.send(encoded),150 })151 ),152 Effect.flatMap((outcome) =>153 Schema.decodeEffect(call.exit)(outcome).pipe(154 Effect.mapError(155 ({ message }) =>156 new Unreached({ detail: message, failure: "undecodable" })157 )158 )159 ),160 Effect.matchEffect({161 onFailure: ({ detail, failure }) =>162 blocked(call.method, { detail: scrub(detail, call.remotes), failure }),163 onSuccess: (exit) => settle(call.method, call.remotes, exit),164 })165 );