diff --git a/packages/cultnet-ts/src/idunn-runtime-authority.ts b/packages/cultnet-ts/src/idunn-runtime-authority.ts index 28e61e97..00f35c2f 100644 --- a/packages/cultnet-ts/src/idunn-runtime-authority.ts +++ b/packages/cultnet-ts/src/idunn-runtime-authority.ts @@ -588,13 +588,27 @@ function readAuthorityRecordBytes(bytes: Buffer, type: string, schemaId: string) } function inheritedDescriptorMap(): Map { - if (process.env.LISTEN_PID !== String(process.pid)) throw new Error("Inherited Idunn descriptors do not belong to this process."); + if (process.env.LISTEN_PID !== currentSystemdListenPid()) throw new Error("Inherited Idunn descriptors do not belong to this process."); const count = Number(process.env.LISTEN_FDS); const names = process.env.LISTEN_FDNAMES?.split(":") ?? []; if (!Number.isInteger(count) || count < 1 || names.length !== count) throw new Error("Inherited Idunn descriptor list is malformed."); return new Map(names.map((name, index) => [name, index + 3])); } +function currentSystemdListenPid(): string { + try { + const status = fs.readFileSync("/proc/self/status", "utf8"); + const namespacePids = /^NSpid:\s+([0-9]+)(?:\s|$)/mu.exec(status); + // systemd records LISTEN_PID in its PID namespace. In a PrivatePIDs + // service, Node's process.pid is namespace-local, while the first NSpid + // value is the same host PID systemd used for LISTEN_PID. + return namespacePids?.[1] ?? String(process.pid); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return String(process.pid); + throw error; + } +} + function readDescriptor(descriptor: number): Buffer { const fdPath = `/proc/self/fd/${descriptor}`; return fs.readFileSync(fdPath); diff --git a/packages/cultnet-ts/test/idunn-runtime-authority.test.ts b/packages/cultnet-ts/test/idunn-runtime-authority.test.ts index dd079c91..d1b4dcdb 100644 --- a/packages/cultnet-ts/test/idunn-runtime-authority.test.ts +++ b/packages/cultnet-ts/test/idunn-runtime-authority.test.ts @@ -86,11 +86,14 @@ test("opens Idunn's machine-bound identity and signs the exact Expected incarnat process.env.GAMECULT_IDUNN_RUNTIME_BUNDLE = bundle; process.env.GAMECULT_IDUNN_CANDIDATE_BIND = candidate; process.env.GAMECULT_IDUNN_PROCESS_WRITE_LEASE = path.join(directory, "lease.cc"); - process.env.LISTEN_PID = String(process.pid); + // systemd reports the host PID while Node inside PrivatePIDs sees PID 1. + const listenPid = String(process.pid + 1000); + process.env.LISTEN_PID = listenPid; process.env.LISTEN_FDS = "2"; process.env.LISTEN_FDNAMES = "gamecult-idunn-runtime-activation-key:gamecult-runtime-presence-identity"; const realReadFileSync = fs.readFileSync.bind(fs); mock.method(fs, "readFileSync", (filePath: fs.PathOrFileDescriptor, ...args: unknown[]) => { + if (filePath === "/proc/self/status") return `Name:\tnode\nNSpid:\t${listenPid}\t1\n`; if (filePath === "/proc/self/fd/3") return activationSeed; if (filePath === "/proc/self/fd/4") return providerCredential; if (filePath === "/etc/machine-id") return machineId;