fix(secrets): read the age identity once per process so <(...) keys survive --all (#36)

A process substitution (`CAST_AGE_KEY_FILE_<ENV>=<(pm read ...)`) is a
read-once pipe, but `diff --all` / `apply --all` call decryptSecrets once
per project. The first project drained the pipe; every later project
re-read the key file, handed age an empty identity, and failed — the
fleet loop then misreported the project as unreachable (diff) or aborted
the fleet (apply). Latent today because only one registered project has
a prod store; real the moment a second one gains one.

Cache the key bytes by key path, module-level, so the identity is read
exactly once per process. Exposure is unchanged: the key already
transits this process's memory on every call.

The regression test uses a FIFO, which really drains — unlike the
existing /proc/self/fd test, whose regular file re-opens at offset 0 on
every read. A second writer serves emptiness after the first decrypt so
a regression fails loudly (age: no secret keys found) instead of
blocking the suite on a writerless FIFO open.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
claude-hdb 2026-07-16 18:27:53 +00:00
parent 6e8ec34398
commit 8c396736c8
2 changed files with 52 additions and 2 deletions

View file

@ -9,12 +9,30 @@ import { join } from "node:path";
// holding the fd — this one. A freshly spawned age has no such fd and fails // holding the fd — this one. A freshly spawned age has no such fd and fails
// with ENOENT. Not `-i /dev/stdin` either: node closes the pipe before age // with ENOENT. Not `-i /dev/stdin` either: node closes the pipe before age
// re-opens it by path (ENXIO); `-` makes age read the inherited fd directly. // re-opens it by path (ENXIO); `-` makes age read the inherited fd directly.
//
// And it is read exactly ONCE per process: a process substitution is a
// read-once pipe, but `diff --all` / `apply --all` call decryptSecrets once
// per project. The first read drains the pipe; a re-read would hand age an
// empty identity and the second project's decrypt would fail. Caching by key
// path changes nothing about exposure — the key already transits this
// process's memory on every call.
const identities = new Map<string, Buffer>();
function readIdentity(keyFile: string): Buffer {
let identity = identities.get(keyFile);
if (identity === undefined) {
identity = readFileSync(keyFile);
identities.set(keyFile, identity);
}
return identity;
}
export function decryptSecrets( export function decryptSecrets(
file: string, file: string,
keyFile: string, keyFile: string,
): Record<string, string> { ): Record<string, string> {
const out = execFileSync("age", ["-d", "-i", "-", file], { const out = execFileSync("age", ["-d", "-i", "-", file], {
input: readFileSync(keyFile), input: readIdentity(keyFile),
encoding: "utf8", encoding: "utf8",
}); });
const secrets: Record<string, string> = {}; const secrets: Record<string, string> = {};

View file

@ -1,4 +1,4 @@
import { execFileSync } from "node:child_process"; import { execFileSync, spawn } from "node:child_process";
import { import {
closeSync, closeSync,
mkdirSync, mkdirSync,
@ -62,6 +62,38 @@ describe("secretsFileFor", () => {
}); });
}); });
// Pins the read-once shape of `<(pm read …)` that the fd-path test above
// cannot: a regular file behind /proc/self/fd re-opens at offset 0 on every
// read, but a pipe drains. `diff --all` / `apply --all` decrypt once per
// project, so the identity must be read once per process and reused.
describe("decryptSecrets identity caching", () => {
it("a read-once pipe key survives two decrypts — the --all loop shape", () => {
const { keyFile, enc } = ageFixture();
const dir = mkdtempSync(join(tmpdir(), "infra-fifo-"));
const fifo = join(dir, "key.fifo");
execFileSync("mkfifo", [fifo]);
// One writer, one serving of the key: exactly what a process substitution
// delivers. It pairs with the first decrypt's open and exits.
const once = spawn("sh", ["-c", `cat "${keyFile}" > "${fifo}"`], {
stdio: "ignore",
});
const expected = { MAILGUN_KEY: "mk-123", OPENROUTER_KEY: "or-456" };
expect(decryptSecrets(enc, fifo)).toEqual(expected);
// The pipe is now drained. A second writer serves nothing, so if the
// per-process cache ever regresses, the re-read hands age an empty
// identity and fails loudly instead of blocking the suite on a
// writerless FIFO open. With the cache, nobody opens the FIFO again and
// the writer is still blocked in open() when we kill it.
const drained = spawn("sh", ["-c", `: > "${fifo}"`], { stdio: "ignore" });
try {
expect(decryptSecrets(enc, fifo)).toEqual(expected);
} finally {
once.kill("SIGKILL");
drained.kill("SIGKILL");
}
});
});
describe("keyFileFor", () => { describe("keyFileFor", () => {
it("an env with no injected var and no standing key refuses, naming both ways in", () => { it("an env with no injected var and no standing key refuses, naming both ways in", () => {
Reflect.deleteProperty(process.env, "CAST_AGE_KEY_FILE_PROD"); Reflect.deleteProperty(process.env, "CAST_AGE_KEY_FILE_PROD");