row334: render P1 proxy system unit in Case 2 installs

This commit is contained in:
jason.woltje committed 2026-10-26 17:54:46 -05:00
1 parent c155f52230
commit 70f7be7f3f
11 files changed
+1414 -25

No files matched your search

@@ -490,3 +490,10 @@ test('C2-21: library install supplies chief identity and books publish for its r
assert.ok(doc.roles.chief.identity?.gitea?.user);
assert.notEqual(doc.roles.chief.identity?.gitea?.user, 'writer');
});
test('row334 install Next tells root to select proxy only when configured', t => {
const x = world(t), r = install(x);
assert.equal(r.code, 0, r.out);
assert.match(r.out, /Next: root runs case2-unit.sh[^\n]+add --proxy when the config has a proxy section/);
});
+170 -2
View File
@@ -7,10 +7,10 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { createServer } from "node:net";
import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
import { chmodSync, cpSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { businessDoc, writeJson } from "../../business/tests/helpers.mjs";
import { businessDoc, systemConfig, writeJson } from "../../business/tests/helpers.mjs";
import { binds, prepareRun, sandboxArgv, sandboxProblem } from "../../seat/src/sandbox.mjs";
import { tmp } from "./helpers.mjs";
import { CASE_SUDO_SESSION, recordedLivePids, killRecordedPids, fixtureGuard, cleanupFixture, pollFixtureGone, blockingQuerySource } from "./case-sudo-fixture.mjs";
@@ -940,3 +940,171 @@ test("Vikunja 614 precheck: notifier config refuses missing, public, malformed a
assert.match(r.out, /REFUSED notifier config:/, `${fault}: ${r.out}`);
}
});
// Row334: render only into the scratch hub; fake systemctl never touches the host.
const PROXY_TEMPLATE = join(REPO, 'packages/cli/systemd/mosaic-proxy.service.in');
const proxyFile = h => join(h.units, 'mosaic-proxy.service');
const proxyUnit = (h, options = {}) => unit(h, { ...options, extra: ['--proxy', ...(options.extra ?? [])] });
test('row334 no-proxy renders stay byte-identical to the pre-row renderer', t => {
const h = hub(t);
// Immutable committed baseline, executed only with the same scratch seams.
const baseline = spawnSync('git', ['show', '78b69e1e6:scripts/case2-unit.sh'], { cwd: REPO, encoding: 'utf8' });
assert.equal(baseline.status, 0, baseline.stderr);
const old = script(join(h.root, 'baseline/scripts/case2-unit.sh'), baseline.stdout);
const args = ['--user', 'mosaic', '--home', h.home, '--business', 'acme', '--copy', COPY_A, '--node', USR_NODE, '--', ...libraryFlags(h)];
const r = spawnSync(old, args, { encoding: 'utf8', env: { PATH: `${h.bin}:${process.env.PATH}`, CASE2_UNIT_DIR: h.units,
CASE2_SYSTEMCTL: join(h.bin, 'systemctl'), CASE2_GETENT: join(h.bin, 'getent'), CASE2_BUS_TEMPLATE: BUS_TEMPLATE, CASE2_WEBUI_TEMPLATE: WEBUI_TEMPLATE } });
assert.equal(r.status, 0, r.stderr);
const before = [read(busFile(h)), read(webuiFile(h))];
assert.equal(unit(h).code, 0);
assert.deepEqual([read(busFile(h)), read(webuiFile(h))], before);
assert.equal(existsSync(proxyFile(h)), false);
});
test('row334 proxy uses P1 runner, bus dependencies and proxy bus Console start order', t => {
const h = hub(t);
const r = proxyUnit(h, { profile: 'factory', flags: ['--port', '443', '--business', 'acme'], extra: ['--claude', TRUE_BIN] });
assert.equal(r.code, 0, r.err);
const proxy = read(proxyFile(h));
const expected = read(PROXY_TEMPLATE).replaceAll('@REPO@', copyDir(h)).replaceAll('@PATH@', '/usr/bin:/usr/local/bin:/bin')
.replaceAll('@NODE@', USR_NODE).replace(/^@CONFIG_ENV@\n/m, '').replace('WantedBy=default.target', 'WantedBy=multi-user.target')
.replace('TimeoutStopSec=60', `TimeoutStopSec=60\nUser=mosaic\nGroup=mosaic\n${HARDENING(h).join('\n')}`);
assert.equal(proxy, expected);
assert.equal(statSync(proxyFile(h)).mode & 0o777, 0o644);
assert.match(read(busFile(h)), /Description=[^\n]+\nWants=mosaic-proxy.service\nAfter=mosaic-proxy.service\n/);
assert.deepEqual(lines(proxy, 'ExecStart'), [`${USR_NODE} ${copyDir(h)}/packages/model-proxy/src/run.mjs`]);
assert.doesNotMatch(proxy, /EnvironmentFile=|Capabilities=|CapabilityBoundingSet=|MOSAIC_CLAUDE_BIN/);
assert.doesNotMatch(read(webuiFile(h)), /Wants=|After=/);
assert.deepEqual(read(h.log).trim().split('\n'), [
'is-active --quiet mosaic-proxy.service', 'is-active --quiet [email protected]', 'is-active --quiet mosaic-webui.service',
'daemon-reload', 'enable mosaic-proxy.service [email protected] mosaic-webui.service',
'start mosaic-proxy.service', 'start [email protected]', 'start mosaic-webui.service',
]);
});
const proxyRefusals = [
['continuation', s => s.replace('Type=simple', 'Type=simple' + String.fromCharCode(92)), /a line continues onto the next/],
['not-setting', s => s.replace('Type=simple', 'not a setting'), /neither a section/],
...['RestrictNamespaces', 'PrivateUsers', 'SystemCallFilter', 'PermissionsStartOnly', 'EnvironmentFile', 'AmbientCapabilities', 'CapabilityBoundingSet'].map(k =>
[k, s => s.replace('Type=simple', `Type=simple\n${k}=yes`), new RegExp(`refusing ${k}=`)]),
['privileged', s => s.replace('ExecStart=', 'ExecStart=+'), /Exec line that runs with full privileges/],
['direct-binary', s => s.replace('packages/model-proxy/src/run.mjs', 'opt/cli-proxy-api'), /ExecStart= must be exactly once/],
['nnp', s => s.replace('NoNewPrivileges=yes', 'NoNewPrivileges=no'), /NoNewPrivileges= must be exactly once/],
['cwd', s => s.replace('WorkingDirectory=@REPO@', 'WorkingDirectory=/elsewhere'), /WorkingDirectory= must be exactly once/],
['environment', s => s.replace('Environment=PATH=@PATH@', 'Environment=PATH=/elsewhere'), /Environment= lines must be exactly/],
['install', s => s.replace('WantedBy=default.target', 'WantedBy=other.target'), /WantedBy= must be exactly once/],
['user', s => s.replace('Type=simple', 'Type=simple\nUser=other'), /User= must be exactly once/],
['group', s => s.replace('Type=simple', 'Type=simple\nGroup=other'), /Group= must be exactly once/],
...['ProtectSystem', 'ReadWritePaths', 'ProtectHome', 'PrivateTmp'].map(k =>
[k, s => s.replace('Type=simple', `Type=simple\n${k}=other`), new RegExp(`${k}= must be exactly once`)]),
['token', s => s.replace('Type=simple', 'Type=simple\nX-Note=@UNKNOWN@'), /a token is left after the render/],
];
for (const [name, edit, message] of proxyRefusals) test(`row334 proxy refuses ${name} by message before writing`, t => {
const h = hub(t);
const file = template(h, PROXY_TEMPLATE, `${name}.in`, edit);
refuses(proxyUnit(h, { env: { CASE2_PROXY_TEMPLATE: file } }), message);
assert.deepEqual(readdirSync(h.units), []);
assert.equal(existsSync(h.log), false);
});
test('row334 proxy template missing or symlink refuses', t => {
const h = hub(t), missing = join(h.root, 'missing.in');
refuses(proxyUnit(h, { env: { CASE2_PROXY_TEMPLATE: missing } }), /template missing or not a regular file/);
symlinkSync(PROXY_TEMPLATE, missing);
refuses(proxyUnit(h, { env: { CASE2_PROXY_TEMPLATE: missing } }), /template missing or not a regular file/);
});
test('row334 rendered ExecStart refuses wrong PIN and missing proxy with P1 status', t => {
const h = hub(t);
assert.equal(proxyUnit(h).code, 0);
const copy = copyDir(h);
cpSync(join(REPO, 'packages/model-proxy'), join(copy, 'packages/model-proxy'), { recursive: true });
cpSync(join(REPO, 'scripts/mosaic-config.mjs'), join(copy, 'scripts/mosaic-config.mjs'));
const config = systemConfig(join(h.root, 'runner'));
const doc = JSON.parse(read(config));
mkdirSync(doc.dataRoot, { recursive: true, mode: 0o700 });
const pin = JSON.parse(read(join(REPO, 'packages/model-proxy/PIN')));
const binary = join(doc.dataRoot, 'opt/cliproxyapi', pin.version, 'cli-proxy-api');
script(binary, `echo unexpected-spawn > '${h.root}/spawned'`);
writeJson(join(dirname(config), 'proxy.json'), { port: 18541, binary });
const argv = lines(read(proxyFile(h)), 'ExecStart')[0].split(' ');
const run = () => spawnSync(argv[0], argv.slice(1), { encoding: 'utf8', env: { PATH: '/usr/bin:/bin', HOME: h.home, MOSAIC_CONFIG: config } });
const bad = run();
assert.equal(bad.status, 3, bad.stderr);
assert.equal(bad.stderr, 'proxy-install-invalid\n');
assert.equal(existsSync(join(h.root, 'spawned')), false);
rmSync(join(dirname(config), 'proxy.json'));
const absent = run();
assert.equal(absent.status, 3, absent.stderr);
assert.equal(absent.stderr, 'proxy-not-configured\n');
});
test('row334 reinstall repins all three and refuses a proxy copy mismatch', t => {
const h = hub(t);
assert.equal(proxyUnit(h).code, 0);
assert.equal(proxyUnit(h, { copy: COPY_B }).code, 0);
for (const file of [proxyFile(h), busFile(h), webuiFile(h)]) assert.match(read(file), new RegExp(copyDir(h, COPY_B)));
writeFileSync(proxyFile(h), read(proxyFile(h)).replaceAll(copyDir(h, COPY_B), copyDir(h)));
const before = [proxyFile(h), busFile(h), webuiFile(h)].map(read);
refuses(proxyUnit(h), /installed units name different copies/);
assert.deepEqual([proxyFile(h), busFile(h), webuiFile(h)].map(read), before);
});
test('row334 installed proxy without --proxy refuses and preserves all bytes', t => {
const h = hub(t);
assert.equal(proxyUnit(h).code, 0);
const files = [proxyFile(h), busFile(h), webuiFile(h)], before = files.map(read);
writeFileSync(h.log, '');
refuses(unit(h), /installed mosaic-proxy.service requires --proxy/);
assert.deepEqual(files.map(read), before);
assert.equal(read(h.log), '');
});
test('row334 dangling proxy symlink without flag refuses without deleting', t => {
const h = hub(t);
symlinkSync(join(h.root, 'absent'), proxyFile(h));
refuses(unit(h), /installed mosaic-proxy.service requires --proxy/);
assert.equal(readdirSync(h.units).includes('mosaic-proxy.service'), true);
});
test('row334 active proxy reports old copy and all three must restart together', t => {
const h = hub(t);
assert.equal(proxyUnit(h).code, 0);
const r = proxyUnit(h, { copy: COPY_B, env: { CASE2_TEST_ACTIVE: 'mosaic-proxy.service [email protected] mosaic-webui.service' } });
assert.equal(r.code, 0, r.err);
assert.match(r.out, /mosaic-proxy.service was already running/);
assert.match(r.out, /restart mosaic-proxy.service, [email protected] and mosaic-webui.service together/);
});
for (const name of ['mosaic-proxy.service', '[email protected]']) test(`row334 failed ${name} start leaves later units unstarted`, t => {
const h = hub(t);
refuses(proxyUnit(h, { env: { CASE2_TEST_FAIL: `start ${name}` } }), /systemctl start .* failed/);
assert.doesNotMatch(read(h.log), /start mosaic-webui.service/);
if (name === 'mosaic-proxy.service') assert.doesNotMatch(read(h.log), /start [email protected]/);
});
test('row334 installed proxy enforces hardening order and remaining common refusals', t => {
const h = hub(t);
assert.equal(proxyUnit(h).code, 0);
const original = read(proxyFile(h));
for (const [edit, message] of [
[s => s.replace('ProtectSystem=strict\nReadWritePaths=' + h.home, 'ReadWritePaths=' + h.home + '\nProtectSystem=strict'), /hardening lines must be in the order/],
[s => s.replace('WorkingDirectory=' + copyDir(h), 'WorkingDirectory=%h'), /names no copy/],
[s => s.replace('Type=simple', 'Type=simple\nX-Note=%h'), /%h is left; a system unit/],
[s => s.replace('RestartSec=15', 'RestartSec=16'), /differs from the render in more than the copy id/],
]) {
writeFileSync(proxyFile(h), edit(original));
refuses(proxyUnit(h), message);
}
});
test('row334 proxy installed path rejects symlink and non-regular file', t => {
const h = hub(t);
symlinkSync(join(h.root, 'absent'), proxyFile(h));
refuses(proxyUnit(h), /is a symlink/);
rmSync(proxyFile(h));
mkdirSync(proxyFile(h));
refuses(proxyUnit(h), /is not a regular file/);
});