Skip to content
quarry Artifact [910bc18576]
Login

Artifact [910bc18576]

Artifact 910bc18576d830c00852eeae472ca4ca9e51cea699494a0ec1953ad5fb06f353:


package main

import (
	"bytes"
	"context"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/runner"
)

// A CHECKOUT TOKEN CLONES ITS RUNNER'S REPOSITORY AND NOTHING ELSE,
// through the sync lane as serve wires it. It reads its one private
// repository, with zit and with a stock fossil, saves no login in what
// it cloned, cannot push, reads no other repository, and stops reading
// when its task or its run ends.
func TestACheckoutTokenClonesButNeverPushes(t *testing.T) {
	r := newFirstPushRig(t, "website", "other")
	r.commit(t, r.dirs["website"])
	r.commit(t, r.dirs["other"])
	db := r.rg.odb
	for _, id := range []string{"r1", "r2"} {
		if err := db.AddCIRun(oltp.CIRun{RunID: id, Repo: "website", Checkin: "c", Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
			t.Fatal(err)
		}
	}
	mint := func(run, job string) (string, string) {
		login, value, _, err := db.MintRunToken(oltp.RunTokenSpec{Run: run, Repo: "website", Job: job, ReadOnly: true, Expires: time.Now().Add(time.Hour)})
		if err != nil {
			t.Fatal(err)
		}
		return login, value
	}
	login, value := mint("r1", "build")

	// More clones than the limiter's free misses, none of them saving
	// the login or the remote.
	var dir string
	for i := 0; i < 8; i++ {
		dir = t.TempDir()
		if out, err := r.zit(t, dir, "clone", "--once", "--user", login, "--password", value, r.base+"website", "."); err != nil {
			t.Fatalf("FAIL: clone %d with a checkout token was refused: %v\n%s", i+1, err, out)
		}
	}
	if out := r.mustZit(t, dir, "remote"); strings.Contains(out, "website") {
		t.Errorf("FAIL: a --once clone saved the remote:\n%s", out)
	}
	if out, err := r.zit(t, dir, "pull", r.base+"website", "--once", "--user", login, "--password", value); err != nil {
		t.Errorf("FAIL: a pull with a checkout token: %v\n%s", err, out)
	}
	filepath.Walk(dir, func(p string, info os.FileInfo, err error) error {
		if err != nil || info.IsDir() || info.Size() > 64<<20 {
			return nil
		}
		if b, _ := os.ReadFile(p); bytes.Contains(b, []byte(value)) {
			t.Errorf("FAIL: the checkout token is on disk in %s", p)
		}
		return nil
	})

	// It may not push: not an unversioned file, not a check-in.
	r.commit(t, dir)
	if out, err := r.zit(t, dir, "push", r.base+"website", "--once", "--user", login, "--password", value); err == nil {
		t.Errorf("FAIL: a checkout token pushed a check-in\n%s", out)
	}
	if got := r.checkins(t, r.dirs["website"]); got != 1 {
		t.Errorf("FAIL: the repository holds %d check-ins after a refused push, want 1", got)
	}

	// Stock fossil reads with it as with any login.
	if _, err := exec.LookPath("fossil"); err == nil {
		out, err := exec.Command("fossil", "clone", "-B", login+":"+value, strings.Replace(r.base+"website", "http://", "http://"+login+":"+value+"@", 1), filepath.Join(t.TempDir(), "w.fossil")).CombinedOutput()
		if err != nil {
			t.Errorf("FAIL: a stock fossil clone with a checkout token: %v\n%s", err, out)
		}
	}

	// One repository only.
	if out, err := r.zit(t, t.TempDir(), "clone", "--once", "--user", login, "--password", value, r.base+"other", "."); err == nil {
		t.Errorf("FAIL: a checkout token for website cloned another repository\n%s", out)
	}

	// Its task's end, then its run's end, stop it.
	if err := db.RevokeCheckoutToken("r1", "build"); err != nil {
		t.Fatal(err)
	}
	if out, err := r.zit(t, t.TempDir(), "clone", "--once", "--user", login, "--password", value, r.base+"website", "."); err == nil {
		t.Errorf("FAIL: a checkout token read after its task ended\n%s", out)
	}
	l2, v2 := mint("r2", "build")
	if out, err := r.zit(t, t.TempDir(), "clone", "--once", "--user", l2, "--password", v2, r.base+"website", "."); err != nil {
		t.Fatalf("FAIL: a second run's checkout token: %v\n%s", err, out)
	}
	if err := db.CompleteCIRun("r2", "succeeded", ""); err != nil {
		t.Fatal(err)
	}
	if out, err := r.zit(t, t.TempDir(), "clone", "--once", "--user", l2, "--password", v2, r.base+"website", "."); err == nil {
		t.Errorf("FAIL: a checkout token read after its run ended\n%s", out)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// THE RUNNER'S OWN ZIT CHECKOUT, AS A TASK'S LOGIN, CLONES A PRIVATE
// REPOSITORY AND LEAVES NO CREDENTIAL IN THE WORKSPACE, in a fresh
// workspace and in one an earlier checkout left holding a saved login.
func TestARunnerChecksOutAPrivateRepositoryAsATasksLogin(t *testing.T) {
	r := newFirstPushRig(t, "website")
	r.commit(t, r.dirs["website"])
	db := r.rg.odb
	if err := db.AddCIRun(oltp.CIRun{RunID: "r1", Repo: "website", Checkin: "c", Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
		t.Fatal(err)
	}
	login, value, _, err := db.MintRunToken(oltp.RunTokenSpec{Run: "r1", Repo: "website", Job: "build", ReadOnly: true, Expires: time.Now().Add(time.Hour)})
	if err != nil {
		t.Fatal(err)
	}
	t.Setenv("HOME", r.home)
	t.Setenv("ZIT_PASSWORD", "")
	co := runner.ZitCheckout{Zit: "zit", URL: r.base + "website"}.As(login, value)
	dir := filepath.Join(t.TempDir(), "ws")
	for i := 0; i < 2; i++ { // the clone, then the refresh of what it left
		if err := co.Checkout(context.Background(), dir, "trunk"); err != nil {
			t.Fatalf("FAIL: checkout %d as the task's login: %v", i+1, err)
		}
	}
	if got := r.checkins(t, dir); got != 1 {
		t.Errorf("FAIL: the workspace holds %d check-ins, want 1", got)
	}
	// An earlier checkout as an account saved its login and remote: the
	// next one, as a login, clears them.
	old := filepath.Join(t.TempDir(), "old")
	if err := os.MkdirAll(old, 0o755); err != nil {
		t.Fatal(err)
	}
	r.mustZit(t, old, "clone", "--user", "jan", "--password", r.token, r.base+"website", ".")
	if out := r.mustZit(t, old, "remote"); !strings.Contains(out, "website") {
		t.Fatalf("FAIL: the earlier checkout saved no remote, so there is nothing to clear: %q", out)
	}
	if err := co.Checkout(context.Background(), old, "trunk"); err != nil {
		t.Fatalf("FAIL: checkout as the task's login over an earlier one: %v", err)
	}
	if out := r.mustZit(t, old, "remote"); strings.Contains(out, "website") {
		t.Errorf("FAIL: the earlier checkout's saved remote, and its login, was kept: %q", out)
	}
	for _, ws := range []string{dir, old} {
		filepath.Walk(ws, func(p string, info os.FileInfo, err error) error {
			if err != nil || info.IsDir() || info.Size() > 64<<20 {
				return nil
			}
			if b, _ := os.ReadFile(p); bytes.Contains(b, []byte(value)) || bytes.Contains(b, []byte(r.token)) {
				t.Errorf("FAIL: a credential is on disk in %s", p)
			}
			return nil
		})
	}
	if !t.Failed() {
		t.Log("ok")
	}
}