@foldrun/core 15.0.0 → 16.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/a2a-publish.d.ts +7 -0
- package/dist/src/a2a-publish.js +15 -1
- package/dist/src/a2a-reach.d.ts +156 -0
- package/dist/src/a2a-reach.js +446 -0
- package/dist/src/browse-egress.d.ts +1 -1
- package/dist/src/browse-egress.js +7 -6
- package/dist/src/capture.js +2 -1
- package/dist/src/env-names.d.ts +12 -0
- package/dist/src/env-names.js +49 -0
- package/dist/src/failure.d.ts +92 -0
- package/dist/src/failure.js +294 -0
- package/dist/src/frontmatter-fields.js +1 -1
- package/dist/src/install-helper.js +10 -5
- package/dist/src/mail.js +2 -1
- package/dist/src/network.js +1 -1
- package/dist/src/notify.js +12 -7
- package/dist/src/ops-log.d.ts +55 -0
- package/dist/src/ops-log.js +393 -0
- package/dist/src/otel.d.ts +16 -1
- package/dist/src/otel.js +121 -5
- package/dist/src/run-container.d.ts +55 -3
- package/dist/src/run-container.js +109 -17
- package/dist/src/run-event.d.ts +179 -0
- package/dist/src/run-event.js +473 -0
- package/dist/src/runner.js +14 -5
- package/dist/src/runtime.d.ts +113 -16
- package/dist/src/runtime.js +528 -44
- package/dist/src/scan.js +80 -18
- package/dist/src/secrets.js +4 -3
- package/dist/src/starter.js +1 -1
- package/dist/src/step-exec.d.ts +4 -2
- package/dist/src/step-exec.js +10 -4
- package/dist/src/storage.js +2 -1
- package/dist/src/store.d.ts +25 -3
- package/dist/src/store.js +40 -10
- package/dist/src/triggers.js +6 -5
- package/dist/src/webhook.js +3 -2
- package/package.json +2 -1
- package/src/a2a-publish.ts +19 -1
- package/src/a2a-reach.ts +503 -0
- package/src/browse-egress.ts +8 -7
- package/src/capture.ts +2 -1
- package/src/env-names.ts +53 -0
- package/src/failure.ts +368 -0
- package/src/frontmatter-fields.ts +1 -1
- package/src/install-helper.ts +10 -5
- package/src/mail.ts +2 -1
- package/src/network.ts +1 -1
- package/src/notify.ts +12 -9
- package/src/ops-log.ts +403 -0
- package/src/otel.ts +122 -7
- package/src/run-container.ts +112 -21
- package/src/run-event.ts +563 -0
- package/src/runner.ts +14 -5
- package/src/runtime.ts +543 -45
- package/src/scan.ts +91 -20
- package/src/secrets.ts +4 -5
- package/src/starter.ts +1 -1
- package/src/step-exec.ts +10 -4
- package/src/storage.ts +2 -1
- package/src/store.ts +59 -11
- package/src/triggers.ts +6 -5
- package/src/webhook.ts +3 -2
package/src/runtime.ts
CHANGED
|
@@ -25,6 +25,7 @@ import fs from "node:fs";
|
|
|
25
25
|
import os from "node:os";
|
|
26
26
|
import path from "node:path";
|
|
27
27
|
import { dataRoot } from "./paths.ts";
|
|
28
|
+
import { LOADER_ENV_CASE } from "./env-names.ts";
|
|
28
29
|
import crypto from "node:crypto";
|
|
29
30
|
import { spawnSync } from "node:child_process";
|
|
30
31
|
|
|
@@ -645,67 +646,549 @@ export const SYSTEM_FROM_INPUT_SH = `FOLDRUN_SYSTEM_PACKAGES="$(node -e 'try{con
|
|
|
645
646
|
export const SYSTEM_STATUS_FILE = "/opt/runner/job/system-install.json";
|
|
646
647
|
export const SYSTEM_LOG_FILE = "/opt/runner/job/system-install.log";
|
|
647
648
|
|
|
649
|
+
/** Where the worker leaves the account's snapshot key for the root start-up
|
|
650
|
+
* (run-container.ts rootStartSh), which moves it somewhere only root can
|
|
651
|
+
* read and deletes this copy before any agent code runs. */
|
|
652
|
+
export const SNAPSHOT_KEY_FILE = "/opt/runner/job/snapshot.key";
|
|
653
|
+
|
|
654
|
+
/** The paths, relative to the root, a snapshot may write under: where Debian
|
|
655
|
+
* packages install. `var` reaches /var/run only through a link to /run, and
|
|
656
|
+
* /var/tmp is everyone's — the verifier refuses both on what is on disk. */
|
|
657
|
+
export const SNAPSHOT_ALLOWED = "etc usr var bin sbin lib lib32 lib64 libx32 opt";
|
|
658
|
+
|
|
659
|
+
/** The step's environment as the worker stages it in a run container: one
|
|
660
|
+
* `export NAME='value'` per line (stepEnvShell). Only the agent reads it —
|
|
661
|
+
* the drop's own shell, after runuser (agentStartSh, FOL-130). */
|
|
662
|
+
export const STEP_ENV_FILE = "/opt/runner/job/env.sh";
|
|
663
|
+
|
|
664
|
+
/** The part of the step's environment root's start-up may see: the proxy
|
|
665
|
+
* apt downloads through, and nothing else. The worker writes it beside
|
|
666
|
+
* STEP_ENV_FILE (rootEnvShell); rootStartSh's subshell reads it. */
|
|
667
|
+
export const ROOT_ENV_FILE = "/opt/runner/job/root-env.sh";
|
|
668
|
+
|
|
669
|
+
/** The names root's start-up takes from the step's environment (FOL-130). */
|
|
670
|
+
export const ROOT_ENV_NAMES: readonly string[] = ["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy", "NO_PROXY", "no_proxy"];
|
|
671
|
+
|
|
672
|
+
/** The environment variables root's start-up keeps (the proxy apt
|
|
673
|
+
* downloads through); every other exported name is unset. */
|
|
674
|
+
const ROOT_ENV_KEEP = [...ROOT_ENV_NAMES, "FOLDRUN_INSTALL_SOCKET"].join("|");
|
|
675
|
+
|
|
676
|
+
/** An environment as a file a POSIX shell sources: `export NAME='value'`,
|
|
677
|
+
* single-quoted, so sourcing it assigns and never runs anything. Names a
|
|
678
|
+
* shell cannot hold are left out. The same shape as the k8s worker's
|
|
679
|
+
* envFileShell. */
|
|
680
|
+
export function stepEnvShell(env: Record<string, string>): string {
|
|
681
|
+
return Object.entries(env)
|
|
682
|
+
.filter(([k, v]) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k) && typeof v === "string")
|
|
683
|
+
.map(([k, v]) => `export ${k}='${v.replaceAll("'", `'\\''`)}'`)
|
|
684
|
+
.join("\n");
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/** ROOT_ENV_FILE's content: the step's environment cut to ROOT_ENV_NAMES. */
|
|
688
|
+
export function rootEnvShell(env: Record<string, string>): string {
|
|
689
|
+
return stepEnvShell(Object.fromEntries(Object.entries(env).filter(([k]) => ROOT_ENV_NAMES.includes(k))));
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** IFS made a shell's own again, then every exported name `arms` (a shell
|
|
693
|
+
* `case` body for "$_fn") picks unset — read from `export -p` with builtins
|
|
694
|
+
* only, so nothing is executed while the environment is still the one being
|
|
695
|
+
* cleaned. */
|
|
696
|
+
const exportedNamesSh = (arms: string) => String.raw`IFS=$(printf ' \t\nx'); IFS=${"$"}{IFS%x}
|
|
697
|
+
_fx=$(export -p)
|
|
698
|
+
while read -r _fl; do
|
|
699
|
+
case "$_fl" in "export "*|"declare -x "*) ;; *) continue;; esac
|
|
700
|
+
_fn=${"$"}{_fl#export }; _fn=${"$"}{_fn#declare -x }; _fn=${"$"}{_fn%%=*}
|
|
701
|
+
case "$_fn" in ${arms} esac
|
|
702
|
+
done <<FOLDRUN_ENV
|
|
703
|
+
$_fx
|
|
704
|
+
FOLDRUN_ENV
|
|
705
|
+
unset _fx _fl _fn`;
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* The first lines of root's start-up section (rootStartSh), run in a
|
|
709
|
+
* subshell: the step's environment is the account's — its secrets by
|
|
710
|
+
* whatever name the account chose, LD_PRELOAD and NODE_OPTIONS among the
|
|
711
|
+
* names it could choose — and none of it may steer what root runs. PATH and
|
|
712
|
+
* IFS are assigned before anything is executed, then `export -p` is read
|
|
713
|
+
* with builtins only and every exported name but the proxy's is unset. The
|
|
714
|
+
* test-only FOLDRUN_SYS_* / FOLDRUN_DPKG_DIR overrides go with the rest.
|
|
715
|
+
* Since FOL-130 the step's environment is not there to begin with — the
|
|
716
|
+
* subshell starts from the root shell's own and ROOT_ENV_FILE's proxy
|
|
717
|
+
* lines — and this stays as the lock behind that.
|
|
718
|
+
*/
|
|
719
|
+
export const ROOT_ENV_RESET_SH = `PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
720
|
+
${exportedNamesSh(`${ROOT_ENV_KEEP}|PATH) ;; ""|*[!A-Za-z0-9_]*) ;; *) unset "$_fn" 2>/dev/null;;`)}
|
|
721
|
+
HOME=/root; export PATH HOME`;
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* The first lines of a run container's root shell (the k8s SHIM, the docker
|
|
725
|
+
* ENTRY), FOL-130: the loader's and the shells' variables (env-names.ts
|
|
726
|
+
* LOADER_ENV — LD_*, BASH_ENV, ENV, NODE_OPTIONS, PYTHONPATH…) unset and IFS
|
|
727
|
+
* made the shell's own. That shell's environment is the image's and the pod
|
|
728
|
+
* spec's, never the step's — the step's reaches the agent alone, after the
|
|
729
|
+
* drop (agentStartSh) — so this is the second lock: nothing the platform
|
|
730
|
+
* itself might one day set there can steer chown, runuser or date as root.
|
|
731
|
+
* PATH and NODE_PATH stay the image's: the agent's programs are found by them.
|
|
732
|
+
*/
|
|
733
|
+
export const ROOT_SHELL_GUARD_SH = exportedNamesSh(`${LOADER_ENV_CASE}) unset "$_fn" 2>/dev/null;; *) ;;`);
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* The snapshot verifier and sealer, a CommonJS script the snippet writes to
|
|
737
|
+
* its root-private directory and runs with node (FOL-129).
|
|
738
|
+
*
|
|
739
|
+
* node v.cjs verify <keyfile> <key> <base> <root> <allowed> <agent uid> <out> <seal> < tar
|
|
740
|
+
* node v.cjs seal <keyfile> <key> <base> <root> <allowed> <agent uid> <tar>
|
|
741
|
+
*
|
|
742
|
+
* verify reads the tar once from stdin — hashing it, copying it to <out> and
|
|
743
|
+
* reading its headers — and says `ok` only when the seal is this account's
|
|
744
|
+
* (HMAC over the key, the base, the time it was built and the tar's sha256),
|
|
745
|
+
* it was built in the last 14 days, and every member passes the audit below.
|
|
746
|
+
* seal audits a tar root has just built and prints its seal line. Exit 3 is a
|
|
747
|
+
* refusal, its reason on stdout; exit 4 is a snapshot past its 14 days.
|
|
748
|
+
*
|
|
749
|
+
* The audit is defence in depth under the seal, so that even sealed bytes can
|
|
750
|
+
* only put package files where packages go: names relative, no `..`, under
|
|
751
|
+
* an allowed root; regular files, directories, symlinks, and hard links to an
|
|
752
|
+
* earlier file of the same archive, nothing else; no setuid or setgid; nothing
|
|
753
|
+
* owned by the agent user; no name twice; nothing under one of the archive's
|
|
754
|
+
* own symlinks unless the disk already has that very link; and on disk, every
|
|
755
|
+
* existing directory a member lands under resolves inside an allowed root and
|
|
756
|
+
* is root's alone (owned by root, not group- or world-writable — a sticky
|
|
757
|
+
* root directory only above an existing entry), so no entry the agent made
|
|
758
|
+
* can sit on a member's path, now or while the extraction runs.
|
|
759
|
+
*
|
|
760
|
+
* Written for a quoted heredoc inside a JS template: no backticks, no dollar
|
|
761
|
+
* brace.
|
|
762
|
+
*/
|
|
763
|
+
const SNAPSHOT_JS = String.raw`"use strict";
|
|
764
|
+
const fs = require("node:fs"), crypto = require("node:crypto"), path = require("node:path");
|
|
765
|
+
const [mode, keyFile, KEY, BASE, root, allowArg, agentArg, a8, a9] = process.argv.slice(2);
|
|
766
|
+
const MAX = 6 * 1024 * 1024 * 1024, DAY = 86400;
|
|
767
|
+
const short = (n) => (String(n).length > 90 ? String(n).slice(0, 30) + "[..]" + String(n).slice(-50) : String(n));
|
|
768
|
+
const say = (s) => String(s).replace(/[^A-Za-z0-9 ._:\/+=,-]/g, "?").slice(0, 240);
|
|
769
|
+
const refuse = (why, code) => { process.stdout.write((code === 4 ? "expired: " : "refused: ") + say(why) + "\n"); process.exit(code || 3); };
|
|
770
|
+
let key = "";
|
|
771
|
+
try { key = fs.readFileSync(keyFile, "utf8").trim(); } catch (e) { key = ""; }
|
|
772
|
+
if (!/^[0-9a-f]{64}$/.test(key)) refuse("no snapshot key");
|
|
773
|
+
const macOf = (built, sha) => crypto.createHmac("sha256", Buffer.from(key, "hex")).update(["v5", KEY, BASE, built, sha].join("\n")).digest("hex");
|
|
774
|
+
const agent = /^[0-9]+$/.test(agentArg || "") ? Number(agentArg) : -1;
|
|
775
|
+
const euid = process.geteuid ? process.geteuid() : 0;
|
|
776
|
+
const rootReal = fs.realpathSync(root);
|
|
777
|
+
const allow = (allowArg || "").split(/\s+/).map((a) => a.replace(/^\/+|\/+$/g, "")).filter(Boolean);
|
|
778
|
+
const under = (p, base) => p === base || p.startsWith(base === "/" ? "/" : base + "/");
|
|
779
|
+
const allowReal = allow.map((a) => { const p = path.join(rootReal, a); try { return fs.realpathSync(p); } catch (e) { return p; } });
|
|
780
|
+
const allowedName = (n) => allow.some((a) => n === a || n.startsWith(a + "/"));
|
|
781
|
+
|
|
782
|
+
function norm(n, what) {
|
|
783
|
+
if (n.indexOf("\0") >= 0) return { bad: what + " has a NUL in its name" };
|
|
784
|
+
if (n.startsWith("/")) return { bad: what + " " + short(n) + " is an absolute path" };
|
|
785
|
+
const parts = n.split("/").filter((p) => p !== "" && p !== ".");
|
|
786
|
+
if (!parts.length) return { bad: what + " has an empty name" };
|
|
787
|
+
if (parts.indexOf("..") >= 0) return { bad: what + " " + short(n) + " climbs out with .." };
|
|
788
|
+
return { name: parts.join("/") };
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// A tar read as a stream: ustar (its prefix only under the POSIX magic), GNU
|
|
792
|
+
// long names and links (L, K), pax per-file headers (x). A problem stops the
|
|
793
|
+
// reading but not the hashing: the seal is judged first, then the members.
|
|
794
|
+
const members = [];
|
|
795
|
+
let problem = null, ended = false, pend = Buffer.alloc(0), skip = 0, ext = null, longName = null, longLink = null, pax = null;
|
|
796
|
+
function num(h, a, b) {
|
|
797
|
+
if (h[a] & 0x80) { if (h[a] & 0x40) return NaN; let v = h[a] & 0x3f; for (let i = a + 1; i < b; i++) v = v * 256 + h[i]; return v; }
|
|
798
|
+
const s = str(h, a, b).trim();
|
|
799
|
+
return /^[0-7]*$/.test(s) ? (s ? parseInt(s, 8) : 0) : NaN;
|
|
800
|
+
}
|
|
801
|
+
function str(h, a, b) { const s = h.subarray(a, b); const z = s.indexOf(0); return (z < 0 ? s : s.subarray(0, z)).toString("utf8"); }
|
|
802
|
+
function header(h) {
|
|
803
|
+
if (h.every((b) => b === 0)) { ended = true; return; }
|
|
804
|
+
let sum = 0;
|
|
805
|
+
for (let i = 0; i < 512; i++) sum += i >= 148 && i < 156 ? 32 : h[i];
|
|
806
|
+
if (num(h, 148, 156) !== sum) { problem = "a header checksum is wrong"; return; }
|
|
807
|
+
const magic = h.subarray(257, 263).toString("latin1");
|
|
808
|
+
let name = str(h, 0, 100);
|
|
809
|
+
if (magic === "ustar\0" && str(h, 345, 500)) name = str(h, 345, 500) + "/" + name;
|
|
810
|
+
const type = h[156] === 0 ? "0" : String.fromCharCode(h[156]);
|
|
811
|
+
let size = num(h, 124, 136), uid = num(h, 108, 116), link = str(h, 157, 257), uname = str(h, 265, 297);
|
|
812
|
+
const m = num(h, 100, 108);
|
|
813
|
+
if ([size, uid, m].some((v) => !Number.isFinite(v))) { problem = "a header holds a number tar cannot read"; return; }
|
|
814
|
+
if (type === "L" || type === "K" || type === "x") {
|
|
815
|
+
if (size > 1048576) { problem = "an extended header is too large"; return; }
|
|
816
|
+
ext = { type, size, parts: [] }; skip = Math.ceil(size / 512) * 512;
|
|
817
|
+
if (!skip) finishExt();
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (pax) { if (pax.path !== undefined) name = pax.path; if (pax.linkpath !== undefined) link = pax.linkpath; if (pax.size !== undefined) size = pax.size; if (pax.uid !== undefined) uid = pax.uid; if (pax.uname !== undefined) uname = pax.uname; }
|
|
821
|
+
if (longName !== null) name = longName;
|
|
822
|
+
if (longLink !== null) link = longLink;
|
|
823
|
+
longName = longLink = pax = null;
|
|
824
|
+
if (!"0125".includes(type)) { problem = "member " + short(name) + " is of a kind a snapshot never holds (type " + type + ")"; return; }
|
|
825
|
+
const n = norm(name, "member");
|
|
826
|
+
if (n.bad) { problem = n.bad; return; }
|
|
827
|
+
members.push({ name: n.name, type, mode: m, uid, uname, link });
|
|
828
|
+
skip = Math.ceil(size / 512) * 512;
|
|
829
|
+
}
|
|
830
|
+
function finishExt() {
|
|
831
|
+
const data = Buffer.concat(ext.parts).subarray(0, ext.size);
|
|
832
|
+
const t = ext.type; ext = null;
|
|
833
|
+
if (t === "L") longName = str(data, 0, data.length);
|
|
834
|
+
else if (t === "K") longLink = str(data, 0, data.length);
|
|
835
|
+
else {
|
|
836
|
+
pax = {};
|
|
837
|
+
let i = 0;
|
|
838
|
+
while (i < data.length) {
|
|
839
|
+
const sp = data.indexOf(32, i);
|
|
840
|
+
const len = sp < 0 ? NaN : parseInt(data.subarray(i, sp).toString("latin1"), 10);
|
|
841
|
+
if (!(len > 0) || i + len > data.length) { problem = "a pax header cannot be read"; return; }
|
|
842
|
+
const rec = data.subarray(sp + 1, i + len - 1).toString("utf8");
|
|
843
|
+
const eq = rec.indexOf("=");
|
|
844
|
+
const k = rec.slice(0, eq), v = rec.slice(eq + 1);
|
|
845
|
+
if (k.startsWith("GNU.sparse")) { problem = "a sparse member"; return; }
|
|
846
|
+
if (k === "path") pax.path = v;
|
|
847
|
+
else if (k === "linkpath") pax.linkpath = v;
|
|
848
|
+
else if (k === "uname") pax.uname = v;
|
|
849
|
+
else if (k === "size" || k === "uid") { if (!/^[0-9]+$/.test(v)) { problem = "a pax " + k + " is not a number"; return; } pax[k] = Number(v); }
|
|
850
|
+
i += len;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
function feed(chunk) {
|
|
855
|
+
let off = 0;
|
|
856
|
+
while (off < chunk.length && !ended && !problem) {
|
|
857
|
+
if (skip > 0) {
|
|
858
|
+
const n = Math.min(skip, chunk.length - off);
|
|
859
|
+
if (ext) ext.parts.push(chunk.subarray(off, off + n));
|
|
860
|
+
skip -= n; off += n;
|
|
861
|
+
if (!skip && ext) finishExt();
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
const take = chunk.subarray(off, off + 512 - pend.length);
|
|
865
|
+
pend = pend.length ? Buffer.concat([pend, take]) : take;
|
|
866
|
+
off += take.length;
|
|
867
|
+
if (pend.length === 512) { const h = Buffer.from(pend); pend = Buffer.alloc(0); header(h); }
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// The disk a member lands on, walked one component at a time from / the way
|
|
872
|
+
// the kernel will walk it when tar writes: every directory passed through,
|
|
873
|
+
// and every link followed (from the directory that holds it), must be root's
|
|
874
|
+
// alone, so nothing on the way can be swapped after this check, even while
|
|
875
|
+
// the agent runs (the install helper restores beside it). A directory anyone
|
|
876
|
+
// may write is passed only when it is root's and sticky and the entry taken
|
|
877
|
+
// from it is root's; nothing is created in one. The directory the member
|
|
878
|
+
// lands in must be under the root and under an allowed path.
|
|
879
|
+
const walked = new Map();
|
|
880
|
+
function onDisk(name) {
|
|
881
|
+
const parent = path.posix.dirname(name);
|
|
882
|
+
if (walked.has(parent)) { const w = walked.get(parent); return w && w.replace("@", short(name)); }
|
|
883
|
+
const why = walk(parent === "." ? [] : parent.split("/"));
|
|
884
|
+
walked.set(parent, why);
|
|
885
|
+
return why && why.replace("@", short(name));
|
|
886
|
+
}
|
|
887
|
+
function walk(parts) {
|
|
888
|
+
const okDir = (st) => (st.uid === 0 || st.uid === euid) && ((st.mode & 0o022) === 0 || ((st.mode & 0o1000) !== 0 && st.uid === 0));
|
|
889
|
+
const open = (st) => (st.mode & 0o022) !== 0;
|
|
890
|
+
let cur = "/", st;
|
|
891
|
+
try { st = fs.lstatSync("/"); } catch (e) { return "member @ has no / to land on"; }
|
|
892
|
+
if (!okDir(st)) return "member @ lies under /, which others can write";
|
|
893
|
+
let queue = (rootReal === "/" ? [] : rootReal.slice(1).split("/")).concat(parts);
|
|
894
|
+
for (let hops = 0; queue.length; ) {
|
|
895
|
+
const c = queue.shift();
|
|
896
|
+
if (c === "" || c === ".") continue;
|
|
897
|
+
if (c === "..") {
|
|
898
|
+
cur = path.dirname(cur);
|
|
899
|
+
try { st = fs.lstatSync(cur); } catch (e) { return "member @ lies under " + short(cur) + ", which cannot be read"; }
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
const next = cur === "/" ? "/" + c : cur + "/" + c;
|
|
903
|
+
let lst;
|
|
904
|
+
try { lst = fs.lstatSync(next); } catch (e) {
|
|
905
|
+
// Missing from here down: tar makes it, inside cur.
|
|
906
|
+
if (open(st)) return "member @ would be made in " + short(cur) + ", which others can write";
|
|
907
|
+
queue = [];
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
if (open(st) && !(lst.uid === 0 || lst.uid === euid)) return "member @ lies under " + short(next) + ", which others could replace";
|
|
911
|
+
if (lst.isSymbolicLink()) {
|
|
912
|
+
if (++hops > 40) return "member @ lies under too many links";
|
|
913
|
+
try { fs.statSync(next); } catch (e) { return "member @ lies under the broken link " + short(next); }
|
|
914
|
+
const t = fs.readlinkSync(next);
|
|
915
|
+
if (t.startsWith("/")) { cur = "/"; st = fs.lstatSync("/"); }
|
|
916
|
+
queue = t.split("/").concat(queue);
|
|
917
|
+
continue;
|
|
918
|
+
}
|
|
919
|
+
if (!lst.isDirectory()) return "member @ lies under " + short(next) + ", which is not a directory";
|
|
920
|
+
if (!okDir(lst)) return "member @ lies under " + short(next) + ", which others can write";
|
|
921
|
+
cur = next; st = lst;
|
|
922
|
+
}
|
|
923
|
+
if (!queue.length && open(st)) return "member @ would be made in " + short(cur) + ", which others can write";
|
|
924
|
+
if (!under(cur, rootReal)) return "member @ leads out of the root, to " + short(cur);
|
|
925
|
+
if (parts.length && !allowReal.some((a) => under(cur, a))) return "member @ leads through a link to " + short(cur) + ", where packages do not install";
|
|
926
|
+
return null;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
function audit() {
|
|
930
|
+
const seen = new Map(), links = new Map();
|
|
931
|
+
for (const m of members) {
|
|
932
|
+
if (seen.has(m.name)) return "member " + short(m.name) + " appears twice";
|
|
933
|
+
if (!allowedName(m.name)) return "member " + short(m.name) + " is outside the paths packages install to";
|
|
934
|
+
if (allow.indexOf(m.name) >= 0 && m.type !== "5") {
|
|
935
|
+
let same = false;
|
|
936
|
+
try { same = m.type === "2" && fs.readlinkSync(path.join(rootReal, m.name)) === m.link; } catch (e) { same = false; }
|
|
937
|
+
if (!same) return "member " + short(m.name) + " would replace a top-level system directory";
|
|
938
|
+
}
|
|
939
|
+
if (m.mode & 0o6000) return "member " + short(m.name) + " is setuid or setgid";
|
|
940
|
+
if ((agent >= 0 && m.uid === agent) || m.uname === "agent") return "member " + short(m.name) + " belongs to the agent user";
|
|
941
|
+
if (m.type === "1") {
|
|
942
|
+
const t = norm(m.link, "hard link " + m.name);
|
|
943
|
+
if (t.bad) return t.bad;
|
|
944
|
+
const target = seen.get(t.name);
|
|
945
|
+
if (!target || target.type !== "0") return "hard link " + short(m.name) + " points at " + short(t.name) + ", not at a file earlier in the snapshot";
|
|
946
|
+
}
|
|
947
|
+
if (m.type === "2") links.set(m.name, m.link);
|
|
948
|
+
seen.set(m.name, m);
|
|
949
|
+
}
|
|
950
|
+
for (const m of members) {
|
|
951
|
+
const parts = m.name.split("/");
|
|
952
|
+
for (let i = 1; i < parts.length; i++) {
|
|
953
|
+
const p = parts.slice(0, i).join("/");
|
|
954
|
+
const up = seen.get(p);
|
|
955
|
+
if (!up) continue;
|
|
956
|
+
if (up.type === "2") {
|
|
957
|
+
let same = false;
|
|
958
|
+
try { same = fs.readlinkSync(path.join(rootReal, p)) === up.link; } catch (e) { same = false; }
|
|
959
|
+
if (!same) return "member " + short(m.name) + " lies under a link in the snapshot itself, " + short(p);
|
|
960
|
+
} else if (up.type !== "5") return "member " + short(m.name) + " lies under the file " + short(p);
|
|
961
|
+
}
|
|
962
|
+
const why = onDisk(m.name);
|
|
963
|
+
if (why) return why;
|
|
964
|
+
}
|
|
965
|
+
return null;
|
|
966
|
+
}
|
|
967
|
+
function finish(sha) {
|
|
968
|
+
if (!problem && !ended) problem = "the archive ends before its end marker";
|
|
969
|
+
return problem || audit();
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
if (mode === "verify") {
|
|
973
|
+
const seal = /^v5 ([0-9]{1,12}) ([0-9a-f]{64}) ([0-9a-f]{64})$/.exec((a9 || "").trim());
|
|
974
|
+
if (!seal) refuse("it carries no seal");
|
|
975
|
+
const built = Number(seal[1]), now = Math.floor(Date.now() / 1000);
|
|
976
|
+
if (built > now + 300) refuse("its seal is dated in the future");
|
|
977
|
+
if (now - built > 14 * DAY) refuse("built " + Math.floor((now - built) / DAY) + " days ago", 4);
|
|
978
|
+
const out = fs.openSync(a8, "wx", 0o600);
|
|
979
|
+
const hash = crypto.createHash("sha256");
|
|
980
|
+
let total = 0;
|
|
981
|
+
process.stdin.on("data", (chunk) => {
|
|
982
|
+
total += chunk.length;
|
|
983
|
+
if (total > MAX) refuse("it is larger than 6 GiB");
|
|
984
|
+
hash.update(chunk); fs.writeSync(out, chunk); feed(chunk);
|
|
985
|
+
});
|
|
986
|
+
process.stdin.on("end", () => {
|
|
987
|
+
fs.closeSync(out);
|
|
988
|
+
const sha = hash.digest("hex");
|
|
989
|
+
if (sha !== seal[2]) refuse("its contents do not match its seal");
|
|
990
|
+
const want = Buffer.from(macOf(seal[1], sha), "hex"), got = Buffer.from(seal[3], "hex");
|
|
991
|
+
if (!crypto.timingSafeEqual(want, got)) refuse("its seal was not made with the account key");
|
|
992
|
+
const why = finish(sha);
|
|
993
|
+
if (why) refuse(why);
|
|
994
|
+
process.stdout.write("ok\n");
|
|
995
|
+
});
|
|
996
|
+
} else if (mode === "seal") {
|
|
997
|
+
const fd = fs.openSync(a8, "r");
|
|
998
|
+
const hash = crypto.createHash("sha256");
|
|
999
|
+
const buf = Buffer.alloc(1 << 20);
|
|
1000
|
+
for (let n; (n = fs.readSync(fd, buf, 0, buf.length, null)) > 0; ) { const c = buf.subarray(0, n); hash.update(c); feed(c); }
|
|
1001
|
+
fs.closeSync(fd);
|
|
1002
|
+
const sha = hash.digest("hex");
|
|
1003
|
+
const why = finish(sha);
|
|
1004
|
+
if (why) refuse(why);
|
|
1005
|
+
const built = String(Math.floor(Date.now() / 1000));
|
|
1006
|
+
process.stdout.write("v5 " + built + " " + sha + " " + macOf(built, sha) + "\n");
|
|
1007
|
+
} else refuse("unknown mode");
|
|
1008
|
+
`;
|
|
1009
|
+
|
|
1010
|
+
/** What the agent user runs to publish a snapshot root built and sealed:
|
|
1011
|
+
* sh -c PUBLISH sh <key dir> <generation> <seal> <base>, the tar on stdin.
|
|
1012
|
+
* As the agent, so a link it planted in the cache leads only where it could
|
|
1013
|
+
* write itself (FOL-129); the -L checks are for the same-user tests. */
|
|
1014
|
+
const PUBLISH_SH = String.raw`cd / || exit 0; K=$1; G=$K/$2
|
|
1015
|
+
[ -L "$(dirname "$K")" ] && exit 0
|
|
1016
|
+
mkdir -p "$K" 2>/dev/null || exit 0
|
|
1017
|
+
[ -L "$K" ] && exit 0
|
|
1018
|
+
T=$(mktemp -d "$K/.stage.XXXXXX" 2>/dev/null) || exit 0
|
|
1019
|
+
if cat > "$T/snap.tar" && printf "%s\n" "$3" > "$T/seal" && printf "%s\n" "$4" > "$T/base"; then
|
|
1020
|
+
[ -e "$G" ] && mv "$G" "$K/.gone.$(basename "$T")" 2>/dev/null
|
|
1021
|
+
[ -e "$G" ] || mv "$T" "$G" 2>/dev/null
|
|
1022
|
+
fi
|
|
1023
|
+
rm -rf "$T" "$K/.gone.$(basename "$T")" 2>/dev/null
|
|
1024
|
+
ls -t "$K" 2>/dev/null | tail -n +5 | while read -r o; do rm -rf "$K/$o"; done`;
|
|
1025
|
+
|
|
1026
|
+
/** What the agent user runs to keep the snapshot folder tidy:
|
|
1027
|
+
* sh -c PRUNE sh <snapshots> <cache>. Generations past their 14 days,
|
|
1028
|
+
* stages a killed pod left, keys left empty, and the v3/v4 layout under
|
|
1029
|
+
* `.apt` (its .deb cache too), which nothing reads any more. Where `.apt`
|
|
1030
|
+
* was made by root (a docker volume) the agent cannot remove it and it
|
|
1031
|
+
* stays; root does not delete in the agent's folder. */
|
|
1032
|
+
const PRUNE_SH = String.raw`cd / || exit 0; S=$1
|
|
1033
|
+
rm -rf "$2/.apt" 2>/dev/null
|
|
1034
|
+
[ -d "$S" ] && [ ! -L "$S" ] || exit 0
|
|
1035
|
+
find "$S" -mindepth 2 -maxdepth 2 -type d ! -name ".*" -mtime +14 -exec rm -rf {} + 2>/dev/null
|
|
1036
|
+
find "$S" -mindepth 2 -maxdepth 2 -type d -name ".*" -mmin +120 -exec rm -rf {} + 2>/dev/null
|
|
1037
|
+
find "$S" -mindepth 1 -maxdepth 1 -type d -empty -mmin +120 -delete 2>/dev/null
|
|
1038
|
+
exit 0`;
|
|
1039
|
+
|
|
648
1040
|
/**
|
|
649
1041
|
* The root start-up's half of `runtime: system:` — one shell snippet, used by
|
|
650
|
-
* the k8s run pod, the docker entry
|
|
651
|
-
* Reads the package names from $FOLDRUN_SYSTEM_PACKAGES
|
|
652
|
-
* each re-checked against DEB_PACKAGE's shape here too, so
|
|
653
|
-
* ride in), installs them with apt as root BEFORE the drop to
|
|
654
|
-
* and writes SYSTEM_STATUS_FILE for prepareRuntime to read.
|
|
1042
|
+
* the k8s run pod, the docker entry, the deploy-time warm pod and the install
|
|
1043
|
+
* helper alike. Reads the package names from $FOLDRUN_SYSTEM_PACKAGES
|
|
1044
|
+
* (space-separated, each re-checked against DEB_PACKAGE's shape here too, so
|
|
1045
|
+
* nothing else can ride in), installs them with apt as root BEFORE the drop to
|
|
1046
|
+
* the agent user, and writes SYSTEM_STATUS_FILE for prepareRuntime to read.
|
|
655
1047
|
*
|
|
656
|
-
* The first step to need a package set installs it with apt
|
|
657
|
-
*
|
|
658
|
-
* with their dpkg records, as a snapshot: `<cache>/.apt/snapshots/<key>.tar`.
|
|
1048
|
+
* The first step to need a package set installs it with apt and then saves
|
|
1049
|
+
* the files those packages added, with their dpkg records, as a snapshot.
|
|
659
1050
|
* Every later step restores the snapshot instead — measured 11 Oct 2026 in a
|
|
660
1051
|
* gVisor run pod on the NFS cache: build-essential, pkg-config, python3-dev,
|
|
661
1052
|
* libcairo2-dev and libpango1.0-dev (154 packages, 432 MB) took 23-30 s
|
|
662
1053
|
* through apt and 0.76 s from the snapshot, gcc and pkg-config working after.
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
1054
|
+
* Package lists stay per pod: two pods updating one lists directory on a
|
|
1055
|
+
* shared volume would corrupt it.
|
|
1056
|
+
*
|
|
1057
|
+
* Who can write the snapshots (FOL-129, 5 Nov 2026). They live in the
|
|
1058
|
+
* account's runtime cache, which the agent user owns and writes — and on EFS
|
|
1059
|
+
* the access point makes every write uid 501 whoever asks, so no ownership
|
|
1060
|
+
* can keep the agent out. Root extracts a snapshot onto `/`. Until v5 that
|
|
1061
|
+
* meant an agent that rewrote snap.tar had its own files written as root
|
|
1062
|
+
* anywhere at the next step's start (an /etc/ld.so.preload, say), in any
|
|
1063
|
+
* later step of the account; and root's mkdir, mv and rm -rf in the agent's
|
|
1064
|
+
* folder followed whatever links the agent left there. Now:
|
|
1065
|
+
*
|
|
1066
|
+
* - a snapshot is restored only under a SEAL: an HMAC, with a key the
|
|
1067
|
+
* worker derives per account from the install key and hands to root in a
|
|
1068
|
+
* file the agent never sees (SNAPSHOT_KEY_FILE), over the package set,
|
|
1069
|
+
* the base, the time it was built and the tar's sha256. The tar is copied
|
|
1070
|
+
* into a directory only root can read while it is hashed, and extracted
|
|
1071
|
+
* from that copy, so a step running beside this one cannot swap it after
|
|
1072
|
+
* the check. No key (a warm pod, an embedder): no snapshots, apt only;
|
|
1073
|
+
* - under the seal, the verifier (SNAPSHOT_JS) audits every member, and
|
|
1074
|
+
* extraction is --numeric-owner --no-overwrite-dir;
|
|
1075
|
+
* - root reads, writes and deletes in the cache only as the agent user
|
|
1076
|
+
* (runuser), so a link the agent planted there leads only where the agent
|
|
1077
|
+
* could write itself;
|
|
1078
|
+
* - only the start-up saves, before any agent code exists in the pod; the
|
|
1079
|
+
* install helper (`save: false`), which runs beside the agent, restores
|
|
1080
|
+
* but never seals what it found on disk;
|
|
1081
|
+
* - the shared .deb cache is gone: apt downloads into the pod's own
|
|
1082
|
+
* /var/cache/apt, so no root download is written through the agent's
|
|
1083
|
+
* folder. Only a snapshot miss pays the download.
|
|
1084
|
+
*
|
|
1085
|
+
* Layout, format v5:
|
|
1086
|
+
*
|
|
1087
|
+
* <cache>/.system-snapshots/v5-<key>/<base>/snap.tar, seal, base
|
|
1088
|
+
*
|
|
1089
|
+
* The KEY is what the snapshot is of: the package set, the Debian release
|
|
1090
|
+
* (os-release ID and VERSION_ID) and the machine (uname -m). Under it, one
|
|
1091
|
+
* GENERATION per base, named by and holding the sha256 of the dpkg status
|
|
1092
|
+
* file it was built on, and restored only onto a base whose status is
|
|
1093
|
+
* byte-identical (FOL-50): the tar carries that status whole and files built
|
|
1094
|
+
* against those exact packages, and a maintainer script reads the base as
|
|
1095
|
+
* well as writing to it. What keeps a snapshot valid across releases is the
|
|
1096
|
+
* image keeping its OS layers (run-container.ts, FOLDRUN_OS_REFRESH):
|
|
1097
|
+
*
|
|
1098
|
+
* - a base change is a new generation beside the old ones, counted in the
|
|
1099
|
+
* status file as `stale` (the run log says "N snapshots did not fit this
|
|
1100
|
+
* image"), and the four newest generations per key are kept;
|
|
1101
|
+
* - a generation is restored for 14 days from the time sealed into it, then
|
|
1102
|
+
* rebuilt — so the packages it added pick up Debian's fixes — and removed;
|
|
1103
|
+
* - it is built in a staging directory and published with one rename, so
|
|
1104
|
+
* a reader never sees half of one.
|
|
1105
|
+
*
|
|
1106
|
+
* A restore that is refused or fails falls back to apt, the status file says
|
|
1107
|
+
* why (`refused`), and prepareRuntime verifies the result either way.
|
|
670
1108
|
*/
|
|
671
|
-
export function systemInstallSh(cacheDir: string, logDir = "/opt/runner/job"): string {
|
|
672
|
-
|
|
1109
|
+
export function systemInstallSh(cacheDir: string, logDir = "/opt/runner/job", opts: { save?: boolean } = {}): string {
|
|
1110
|
+
const save = opts.save !== false;
|
|
1111
|
+
// String.raw so the shell's backslashes are its own. No shell `${…}` may
|
|
1112
|
+
// appear below (it would be a JS interpolation): defaults are spelled out.
|
|
1113
|
+
return String.raw`if [ -n "$FOLDRUN_SYSTEM_PACKAGES" ]; then
|
|
673
1114
|
SYS=""; for p in $FOLDRUN_SYSTEM_PACKAGES; do case "$p" in -*|*[!a-z0-9+.-]*) ;; *) SYS="$SYS $p";; esac; done
|
|
674
|
-
D
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
1115
|
+
D=$FOLDRUN_DPKG_DIR; [ -n "$D" ] || D=/var/lib/dpkg
|
|
1116
|
+
R=$FOLDRUN_SYS_ROOT; [ -n "$R" ] || R=/
|
|
1117
|
+
SCAN=$FOLDRUN_SYS_SCAN; [ -n "$SCAN" ] || SCAN="/etc /usr /var/lib"
|
|
1118
|
+
OSR=$FOLDRUN_OS_RELEASE; [ -n "$OSR" ] || OSR=/etc/os-release
|
|
1119
|
+
ALLOW=$FOLDRUN_SYS_ALLOW; [ -n "$ALLOW" ] || ALLOW="${SNAPSHOT_ALLOWED}"
|
|
1120
|
+
KF=$FOLDRUN_SNAP_KEY_FILE; S=${cacheDir}/.system-snapshots
|
|
1121
|
+
# The cache is the agent's: as root, touch it only as the agent user.
|
|
1122
|
+
AS=""; AU=""
|
|
1123
|
+
if [ "$(id -u)" = 0 ]; then AU=$(id -u agent 2>/dev/null) && AS="runuser -u agent --" || S=""; fi
|
|
1124
|
+
# Bounded: a FIFO or an endless file in place of a snapshot must not hold
|
|
1125
|
+
# every later start-up of the account.
|
|
1126
|
+
command -v timeout > /dev/null 2>&1 && AS="timeout 300 $AS"
|
|
1127
|
+
W=$(mktemp -d /tmp/.foldrun-sys.XXXXXX 2>/dev/null) || { W=""; S=""; }
|
|
1128
|
+
{ [ -n "$KF" ] && [ -f "$KF" ] && command -v node > /dev/null 2>&1; } || S=""
|
|
1129
|
+
_fh() { if command -v sha256sum > /dev/null 2>&1; then sha256sum | cut -c1-64; else shasum -a 256 | cut -c1-64; fi; }
|
|
1130
|
+
KEY=v5-$( { printf '%s\n' $SYS | LC_ALL=C sort -u; grep -E '^(ID|VERSION_ID)=' "$OSR" 2>/dev/null; uname -m; } | _fh | cut -c1-16)
|
|
1131
|
+
BASE=$(_fh < "$D/status"); GN=$(echo "$BASE" | cut -c1-16); K=$S/$KEY; G=$K/$GN
|
|
1132
|
+
[ -n "$BASE" ] && [ "$KEY" != v5- ] || S=""
|
|
1133
|
+
XF="--numeric-owner"; tar --version 2>/dev/null | grep -q GNU && XF="--numeric-owner --no-overwrite-dir --keep-directory-symlink"
|
|
1134
|
+
T0=$(date +%s); MODE=apt; RC=1; STALE=0; WHY=""; UNSAVED=""
|
|
1135
|
+
if [ -n "$S" ]; then
|
|
1136
|
+
cat > "$W/v.cjs" <<'FOLDRUN_SNAPSHOT_JS'
|
|
1137
|
+
${SNAPSHOT_JS}
|
|
1138
|
+
FOLDRUN_SNAPSHOT_JS
|
|
1139
|
+
fi
|
|
1140
|
+
if [ -n "$S" ] && $AS test -f "$G/snap.tar" 2>/dev/null; then
|
|
1141
|
+
# Read bounded: the seal is one line of ~150 bytes, the base 65 — a file
|
|
1142
|
+
# the agent made endless must not fill root's memory.
|
|
1143
|
+
if [ "$($AS head -c 128 "$G/base" 2>/dev/null)" = "$BASE" ]; then
|
|
1144
|
+
SEAL=$($AS head -c 512 "$G/seal" 2>/dev/null)
|
|
1145
|
+
V=$($AS cat "$G/snap.tar" 2>/dev/null | node "$W/v.cjs" verify "$KF" "$KEY" "$BASE" "$R" "$ALLOW" "$AU" "$W/snap.tar" "$SEAL" 2>/dev/null); VR=$?
|
|
1146
|
+
if [ $VR -eq 0 ] && [ "$V" = ok ] && tar -xf "$W/snap.tar" -C "$R" $XF > ${logDir}/system-install.log 2>&1; then
|
|
1147
|
+
MODE=snapshot; RC=0
|
|
1148
|
+
# apt runs ldconfig through dpkg's trigger; a restore must too, or a
|
|
1149
|
+
# library the snapshot brought is missing from /etc/ld.so.cache.
|
|
1150
|
+
[ "$R" = / ] && ldconfig 2>/dev/null
|
|
1151
|
+
elif [ $VR -eq 3 ]; then WHY=$(printf '%s' "$V" | sed 's/^refused: //')
|
|
1152
|
+
fi
|
|
1153
|
+
rm -f "$W/snap.tar"
|
|
1154
|
+
fi
|
|
1155
|
+
fi
|
|
1156
|
+
if [ -n "$S" ] && [ "$MODE" != snapshot ]; then
|
|
1157
|
+
# Generations of this package set built on other bases: why apt runs.
|
|
1158
|
+
STALE=$($AS ls "$K" 2>/dev/null | grep -cvx "$GN")
|
|
1159
|
+
fi
|
|
1160
|
+
if [ "$MODE" != snapshot ]; then
|
|
1161
|
+
[ -n "$S" ] && [ ${save ? "1" : "0"} = 1 ] && { ls "$D"/info/*.list 2>/dev/null | sort > "$W/before"; touch "$W/mark"; sleep 1; }
|
|
1162
|
+
{ apt-get -o Acquire::Retries=3 -qq update && DEBIAN_FRONTEND=noninteractive apt-get -o Acquire::Retries=3 -o Dpkg::Use-Pty=0 install -y -qq --no-install-recommends $SYS; } > ${logDir}/system-install.log 2>&1
|
|
688
1163
|
RC=$?
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
1164
|
+
apt-get -qq clean > /dev/null 2>&1
|
|
1165
|
+
if [ $RC -eq 0 ] && [ -n "$S" ] && [ ${save ? "1" : "0"} = 1 ]; then
|
|
1166
|
+
ls "$D"/info/*.list 2>/dev/null | sort > "$W/after"
|
|
1167
|
+
comm -13 "$W/before" "$W/after" > "$W/new"
|
|
1168
|
+
if [ -s "$W/new" ]; then
|
|
1169
|
+
{ cat $(cat "$W/new") | sort -u | while read -r f; do if [ -f "$f" ] || [ -L "$f" ]; then echo "$f"; fi; done
|
|
1170
|
+
for l in $(cat "$W/new"); do ls "$(dirname "$l")/$(basename "$l" .list)".* 2>/dev/null; done
|
|
695
1171
|
echo "$D/status"
|
|
696
1172
|
# What the packages' install scripts made, which dpkg's lists do not
|
|
697
1173
|
# name: update-alternatives links (libblas.so.3 → /etc/alternatives),
|
|
698
1174
|
# generated caches and configs. By ctime, not mtime: those links carry
|
|
699
1175
|
# the package's own (2025) mtime, and only ctime cannot be backdated.
|
|
700
|
-
find
|
|
701
|
-
} | sort -u > /
|
|
702
|
-
|
|
703
|
-
|
|
1176
|
+
find $SCAN -xdev \( -path /var/lib/apt -o -path "$D/info" -o -path "${cacheDir}" \) -prune -o \( -type f -o -type l \) -cnewer "$W/mark" -print 2>/dev/null
|
|
1177
|
+
} | grep '^/' | sort -u > "$W/files"
|
|
1178
|
+
# Built where only root can read, audited and sealed, then handed to
|
|
1179
|
+
# the agent user to publish.
|
|
1180
|
+
if tar -cf "$W/snap.tar" --numeric-owner -T "$W/files" 2>/dev/null && SEAL=$(node "$W/v.cjs" seal "$KF" "$KEY" "$BASE" "$R" "$ALLOW" "$AU" "$W/snap.tar" 2>/dev/null); then
|
|
1181
|
+
$AS sh -c '${PUBLISH_SH}' sh "$K" "$GN" "$SEAL" "$BASE" < "$W/snap.tar" 2>/dev/null
|
|
1182
|
+
else
|
|
1183
|
+
UNSAVED=$(printf '%s' "$SEAL" | sed 's/^refused: //')
|
|
1184
|
+
fi
|
|
1185
|
+
rm -f "$W/snap.tar"
|
|
704
1186
|
fi
|
|
705
|
-
|
|
1187
|
+
$AS sh -c '${PRUNE_SH}' sh "$S" "${cacheDir}" 2>/dev/null
|
|
706
1188
|
fi
|
|
707
1189
|
fi
|
|
708
|
-
printf '{"ok": %s, "exit": %s, "seconds": %s, "mode": "%s", "packages": "%s"}
|
|
1190
|
+
printf '{"ok": %s, "exit": %s, "seconds": %s, "mode": "%s", "stale": %s, "packages": "%s", "refused": "%s", "unsaved": "%s"}\n' "$([ $RC -eq 0 ] && echo true || echo false)" "$RC" "$(( $(date +%s) - T0 ))" "$MODE" "$STALE" "$SYS" "$WHY" "$UNSAVED" > ${logDir}/system-install.json
|
|
1191
|
+
[ -n "$W" ] && rm -rf "$W"
|
|
709
1192
|
rm -rf /var/lib/apt/lists/* 2>/dev/null || true
|
|
710
1193
|
fi`;
|
|
711
1194
|
}
|
|
@@ -720,7 +1203,7 @@ export function checkSystemPackages(pkgs: string[]): { log: string[]; error: str
|
|
|
720
1203
|
if (!pkgs.length) return { log: [], error: null };
|
|
721
1204
|
const statusFile = process.env.FOLDRUN_SYSTEM_STATUS ?? SYSTEM_STATUS_FILE;
|
|
722
1205
|
const logFile = process.env.FOLDRUN_SYSTEM_LOG ?? SYSTEM_LOG_FILE;
|
|
723
|
-
let status: { ok?: boolean; seconds?: number; mode?: string } | null = null;
|
|
1206
|
+
let status: { ok?: boolean; seconds?: number; mode?: string; stale?: number; refused?: string; unsaved?: string } | null = null;
|
|
724
1207
|
try {
|
|
725
1208
|
status = JSON.parse(fs.readFileSync(statusFile, "utf8"));
|
|
726
1209
|
} catch {
|
|
@@ -739,7 +1222,22 @@ export function checkSystemPackages(pkgs: string[]): { log: string[]; error: str
|
|
|
739
1222
|
);
|
|
740
1223
|
const missing = pkgs.filter((p) => !installed.has(p));
|
|
741
1224
|
if (!missing.length) {
|
|
742
|
-
|
|
1225
|
+
// `stale`: snapshots of this package set that were there but did not fit
|
|
1226
|
+
// this image (systemInstallSh) — what says why a step paid apt again.
|
|
1227
|
+
const stale = status?.mode !== "snapshot" && status?.stale ? `, ${status.stale} snapshot${status.stale === 1 ? "" : "s"} did not fit this image` : "";
|
|
1228
|
+
// FOL-129: a snapshot whose seal or contents did not pass, and one the
|
|
1229
|
+
// start-up would not seal — said, so a step that paid apt says why.
|
|
1230
|
+
const notes = [
|
|
1231
|
+
...(status?.mode !== "snapshot" && status?.refused ? [`snapshot refused: ${status.refused}`] : []),
|
|
1232
|
+
...(status?.unsaved ? [`no snapshot kept: ${status.unsaved}`] : []),
|
|
1233
|
+
];
|
|
1234
|
+
return {
|
|
1235
|
+
log: [
|
|
1236
|
+
`system packages ready: ${pkgs.join(", ")}${status?.seconds !== undefined ? ` (${status.mode === "snapshot" ? "snapshot" : "apt"}, ${status.seconds}s${stale})` : ""}`,
|
|
1237
|
+
...notes,
|
|
1238
|
+
],
|
|
1239
|
+
error: null,
|
|
1240
|
+
};
|
|
743
1241
|
}
|
|
744
1242
|
let aptOut = "";
|
|
745
1243
|
try {
|