Skip to content
quarry Artifact [c095964000]
Login

Artifact [c095964000]

Artifact c09596400037328d55ae966c592e0eb33f21a6b71638a4243d85dd488cf2b015:


package main

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/cibridge"
	"craftdesign.group/quarry/internal/events"
	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/ingest"
	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/runner"
	"craftdesign.group/quarry/internal/zitcli"
)

// archiveStore is a store with a trunk and a branch off it, and the
// full hashes of both tips.
func archiveStore(t *testing.T) (dir, trunk, branch string) {
	t.Helper()
	if _, err := exec.LookPath("zit"); err != nil {
		t.Skip("zit not on PATH")
	}
	dir = t.TempDir()
	zitIn(t, dir, "init", ".")
	write := func(name, body string) {
		if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
			t.Fatal(err)
		}
	}
	write("f.txt", "root\n")
	zitIn(t, dir, "add", "f.txt")
	zitIn(t, dir, "commit", "-m", "root", "--user", "u")
	write("g.txt", "feat\n")
	zitIn(t, dir, "add", "g.txt")
	zitIn(t, dir, "commit", "-m", "feat", "--branch", "feat", "--user", "u")
	zitIn(t, dir, "update", "trunk")
	write("h.txt", "trunk\n")
	zitIn(t, dir, "add", "h.txt")
	zitIn(t, dir, "commit", "-m", "more", "--user", "u")
	tip := func(b string) string {
		for _, f := range strings.Fields(zitIn(t, dir, "branch", "info", b)) {
			if len(f) == 64 && strings.Trim(f, "0123456789abcdef") == "" {
				return f
			}
		}
		t.Fatalf("no tip hash for %s", b)
		return ""
	}
	return dir, tip("trunk"), tip("feat")
}

