Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<file>` | Flags config files that are group/world readable. | Run `./install.sh config` or `chmod 600 <file>`. |
| `perms:<file>` | Flags config files that are group/world readable. | Run `./install.sh config --harden` (or `chmod 600 <file>`). |
| `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 <tool>` to back up and migrate to `lpt-*`. |

Expand Down
15 changes: 15 additions & 0 deletions lpt_installer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from lpt_installer.configs import (
CONFIG_TEMPLATES,
ensure_configs,
harden_existing_configs,
open_in_editor,
validate_configs,
)
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions lpt_installer/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
2 changes: 1 addition & 1 deletion lpt_installer/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_lpt_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()