Lines 2–9 at 318 (were 2–8 at 89)
22// them, and the token. 44// The token is never in the folder: it comes from the ABSO_TOKEN environment 5−// variable, or else from ~/.avcs/token, outside every repo.
5+// variable, or else from ~/.avcs/token, outside every repo. A pipeline run's 6+// key comes from the file ABSO_KEY_FILE names, and is only ever held in memory. 78import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; 89import { homedir } from "node:os"; Lines 11–17 at 318 (were 10–15 at 89)
1011import { UsageError } from "./args.ts"; 1112import { type MachineServers, machineServers, TOKEN_SERVERS, tokenGoesTo, trustedList } from "./core.ts"; 1213import { fromEnv } from "./env-names.ts"; 14+import { isLocal, storeDir } from "./local-store.ts"; 1315import { homeFile, stateDir } from "./state-dir.ts"; 1517/** What .avcs/config.json holds. */ Lines 28–41 at 318 (were 26–35 at 89)
2628 * routes anyone can read and sends no credential. It has no agent; its config holds server, project and this. 2830 readonly visitor?: boolean; 31+ /** A local project's store (abso init --local --store), when it isn't the machine's own: ABSO_STORE or the OS data folder. */ 32+ readonly store?: string; 3135export interface Config extends Settings { 3236 readonly token?: string; 37+ /** For a local project (server "local"): the store folder its history is in, or undefined when there's none to use. */ 38+ readonly localStore?: string; 3541export function configPath(root: string): string { Lines 53–63 at 318 (were 47–53 at 89)
50−export function readConfig(root: string, env: Readonly<Record<string, string | undefined>> = process.env): Config {
56+export function readConfig( 58+ env: Readonly<Record<string, string | undefined>> = process.env, 5161 const raw = JSON.parse(readFileSync(configPath(root), "utf8")) as Partial<Settings>; 5262 if (raw.visitor === true) { 5363 for (const key of ["server", "project"] as const) { Lines 69–83 at 318 (were 59–69 at 89)
5969 for (const key of ["server", "project", "agent"] as const) { 6070 if (typeof raw[key] !== "string" || raw[key] === "") throw new Error(`${configPath(root)} has no "${key}"`); 62− const token = tokenFor(raw.server!, env, root);
72+ // A local project's calls never leave this process, so no credential goes with them. 73+ const local = isLocal(raw.server); 74+ const token = local ? undefined : tokenFor(raw.server!, env, root, tokenFileOk); 6476 server: raw.server!, 6577 project: raw.project!, 79+ ...(typeof raw.store === "string" && raw.store !== "" ? { store: raw.store } : {}), 80+ ...(local ? { localStore: storeDir(env, raw.store, root) } : {}), 6781 ...(typeof raw.follow === "string" && raw.follow !== "" ? { follow: raw.follow } : {}), 6882 ...(raw.reader === true ? { reader: true } : {}), 6983 ...(token ? { token } : {}), Lines 111–126 at 318 (were 97–109 at 89)
99113 env: Readonly<Record<string, string | undefined>> = process.env, 100−): { kind: "agent key" | "shared token" | "none"; id: string | null } {
114+ tokenFileOk = false, 115+): { kind: "agent key" | "shared token" | "pipeline run key" | "none"; id: string | null } { 101116 const key = readKeyFile(root); 103118 if (originOf(key.server) !== originOf(server)) return { kind: "none", id: null }; 104119 return { kind: "agent key", id: key.id }; 106− return { kind: readToken(env) ? "shared token" : "none", id: null };
121+ if (fromEnv(env, "TOKEN")) return { kind: "shared token", id: null }; 122+ if (fromEnv(env, "KEY_FILE")) return { kind: "pipeline run key", id: null }; 123+ return { kind: tokenFileOk && tokenFile() ? "shared token" : "none", id: null }; 109126interface StoredKey { Lines 192–226 at 318 (were 175–192 at 89)
176193 env: Readonly<Record<string, string | undefined>> = process.env, 195+ tokenFileOk = false, 178196): string | undefined { 180198 const key = readKeyFile(root); 199+ // A folder with a key signs in with that key and nothing else, even when the server refuses it. 200+ if (!key && existsSync(keyPath(root))) 201+ throw new UsageError( 202+ `this folder's key file (${keyPath(root)}) can't be read, so abso sends nothing; abso key install puts a key back`, 182205 if (originOf(key.server) !== originOf(server)) 183206 throw new UsageError("this folder's key was issued for a different server, so abso will not send it"); 187− const token = readToken(env);
188− const trusted = trustedServers(env);
189− if (token === undefined || tokenGoesTo(server, trusted)) return token;
210+ return trustedToken( 213+ fromEnv(env, "TOKEN")?.trim() || runKey(env) || (tokenFileOk ? readToken(env) : undefined), 217+/** The token as given, if abso may send it to this server; refused, in words, if not. */ 218+export function trustedToken( 220+ env: Readonly<Record<string, string | undefined>>, 221+ token: string | undefined, 222+): string | undefined { 223+ if (token === undefined || tokenGoesTo(server, trustedServers(env))) return token; 190224 throw new UsageError( 191225 `this folder's server is ${server}, and abso sends your token only to ${[...TOKEN_SERVERS, "this machine"].join(", ")}, and the servers ~/.abso/servers.json or ABSO_TRUSTED_SERVERS name. ` + 192226 `If ${server} is yours, name it in ~/.abso/servers.json or ABSO_TRUSTED_SERVERS (comma-separated); if you didn't set it, someone else wrote .abso/config.json.`, Lines 247–276 at 318 (were 213–218 at 89)
213247 return trustedList(fromEnv(env, "TRUSTED_SERVERS"), readMachineServers(env)); 251+ * A pipeline run's key, from the file ABSO_KEY_FILE names (the runner writes it, mode 600, holding the bare key). 252+ * Read each time and kept in memory only; nothing abso writes or prints holds it, errors included. 254+export function runKey(env: Readonly<Record<string, string | undefined>> = process.env): string | undefined { 255+ const path = fromEnv(env, "KEY_FILE"); 256+ if (!path) return undefined; 259+ text = readFileSync(path, "utf8"); 261+ throw new UsageError(`ABSO_KEY_FILE names ${path}, which abso can't read`); 263+ const key = text.trim(); 264+ if (!/^abso_sk_[A-Za-z0-9_-]+$/.test(key)) 265+ throw new UsageError(`ABSO_KEY_FILE names ${path}, which doesn't hold an abso key`); 269+/** This machine's token file (~/.abso/token, else ~/.avcs/token), or none. Read only for a command given --token-file. */ 270+export function tokenFile(): string | undefined { 271+ return readToken({}); 216274/** ABSO_TOKEN if set, else ~/.abso/token, else ~/.avcs/token, else none. */ 217275export function readToken(env: Readonly<Record<string, string | undefined>> = process.env): string | undefined { 218276 const fromVar = fromEnv(env, "TOKEN"); Lines 284–300 at 318 (were 226–235 at 89)
227285export function writeConfig(root: string, settings: Settings): void { 228286 mkdirSync(stateDir(root), { recursive: true }); 229− const { server, project, agent, follow, reader, visitor } = settings;
287+ const { server, project, agent, follow, reader, visitor, store } = settings; 230288 const written = visitor 231289 ? { server, project, visitor: true } 232− : { server, project, agent, ...(follow ? { follow } : {}), ...(reader ? { reader: true } : {}) };
294+ ...(follow ? { follow } : {}), 295+ ...(reader ? { reader: true } : {}), 296+ ...(store ? { store } : {}), 233298 writeFileSync(configPath(root), `${JSON.stringify(written, null, 2)}\n`); 234299 excludeFromGit(root);