Skip to content
quarry Artifact [f2143d0e7d]
Login

Artifact [f2143d0e7d]

Artifact f2143d0e7db4d32efa23497e12c8ed1b0699e0449c1278d106b37cbb20d2fca9:


package install

import (
	"bytes"
	"context"
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"strings"
	"testing"
	"time"
)

const (
	macHome = "/Users/dev"
	macDir  = macHome + "/Library/Application Support/quarry-runner/suit"
	macPlst = macHome + "/Library/LaunchAgents/app.zeitforge.quarry-runner.suit.plist"
)

func newMacRig(t *testing.T) *rig {
	t.Helper()
	r := newRig(t)
	r.sys.uid = 501
	r.sys.tools["launchctl"] = true
	for _, d := range []string{"/Users", macHome, macHome + "/Library", macHome + "/Library/Application Support"} {
		r.sys.files[d] = &fakeFile{dir: true, owner: "dev", group: "staff", mode: 0o755}
	}
	r.env.GOOS = "darwin"
	r.env.Poll = time.Millisecond
	r.env.Wait = 2 * time.Second
	return r
}

const wantPlist = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>app.zeitforge.quarry-runner.suit</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/dev/Library/Application Support/quarry-runner/suit/quarry-runner</string>
		<string>--work</string>
		<string>/Users/dev/Library/Application Support/quarry-runner/suit/work</string>
		<string>--max-tasks</string>
		<string>2</string>
		<string>--token-file</string>
		<string>/Users/dev/Library/Application Support/quarry-runner/suit/token</string>
		<string>URL</string>
	</array>
	<key>WorkingDirectory</key>
	<string>/Users/dev/Library/Application Support/quarry-runner/suit</string>
	<key>EnvironmentVariables</key>
	<dict>
		<key>HOME</key>
		<string>/Users/dev</string>
	</dict>
	<key>RunAtLoad</key>
	<true/>
	<key>KeepAlive</key>
	<true/>
	<key>ProcessType</key>
	<string>Standard</string>
	<key>ThrottleInterval</key>
	<integer>5</integer>
	<key>StandardOutPath</key>
	<string>/Users/dev/Library/Application Support/quarry-runner/suit/runner.log</string>
	<key>StandardErrorPath</key>
	<string>/Users/dev/Library/Application Support/quarry-runner/suit/runner.log</string>
