From 3d0c034394754ac93ef07f0a33fb046a1e734e87 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 23:38:51 +0100 Subject: [PATCH] fix(install): a fresh install creates ntfy accounts without prompts (database created first; phone uses the web password) --- scripts/save-ntfy-token.py | 25 ++++++++++------ scripts/setup-native.py | 51 ++++++++++++++++++++++++++++----- scripts/test_save_ntfy_token.py | 17 +++++++++++ scripts/test_setup_native.py | 20 ++++++++++++- 4 files changed, 97 insertions(+), 16 deletions(-) diff --git a/scripts/save-ntfy-token.py b/scripts/save-ntfy-token.py index 42ea320a..d4b781ac 100644 --- a/scripts/save-ntfy-token.py +++ b/scripts/save-ntfy-token.py @@ -34,12 +34,15 @@ def save_token(output, path, mode=0o444): return path -def bootstrap_accounts(run, token_path, mode=0o444, log=print): +def bootstrap_accounts(run, token_path, mode=0o444, log=print, publisher_password=None, + reader_password=None): """Create the pipeline-publisher and phone-reader accounts on an ntfy server. - ``run(args, capture=False, interactive=False)`` executes the ntfy CLI (directly - or through Compose) and returns the completed process. Existing accounts and an - existing token file are preserved on reinstall. + ``run(args, capture=False, interactive=False, password=None)`` executes the ntfy + CLI (directly or through Compose) and returns the completed process; a given + password is supplied without a prompt. Without one the CLI asks on the + terminal. Existing accounts and an existing token file are preserved on + reinstall. """ token_path = Path(token_path) if token_path.is_symlink(): @@ -47,14 +50,20 @@ def bootstrap_accounts(run, token_path, mode=0o444, log=print): if token_path.exists() and token_path.stat().st_size: log("Keeping existing ntfy publishing token.") else: - log("Set a password for ntfy's pipeline-publisher account (not a model-provider account).") - run(["user", "add", "--ignore-exists", "pipeline-publisher"], interactive=True) + if publisher_password is None: + log("Set a password for ntfy's pipeline-publisher account (not a model-provider account).") + run(["user", "add", "--ignore-exists", "pipeline-publisher"], interactive=True) + else: + run(["user", "add", "--ignore-exists", "pipeline-publisher"], password=publisher_password) run(["access", "pipeline-publisher", "pipelines", "write-only"]) result = run(["token", "add", "pipeline-publisher"], capture=True) save_token(result.stdout + result.stderr, token_path, mode) # --ignore-exists preserves the phone account's password on reinstall. - log("Set the phone-reader password if this is a new ntfy account; existing accounts are preserved.") - run(["user", "add", "--ignore-exists", "phone-reader"], interactive=True) + if reader_password is None: + log("Set the phone-reader password if this is a new ntfy account; existing accounts are preserved.") + run(["user", "add", "--ignore-exists", "phone-reader"], interactive=True) + else: + run(["user", "add", "--ignore-exists", "phone-reader"], password=reader_password) run(["access", "phone-reader", "pipelines", "read-only"]) diff --git a/scripts/setup-native.py b/scripts/setup-native.py index c0b7bbe2..340b86d6 100644 --- a/scripts/setup-native.py +++ b/scripts/setup-native.py @@ -20,6 +20,7 @@ import platform import getpass import plistlib import secrets +import time import shlex import shutil import subprocess @@ -61,10 +62,12 @@ DEFAULT_BIND_ADDRESS = "127.0.0.1" class Setup: - def __init__(self, directory, prefix=None, runner=subprocess.run, resolver=None, environ=None): + def __init__(self, directory, prefix=None, runner=subprocess.run, resolver=None, environ=None, + popen=subprocess.Popen): self.directory = Path(directory) self.prefix_argument = prefix self.runner = runner + self.popen = popen self.resolver = resolver or release_lookup.resolve_ntfy self.environ = environ if environ is not None else os.environ @@ -543,22 +546,54 @@ class Setup: # --- ntfy users and publishing token -------------------------------- - def ntfy(self, prefix, config_path, args, capture=False): + def ntfy(self, prefix, config_path, args, capture=False, password=None): # The account subcommands locate the server configuration through # NTFY_CONFIG_FILE; --config is a `serve`-only flag. env = dict(self.environ) env["NTFY_CONFIG_FILE"] = str(config_path) + if password is not None: + # `ntfy user add` takes the password from here instead of a prompt. + env["NTFY_PASSWORD"] = password result = self.runner([str(prefix / "bin/ntfy"), *args], capture_output=capture, text=True, env=env) if result.returncode: step = " ".join(args[:2]) raise ValueError(f"ntfy {step} failed; the native install is incomplete") return result - def bootstrap_ntfy(self, prefix, config_path): - def run_ntfy(args, capture=False, interactive=False): - return self.ntfy(prefix, config_path, list(args), capture=capture) + def ensure_ntfy_auth_db(self, prefix, config_path): + """ntfy creates its account database the first time its server runs; + the account commands refuse to run before that, so a fresh install + runs it once.""" + auth = config_path.parent / "auth.db" + if auth.exists(): + return + env = dict(self.environ) + process = self.popen([str(prefix / "bin/ntfy"), "serve", "--config", str(config_path)], env=env) + try: + deadline = time.monotonic() + 20 + while not auth.exists(): + if process.poll() is not None or time.monotonic() > deadline: + raise ValueError("ntfy did not create its account database; is its port free?") + time.sleep(0.2) + finally: + if process.poll() is None: + process.terminate() + try: + process.wait(timeout=10) + except subprocess.TimeoutExpired: + process.kill() + process.wait() - tokens.bootstrap_accounts(run_ntfy, prefix / "secrets/publisher-token", 0o600) + def bootstrap_ntfy(self, prefix, config_path, reader_password=None): + def run_ntfy(args, capture=False, interactive=False, password=None): + return self.ntfy(prefix, config_path, list(args), capture=capture, password=password) + + self.ensure_ntfy_auth_db(prefix, config_path) + # The publisher authenticates with its token; its password is never + # needed, so it is random. The phone signs in with the web password. + tokens.bootstrap_accounts(run_ntfy, prefix / "secrets/publisher-token", 0o600, + publisher_password=secrets.token_urlsafe(24), + reader_password=reader_password) # --- Docker migration ------------------------------------------------ @@ -726,7 +761,8 @@ class Setup: print("Starting empty (--fresh); existing Docker volumes are left untouched.") else: self.migrate_docker(prefix, args.replace_data) - self.bootstrap_ntfy(prefix, config_path) + self.bootstrap_ntfy(prefix, config_path, + reader_password=json.loads(Path(web["auth_file"]).read_text())["password"]) timezone = args.timezone if not timezone: try: @@ -793,6 +829,7 @@ class Setup: "Check your login service manager and that ports " f"{args.port}, {args.ntfy_port} and {args.web_port} are free (a running Docker installation may use them).") print(f"ntfy is configured for {base_url}; server, web proxy and ntfy have login autostart ({service_path}).") + print("Phone notifications: subscribe to the 'pipelines' topic as phone-reader with the web password.") print(f"Web: http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}/ " f"(sign-in password in {web['auth_file']}; existing credentials retained).") print("Login autostart is already enabled. To enable/start it again, run:\n " diff --git a/scripts/test_save_ntfy_token.py b/scripts/test_save_ntfy_token.py index ad0d2aac..fbe74784 100644 --- a/scripts/test_save_ntfy_token.py +++ b/scripts/test_save_ntfy_token.py @@ -1,3 +1,4 @@ +import subprocess import importlib.util from pathlib import Path import tempfile @@ -68,5 +69,21 @@ class TokenTests(unittest.TestCase): self.assertEqual(path.stat().st_mode & 0o777, 0o600) +class NonInteractiveAccounts(unittest.TestCase): + def test_given_passwords_are_used_without_prompts(self): + calls = [] + + def run(args, capture=False, interactive=False, password=None): + calls.append((tuple(args), interactive, password)) + out = "token tk_abcdefghijklmnopqrstuvwxyz123 created" if args[0] == "token" else "" + return subprocess.CompletedProcess(args, 0, stdout=out, stderr="") + + with tempfile.TemporaryDirectory() as directory: + save_token.bootstrap_accounts(run, Path(directory) / "token", 0o600, log=lambda _m: None, + publisher_password="p1", reader_password="p2") + adds = [(args[3], interactive, password) for args, interactive, password in calls if args[:2] == ("user", "add")] + self.assertEqual(adds, [("pipeline-publisher", False, "p1"), ("phone-reader", False, "p2")]) + + if __name__ == "__main__": unittest.main() diff --git a/scripts/test_setup_native.py b/scripts/test_setup_native.py index ae2342b0..e5c41be9 100644 --- a/scripts/test_setup_native.py +++ b/scripts/test_setup_native.py @@ -29,6 +29,24 @@ def resolution(): "version": NTFY_VERSION, "tag": "v" + NTFY_VERSION} +class _Served: + def poll(self): + return None + + def terminate(self): + pass + + def wait(self, timeout=None): + return 0 + + +def fake_ntfy_serve(command, **kwargs): + """`ntfy serve` creates its account database next to its configuration.""" + config = Path(command[command.index("--config") + 1]) + (config.parent / "auth.db").write_bytes(b"") + return _Served() + + class Recorder: def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest", fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False, @@ -115,7 +133,7 @@ class SetupNativeTests(unittest.TestCase): # A disposable XDG config home keeps the client default out of the real # user configuration during tests. return setup_native.Setup(directory, prefix=Path(directory) / "prefix", runner=recorder, - resolver=resolution, + resolver=resolution, popen=fake_ntfy_serve, environ={"XDG_CONFIG_HOME": str(Path(directory) / "config"), "PIPELINES_WEB_PASSWORD": "chosen-password"})