From bb32383bbec72a9b62fa259258f1b711870c7aa1 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 20 Oct 2026 21:41:05 +0100 Subject: [PATCH] Route native service commands and configure fresh authoring role Select native installs only via explicit PIPELINES_PREFIX or the matching owned prefix/bin/pipelines executable. Route enable/status/disable to the existing native supervisor; validate registration ownership, label and command, keep preview read-only, and disable login registration before stopping. Never fall back to a standalone service for an invalid explicit prefix. Fresh native installs explicitly enable authoring, while reinstalls preserve false or missing legacy role settings. Add explicit --allow-authoring/--no-authoring overrides and pass the server flag only for manifest true. Print a copyable native enable command and update installation help/docs. No Docker configuration changes. Checks: 54 mocked native installer/lifecycle tests; 8 entrypoint tests; offline Go installation/CLI tests and vet; gofmt clean; shell syntax and diff checks. No live install, restart, deployment, permission/config changes, or native Linux verification. --- docs/install.md | 41 +++++-- internal/installation/installation.go | 32 +++++- internal/installation/installation_test.go | 97 ++++++++++++++++ internal/installation/owner_other.go | 8 ++ internal/installation/owner_unix.go | 13 +++ internal/installation/service.go | 22 +++- internal/installation/service_test.go | 40 +++++++ scripts/manage.py | 82 +++++++++++++- scripts/setup-native.py | 26 +++++ scripts/test_manage_native.py | 124 +++++++++++++++++++++ scripts/test_setup_native.py | 41 +++++++ 11 files changed, 508 insertions(+), 18 deletions(-) create mode 100644 internal/installation/owner_other.go create mode 100644 internal/installation/owner_unix.go diff --git a/docs/install.md b/docs/install.md index 7b23adb5..6cff9513 100644 --- a/docs/install.md +++ b/docs/install.md @@ -49,6 +49,19 @@ a private 0600 file there with `password` and a cookie-signing `secret`; read it non-private existing file stops setup instead of resetting credentials. The service never inherits the test-only `PIPELINES_WEB_AUTH=disabled` bypass. +A **fresh** native installation records `allow_authoring: true` in its manifest +and explicitly starts Go with `-allow-authoring`, so the single-user web workflow +can create and publish pipelines. `./install --no-authoring` instead selects an +execution-only role. Reinstalls preserve the recorded role; an older manifest +without this field stays execution-only. To deliberately enable authoring for +such an installation, use `./install --allow-authoring`. The installer prints the +selected role. This is a server permission, not a browser authentication bypass; +the authenticated proxy and loopback API defaults still apply. Docker's role and +startup flags are unchanged. +Role changes apply on the supervisor's next start. A reinstall does not forcibly +restart an already-running supervisor; if reconfiguring an active installation, +use its `pipelines restart` only when no work is active. + Setup resolves `PIPELINES_PREFIX` or defaults to `~/.local/share/pipelines`, creates `bin/`, `lib/`, `data/`, `logs/` and `secrets/` (`secrets` and `data` at 0700), copies `scripts/manage.py` to `/lib/manage.py`, builds exactly @@ -107,12 +120,17 @@ present but its volumes cannot be inspected (for example a stopped daemon), setu fails clearly instead of silently skipping migration, so an existing Docker database is never shadowed by an empty native start. -Setup already starts it and enables login autostart. Optional CLI management -requires the prefix selector (the executable path alone does not select it): +Setup already starts it and enables login autostart, and prints a copyable +`/bin/pipelines service enable` command. That installed executable safely +selects its own native manifest; an unrelated CLI never guesses a default prefix. +Optional terminal use needs only the installed binary on PATH: ```sh -export PIPELINES_PREFIX="$HOME/.local/share/pipelines" -export PATH="$PIPELINES_PREFIX/bin:$PATH" +export PATH="$HOME/.local/share/pipelines/bin:$PATH" +pipelines service enable # idempotently enable/start this native install +pipelines service enable --print # read-only existing definition preview +pipelines service status # login registration state; no credentials +pipelines service disable # disable login autostart AND stop its supervisor pipelines doctor # pipelines stop / start / restart / logs ``` @@ -130,9 +148,12 @@ first; the migration still needs its volumes and image). ntfy runs from `$XDG_CONFIG_HOME/systemd/user/net.a-wan.pipelines.service` (default `~/.config`) and runs `systemctl --user daemon-reload` then `enable --now net.a-wan.pipelines.service`. This is login autostart, not boot-without-login; -Linux lingering is an explicit administrator decision. Do **not** run the separate -`pipelines service enable` for this installation: it configures a standalone -developer server, not the manifest database/ntfy/web supervisor. Logs are in +Linux lingering is an explicit administrator decision. `pipelines service` uses +this existing supervisor and database, never a second developer server. An +explicit `PIPELINES_PREFIX` can also select it from another CLI. Native service +commands reject standalone `--db`/`--addr` options; use installer flags to change +the manifest. Definitions belonging to a developer service or another native +prefix are never overwritten by these commands. Logs are in `/logs/{server,ntfy,web,supervisor}.log` (Linux supervisor output also goes to `journalctl --user -u net.a-wan.pipelines.service`). @@ -370,8 +391,8 @@ Drafts use the OS user cache; `PIPELINES_DRAFT_DIR` overrides that location. ## Developer/legacy autostart (unsupported) -Stop any existing foreground or legacy server first, then explicitly configure -the OS user service: +For a standalone developer binary (not the CLI inside a native install), stop any +existing foreground or legacy server first, then explicitly configure the OS user service: ```sh pipelines service enable @@ -482,7 +503,7 @@ does not purge data. The previous Python-supervised setup is retained only for explicit legacy use. Set `PIPELINES_PREFIX` to its existing directory for its lifecycle commands; -no command infers that directory from an executable location. The old +legacy manifests are never inferred from an executable location. The old `--system-command`, provisioning, and autostart installer flags have been removed. To migrate, stop the old server first, install normal Go binaries, and start with diff --git a/internal/installation/installation.go b/internal/installation/installation.go index cc5b7053..32f0ded4 100644 --- a/internal/installation/installation.go +++ b/internal/installation/installation.go @@ -17,13 +17,22 @@ type Config struct { OpenCode string `json:"opencode"` OpenCodeEnv map[string]string `json:"opencode_env"` Path string `json:"path"` + Ntfy struct { + Mode string `json:"mode"` + } `json:"ntfy"` } -// Load selects a legacy managed installation only via an explicit PIPELINES_PREFIX. +// Load selects an explicit prefix, or a native installation containing this exact +// executable. It never guesses a default prefix for an unrelated Go-installed CLI. func Load(executable string) (*Config, error) { prefix := os.Getenv("PIPELINES_PREFIX") + inferred := prefix == "" if prefix == "" { - return nil, nil + resolved, err := filepath.EvalSymlinks(executable) + if err != nil || filepath.Base(resolved) != "pipelines" || filepath.Base(filepath.Dir(resolved)) != "bin" { + return nil, nil + } + prefix = filepath.Dir(filepath.Dir(resolved)) } prefix, err := filepath.Abs(prefix) if err != nil { @@ -35,13 +44,21 @@ func Load(executable string) (*Config, error) { if resolved, err := filepath.EvalSymlinks(prefix); err == nil { prefix = resolved } - data, err := os.ReadFile(filepath.Join(prefix, "pipelines-install.json")) + manifest := filepath.Join(prefix, "pipelines-install.json") + info, err := os.Lstat(manifest) if os.IsNotExist(err) { return nil, nil } if err != nil { return nil, err } + if !info.Mode().IsRegular() || !ownedByUser(info) { + return nil, fmt.Errorf("refusing installation manifest not a regular file owned by this user") + } + data, err := os.ReadFile(manifest) + if err != nil { + return nil, err + } var config Config if err = json.Unmarshal(data, &config); err != nil { return nil, err @@ -49,6 +66,15 @@ func Load(executable string) (*Config, error) { if config.Format != 1 || config.Prefix != prefix || config.ServerPort < 1024 || config.ServerPort > 65535 { return nil, fmt.Errorf("invalid managed installation manifest") } + if inferred && config.Ntfy.Mode != "native" { + return nil, nil + } + if inferred { + binary, err := os.Stat(filepath.Join(prefix, "bin", "pipelines")) + if err != nil || !binary.Mode().IsRegular() || binary.Mode().Perm()&0111 == 0 || !ownedByUser(binary) { + return nil, fmt.Errorf("native installation executable must be owned by this user") + } + } return &config, nil } diff --git a/internal/installation/installation_test.go b/internal/installation/installation_test.go index 7e31c2f8..c9af0f61 100644 --- a/internal/installation/installation_test.go +++ b/internal/installation/installation_test.go @@ -48,3 +48,100 @@ func TestStandaloneGoInstalledCommandHasNoServiceDependency(t *testing.T) { t.Fatal(config, err) } } + +func nativeFixture(t *testing.T) (string, string) { + t.Helper() + root, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + if err = os.Mkdir(filepath.Join(root, "bin"), 0700); err != nil { + t.Fatal(err) + } + binary := filepath.Join(root, "bin", "pipelines") + if err = os.WriteFile(binary, []byte("binary"), 0700); err != nil { + t.Fatal(err) + } + config := Config{Format: 1, Prefix: root, ServerPort: 18080, Python: filepath.Join(root, "python3")} + config.Ntfy.Mode = "native" + data, _ := json.Marshal(config) + if err = os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600); err != nil { + t.Fatal(err) + } + return root, binary +} + +func TestNativeExecutableDiscoveryAndExplicitPrefixPrecedence(t *testing.T) { + t.Setenv("PIPELINES_PREFIX", "") + root, binary := nativeFixture(t) + link := filepath.Join(t.TempDir(), "pipelines") + if err := os.Symlink(binary, link); err != nil { + t.Fatal(err) + } + loaded, err := Load(link) + if err != nil || loaded == nil || loaded.Prefix != root { + t.Fatal("native executable not selected", loaded, err) + } + other, _ := nativeFixture(t) + t.Setenv("PIPELINES_PREFIX", other) + loaded, err = Load(link) + if err != nil || loaded == nil || loaded.Prefix != other { + t.Fatal("explicit selection lost precedence", loaded, err) + } +} + +func TestDiscoveryNeverGuessesHomeOrLegacyInstallation(t *testing.T) { + t.Setenv("PIPELINES_PREFIX", "") + root, binary := nativeFixture(t) + home := t.TempDir() + t.Setenv("HOME", home) + if err := os.MkdirAll(filepath.Join(home, ".local", "share"), 0700); err != nil { + t.Fatal(err) + } + if err := os.Symlink(root, filepath.Join(home, ".local", "share", "pipelines")); err != nil { + t.Fatal(err) + } + unrelated := filepath.Join(t.TempDir(), "pipelines") + if err := os.WriteFile(unrelated, []byte("binary"), 0700); err != nil { + t.Fatal(err) + } + loaded, err := Load(unrelated) + if err != nil || loaded != nil { + t.Fatal("unrelated executable selected installation", loaded, err) + } + config := Config{Format: 1, Prefix: root, ServerPort: 18080} + data, _ := json.Marshal(config) + os.WriteFile(filepath.Join(root, "pipelines-install.json"), data, 0600) + loaded, err = Load(binary) + if err != nil || loaded != nil { + t.Fatal("legacy manifest was inferred", loaded, err) + } + t.Setenv("PIPELINES_PREFIX", root) + loaded, err = Load(binary) + if err != nil || loaded == nil { + t.Fatal("explicit legacy selection stopped working", loaded, err) + } +} + +func TestNativeDiscoveryRejectsSymlinkManifestAndMismatchedPrefix(t *testing.T) { + for _, test := range []string{"symlink", "mismatch"} { + t.Run(test, func(t *testing.T) { + t.Setenv("PIPELINES_PREFIX", "") + root, binary := nativeFixture(t) + manifest := filepath.Join(root, "pipelines-install.json") + if test == "symlink" { + target := filepath.Join(root, "other.json") + os.Rename(manifest, target) + os.Symlink(target, manifest) + } else { + config := Config{Format: 1, Prefix: "/another/prefix", ServerPort: 18080} + config.Ntfy.Mode = "native" + data, _ := json.Marshal(config) + os.WriteFile(manifest, data, 0600) + } + if _, err := Load(binary); err == nil { + t.Fatal("unsafe manifest was accepted") + } + }) + } +} diff --git a/internal/installation/owner_other.go b/internal/installation/owner_other.go new file mode 100644 index 00000000..b0be4eeb --- /dev/null +++ b/internal/installation/owner_other.go @@ -0,0 +1,8 @@ +//go:build !darwin && !linux + +package installation + +import "os" + +// Native managed installations are supported only on macOS and Linux. +func ownedByUser(info os.FileInfo) bool { return false } diff --git a/internal/installation/owner_unix.go b/internal/installation/owner_unix.go new file mode 100644 index 00000000..bf51184c --- /dev/null +++ b/internal/installation/owner_unix.go @@ -0,0 +1,13 @@ +//go:build darwin || linux + +package installation + +import ( + "os" + "syscall" +) + +func ownedByUser(info os.FileInfo) bool { + stat, ok := info.Sys().(*syscall.Stat_t) + return ok && int(stat.Uid) == os.Getuid() +} diff --git a/internal/installation/service.go b/internal/installation/service.go index bc489906..efb37311 100644 --- a/internal/installation/service.go +++ b/internal/installation/service.go @@ -162,9 +162,27 @@ func ServerExecutable(executable string) (string, error) { return server, nil } -// ServiceCommand uses the OS service manager directly, without a second supervisor. +// ServiceCommand manages the selected native supervisor, or a standalone +// developer service when no native installation was selected. func ServiceCommand(ctx context.Context, executable string, args []string, out, stderr io.Writer) error { - help := "Usage: pipelines service enable [--db FILE] [--addr HOST:PORT] [--print]\n pipelines service disable | status\nAutostart is per-user, at login. enable starts the server now; --print only previews.\nRuns use the directory you invoke pipelines run from.\n" + config, err := Load(executable) + if err != nil { + return err + } + if config != nil && config.Ntfy.Mode == "native" { + // Use an internal verb so an older manager fails instead of forwarding + // "service" back into this CLI recursively. Reinstall refreshes manage.py. + command := exec.CommandContext(ctx, config.Python, append([]string{filepath.Join(config.Prefix, "lib/manage.py"), "--prefix", config.Prefix, "native-service"}, args...)...) + command.Stdout, command.Stderr = out, stderr + if err := command.Run(); err != nil { + return fmt.Errorf("native service command failed (rerun ./install if the manager is outdated): %w", err) + } + return nil + } + if config == nil && os.Getenv("PIPELINES_PREFIX") != "" { + return fmt.Errorf("PIPELINES_PREFIX does not select an existing installation; refusing to configure a standalone service") + } + help := "Usage: pipelines service enable [--print]\n pipelines service disable | status\nNative installs manage the existing server/web/ntfy supervisor, selected by PIPELINES_PREFIX or the installed CLI path.\nAutostart is per-user, at login. enable starts now; --print only previews.\nStandalone developer binaries also support enable --db FILE --addr HOST:PORT.\nRuns use the directory you invoke pipelines run from.\n" if len(args) == 0 || args[0] == "--help" || args[0] == "help" { _, err := io.WriteString(out, help) return err diff --git a/internal/installation/service_test.go b/internal/installation/service_test.go index 51556f29..2736914f 100644 --- a/internal/installation/service_test.go +++ b/internal/installation/service_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/xml" + "fmt" "io" "os" "os/exec" @@ -111,6 +112,7 @@ func TestServiceWritesRefuseUnmanagedFilesAndSymlinks(t *testing.T) { } func TestServiceConfigurationPreviewAndLifecycleWithMockOSManager(t *testing.T) { + t.Setenv("PIPELINES_PREFIX", "") if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { t.Skip("no supported OS manager") } @@ -184,3 +186,41 @@ func TestServiceConfigurationPreviewAndLifecycleWithMockOSManager(t *testing.T) } } } + +func TestNativeServiceRoutesToManifestSupervisor(t *testing.T) { + for _, explicit := range []bool{false, true} { + t.Run(fmt.Sprint(explicit), func(t *testing.T) { + t.Setenv("PIPELINES_PREFIX", "") + root, binary := nativeFixture(t) + capture := filepath.Join(root, "arguments") + t.Setenv("TEST_MANAGED_ARGS", capture) + if err := os.WriteFile(filepath.Join(root, "python3"), []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$TEST_MANAGED_ARGS\"\n"), 0700); err != nil { + t.Fatal(err) + } + if explicit { + t.Setenv("PIPELINES_PREFIX", root) + binary = filepath.Join(t.TempDir(), "unrelated-cli") + } + var out bytes.Buffer + for _, args := range [][]string{{"enable"}, {"enable", "--print"}, {"status"}, {"disable"}} { + if err := ServiceCommand(context.Background(), binary, args, &out, &out); err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(capture) + want := append([]string{filepath.Join(root, "lib/manage.py"), "--prefix", root, "native-service"}, args...) + if err != nil || string(data) != strings.Join(want, "\n")+"\n" { + t.Fatal("wrong managed dispatch", string(data), err) + } + } + }) + } +} + +func TestInvalidExplicitPrefixCannotCreateStandaloneService(t *testing.T) { + t.Setenv("PIPELINES_PREFIX", t.TempDir()) + var out bytes.Buffer + err := ServiceCommand(context.Background(), "/unrelated/pipelines", []string{"enable"}, &out, &out) + if err == nil || !strings.Contains(err.Error(), "refusing to configure a standalone service") { + t.Fatal(err) + } +} diff --git a/scripts/manage.py b/scripts/manage.py index 5934a784..3ee93ddd 100644 --- a/scripts/manage.py +++ b/scripts/manage.py @@ -6,6 +6,8 @@ import json import os from pathlib import Path import platform +import plistlib +import re import shutil import signal import subprocess @@ -15,6 +17,7 @@ import urllib.request DEFAULT_PREFIX = Path.home() / ".local/share/pipelines" MARKER = "pipelines-install.json" +SERVICE_MARKER = "Managed by Pipelines native installer" def ntfy_mode(config): @@ -105,6 +108,74 @@ def registration(config): return config.get("registration") +def service_command(config, arguments): + parser = argparse.ArgumentParser(prog="pipelines service", + description="Manage this native installation's server, authenticated web and ntfy login service.") + parser.add_argument("action", choices=("enable", "disable", "status")) + parser.add_argument("--print", dest="preview", action="store_true", + help="Preview enable's existing service definition without changing or starting anything") + if not arguments or arguments == ["help"]: + parser.print_help() + return + args = parser.parse_args(arguments) + if args.preview and args.action != "enable": + parser.error("--print is only supported with enable") + if ntfy_mode(config) != "native" or config.get("label") != "net.a-wan.pipelines" or not registration(config): + raise ValueError("No native installer login service is recorded; rerun ./install") + path = Path(registration(config)) + if platform.system() == "Darwin": + expected = Path.home() / "Library/LaunchAgents/net.a-wan.pipelines.plist" + elif platform.system() == "Linux": + root = Path(config.get("opencode_env", {}).get("XDG_CONFIG_HOME") or Path.home() / ".config") + expected = root / "systemd/user/net.a-wan.pipelines.service" + else: + raise ValueError("Native login services require macOS launchd or Linux systemd --user") + if (path != expected or path.is_symlink() or not path.is_file() + or path.stat().st_uid != os.getuid()): + raise ValueError("Refusing an unexpected native service definition path or owner") + content = path.read_text() + if SERVICE_MARKER not in content: + raise ValueError("Refusing an unmanaged or standalone developer service definition") + if platform.system() == "Darwin": + definition = plistlib.loads(content.encode()) + command = [config["python"], str(Path(config["prefix"]) / "lib/manage.py"), + "supervise", "--prefix", config["prefix"]] + if definition.get("Label") != config["label"] or definition.get("ProgramArguments") != command: + raise ValueError("Native service definition does not match this installation") + else: + def quote(value): + return '"' + value.replace('\\', '\\\\').replace('"', '\\"').replace('%', '%%').replace('$', '$$') + '"' + + command = [config["python"], str(Path(config["prefix"]) / "lib/manage.py"), + "supervise", "--prefix", config["prefix"]] + if "ExecStart=" + " ".join(map(quote, command)) not in content.splitlines(): + raise ValueError("Native service definition does not match this installation") + if args.preview: + print(content, end="") + elif args.action == "enable": + start(config) + print("Native login autostart enabled: server, authenticated web and ntfy.") + elif args.action == "disable": + if platform.system() == "Darwin": + run(["launchctl", "disable", f"gui/{os.getuid()}/{config['label']}"], quiet=True) + else: + run(["systemctl", "--user", "disable", config["label"] + ".service"], quiet=True) + stop(config) + print("Native login autostart disabled; supervisor stopped. Data and credentials retained.") + elif platform.system() == "Darwin": + domain = f"gui/{os.getuid()}" + disabled = run(["launchctl", "print-disabled", domain], quiet=True) + enabled = not re.search(r'"' + re.escape(config["label"]) + r'"\s*=>\s*true', disabled.stdout) + active = run(["launchctl", "print", domain + "/" + config["label"]], quiet=True, check=False).returncode == 0 + print("Autostart:", "enabled" if enabled else "disabled") + print("Supervisor:", "loaded" if active else "not loaded") + else: + enabled = run(["systemctl", "--user", "is-enabled", config["label"] + ".service"], quiet=True, check=False) + active = run(["systemctl", "--user", "is-active", config["label"] + ".service"], quiet=True, check=False) + print("Autostart:", "enabled" if enabled.returncode == 0 else "disabled") + print("Supervisor:", "active" if active.returncode == 0 else "inactive") + + def start(config): root = Path(config["prefix"]) if registration(config): @@ -235,10 +306,12 @@ def supervise(config): PIPELINES_NTFY_PUBLIC_URL=config["public_url"], PIPELINES_NTFY_TOKEN_FILE=str(root / "secrets/publisher-token")) env.pop("PIPELINES_NTFY_TOKEN", None) + server_command = [str(root / "bin/pipelines-server"), "-addr", + server_address(config), "-db", str(root / "data/pipelines.db")] + if config.get("allow_authoring") is True: + server_command.append("-allow-authoring") with (root / "logs/server.log").open("a") as log: - child = subprocess.Popen([str(root / "bin/pipelines-server"), "-addr", - server_address(config), "-db", str(root / "data/pipelines.db")], - cwd=config["workspace"], env=env, stdout=log, stderr=log) + child = subprocess.Popen(server_command, cwd=config["workspace"], env=env, stdout=log, stderr=log) if config.get("web"): web = config["web"] web_env = dict(env) @@ -325,6 +398,9 @@ def main(): selector.add_argument("--prefix", type=Path, default=DEFAULT_PREFIX) selected, forwarded = selector.parse_known_args() config = load(selected.prefix) + if forwarded and forwarded[0] in ("service", "native-service"): + service_command(config, forwarded[1:]) + return if not forwarded or forwarded[0] not in lifecycle: binary = str(Path(config["prefix"]) / "bin/pipelines") env = dict(os.environ) diff --git a/scripts/setup-native.py b/scripts/setup-native.py index 6045383d..288f6932 100644 --- a/scripts/setup-native.py +++ b/scripts/setup-native.py @@ -19,6 +19,7 @@ import os import platform import plistlib import secrets +import shlex import shutil import subprocess import sys @@ -227,6 +228,20 @@ class Setup: raw = self.prefix_argument or self.environ.get("PIPELINES_PREFIX") or default return Path(raw).expanduser().resolve() + def authoring_role(self, prefix, requested): + """Fresh single-user installs author; never widen an existing server's role.""" + if requested is not None: + return requested + path = prefix / MARKER + if not path.exists() and not path.is_symlink(): + return True + if path.is_symlink() or not path.is_file(): + raise ValueError("Refusing to read authoring role from a non-regular installation manifest") + existing = json.loads(path.read_text()) + if not isinstance(existing, dict) or not isinstance(existing.get("allow_authoring", False), bool): + raise ValueError("Invalid existing authoring role; no permission changes were made") + return existing.get("allow_authoring", False) + # --- prefix layout --------------------------------------------------- def prepare_layout(self, prefix): @@ -581,6 +596,7 @@ class Setup: except ValueError: raise ValueError("Invalid API/web bind address; use an IPv4 address such as 0.0.0.0 or 127.0.0.1") from None prefix = self.resolve_prefix() + allow_authoring = self.authoring_role(prefix, args.allow_authoring) harness = self.harness() service_path = self.service_path() # Reuse an already built ntfy binary and skip the release lookup, so a @@ -625,6 +641,7 @@ class Setup: path_value = str(prefix / "bin") + os.pathsep + self.environ.get("PATH", "") manifest = { "format": 1, + "allow_authoring": allow_authoring, "prefix": str(prefix), "python": python, "server_port": args.port, @@ -673,6 +690,10 @@ class Setup: print(f"ntfy is configured for {base_url}; server, web proxy and ntfy have login autostart ({service_path}).") 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 " + + shlex.join([str(prefix / "bin/pipelines"), "service", "enable"])) + print("Configured server role: " + ("single-user authoring enabled (web pipeline creation/publication allowed)." + if allow_authoring else "execution-only (pipeline publication disabled).")) if args.bind_address == "0.0.0.0": print(f"Phone client server URL: ws://{lan_ip}:{args.port}/connect " "(use a workspace path that exists on this server).") @@ -692,6 +713,11 @@ def arguments(argv=None): help="Unauthenticated server bind address (default 127.0.0.1; expose only the web proxy)") parser.add_argument("--ntfy-port", type=int, default=DEFAULT_NTFY_PORT, help="ntfy listen port") parser.add_argument("--model", help="Record an explicit default model; otherwise host OpenCode configuration is reused") + role = parser.add_mutually_exclusive_group() + role.add_argument("--allow-authoring", dest="allow_authoring", action="store_true", default=None, + help="Explicitly enable pipeline publication; fresh native installs default to enabled") + role.add_argument("--no-authoring", dest="allow_authoring", action="store_false", + help="Execution-only server; disable pipeline publication (reinstalls otherwise preserve the recorded role)") parser.add_argument("--replace-data", action="store_true", help="Allow Docker migration to replace an existing native database/auth store") return parser.parse_args(argv) diff --git a/scripts/test_manage_native.py b/scripts/test_manage_native.py index 8be55efe..4738e557 100644 --- a/scripts/test_manage_native.py +++ b/scripts/test_manage_native.py @@ -1,14 +1,20 @@ import importlib.util +import io import os from pathlib import Path import signal +import subprocess import tempfile import unittest +from contextlib import redirect_stdout from unittest.mock import MagicMock, patch spec = importlib.util.spec_from_file_location("manage_native", Path(__file__).with_name("manage.py")) manage = importlib.util.module_from_spec(spec) spec.loader.exec_module(manage) +setup_spec = importlib.util.spec_from_file_location("setup_native_service", Path(__file__).with_name("setup-native.py")) +setup_native = importlib.util.module_from_spec(setup_spec) +setup_spec.loader.exec_module(setup_native) class FakeProcess: @@ -20,6 +26,124 @@ class FakeProcess: class ManageNativeTests(unittest.TestCase): + def service_config(self, home, system): + config = self.config(home / "prefix") + config["opencode_env"] = {"XDG_CONFIG_HOME": str(home / ".config")} + path = (home / "Library/LaunchAgents/net.a-wan.pipelines.plist" if system == "Darwin" + else home / ".config/systemd/user/net.a-wan.pipelines.service") + setup_native.Setup(home, environ={}).register_service(path, config) + return config + + def test_native_service_preview_is_read_only_on_both_platforms(self): + for system in ("Darwin", "Linux"): + with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \ + patch.object(manage.Path, "home", return_value=Path(directory)), \ + patch.object(manage.platform, "system", return_value=system): + config = self.service_config(Path(directory), system) + path = Path(config["registration"]) + before = path.read_bytes() + with patch.object(manage, "run", side_effect=AssertionError("preview invoked OS manager")), \ + patch.object(manage, "start", side_effect=AssertionError("preview started services")), \ + redirect_stdout(io.StringIO()) as output: + manage.service_command(config, ["enable", "--print"]) + self.assertEqual(path.read_bytes(), before) + self.assertEqual(output.getvalue(), before.decode()) + self.assertNotIn("publisher-token", output.getvalue()) + + def test_native_service_enable_and_disable_use_existing_registration(self): + for system in ("Darwin", "Linux"): + with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \ + patch.object(manage.Path, "home", return_value=Path(directory)), \ + patch.object(manage.platform, "system", return_value=system): + config = self.service_config(Path(directory), system) + before = Path(config["registration"]).read_bytes() + with patch.object(manage, "start") as start, redirect_stdout(io.StringIO()): + manage.service_command(config, ["enable"]) + start.assert_called_once_with(config) + calls = [] + with patch.object(manage, "run", side_effect=lambda args, **kw: calls.append(args)), \ + patch.object(manage, "stop", side_effect=lambda cfg: calls.append("stop")), \ + redirect_stdout(io.StringIO()): + manage.service_command(config, ["disable"]) + expected = (["launchctl", "disable", f"gui/{os.getuid()}/net.a-wan.pipelines"] if system == "Darwin" + else ["systemctl", "--user", "disable", "net.a-wan.pipelines.service"]) + self.assertEqual(calls, [expected, "stop"]) + self.assertEqual(Path(config["registration"]).read_bytes(), before) + + def test_native_service_status_does_not_print_raw_manager_output(self): + for system in ("Darwin", "Linux"): + with self.subTest(system=system), tempfile.TemporaryDirectory() as directory, \ + patch.object(manage.Path, "home", return_value=Path(directory)), \ + patch.object(manage.platform, "system", return_value=system): + config = self.service_config(Path(directory), system) + + def manager(args, **kwargs): + self.assertTrue(kwargs["quiet"]) + return subprocess.CompletedProcess(args, 0, stdout='"net.a-wan.pipelines" => true\nprivate-output') + + with patch.object(manage, "run", side_effect=manager), redirect_stdout(io.StringIO()) as output: + manage.service_command(config, ["status"]) + self.assertNotIn("private-output", output.getvalue()) + self.assertIn("Autostart: " + ("disabled" if system == "Darwin" else "enabled"), output.getvalue()) + + def test_native_service_rejects_unmanaged_symlink_and_wrong_prefix_definitions(self): + for variant in ("unmanaged", "symlink", "wrong-prefix", "wrong-label"): + with self.subTest(variant=variant), tempfile.TemporaryDirectory() as directory, \ + patch.object(manage.Path, "home", return_value=Path(directory)), \ + patch.object(manage.platform, "system", return_value="Linux"): + config = self.service_config(Path(directory), "Linux") + path = Path(config["registration"]) + if variant == "unmanaged": + path.write_text("Managed by pipelines service\nExecStart=developer-server") + elif variant == "symlink": + target = path.with_suffix(".other") + path.rename(target) + path.symlink_to(target) + elif variant == "wrong-prefix": + config["prefix"] += "-other" + else: + config["label"] = "pipelines" + with patch.object(manage, "run", side_effect=AssertionError("unsafe OS manager invocation")), \ + patch.object(manage, "start", side_effect=AssertionError("unsafe startup")): + with self.assertRaises(ValueError): + manage.service_command(config, ["enable"]) + + def test_native_service_rejects_standalone_database_flags(self): + with redirect_stdout(io.StringIO()), patch("sys.stderr", new_callable=io.StringIO): + with self.assertRaises(SystemExit) as raised: + manage.service_command({}, ["enable", "--db", "/other.db"]) + self.assertEqual(raised.exception.code, 2) + + def test_manager_dispatches_internal_service_verb_without_forwarding_to_go(self): + config = {"prefix": "/scratch/native"} + with patch("sys.argv", ["manage.py", "--prefix", "/scratch/native", "native-service", "enable", "--print"]), \ + patch.object(manage, "load", return_value=config), \ + patch.object(manage, "service_command") as service, \ + patch.object(manage.os, "execve", side_effect=AssertionError("recursive Go dispatch")): + manage.main() + service.assert_called_once_with(config, ["enable", "--print"]) + + def test_supervisor_authoring_flag_requires_explicit_manifest_true(self): + for role in (True, False, None, "true"): + with self.subTest(role=role), tempfile.TemporaryDirectory() as directory: + root = Path(directory) + (root / "logs").mkdir() + config = self.config(root) + if role is not None: + config["allow_authoring"] = role + ntfy, server = MagicMock(), MagicMock() + ntfy.poll.return_value = None + server.poll.return_value = 0 + server.returncode = 0 + with patch.object(manage, "ntfy_start", return_value=ntfy), \ + patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \ + patch.object(manage.subprocess, "Popen", return_value=server) as popen: + manage.supervise(config) + command = popen.call_args.args[0] + self.assertEqual(command[:5], [str(root / "bin/pipelines-server"), "-addr", "127.0.0.1:8080", + "-db", str(root / "data/pipelines.db")]) + self.assertEqual("-allow-authoring" in command, role is True) + def config(self, root, mode="native"): return { "format": 1, "prefix": str(root), "python": "python3", "server_port": 8080, diff --git a/scripts/test_setup_native.py b/scripts/test_setup_native.py index be10124b..12b89b31 100644 --- a/scripts/test_setup_native.py +++ b/scripts/test_setup_native.py @@ -250,6 +250,46 @@ class SetupNativeTests(unittest.TestCase): installer = setup_native.Setup(directory, runner=Recorder(), environ={"PIPELINES_PREFIX": str(custom)}) self.assertEqual(installer.resolve_prefix(), custom.resolve()) + def test_authoring_role_defaults_only_fresh_install_to_enabled(self): + with tempfile.TemporaryDirectory() as directory: + installer = self.installer(directory, Recorder()) + prefix = Path(directory) / "prefix" + self.assertTrue(installer.authoring_role(prefix, None)) + prefix.mkdir() + for previous, expected in (({}, False), ({"allow_authoring": False}, False), + ({"allow_authoring": True}, True)): + with self.subTest(previous=previous): + manifest = prefix / setup_native.MARKER + manifest.write_text(json.dumps(previous)) + before = manifest.read_bytes() + self.assertIs(installer.authoring_role(prefix, None), expected) + self.assertTrue(installer.authoring_role(prefix, True)) + self.assertFalse(installer.authoring_role(prefix, False)) + self.assertEqual(manifest.read_bytes(), before) + manifest.write_text('{"allow_authoring":"true"}') + with self.assertRaisesRegex(ValueError, "Invalid existing authoring role"): + installer.authoring_role(prefix, None) + + def test_authoring_flags_are_explicit_optional_overrides(self): + self.assertIsNone(setup_native.arguments([]).allow_authoring) + self.assertTrue(setup_native.arguments(["--allow-authoring"]).allow_authoring) + self.assertFalse(setup_native.arguments(["--no-authoring"]).allow_authoring) + + def test_full_run_keeps_existing_execution_only_and_accepts_fresh_opt_out(self): + for previous, arguments in (({}, []), ({"allow_authoring": False}, []), (None, ["--no-authoring"])): + with self.subTest(previous=previous), tempfile.TemporaryDirectory() as directory: + installer = self.installer(directory, Recorder()) + prefix = Path(directory) / "prefix" + if previous is not None: + prefix.mkdir() + (prefix / setup_native.MARKER).write_text(json.dumps(previous)) + with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ + patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ + patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \ + redirect_stdout(io.StringIO()): + installer.run(setup_native.arguments(arguments)) + self.assertFalse(manage.load(prefix)["allow_authoring"]) + def test_missing_opencode_fails_actionably(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) @@ -268,6 +308,7 @@ class SetupNativeTests(unittest.TestCase): installer.run(setup_native.arguments([])) config = manage.load(prefix) self.assertEqual(config["format"], 1) + self.assertTrue(config["allow_authoring"]) self.assertEqual(config["prefix"], str(prefix.resolve())) self.assertEqual(config["server_port"], 8080) self.assertEqual(config["bind_address"], "127.0.0.1")