fix(install): a fresh install creates ntfy accounts without prompts (database created first; phone uses the web password)
This commit is contained in:
@@ -34,12 +34,15 @@ def save_token(output, path, mode=0o444):
|
|||||||
return path
|
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.
|
"""Create the pipeline-publisher and phone-reader accounts on an ntfy server.
|
||||||
|
|
||||||
``run(args, capture=False, interactive=False)`` executes the ntfy CLI (directly
|
``run(args, capture=False, interactive=False, password=None)`` executes the ntfy
|
||||||
or through Compose) and returns the completed process. Existing accounts and an
|
CLI (directly or through Compose) and returns the completed process; a given
|
||||||
existing token file are preserved on reinstall.
|
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)
|
token_path = Path(token_path)
|
||||||
if token_path.is_symlink():
|
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:
|
if token_path.exists() and token_path.stat().st_size:
|
||||||
log("Keeping existing ntfy publishing token.")
|
log("Keeping existing ntfy publishing token.")
|
||||||
else:
|
else:
|
||||||
log("Set a password for ntfy's pipeline-publisher account (not a model-provider account).")
|
if publisher_password is None:
|
||||||
run(["user", "add", "--ignore-exists", "pipeline-publisher"], interactive=True)
|
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"])
|
run(["access", "pipeline-publisher", "pipelines", "write-only"])
|
||||||
result = run(["token", "add", "pipeline-publisher"], capture=True)
|
result = run(["token", "add", "pipeline-publisher"], capture=True)
|
||||||
save_token(result.stdout + result.stderr, token_path, mode)
|
save_token(result.stdout + result.stderr, token_path, mode)
|
||||||
# --ignore-exists preserves the phone account's password on reinstall.
|
# --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.")
|
if reader_password is None:
|
||||||
run(["user", "add", "--ignore-exists", "phone-reader"], interactive=True)
|
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"])
|
run(["access", "phone-reader", "pipelines", "read-only"])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+44
-7
@@ -20,6 +20,7 @@ import platform
|
|||||||
import getpass
|
import getpass
|
||||||
import plistlib
|
import plistlib
|
||||||
import secrets
|
import secrets
|
||||||
|
import time
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -61,10 +62,12 @@ DEFAULT_BIND_ADDRESS = "127.0.0.1"
|
|||||||
|
|
||||||
|
|
||||||
class Setup:
|
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.directory = Path(directory)
|
||||||
self.prefix_argument = prefix
|
self.prefix_argument = prefix
|
||||||
self.runner = runner
|
self.runner = runner
|
||||||
|
self.popen = popen
|
||||||
self.resolver = resolver or release_lookup.resolve_ntfy
|
self.resolver = resolver or release_lookup.resolve_ntfy
|
||||||
self.environ = environ if environ is not None else os.environ
|
self.environ = environ if environ is not None else os.environ
|
||||||
|
|
||||||
@@ -543,22 +546,54 @@ class Setup:
|
|||||||
|
|
||||||
# --- ntfy users and publishing token --------------------------------
|
# --- 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
|
# The account subcommands locate the server configuration through
|
||||||
# NTFY_CONFIG_FILE; --config is a `serve`-only flag.
|
# NTFY_CONFIG_FILE; --config is a `serve`-only flag.
|
||||||
env = dict(self.environ)
|
env = dict(self.environ)
|
||||||
env["NTFY_CONFIG_FILE"] = str(config_path)
|
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)
|
result = self.runner([str(prefix / "bin/ntfy"), *args], capture_output=capture, text=True, env=env)
|
||||||
if result.returncode:
|
if result.returncode:
|
||||||
step = " ".join(args[:2])
|
step = " ".join(args[:2])
|
||||||
raise ValueError(f"ntfy {step} failed; the native install is incomplete")
|
raise ValueError(f"ntfy {step} failed; the native install is incomplete")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def bootstrap_ntfy(self, prefix, config_path):
|
def ensure_ntfy_auth_db(self, prefix, config_path):
|
||||||
def run_ntfy(args, capture=False, interactive=False):
|
"""ntfy creates its account database the first time its server runs;
|
||||||
return self.ntfy(prefix, config_path, list(args), capture=capture)
|
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 ------------------------------------------------
|
# --- Docker migration ------------------------------------------------
|
||||||
|
|
||||||
@@ -726,7 +761,8 @@ class Setup:
|
|||||||
print("Starting empty (--fresh); existing Docker volumes are left untouched.")
|
print("Starting empty (--fresh); existing Docker volumes are left untouched.")
|
||||||
else:
|
else:
|
||||||
self.migrate_docker(prefix, args.replace_data)
|
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
|
timezone = args.timezone
|
||||||
if not timezone:
|
if not timezone:
|
||||||
try:
|
try:
|
||||||
@@ -793,6 +829,7 @@ class Setup:
|
|||||||
"Check your login service manager and that ports "
|
"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).")
|
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(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}/ "
|
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).")
|
f"(sign-in password in {web['auth_file']}; existing credentials retained).")
|
||||||
print("Login autostart is already enabled. To enable/start it again, run:\n "
|
print("Login autostart is already enabled. To enable/start it again, run:\n "
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import subprocess
|
||||||
import importlib.util
|
import importlib.util
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -68,5 +69,21 @@ class TokenTests(unittest.TestCase):
|
|||||||
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
@@ -29,6 +29,24 @@ def resolution():
|
|||||||
"version": NTFY_VERSION, "tag": "v" + NTFY_VERSION}
|
"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:
|
class Recorder:
|
||||||
def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest",
|
def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest",
|
||||||
fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False,
|
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
|
# A disposable XDG config home keeps the client default out of the real
|
||||||
# user configuration during tests.
|
# user configuration during tests.
|
||||||
return setup_native.Setup(directory, prefix=Path(directory) / "prefix", runner=recorder,
|
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"),
|
environ={"XDG_CONFIG_HOME": str(Path(directory) / "config"),
|
||||||
"PIPELINES_WEB_PASSWORD": "chosen-password"})
|
"PIPELINES_WEB_PASSWORD": "chosen-password"})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user