</dict>
</plist>
`

// noSecretAnywhere is that the token is in no command the machine was
// asked to run and in nothing printed.
func noSecretAnywhere(t *testing.T, r *rig) {
	t.Helper()
	for _, c := range r.sys.calls {
		if strings.Contains(c, r.forge.token) {
			t.Errorf("FAIL: the token is in a command: %.40s", c)
		}
	}
	if strings.Contains(r.out.String(), r.forge.token) {
		t.Errorf("FAIL: the token was printed")
	}
}

// ON MACOS, INSTALL IS PER USER: THE PROGRAM, A WORK DIRECTORY, A 0600
// TOKEN FILE, A MANIFEST AND A LAUNCHAGENT, ALL UNDER THE HOME, LOADED
// WITH launchctl bootstrap; NO ROOT, NO KEYCHAIN, NO TOKEN IN ARGV.
func TestMacInstallIsPerUser(t *testing.T) {
	r := newMacRig(t)
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Fatalf("FAIL: install: %v\n%s", err, r.out)
	}
	type want struct {
		mode uint32
		dir  bool
	}
	for p, w := range map[string]want{
		macDir:                            {0o700, true},
		macDir + "/work":                  {0o700, true},
		macDir + "/quarry-runner":         {0o755, false},
		macDir + "/token":                 {0o600, false},
		macDir + "/manifest.json":         {0o644, false},
		macPlst:                           {0o644, false},
		macHome + "/Library/LaunchAgents": {0o755, true},
	} {
		x, ok := r.sys.files[p]
		if !ok {
			t.Errorf("FAIL: %s was not made", p)
			continue
		}
		if uint32(x.mode) != w.mode || x.dir != w.dir {
			t.Errorf("FAIL: %s is %04o dir=%v, want %04o dir=%v", p, x.mode, x.dir, w.mode, w.dir)
		}
	}
	if got := string(r.sys.files[macDir+"/token"].data); strings.TrimSpace(got) != r.forge.token {
		t.Errorf("FAIL: the token file does not hold the token")
	}
	if got, want := string(r.sys.files[macPlst].data), strings.ReplaceAll(wantPlist, "URL", r.forge.repoURL()); got != want {
		t.Errorf("FAIL: plist\n%s\nwant\n%s", got, want)
	}
	if string(r.sys.files[macDir+"/quarry-runner"].data) != "PROGRAM-BYTES" {
		t.Errorf("FAIL: the program is not a copy of the running binary")
	}
	var m map[string]any
	if err := json.Unmarshal(r.sys.files[macDir+"/manifest.json"].data, &m); err != nil {
		t.Fatalf("FAIL: manifest is not JSON: %v", err)
	}
	if m["platform"] != "darwin" || m["home"] != macHome || m["token_id"] != "wt1" || m["name"] != "suit" {
		t.Errorf("FAIL: manifest %v", m)
	}
	if n := len(r.sys.users) + len(r.sys.groups); n != 0 {
		t.Errorf("FAIL: %d users or groups were made on macOS", n)
	}
	boot := r.sys.indexOf("launchctl bootstrap gui/501 " + macPlst)
	if boot < 0 || boot < r.sys.indexOf("WriteFile "+macDir+"/token") {
		t.Errorf("FAIL: bootstrap at %d, token written at %d: %v", boot, r.sys.indexOf("WriteFile "+macDir+"/token"), r.sys.calls)
	}
	for _, c := range r.sys.calls {
		if strings.HasPrefix(c, "launchctl") && strings.Contains(c, "security") || strings.HasPrefix(c, "security") {
			t.Errorf("FAIL: the Keychain was used: %s", c)
		}
	}
	noSecretAnywhere(t, r)
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestMacInstallRefusesRootAndLeavesNothing(t *testing.T) {
	r := newMacRig(t)
	r.sys.uid = 0
	before := r.sys.snapshot()
	if err := r.install("--code", "qri_abc"); err == nil {
		t.Fatalf("FAIL: install as root succeeded")
	}
	if r.sys.snapshot() != before || len(r.forge.redeems) != 0 || len(r.sys.calls) != 0 {
		t.Errorf("FAIL: a refused install changed something: %v", r.sys.calls)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestMacInstallTwiceRepairs(t *testing.T) {
	r := newMacRig(t)
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	r.vars["QUARRY_RUNNER_TOKEN"] = ""
	before := r.sys.snapshot()
	if err := r.install(); err != nil {
		t.Fatalf("FAIL: second install: %v\n%s", err, r.out)
	}
	if len(r.forge.redeems) != 1 || len(r.forge.starts) != 0 {
		t.Errorf("FAIL: the second install asked for another token (%d redeems, %d starts)", len(r.forge.redeems), len(r.forge.starts))
	}
	if r.sys.snapshot() != before {
		t.Errorf("FAIL: the second install changed the machine")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func macInstalled(t *testing.T) *rig {
	t.Helper()
	r := newMacRig(t)
	if err := r.install("--code", "qri_abc"); err != nil {
		t.Fatalf("FAIL: install: %v\n%s", err, r.out)
	}
	r.out.Reset()
	r.sys.calls = nil
	return r
}

// UNINSTALL BOOTS THE AGENT OUT, RETIRES THE TOKEN WITH THE ROUTE THE
// TOKEN HOLDER MAY CALL, AND REMOVES EXACTLY THE MANIFEST'S PATHS.
func TestMacUninstallRemovesOnlyWhatTheManifestLists(t *testing.T) {
	r := macInstalled(t)
	other := macHome + "/Library/Application Support/quarry-runner/other"
	r.sys.files[other] = &fakeFile{dir: true, mode: 0o700}
	r.sys.files[other+"/token"] = &fakeFile{data: []byte("kept"), mode: 0o600}
	r.sys.files[macDir+"/mine.txt"] = &fakeFile{data: []byte("not the manifest's"), mode: 0o644}
	r.sys.files[macHome+"/Library/LaunchAgents/com.other.plist"] = &fakeFile{data: []byte("x"), mode: 0o644}
	if err := Uninstall(context.Background(), r.env, []string{"suit"}); err != nil {
		t.Fatalf("FAIL: uninstall: %v\n%s", err, r.out)
	}
	if r.sys.indexOf("launchctl bootout gui/501/app.zeitforge.quarry-runner.suit") < 0 {
		t.Errorf("FAIL: the agent was not booted out: %v", r.sys.calls)
	}
	if len(r.forge.retires) != 1 || r.forge.retires[0]["token"] != r.forge.token {
		t.Errorf("FAIL: the token was retired %d times", len(r.forge.retires))
	}
	for _, p := range []string{macPlst, macDir + "/token", macDir + "/quarry-runner", macDir + "/manifest.json", macDir + "/work"} {
		if r.sys.Exists(p) {
			t.Errorf("FAIL: %s remains", p)
		}
	}
	for _, p := range []string{macDir + "/mine.txt", macDir, other, other + "/token", macHome + "/Library/LaunchAgents/com.other.plist"} {
		if !r.sys.Exists(p) {
			t.Errorf("FAIL: %s was removed, and the manifest does not list it", p)
		}
	}
	for _, c := range r.sys.calls {
		if strings.HasPrefix(c, "RemoveAll") && !strings.HasSuffix(c, macDir+"/work") {
			t.Errorf("FAIL: a recursive removal outside the work directory: %s", c)
		}
	}
	noSecretAnywhere(t, r)
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestMacUninstallRefusesAManifestNamingOtherPaths(t *testing.T) {
	r := macInstalled(t)
	var m Manifest
	if err := json.Unmarshal(r.sys.files[macDir+"/manifest.json"].data, &m); err != nil {
		t.Fatal(err)
	}
	m.Paths = append(m.Paths, macHome+"/Documents")
	raw, _ := json.Marshal(m)
	r.sys.files[macDir+"/manifest.json"].data = raw
	r.sys.files[macHome+"/Documents"] = &fakeFile{dir: true, mode: 0o755}
	if err := Uninstall(context.Background(), r.env, []string{"suit"}); err == nil {
		t.Errorf("FAIL: a manifest naming ~/Documents was acted on")
	}
	if !r.sys.Exists(macHome+"/Documents") || len(r.forge.retires) != 0 {
		t.Errorf("FAIL: the refusal still changed something")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func (r *rig) macDoctor(args ...string) (int, []Check) {
	r.out.Reset()
	err := Doctor(context.Background(), r.env, append([]string{"--json"}, args...))
	var out struct {
		Checks []Check `json:"checks"`
	}
	_ = json.Unmarshal(r.out.Bytes(), &out)
	return exitCode(err), out.Checks
}

// DOCTOR ON MACOS: MANIFEST, PROGRAM, AGENT LOADED, TOKEN FILE 0600,
// FORGE, BRIDGE, DISK; EXIT 0 / 3 / 1 BY THE WORST LINE.
func TestMacDoctorExitCodesFollowTheWorstLine(t *testing.T) {
	r := macInstalled(t)
	if code, checks := r.macDoctor(); code != 0 || len(checks) != 10 {
		t.Fatalf("FAIL: a healthy install gave exit %d with %d checks", code, len(checks))
	}
	byName := func(cs []Check, name string) Check {
		for _, c := range cs {
			if c.Name == name {
				return c
			}
		}
		t.Fatalf("FAIL: no check named %s", name)
		return Check{}
	}
	cases := []struct {
		name   string
		break_ func()
		fix    func()
		code   int
		check  string
	}{
		{"agent not loaded", func() { r.sys.fail["launchctl print"] = errString("not found") }, func() { delete(r.sys.fail, "launchctl print") }, 1, "agent"},
		{"token file too open", func() { r.sys.files[macDir+"/token"].mode = 0o644 }, func() { r.sys.files[macDir+"/token"].mode = 0o600 }, 1, "token"},
		{"token file gone", func() { delete(r.sys.files, macDir+"/token") }, func() {
			r.sys.files[macDir+"/token"] = &fakeFile{data: []byte(r.forge.token + "\n"), mode: 0o600}
		}, 1, "token"},
		{"program gone", func() { delete(r.sys.files, macDir+"/quarry-runner") }, func() {
			r.sys.files[macDir+"/quarry-runner"] = &fakeFile{data: []byte("PROGRAM-BYTES"), mode: 0o755}
		}, 1, "program"},
		{"a nearly full disk", func() { r.sys.diskFree = 10 << 20 }, func() { r.sys.diskFree = 50 << 30 }, 3, "disk"},
		{"nothing to run", func() { r.env.Trunk = func(context.Context, string) (bool, error) { return false, nil } }, func() {
			r.env.Trunk = func(context.Context, string) (bool, error) { return true, nil }
		}, 3, "trunk"},
	}
	for _, c := range cases {
		c.break_()
		code, checks := r.macDoctor()
		if code != c.code {
			t.Errorf("FAIL: %s: exit %d, want %d", c.name, code, c.code)
		}
		if got := byName(checks, c.check); got.Status == statOK {
			t.Errorf("FAIL: %s: check %s says ok", c.name, c.check)
		}
		c.fix()
		if code, _ := r.macDoctor(); code != 0 {
			t.Errorf("FAIL: %s: not healthy again after the repair: %d", c.name, code)
		}
	}
	noSecretAnywhere(t, r)
	if !t.Failed() {
		t.Log("ok")
	}
}

// A TOKEN THAT BELONGS TO ANOTHER REPOSITORY IS SAID TO, BY NAME.
func TestDoctorSaysWhichRepositoryATokenBelongsTo(t *testing.T) {
	for _, mac := range []bool{true, false} {
		var r *rig
		if mac {
			r = macInstalled(t)
		} else {
			r = installed(t)
		}
		r.forge.other = "the-other-repo"
		code, checks := r.macDoctor()
		var bridge Check
		for _, c := range checks {
			if c.Name == "bridge" {
				bridge = c
			}
		}
		if code != 1 || bridge.Status != statFail || !strings.Contains(bridge.Detail, "the-other-repo") {
			t.Errorf("FAIL: mac=%v: bridge check %+v, exit %d", mac, bridge, code)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

type errString string

func (e errString) Error() string { return string(e) }

// A RUNNER REQUEST: WITH NO CODE AND NO TOKEN, INSTALL FILES ONE, WAITS,
// COLLECTS THE TOKEN WITH ITS KEY, WRITES IT TO THE PRIVATE FILE AND
// CARRIES ON. THE TOKEN IS IN NO ARGV AND NO OUTPUT.
func TestAnInstallWithNoCodeFilesARunnerRequestAndUsesTheToken(t *testing.T) {
	for _, mac := range []bool{true, false} {
		var r *rig
		tokenFile, keyFile := "/etc/quarry-runner/suit.env", "/etc/quarry-runner/suit.pickup"
		if mac {
			r = newMacRig(t)
			tokenFile, keyFile = macDir+"/token", macDir+"/pickup.key"
		} else {
			r = newRig(t)
			r.env.Poll, r.env.Wait = time.Millisecond, 2*time.Second
		}
		if err := r.install(); err != nil {
			t.Fatalf("FAIL: mac=%v: install: %v\n%s", mac, err, r.out)
		}
		if len(r.forge.starts) != 1 || len(r.forge.redeems) != 0 {
			t.Fatalf("FAIL: mac=%v: %d starts and %d redeems", mac, len(r.forge.starts), len(r.forge.redeems))
		}
		st := r.forge.starts[0]
		if st["kind"] != "runner" || st["repo"] != "suit" || st["host"] != "build-box" {
			t.Errorf("FAIL: mac=%v: the start was %v", mac, st)
		}
		if len(r.forge.pickups) == 0 {
			t.Fatalf("FAIL: mac=%v: nothing was collected", mac)
		}
		key, err := base64.RawURLEncoding.DecodeString(r.forge.pickups[0])
		if err != nil {
			t.Fatal(err)
		}
		sum := sha256.Sum256(key)
		if st["pickup"] != hex.EncodeToString(sum[:]) {
			t.Errorf("FAIL: mac=%v: the key collected with is not the one the start's code names", mac)
		}
		if !strings.Contains(string(r.sys.files[tokenFile].data), r.forge.token) || r.sys.files[tokenFile].mode != 0o600 {
			t.Errorf("FAIL: mac=%v: the token file is %04o", mac, r.sys.files[tokenFile].mode)
		}
		if r.sys.Exists(keyFile) {
			t.Errorf("FAIL: mac=%v: the pickup key was left behind", mac)
		}
		// The key was written private, before the start.
		if i := r.sys.indexOf("WriteFile " + keyFile); i < 0 || !strings.Contains(r.sys.calls[i], "0600") {
			t.Errorf("FAIL: mac=%v: the key was not written 0600: %v", mac, r.sys.calls)
		}
		if !strings.Contains(r.out.String(), "ABCD-EFGH") {
			t.Errorf("FAIL: mac=%v: the short code was not shown", mac)
		}
		if mac && r.sys.indexOf("launchctl bootstrap") < 0 || !mac && r.sys.indexOf("systemctl enable --now") < 0 {
			t.Errorf("FAIL: mac=%v: the service was not started", mac)
		}
		noSecretAnywhere(t, r)
		var m Manifest
		raw := r.sys.files["/etc/quarry-runner/suit.json"]
		if mac {
			raw = r.sys.files[macDir+"/manifest.json"]
		}
		if err := json.Unmarshal(raw.data, &m); err != nil || m.TokenID != "wt1" {
			t.Errorf("FAIL: mac=%v: the manifest does not name the token: %v", mac, err)
		}
		for _, p := range m.Paths {
			if p == keyFile {
				t.Errorf("FAIL: mac=%v: the manifest lists the removed key", mac)
			}
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A DENIAL, A LAPSE, A REQUEST NOBODY ANSWERS, AN UNSEEABLE REPOSITORY
// AND A STRANGER'S TOKEN EACH STOP THE INSTALL WITH NOTHING LEFT.
func TestARunnerRequestThatEndsWithoutATokenLeavesNothing(t *testing.T) {
	cases := map[string]func(r *rig){
		"denied":   func(r *rig) { r.forge.answer = "deny" },
		"lapsed":   func(r *rig) { r.forge.answer = "expire" },
		"nobody":   func(r *rig) { r.forge.answer = "wait"; r.env.Wait = 30 * time.Millisecond },
		"not seen": func(r *rig) { r.forge.startStatus = 404 },
		"stranger": func(r *rig) { r.forge.answer = "stranger" },
	}
	for name, setup := range cases {
		for _, mac := range []bool{true, false} {
			var r *rig
			if mac {
				r = newMacRig(t)
			} else {
				r = newRig(t)
				r.env.Poll, r.env.Wait = time.Millisecond, 2*time.Second
			}
			setup(r)
			before := r.sys.snapshot()
			err := r.install()
			if err == nil {
				t.Errorf("FAIL: %s mac=%v: install succeeded", name, mac)
				continue
			}
			if exitCode(err) == 0 {
				t.Errorf("FAIL: %s mac=%v: exit 0", name, mac)
			}
			if after := r.sys.snapshot(); after != before {
				t.Errorf("FAIL: %s mac=%v: the machine was left changed:\n%s", name, mac, diffLines(before, after))
			}
			for _, c := range r.sys.calls {
				if strings.HasPrefix(c, "launchctl bootstrap") || strings.HasPrefix(c, "systemctl enable") {
					t.Errorf("FAIL: %s mac=%v: a service was started: %s", name, mac, c)
				}
			}
			noSecretAnywhere(t, r)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func diffLines(a, b string) string {
	have := map[string]bool{}
	for _, l := range strings.Split(a, "\n") {
		have[l] = true
	}
	var out bytes.Buffer
	for _, l := range strings.Split(b, "\n") {
		if !have[l] {
			out.WriteString("+ " + l + "\n")
		}
	}
	return out.String()
}

// A LINK WHERE A SHARED DIRECTORY BELONGS IS REFUSED, NOT FOLLOWED.
func TestMacInstallRefusesALinkedSharedDirectory(t *testing.T) {
	for _, dir := range []string{macHome + "/Library/LaunchAgents", macHome + "/Library/Application Support/quarry-runner"} {
		r := newMacRig(t)
		r.sys.files[dir] = &fakeFile{dir: true, link: true, mode: 0o755}
		before := r.sys.snapshot()
		if err := r.install("--code", "qri_abc"); err == nil {
			t.Errorf("FAIL: install went on with %s a link", dir)
		}
		if r.sys.snapshot() != before || r.sys.indexOf("launchctl bootstrap") >= 0 {
			t.Errorf("FAIL: a refused install changed something (%s)", dir)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}