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.
This commit is contained in:
2026-10-20 21:41:05 +01:00
parent d58b3f49fc
commit bb32383bbe
11 changed files with 508 additions and 18 deletions
+29 -3
View File
@@ -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
}
@@ -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")
}
})
}
}
+8
View File
@@ -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 }
+13
View File
@@ -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()
}
+20 -2
View File
@@ -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
+40
View File
@@ -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)
}
}