diff --git a/bin/agentbox b/bin/agentbox index 733d34f9..f186293c 100755 --- a/bin/agentbox +++ b/bin/agentbox @@ -39,6 +39,7 @@ comparison. import argparse import contextlib +import fcntl import hashlib import html import ipaddress @@ -78,6 +79,10 @@ def ttyd_socket(user_name): return f"{TTYD_SOCKET_DIR}/{user_name}/ttyd.sock" DEFAULT_CONFIG = "/etc/agent-box/config.yaml" +# Workspaces (Linux users) added after deploy, issue #1088. Root-owned state +# beside the declared config: `agentbox workspace add` writes it and every +# apply merges it into `users`, so config.yaml stays the operator's file. +WORKSPACES_FILE = "/var/lib/agent-box/workspaces.json" # The self-update unit (issue #358). `agentbox apply` renders it but never # enables or starts it: it is on-demand only, triggered through the sudo @@ -327,6 +332,45 @@ def load_config(path): return data +def load_workspaces(path=None): + """The names recorded by `agentbox workspace add`, as a sorted list.""" + try: + data = json.loads(Path(path or WORKSPACES_FILE).read_text()) + except FileNotFoundError: + return [] + except (OSError, ValueError) as e: + raise ConfigError(f"{path or WORKSPACES_FILE} is unreadable: {e}") + names = data.get("workspaces") if isinstance(data, dict) else None + if not isinstance(names, list) or not all( + isinstance(n, str) and USER_RE.fullmatch(n) for n in names): + raise ConfigError( + f"{path or WORKSPACES_FILE}: expected {{\"workspaces\": " + "[user names]}") + return sorted(set(names)) + + +def merge_workspaces(data, path=None): + """Add the recorded workspaces to the config's users (a copy). + + A name config.yaml already declares keeps the operator's settings. + """ + names = load_workspaces(path) + if not names: + return data + merged = dict(data) + users = dict(merged.get("users") or {}) + # Spec picks the first user in sorted order as root when none is flagged. + # A recorded name sorting earlier must not take that over, so pin the + # declared choice before adding anything. + if users and not any((u or {}).get("root") for u in users.values()): + first = sorted(users)[0] + users[first] = {**(users[first] or {}), "root": True} + for name in names: + users.setdefault(name, {}) + merged["users"] = users + return merged + + def profile_manifest(profile): """What the installed runtime profile records about its own origin. @@ -5271,7 +5315,10 @@ def cmd_apply(args): back to the previous release on a non-zero apply. """ profile = resolve_profile(args.profile) - spec = Spec(load_config(args.config), profile, root=args.root or "/") + config = load_config(args.config) + if args.root is None: + config = merge_workspaces(config) + spec = Spec(config, profile, root=args.root or "/") if args.first_boot: first_boot(spec, args) rend = Renderer(spec, profile, root=args.root or "/", config=args.config) @@ -6097,7 +6144,7 @@ def start_harness_upgrades(config, profile): than holding the one the settings page polls. """ try: - spec = Spec(load_config(config), profile) + spec = Spec(merge_workspaces(load_config(config)), profile) except Exception: # noqa: BLE001 - never fail an applied update here. # The apply above already succeeded against this same file, so a # parse failure here is not a thing this box can act on - and the @@ -6366,6 +6413,104 @@ def cmd_signal(args): # -------------------------------------------------------------------------- +def cmd_workspace(args): + """`workspace ls|add NAME`: list or create a workspace (a Linux user). + + add records the name in WORKSPACES_FILE and runs a normal apply, which + creates the user, its home, units, sudoers and Caddy route like any other + declared user. Only root may do it: this is the one step an agent user + can never take for itself. + """ + declared = load_config(args.config).get("users") or {} + recorded = load_workspaces(args.state) + if args.action == "ls": + for name in sorted(set(declared) | set(recorded)): + print(name + ("" if name in declared else " (added)")) + return 0 + name = args.name + if not name or not USER_RE.fullmatch(name): + raise ConfigError(f"invalid workspace name {name!r}: lower-case " + "letters, digits, _ and -, at most 32") + try: + import pwd + pwd.getpwnam(name) + except KeyError: + pass + else: + if name not in recorded: + raise ConfigError(f"{name!r} is already a login on this host " + "that agent-box does not manage; pick another " + "name") + if name in declared: + print(f"workspace {name} already exists") + return 0 + if os.geteuid() != 0 and not args.no_apply: + raise ConfigError("adding a workspace needs root") + path = Path(args.state or WORKSPACES_FILE) + path.parent.mkdir(parents=True, exist_ok=True) + # Serialize read-modify-write: two concurrent adds must not drop a name. + with open(str(path) + ".lock", "w") as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + recorded = load_workspaces(args.state) + if name not in recorded: + fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=".workspaces.") + with os.fdopen(fd, "w") as fh: + json.dump({"workspaces": sorted(recorded + [name])}, fh) + fh.write("\n") + os.chmod(tmp, 0o644) + os.replace(tmp, path) + print(f"recorded workspace {name} in {path}") + if args.no_apply: + return 0 + # Also reached when the name was recorded by an earlier attempt whose + # apply failed: re-running finishes the provisioning instead of + # reporting success over a half-made user. + profile = resolve_profile(args.profile) + spec = Spec(merge_workspaces(load_config(args.config), args.state), + profile, root="/") + ensure_web_hash(spec, name) + project_web_secrets(spec) + return cmd_apply(argparse.Namespace( + config=args.config, profile=args.profile, root=None, dry_run=False, + first_boot=False, settle_delay=0)) + + +def project_web_secrets(spec, etc="/etc/agent-box"): + """Put every web user's secrets in /run/agent-box-web/env now. + + apply validates the rendered Caddyfile against that file before it + commits anything, so the new user's WEB_PASSWORD_* variables must be + there first or the validation fails and no route is made. + """ + if not spec.web_enable: + return + write_auth_env([(u.name, f"{etc}/{u.name}.hash") for u in spec.users + if Path(f"{etc}/{u.name}.hash").is_file()]) + + +def ensure_web_hash(spec, name, etc="/etc/agent-box"): + """Give a new web user a password hash before apply lists it. + + write_auth_env reads every listed hash file, so a missing one would stop + /run/agent-box-web/env from being written and caddy from starting for + every user. Same rule as first_boot: all users share the box's web + password, so the new user reuses the root user's existing hash. An + existing hash file is never touched. + """ + if not spec.web_enable: + return + target = Path(etc) / f"{name}.hash" + if target.is_file(): + return + source = Path(etc) / f"{spec.root_user.name}.hash" + if not source.is_file(): + raise ConfigError(f"cannot give {name} web access: {source} is " + "missing; nothing was applied") + fd = os.open(target, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, "w") as fh: + fh.write(source.read_text().strip() + "\n") + + def main(argv=None): ap = argparse.ArgumentParser( prog="agentbox", description=__doc__.split("\n")[0]) @@ -6389,6 +6534,18 @@ def main(argv=None): help="seconds to wait before the first IP probe") a.set_defaults(func=cmd_apply) + ws = sub.add_parser( + "workspace", help="list or add workspaces (Linux users)") + ws.add_argument("action", choices=["ls", "add"]) + ws.add_argument("name", nargs="?") + ws.add_argument("--config", default=DEFAULT_CONFIG) + ws.add_argument("--profile", + help="runtime profile path (default: derived from argv[0])") + ws.add_argument("--state", help=argparse.SUPPRESS) + ws.add_argument("--no-apply", action="store_true", + help="record the name only; apply later") + ws.set_defaults(func=cmd_workspace) + up = sub.add_parser( "update", help="fast-forward the runtime profile to a newer rev and re-apply") diff --git a/tests/test_agentbox.py b/tests/test_agentbox.py index 41b89b0a..a8e9ea45 100644 --- a/tests/test_agentbox.py +++ b/tests/test_agentbox.py @@ -4109,6 +4109,98 @@ def test_a_source_tree_failure_is_reported_not_raised(self): "a failed pull must not touch the profile") +class WorkspaceTest(unittest.TestCase): + """`agentbox workspace add` (issue #1088): extra users merge into apply.""" + + def setUp(self): + self.mod = load_agentbox() + + def test_add_records_and_merges_without_touching_config(self): + with tempfile.TemporaryDirectory() as tmp: + state = str(Path(tmp) / "workspaces.json") + with mock.patch("pwd.getpwnam", side_effect=KeyError): + rc = self.mod.cmd_workspace(argparse.Namespace( + action="add", name="research", config=str(CONFIG_JSON), + profile=None, state=state, no_apply=True)) + self.assertEqual(rc, 0) + self.assertEqual(self.mod.load_workspaces(state), ["research"]) + data = self.mod.load_config(str(CONFIG_JSON)) + merged = self.mod.merge_workspaces(data, state) + self.assertIn("research", merged["users"]) + self.assertNotIn("research", data["users"]) + self.mod.Spec(merged, str(Path(tmp))) # still a valid box + + def test_recorded_names_do_not_change_the_root_user(self): + with tempfile.TemporaryDirectory() as tmp: + state = Path(tmp) / "workspaces.json" + state.write_text('{"workspaces": ["aardvark"]}') + data = {"users": {"agent": {}, "zed": {}}} + merged = self.mod.merge_workspaces(data, str(state)) + self.assertTrue(merged["users"]["agent"]["root"]) + self.assertNotIn("root", merged["users"]["aardvark"]) + self.assertEqual(data, {"users": {"agent": {}, "zed": {}}}) + + def test_projects_secrets_for_users_with_a_hash(self): + spec = mock.Mock(web_enable=True) + spec.users = [mock.Mock(), mock.Mock()] + spec.users[0].name, spec.users[1].name = "agent", "research" + with tempfile.TemporaryDirectory() as tmp: + (Path(tmp) / "agent.hash").write_text("H\n") + (Path(tmp) / "research.hash").write_text("H\n") + with mock.patch.object(self.mod, "write_auth_env") as wae: + self.mod.project_web_secrets(spec, etc=tmp) + self.assertEqual([n for n, _ in wae.call_args[0][0]], + ["agent", "research"]) + spec.web_enable = False + with mock.patch.object(self.mod, "write_auth_env") as wae: + self.mod.project_web_secrets(spec, etc=tmp) + wae.assert_not_called() + + def test_bad_and_taken_names_refused(self): + with tempfile.TemporaryDirectory() as tmp: + state = str(Path(tmp) / "workspaces.json") + for name in ("Bad Name", "../x", "", "research\n"): + with self.assertRaises(self.mod.ConfigError): + self.mod.cmd_workspace(argparse.Namespace( + action="add", name=name, config=str(CONFIG_JSON), + profile=None, state=state, no_apply=True)) + with mock.patch("pwd.getpwnam", return_value=object()): + with self.assertRaises(self.mod.ConfigError): + self.mod.cmd_workspace(argparse.Namespace( + action="add", name="nobody", config=str(CONFIG_JSON), + profile=None, state=state, no_apply=True)) + self.assertFalse(Path(state).exists()) + + +class WorkspaceWebHashTest(unittest.TestCase): + """A new web user gets a hash before apply lists it (CodeRabbit, #1091).""" + + def test_copies_root_hash_once_and_never_overwrites(self): + mod = load_agentbox() + spec = mock.Mock(web_enable=True) + spec.root_user.name = "agent" + with tempfile.TemporaryDirectory() as tmp: + (Path(tmp) / "agent.hash").write_text("HASH\n") + mod.ensure_web_hash(spec, "research", etc=tmp) + new = Path(tmp) / "research.hash" + self.assertEqual(new.read_text(), "HASH\n") + self.assertEqual(new.stat().st_mode & 0o777, 0o600) + new.write_text("OWN\n") + mod.ensure_web_hash(spec, "research", etc=tmp) + self.assertEqual(new.read_text(), "OWN\n") + + def test_refuses_without_a_source_hash_and_skips_when_web_off(self): + mod = load_agentbox() + spec = mock.Mock(web_enable=True) + spec.root_user.name = "agent" + with tempfile.TemporaryDirectory() as tmp: + with self.assertRaises(mod.ConfigError): + mod.ensure_web_hash(spec, "research", etc=tmp) + spec.web_enable = False + mod.ensure_web_hash(spec, "research", etc=tmp) + self.assertFalse((Path(tmp) / "research.hash").exists()) + + class ApplyActivationTest(unittest.TestCase): """What `agentbox apply` does when a host command says no (issue #526).