From 0e2d0882fbaccd1dc23598258f243fcc3ba84fb1 Mon Sep 17 00:00:00 2001 From: Hakuna <35168804+TrezorHannes@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:19:14 +0100 Subject: [PATCH] fix(cli): implement ./install.sh config --harden flag - Add harden_existing_configs() in lpt_installer/configs.py to tighten chmod 0600 on existing config files and 0700 on logs/ and data/ without copying missing *.example templates or validating unrelated tools. - Wire --harden into ./install.sh config, clarify doctor's suggestion to './install.sh config --harden', and document the flag in docs/CONFIGURATION.md and docs/TROUBLESHOOTING.md. - Add unit test covering --harden and --harden --dry-run (227/227 passing). --- docs/CONFIGURATION.md | 3 +++ docs/TROUBLESHOOTING.md | 2 +- lpt_installer/cli.py | 15 +++++++++++++ lpt_installer/configs.py | 26 +++++++++++++++++++++++ lpt_installer/env.py | 2 +- tests/test_lpt_installer.py | 42 +++++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 2 deletions(-) diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index c8914b6..59ffc2f 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -8,6 +8,9 @@ All user configuration files containing credentials, pubkeys, or node rules are # Copy missing templates, set chmod 600 on configs (chmod 700 on logs/ & data/), and validate ./install.sh config +# Only tighten chmod 600/700 permissions on existing configs without copying templates or validating all tools +./install.sh config --harden + # Limit to specific tools and open config.ini in $VISUAL / $EDITOR / nano ./install.sh config --tools fee-adjuster pocketmoney --edit config.ini ``` diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index 28c0226..a392a2d 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -13,7 +13,7 @@ Run the built-in diagnostic command first whenever something fails to install, v | `python` | Host Python must be `3.11`, `3.12`, or `3.13`. | Install Python 3.11–3.13 (`sudo apt-get install -y python3 python3-venv`). | | `venv-certifi` | Checks `.venv` exists and `certifi >= 2026.10.19` (`requirements.txt` requires `>= 2026.11.20`). | Run `./install.sh env` (or `./install.sh env --recreate`). | | `ca-bundle` | Verifies an OS CA certificate bundle exists (`/etc/ssl/certs/ca-certificates.crt`, `/etc/pki/tls/certs/ca-bundle.crt`, or `/etc/ssl/cert.pem`). | On Debian/Ubuntu, run `sudo apt-get install -y ca-certificates`. | -| `perms:` | Flags config files that are group/world readable. | Run `./install.sh config` or `chmod 600 `. | +| `perms:` | Flags config files that are group/world readable. | Run `./install.sh config --harden` (or `chmod 600 `). | | `ownership` | Flags root-owned files in `logs/` or `data/` (common after switching `fee_adjuster` from `User=root` to `User=admin`). | Fix with `sudo chown "$USER:$USER" logs/* data/*`. | | `legacy-units` | Detects pre-existing unprefixed systemd units (`fee_adjuster.*`, `pocketmoney.*`, etc.). | Run `./install.sh install ` to back up and migrate to `lpt-*`. | diff --git a/lpt_installer/cli.py b/lpt_installer/cli.py index 501e278..65de93a 100644 --- a/lpt_installer/cli.py +++ b/lpt_installer/cli.py @@ -18,6 +18,7 @@ from lpt_installer.configs import ( CONFIG_TEMPLATES, ensure_configs, + harden_existing_configs, open_in_editor, validate_configs, ) @@ -122,6 +123,11 @@ def build_parser() -> argparse.ArgumentParser: ) cfg_p.add_argument("--tools", nargs="*", help="Limit config check/copy to specific tool IDs.") cfg_p.add_argument("--edit", metavar="FILE", help="Open config file in $EDITOR after copying.") + cfg_p.add_argument( + "--harden", + action="store_true", + help="Only enforce 0600/0700 permissions on existing configs and logs/data without copying templates or validating.", + ) cfg_p.add_argument("--dry-run", action="store_true", help="Preview without modifying files.") inst_p = sub.add_parser("install", parents=[shared], help="Install and schedule one or more scripts.") @@ -391,6 +397,15 @@ def main( return 0 if args.command == "config": + if args.harden: + harden_summary = harden_existing_configs(repo_root, dry_run=args.dry_run) + if args.json: + output_fn(json.dumps(harden_summary, indent=2)) + else: + output_fn( + f"Hardened configs: {harden_summary['hardened']} | dirs: {harden_summary['hardened_dirs']}" + ) + return 0 try: selected = [get_tool(tid) for tid in args.tools] if args.tools else list(TOOLS) except KeyError as exc: diff --git a/lpt_installer/configs.py b/lpt_installer/configs.py index 249f554..2a4d7fa 100644 --- a/lpt_installer/configs.py +++ b/lpt_installer/configs.py @@ -94,6 +94,32 @@ def ensure_configs( return {"copied": copied, "preserved": preserved, "hardened": hardened} +def harden_existing_configs( + repo_root: Path, + *, + dry_run: bool = False, +) -> dict[str, list[str]]: + """Enforce 0600/0700 permissions on existing config files and logs/data without copying templates.""" + hardened: list[str] = [] + hardened_dirs: list[str] = [] + + for rel_path in CONFIG_TEMPLATES: + dst = repo_root / rel_path + if dst.is_file(): + hardened.append(rel_path) + if not dry_run: + os.chmod(dst, 0o600) + + for dir_name in ("logs", "data"): + dir_path = repo_root / dir_name + if dir_path.is_dir(): + hardened_dirs.append(dir_name) + if not dry_run: + os.chmod(dir_path, 0o700) + + return {"hardened": hardened, "hardened_dirs": hardened_dirs} + + def validate_configs(repo_root: Path, tools: list[ToolSpec]) -> list[str]: """Validate required config files, INI keys, schedules, and JSON syntax.""" errors: list[str] = [] diff --git a/lpt_installer/env.py b/lpt_installer/env.py index 4e68b1e..a21f658 100644 --- a/lpt_installer/env.py +++ b/lpt_installer/env.py @@ -184,7 +184,7 @@ def run_doctor(repo_root: Path, env: SystemEnv) -> list[dict[str, str]]: { "name": f"perms:{rel_path}", "status": "warn", - "detail": f"{rel_path} mode {oct(mode)} is group/world readable; run 'config --harden'", + "detail": f"{rel_path} mode {oct(mode)} is group/world readable; run './install.sh config --harden'", } ) else: diff --git a/tests/test_lpt_installer.py b/tests/test_lpt_installer.py index 708189b..43f2ee1 100644 --- a/tests/test_lpt_installer.py +++ b/tests/test_lpt_installer.py @@ -706,3 +706,45 @@ def systemctl(self, *args: str, use_sudo: bool = True, check: bool = True) -> Co assert cli_main(["--repo-root", str(repo), "config", "--tools", "no-such-tool"], env=env, output_fn=out.append) == 1 assert cli_main(["--repo-root", str(repo), "uninstall", "no-such-tool"], env=env, output_fn=out.append) == 1 + +def test_cli_config_harden_tightens_existing_without_copying_templates(tmp_path): + repo = _seed_repo(tmp_path, valid_configs=False) + (repo / "config.ini").write_text("[credentials]\n", encoding="utf-8") + (repo / "wallets.json").write_text("{}", encoding="utf-8") + (repo / "config.ini").chmod(0o644) + (repo / "wallets.json").chmod(0o644) + (repo / "logs").mkdir(exist_ok=True) + (repo / "logs").chmod(0o755) + + env = FakeEnv(tmp_path) + out: list[str] = [] + + # --dry-run must not mutate permissions or copy missing templates + assert ( + cli_main( + ["--repo-root", str(repo), "config", "--harden", "--dry-run"], + env=env, + output_fn=out.append, + ) + == 0 + ) + assert stat.S_IMODE((repo / "config.ini").stat().st_mode) == 0o644 + assert not (repo / "Magma" / "magma_config.ini").exists() + assert not (repo / "feeConfig.json").exists() + + # Live --harden tightens existing files/dirs to 0600/0700 without copying missing templates + out.clear() + assert ( + cli_main( + ["--repo-root", str(repo), "config", "--harden"], + env=env, + output_fn=out.append, + ) + == 0 + ) + assert stat.S_IMODE((repo / "config.ini").stat().st_mode) == 0o600 + assert stat.S_IMODE((repo / "wallets.json").stat().st_mode) == 0o600 + assert stat.S_IMODE((repo / "logs").stat().st_mode) == 0o700 + assert not (repo / "Magma" / "magma_config.ini").exists() + assert not (repo / "feeConfig.json").exists() +