Files
alzeo/tests/test_install_root_refusal.py
T
Hasan BasbunarandClaude Opus 5 a856bcac84 v1.12.0: one command, and the thing you open
The one-liner left an engine and an API port. The cockpit, which is the point
of the last four releases, sat behind a second command in a README nobody
reads after a 30 GB download. And the installer accepted being run as root,
which on 2026-09-13 produced a silent success into the wrong home: a clone in
/root, a new API key in /root/.config/qwen38, both units as User=root pointing
at it, the engine started, its own generation smoke test passed, success
reported, and every client on the box on 401 against a healthy engine. Nothing
failed, so nothing could be read from a log.

- get.sh refuses root before it clones, install.sh before it writes. With
  SUDO_USER set the refusal names the login, the key path that would stop
  working and the command that works, with no override; a genuine root login
  is refused by default and ALLOW_ROOT=1 gets through.
- The cockpit installs as step 10/10, after the engine answers a real
  generation. The Agent tab comes with it when opencode is on the PATH and is
  skipped with a note when it is not. A cockpit that fails to install never
  fails an engine that is already serving.
- The summary leads with the cockpit URL, read back from the installed unit,
  with a wildcard bind resolved into an address you can type.
- First install binds the tailnet address when there is one; a re-run
  converges on the installed unit. --no-cockpit persists in cockpit.off.

Three bugs the tests found while being written:
- getent exits 2 on an unknown user, and under set -e + pipefail that killed
  the sudo refusal itself: "Install failed at line 36" instead of the message.
- Four tracked test files were run by no CI step: test_install_preflight,
  test_install_sudo, test_proxy_tls, test_uninstall_inventory. Gate 8 covers
  dashboard/tests only, and tests/ had a hand-kept list they had fallen off.
- test_install_sudo declared 2 tests and ran 0 wherever it skipped, which is
  every runner: SkipTest from setUpClass is one skip with testsRun 0.

New gate "No test file in tests/ is left unrun": unittest modules discovered,
script-style files each named by a step, declared == ran in aggregate and per
file. Negative-controlled both halves.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-14 11:19:41 +02:00

139 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""The installer refuses root, and says which mistake was made.
On 2026-09-13 the reference box was installed with
`curl .../get.sh | sudo bash`. Nothing failed: git cloned into /root, install.sh
wrote /root/.config/qwen38 with a brand new API key, rendered the units as
User=root pointing at that key, started the engine, ran its smoke test and
reported success. The box then answered 401 to every client on it, because they
all read ~/.config/qwen38/api-key, and finding out why took a read of the sudo
audit log. A silent success into the wrong home is the worst failure shape this
installer has, so both entry points refuse root before writing anything.
The refusal has two shapes because the mistakes are different. With SUDO_USER
set there is always a right answer (drop the sudo) and no override: the message
names the login and the path whose key would stop working. A genuine root login
is merely unusual, so it refuses by default and ALLOW_ROOT=1 gets through.
Root is faked by stubbing `id` on PATH rather than by being root: a test suite
must not need root to prove what happens under it, and a PATH that can lie
about `id` could edit the script it is lying to.
"""
import os
import pathlib
import stat
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
ONELINER = "https://raw.githubusercontent.com/hasso5703/dgx-spark-qwen38/main/get.sh"
def fake_root_bin():
"""A PATH entry whose `id` claims uid 0, and nothing else."""
d = tempfile.mkdtemp(prefix="fake-root-bin-")
idsh = os.path.join(d, "id")
with open(idsh, "w") as f:
f.write("#!/bin/sh\n"
"case \"$*\" in\n"
" -u) echo 0 ;;\n"
" -un) echo root ;;\n"
" -g) echo 0 ;;\n"
" -gn) echo root ;;\n"
" *) exec /usr/bin/id \"$@\" ;;\n"
"esac\n")
os.chmod(idsh, os.stat(idsh).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
return d
def run(script, *, sudo_user=None, allow_root=None, args=(), **extra):
"""Run script with a faked root identity, in a throwaway HOME."""
env = {"PATH": fake_root_bin() + ":/usr/local/bin:/usr/bin:/bin",
"HOME": tempfile.mkdtemp(prefix="root-refusal-home-")}
if sudo_user is not None:
env["SUDO_USER"] = sudo_user
if allow_root is not None:
env["ALLOW_ROOT"] = allow_root
env.update(extra)
r = subprocess.run(["bash", str(REPO / script), *args],
capture_output=True, text=True, env=env, cwd=str(REPO), timeout=60)
return r.returncode, r.stdout + r.stderr, env["HOME"]
class InstallRefusesSudo(unittest.TestCase):
def test_sudo_in_front_is_refused_by_name(self):
rc, out, _ = run("install.sh", sudo_user="alice")
self.assertEqual(rc, 1)
self.assertIn("do not put sudo in front of this installer", out)
def test_the_message_names_the_login_and_the_key_that_would_break(self):
rc, out, _ = run("install.sh", sudo_user="alice")
self.assertIn("alice", out)
self.assertIn("/.config/qwen38/api-key", out)
self.assertIn("401", out)
def test_the_message_gives_the_command_that_works(self):
_, out, _ = run("install.sh", sudo_user="alice")
self.assertIn("./install.sh", out)
self.assertIn(ONELINER, out)
def test_allow_root_does_not_excuse_sudo(self):
# ALLOW_ROOT is for a box with no other user. Under sudo there IS
# another user, and their home is the one the units must point at.
rc, out, _ = run("install.sh", sudo_user="alice", allow_root="1")
self.assertEqual(rc, 1)
self.assertIn("do not put sudo in front of this installer", out)
def test_nothing_is_written_before_the_refusal(self):
_, _, home = run("install.sh", sudo_user="alice")
self.assertEqual(os.listdir(home), [],
"the refusal must land before the first write")
class InstallRefusesPlainRoot(unittest.TestCase):
def test_plain_root_is_refused_with_the_way_out(self):
rc, out, _ = run("install.sh")
self.assertEqual(rc, 1)
self.assertIn("not as root", out)
self.assertIn("ALLOW_ROOT=1", out)
def test_allow_root_gets_through_the_wall(self):
# Proven without installing anything: a deliberately invalid PORT dies
# at the next refusal down, which can only be reached past the wall.
rc, out, _ = run("install.sh", allow_root="1", PORT="abc")
self.assertEqual(rc, 1)
self.assertIn("PORT must be a number", out)
self.assertNotIn("not as root", out)
class GetShRefusesRootBeforeTheClone(unittest.TestCase):
def test_piping_into_sudo_bash_is_refused(self):
rc, out, _ = run("get.sh", sudo_user="alice")
self.assertEqual(rc, 1)
self.assertIn('do not pipe this into "sudo bash"', out)
self.assertIn("alice", out)
def test_plain_root_is_refused_with_the_way_out(self):
rc, out, _ = run("get.sh")
self.assertEqual(rc, 1)
self.assertIn("not as root", out)
self.assertIn("ALLOW_ROOT=1", out)
def test_the_refusal_lands_before_any_clone(self):
_, out, home = run("get.sh", sudo_user="alice")
self.assertNotIn("Cloning", out)
self.assertEqual(os.listdir(home), [])
def test_allow_root_gets_through_the_wall(self):
# DIR points at a path that is not a clone, so the run stops at the
# origin check: past the wall, still no network and nothing written.
rc, out, _ = run("get.sh", allow_root="1", DIR="/nonexistent/not-a-clone")
self.assertEqual(rc, 1)
self.assertIn("not a clone of this repo", out)
self.assertNotIn("not as root", out)
if __name__ == "__main__":
unittest.main(verbosity=2)