Add explicit user-service autostart configuration
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
package installation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServiceDefinitionsQuotePathsAndKeepEnvironmentLimited(t *testing.T) {
|
||||
d := serviceDefinition{Binary: `/some bin/server`, Directory: `/repo "quoted" 100% $USER`, Args: []string{"-workspace-root", `/repo "quoted" 100% $USER`}, Environment: map[string]string{"PATH": "/usr/bin:/custom bin", "PIPELINES_NTFY_TOKEN_FILE": "/secret & token", "PROVIDER_SECRET": "must-not-export"}}
|
||||
for _, platform := range []string{"linux", "darwin"} {
|
||||
text, err := d.render(platform)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(text, "must-not-export") || !strings.Contains(text, serviceMarker) {
|
||||
t.Fatal("unexpected credentials or missing marker", text)
|
||||
}
|
||||
if platform == "linux" {
|
||||
if !strings.Contains(text, `"/some bin/server"`) || !strings.Contains(text, `100%% $$USER`) || !strings.Contains(text, `Environment="PATH=/usr/bin:/custom bin"`) {
|
||||
t.Fatal("unsafe systemd escaping", text)
|
||||
}
|
||||
} else {
|
||||
decoder := xml.NewDecoder(strings.NewReader(text))
|
||||
for {
|
||||
_, err := decoder.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal("invalid launchd XML", err)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(text, "/secret & token") {
|
||||
t.Fatal("missing XML escaping", text)
|
||||
}
|
||||
}
|
||||
}
|
||||
d.Args = []string{"newline\ninjection"}
|
||||
if _, err := d.render("linux"); err == nil {
|
||||
t.Fatal("accepted newline injection")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceWritesRefuseUnmanagedFilesAndSymlinks(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "pipelines.service")
|
||||
if err := os.WriteFile(path, []byte("user service"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeServiceFile(path, serviceMarker); err == nil {
|
||||
t.Fatal("replaced unmanaged file")
|
||||
}
|
||||
if err := os.Remove(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(dir, "unrelated"), path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeServiceFile(path, serviceMarker); err == nil {
|
||||
t.Fatal("replaced symlink")
|
||||
}
|
||||
if err := os.Remove(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := writeServiceFile(path, serviceMarker+"\nupdated"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || info.Mode().Perm() != 0600 {
|
||||
t.Fatal("service file must be private", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceConfigurationPreviewAndLifecycleWithMockOSManager(t *testing.T) {
|
||||
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
|
||||
t.Skip("no supported OS manager")
|
||||
}
|
||||
home := t.TempDir()
|
||||
bin := filepath.Join(home, "bin")
|
||||
root := filepath.Join(home, "repo with spaces")
|
||||
for _, dir := range []string{bin, root} {
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config"))
|
||||
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
|
||||
t.Setenv("PATH", bin)
|
||||
t.Setenv("PIPELINES_NTFY_TOKEN", "")
|
||||
server := filepath.Join(bin, "pipelines-server")
|
||||
if err := os.WriteFile(server, []byte("#!/bin/sh\nexit 0\n"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manager := "systemctl"
|
||||
if runtime.GOOS == "darwin" {
|
||||
manager = "launchctl"
|
||||
}
|
||||
capture := filepath.Join(home, "manager-commands")
|
||||
if err := os.WriteFile(filepath.Join(bin, manager), []byte("#!/bin/sh\nprintf '%s\\n' \"$*\" >> '"+capture+"'\n"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
ctx := context.Background()
|
||||
path, err := serviceFile(runtime.GOOS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ServiceCommand(ctx, server, []string{"enable"}, &out, &out); err == nil {
|
||||
t.Fatal("workspace scope must be explicit")
|
||||
}
|
||||
args := []string{"enable", "--workspace-root", root}
|
||||
if err := ServiceCommand(ctx, server, append(args, "--print"), &out, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||
t.Fatal("preview wrote service file", err)
|
||||
}
|
||||
if _, err := os.Lstat(capture); !os.IsNotExist(err) {
|
||||
t.Fatal("preview invoked OS manager", err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := ServiceCommand(ctx, server, args, &out, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := verifyServiceFile(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Uninstall(server, []string{"--yes"}, strings.NewReader(""), &out); err == nil || !strings.Contains(err.Error(), "service disable") {
|
||||
t.Fatal("uninstall must refuse registered service", err)
|
||||
}
|
||||
if err := ServiceCommand(ctx, server, []string{"status"}, &out, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ServiceCommand(ctx, server, []string{"disable"}, &out, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||
t.Fatal("disable left definition", err)
|
||||
}
|
||||
commands, err := os.ReadFile(capture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, command := range map[string][]string{"linux": {"daemon-reload", "enable pipelines.service", "restart pipelines.service", "status pipelines.service", "disable --now pipelines.service"}, "darwin": {"enable gui/", "bootstrap gui/", "print gui/", "disable gui/", "bootout gui/"}}[runtime.GOOS] {
|
||||
if !strings.Contains(string(commands), command) {
|
||||
t.Fatal("missing OS manager operation", command, string(commands))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user