// A PRIVATE REPOSITORY'S ARCHIVE IS READ BY THE CREDENTIAL THE RUNNER
// HOLDS. A branch run clones over the sync lane with the account's sync
// token; a pair run has no check-in to clone and fetches the merged
// tree from /archive/pair:... with that same Basic credential. The
// archive route answered "no such repository" to it (ticket 440).
func TestAPrivateRepositoryArchiveServesTheRunnersCredential(t *testing.T) {
	for _, namespaced := range []bool{false, true} {
		privateArchiveDoors(t, namespaced)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func privateArchiveDoors(t *testing.T, namespaced bool) {
	dir, trunk, branch := archiveStore(t)
	g := newSiteRigWith(t, func(name string, r *api.Repo) {
		if name == "granted" {
			r.Zit = zitcli.New(dir)
		}
	})
	doors := []string{"/r/granted/archive/", "/api/v1/r/granted/archive/"}
	if namespaced {
		if err := g.db.SetNamespace("granted", "alice", "alice", time.Now()); err != nil {
			t.Fatal(err)
		}
		// The flat page path redirects to the address; the API's bare
		// name and both owner paths are the doors a runner may use.
		doors = []string{"/api/v1/r/granted/archive/", "/r/alice/granted/archive/", "/api/v1/r/alice/granted/archive/"}
	}
	if err := g.db.AddAccount("stranger", doorPassword, false); err != nil {
		t.Fatal(err)
	}
	if err := g.db.Grant("alice", "granted", oltp.RepoAdmin); err != nil {
		t.Fatal(err)
	}
	syncTok, err := g.db.MintSyncToken("alice")
	if err != nil {
		t.Fatal(err)
	}
	strangerTok, err := g.db.MintSyncToken("stranger")
	if err != nil {
		t.Fatal(err)
	}
	elsewhere, _, err := g.db.MintToken("alice", oltp.TokenSpec{Name: "other", Scopes: []string{oltp.ScopeSync}, Repos: []string{"pub"}})
	if err != nil {
		t.Fatal(err)
	}
	pair := "pair:" + branch + ":" + trunk
	for _, door := range doors {
		for name, subject := range map[string]string{"tip": trunk, "pair": pair} {
			if code, body := g.doAs("alice", http.MethodGet, door+subject, syncTok, "", nil); code != http.StatusOK {
				t.Errorf("FAIL: %s %s: the account's sync token answered %d, want 200: %.120s", door, name, code, body)
			}
			for who, c := range map[string][2]string{
				"a sync token of an account with no role": {"stranger", strangerTok},
				"a sync token limited to another repo":    {"alice", elsewhere},
				"a wrong secret":                          {"alice", "qat_nosuchid_secretsecret"},
				"no credential":                           {"", ""},
			} {
				if code, _ := g.doAs(c[0], http.MethodGet, door+subject, c[1], "", nil); code != http.StatusNotFound {
					t.Errorf("FAIL: %s %s: %s answered %d, want the 404 a stranger gets", door, name, who, code)
				}
			}
		}
	}
}

// A RUN TOKEN READS THE ARCHIVE OF ITS OWN REPOSITORY AND NO OTHER,
// WITH THE READ LETTERS ITS JOB WAS GIVEN, UNTIL ITS RUN ENDS. It is
// scoped to the repository, not to one subject: the run's tip and the
// run's pair are the same grant, and a job given no read letters is
// refused both, as it is refused a clone.
func TestARunTokenReadsOnlyItsOwnRepositoriesArchive(t *testing.T) {
	dir, trunk, branch := archiveStore(t)
	g := newSiteRigWith(t, func(name string, r *api.Repo) {
		if name == "granted" || name == "secret" {
			r.Zit = zitcli.New(dir)
		}
	})
	g.db.SyncProjectCode = func(string) (string, bool) { return "CODE", true }
	for _, id := range []string{"r1", "r2", "r3"} {
		if err := g.db.AddCIRun(oltp.CIRun{RunID: id, Repo: "granted", Checkin: "c", Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
			t.Fatal(err)
		}
	}
	mint := func(run, letters string) (string, string) {
		login, value, _, err := g.db.MintRunToken(oltp.RunTokenSpec{Run: run, Repo: "granted", Job: "j" + run, Letters: letters, Expires: time.Now().Add(time.Hour)})
		if err != nil {
			t.Fatal(err)
		}
		return login, value
	}
	reader, rv := mint("r1", "goz")
	blind, bv := mint("r2", "")
	ended, ev := mint("r3", "goz")
	if _, err := g.db.EndCIRun("r3", "cancelled", "over"); err != nil {
		t.Fatal(err)
	}
	pair := "pair:" + branch + ":" + trunk
	// The page door is the runner's; the API's archive op also asks for
	// the z letter, which a run token is never given.
	for _, door := range []string{"/r/"} {
		for name, subject := range map[string]string{"tip": trunk, "pair": pair} {
			at := func(repo, login, value string) int {
				code, _ := g.doAs(login, http.MethodGet, door+repo+"/archive/"+subject, value, "", nil)
				return code
			}
			if code := at("granted", reader, rv); code != http.StatusOK {
				t.Errorf("FAIL: %s %s: a run token with read letters answered %d, want 200", door, name, code)
			}
			if code := at("secret", reader, rv); code != http.StatusNotFound {
				t.Errorf("FAIL: %s %s: a run token read another private repository: %d", door, name, code)
			}
			if code := at("granted", blind, bv); code != http.StatusNotFound {
				t.Errorf("FAIL: %s %s: a run token with no read letters answered %d, want 404", door, name, code)
			}
			if code := at("granted", ended, ev); code == http.StatusOK {
				t.Errorf("FAIL: %s %s: a run token still reads after its run ended", door, name)
			}
			if code := at("granted", "run-nosuchrun", "qrt_nosuchid_secretsecret"); code != http.StatusNotFound && code != http.StatusUnauthorized {
				t.Errorf("FAIL: %s %s: a foreign run token answered %d", door, name, code)
			}
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// THE RUNNER'S OWN CHECKOUT FETCHES A PRIVATE REPOSITORY'S PAIR.
// Served as the forge serves, at the owner's address the runner is
// given: the merged tree arrives whole with the account's sync token,
// and a stranger's token is refused with the status named, never the
// credential.
func TestTheRunnerChecksOutAPrivatePairEndToEnd(t *testing.T) {
	dir, trunk, branch := archiveStore(t)
	g := newSiteRigWith(t, func(name string, r *api.Repo) {
		if name == "granted" {
			r.Zit = zitcli.New(dir)
		}
	})
	if err := g.db.SetNamespace("granted", "alice", "alice", time.Now()); err != nil {
		t.Fatal(err)
	}
	if err := g.db.AddAccount("stranger", doorPassword, false); err != nil {
		t.Fatal(err)
	}
	tok, err := g.db.MintSyncToken("alice")
	if err != nil {
		t.Fatal(err)
	}
	other, err := g.db.MintSyncToken("stranger")
	if err != nil {
		t.Fatal(err)
	}
	srv := httptest.NewServer(g.h)
	defer srv.Close()
	pair := "pair:" + branch + ":" + trunk

	into := filepath.Join(t.TempDir(), "ws")
	co := runner.ArchiveCheckout{URL: srv.URL + "/r/alice/granted", User: "alice", Password: tok}
	if err := co.Checkout(context.Background(), into, pair); err != nil {
		t.Fatalf("FAIL: the runner's checkout of a private pair: %v", err)
	}
	for _, f := range []string{"f.txt", "g.txt", "h.txt"} {
		if _, err := os.Stat(filepath.Join(into, f)); err != nil {
			t.Errorf("FAIL: the merged tree lacks %s: %v", f, err)
		}
	}

	bad := runner.ArchiveCheckout{URL: srv.URL + "/r/alice/granted", User: "stranger", Password: other}
	err = bad.Checkout(context.Background(), filepath.Join(t.TempDir(), "ws"), pair)
	var refused *runner.ArchiveRefused
	if !errors.As(err, &refused) || refused.Status != http.StatusNotFound {
		t.Fatalf("FAIL: a stranger's checkout answered %v, want a 404 refusal", err)
	}
	if strings.Contains(err.Error(), other) || strings.Contains(err.Error(), srv.URL) {
		t.Errorf("FAIL: the refusal carries a credential or an address: %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// THE ARCHIVE DOOR OPENS FOR THE ARCHIVE ROUTE ALONE. A path that
// merely has "archive" in it (a tree, a file or a wiki page called
// that, an API alias) is a different route, and a sync token there is
// what an anonymous caller is: the same answer, a private repository's
// 404.
func TestASyncTokenOpensNoRouteButTheArchive(t *testing.T) {
	g := newSiteRig(t)
	tok, err := g.db.MintSyncToken("alice")
	if err != nil {
		t.Fatal(err)
	}
	webTok, _, err := g.db.MintToken("alice", oltp.TokenSpec{Name: "w", Scopes: []string{oltp.ScopeWeb, oltp.ScopeAPI}, Repos: []string{"pub"}})
	if err != nil {
		t.Fatal(err)
	}
	for _, path := range []string{
		"/r/granted/tree/archive/x.txt",
		"/r/granted/file/archive/x.txt",
		"/r/granted/doc/archive/x",
		"/r/granted/wiki/archive/x",
		"/r/granted/checkin/archive/diff",
		"/r/granted/archive/archive/x",
		"/r/granted%2Farchive/timeline",
		"/r/granted%2Farchive%2Fx",
		"/api/v1/r/granted/tree/archive/x.txt",
		"/api/v1/r/granted/mergebase/archive/trunk",
		"/api/v1/r/granted/checkin/archive/diff",
		"/r/granted",
		"/api/v1/r/granted/info",
	} {
		anon, _ := g.doAs("", http.MethodGet, path, "", "", nil)
		if got, _ := g.doAs("alice", http.MethodGet, path, tok, "", nil); got != anon {
			t.Errorf("FAIL: %s: a sync token answered %d, an anonymous caller %d", path, got, anon)
		}
		if anon == http.StatusOK {
			t.Errorf("FAIL: %s: an anonymous caller reads a private repository: %d", path, anon)
		}
		if got, _ := g.doAs("alice", http.MethodGet, path, webTok, "", nil); got != anon {
			t.Errorf("FAIL: %s: a web token limited to another repository answered %d, an anonymous caller %d", path, got, anon)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// BOTH DOORS ASK THE SAME LETTER OF AN ARCHIVE: z. A reader of a
// private repository with source and wiki letters but not z is refused
// the archive at the page and at the API alike.
func TestTheArchiveDoorsAgreeOnTheLetter(t *testing.T) {
	dir, trunk, _ := archiveStore(t)
	g := newSiteRigWith(t, func(name string, r *api.Repo) {
		if name == "granted" {
			r.Zit = zitcli.New(dir)
		}
	})
	if err := g.db.AddAccount("reader", doorPassword, false); err != nil {
		t.Fatal(err)
	}
	statuses := map[string][2]int{}
	for name, letters := range map[string]string{"with z": "gjorz", "without z": "gjor"} {
		if err := g.db.SetCaps("reader", "granted", letters); err != nil {
			t.Fatal(err)
		}
		tok, err := g.db.MintSyncToken("reader")
		if err != nil {
			t.Fatal(err)
		}
		var got [2]int
		for i, door := range []string{"/r/granted/archive/", "/api/v1/r/granted/archive/"} {
			got[i], _ = g.doAs("reader", http.MethodGet, door+trunk, tok, "", nil)
		}
		statuses[name] = got
	}
	if s := statuses["with z"]; s != [2]int{200, 200} {
		t.Errorf("FAIL: a reader with z got %v, want 200 at both doors", s)
	}
	s := statuses["without z"]
	if s[0] == http.StatusOK || s[1] == http.StatusOK || s[0] != s[1] {
		t.Errorf("FAIL: a reader without z got %v, want the same refusal at both doors", s)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A JOB ON A PRIVATE REPOSITORY GETS ITS CHANGED PATHS FROM THE
// FORGE'S OWN RECORD OF ITS RUN, not from a read credential on the API.
// The paths are the run's check-in's (a pair's branch tip), and a run
// that is another repository's or that nobody recorded is refused.
func TestTheForgeNamesTheChangedPathsOfARunsCheckin(t *testing.T) {
	dir, _, branch := archiveStore(t)
	trunk := ""
	for _, f := range strings.Fields(zitIn(t, dir, "branch", "info", "trunk")) {
		if len(f) == 64 && strings.Trim(f, "0123456789abcdef") == "" {
			trunk = f
			break
		}
	}
	cli := zitcli.New(dir)
	ev, err := events.Start(filepath.Join(t.TempDir(), "nats"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(ev.Close)
	ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { ix.Close() })
	if _, err := ingest.NewPipeline(cli, ev, ix, "r", "n1").Sync(); err != nil {
		t.Fatal(err)
	}
	db := newTestRegistrar(t, 25).odb
	for _, repo := range []string{"granted", "secret"} {
		if err := db.AddRepo(repo, t.TempDir(), "", oltp.Private); err != nil {
			t.Fatal(err)
		}
	}
	for id, c := range map[string]string{"r-tip": branch, "r-pair": "pair:" + branch + ":" + trunk} {
		if err := db.AddCIRun(oltp.CIRun{RunID: id, Repo: "granted", Checkin: c, Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
			t.Fatal(err)
		}
	}
	d := api.Deps{Secrets: db, Repos: api.NewRepos(map[string]*api.Repo{"granted": {Chat: "granted", Zit: cli, Index: ix}, "secret": {Chat: "secret", Zit: cli, Index: ix}})}

	got, err := d.DeliverChanged("granted", "r-tip.job")
	if err != nil || len(got.Changed) != 1 || got.Changed[0] != "g.txt" || len(got.Touched) != 1 || got.Touched[0] != "g.txt" {
		t.Errorf("FAIL: a branch run's lists: %+v, %v; want g.txt in both", got, err)
	}
	if got.Branch != "feat" {
		t.Errorf("FAIL: a branch run's branch is %q, want feat", got.Branch)
	}
	// Served as the forge serves it: the worker token that holds the task
	// reads the branch and the paths, with no credential on the repository.
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("Authorization") != "Bearer WORKER" {
			http.Error(w, "not claimed", http.StatusForbidden)
		}
	}))
	defer engine.Close()
	h, err := cibridge.Handler(engine.URL, "ADMIN", func(string) string { return "g" }, func(string) bool { return true }, nil, nil, cibridge.WithChanged(d.DeliverChanged))
	if err != nil {
		t.Fatal(err)
	}
	served := httptest.NewServer(h)
	defer served.Close()
	for bearer, want := range map[string]int{"WORKER": http.StatusOK, "OTHER": http.StatusForbidden} {
		req, _ := http.NewRequest("POST", served.URL+"/r/granted/ci/bridge/v1/tasks/r-tip.job/changed", strings.NewReader("{}"))
		req.Header.Set("Authorization", "Bearer "+bearer)
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		var body cibridge.ChangedPaths
		json.NewDecoder(resp.Body).Decode(&body)
		resp.Body.Close()
		if resp.StatusCode != want || (want == http.StatusOK && body.Branch != "feat") {
			t.Errorf("FAIL: worker %s got %d %+v, want %d", bearer, resp.StatusCode, body, want)
		}
	}
	got, err = d.DeliverChanged("granted", "r-pair.job")
	if err != nil || len(got.Changed) != 1 || got.Changed[0] != "g.txt" || got.Touched != nil {
		t.Errorf("FAIL: a pair run's lists: %+v, %v; want g.txt changed and no touched", got, err)
	}
	for name, c := range map[string][2]string{
		"another repository's run": {"secret", "r-tip.job"},
		"a run nobody recorded":    {"granted", "r-none.job"},
		"a task that is no task":   {"granted", "nodot"},
	} {
		if _, err := d.DeliverChanged(c[0], c[1]); err == nil {
			t.Errorf("FAIL: %s was answered", name)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}