package api
import (
"errors"
"path/filepath"
"strings"
"testing"
"time"
"craftdesign.group/quarry/internal/oltp"
)
// A RUNNER'S TASK GETS A READ-ONLY CHECKOUT TOKEN FOR ITS OWN RUN'S
// REPOSITORY, whatever the task is and whatever branch it is on, and
// nothing else: not for a run the forge has no record of, not for
// another repository's, not once the run is over.
func TestATaskIsDeliveredAReadOnlyCheckoutToken(t *testing.T) {
wait := runStartWait
runStartWait = 20 * time.Millisecond
t.Cleanup(func() { runStartWait = wait })
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
db.SyncProjectCode = func(repo string) (string, bool) { return "CODE-" + repo, true }
for _, name := range []string{"test", "other"} {
if err := db.AddRepo(name, filepath.Join(t.TempDir(), name), "", "private"); err != nil {
t.Fatal(err)
}
}
for _, id := range []string{"manifest", "jobrun", "pair", "slow", "done"} {
checkin := "c"
if id == "pair" {
checkin = "pair:a:b"
}
if err := db.AddCIRun(oltp.CIRun{RunID: id, Repo: "test", Checkin: checkin, Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
t.Fatal(err)
}
}
if err := db.RecordCIRunStart("jobrun", "push", "", map[string]oltp.RunJobGrant{"build": {Letters: "iy", Timeout: 10}}); err != nil {
t.Fatal(err)
}
if err := db.RecordCIRunStart("slow", "push", "", map[string]oltp.RunJobGrant{"build": {Timeout: 24 * 60}}); err != nil {
t.Fatal(err)
}
if err := db.CompleteCIRun("done", "succeeded", ""); err != nil {
t.Fatal(err)
}
d := Deps{Secrets: db}
for _, task := range []string{"manifest.test", "jobrun.build", "pair.merge", "slow.build"} {
got, err := d.DeliverCheckoutToken("test", task)
if err != nil || !strings.HasPrefix(got.User, "run-") || !strings.HasPrefix(got.Token, "qrt_") {
t.Fatalf("FAIL: %s got %+v, %v; want a run login and token", task, got, err)
}
tok, ok := db.ResolveToken(got.User, got.Token, oltp.ScopeSync)
if !ok || strings.Join(tok.Scopes, ",") != oltp.ScopeSync || !tok.Reaches("test") || tok.Reaches("other") {
t.Errorf("FAIL: %s: the token is %+v (live %v); want sync alone, on test alone", task, tok, ok)
}
if _, ok := db.ResolveToken(got.User, got.Token, oltp.ScopeAPI); ok {
t.Errorf("FAIL: %s: the checkout token opened the API door", task)
}
if strings.ContainsAny(tok.Ceiling, "iy") {
t.Errorf("FAIL: %s: the checkout token carries %q, a write letter", task, tok.Ceiling)
}
exp, err := time.Parse(time.RFC3339, got.Expires)
if err != nil || time.Until(exp) > oltp.RunTokenMaxLife {
t.Errorf("FAIL: %s: expires %q, %v; want within %s", task, got.Expires, err, oltp.RunTokenMaxLife)
}
if task == "jobrun.build" && time.Until(exp) > 10*time.Minute {
t.Errorf("FAIL: a job with a 10 minute timeout was given a token that lives %s", time.Until(exp))
}
}
// A retried checkout replaces the first token.
first, _ := d.DeliverCheckoutToken("test", "manifest.test")
second, err := d.DeliverCheckoutToken("test", "manifest.test")
if err != nil {
t.Fatal(err)
}
if _, ok := db.ResolveToken(first.User, first.Token, oltp.ScopeSync); ok {
t.Errorf("FAIL: a retried checkout left the first token live")
}
if _, ok := db.ResolveToken(second.User, second.Token, oltp.ScopeSync); !ok {
t.Errorf("FAIL: the retried checkout's token is not live")
}
// The task's end revokes it, by the task id alone.
if err := d.TaskEnded("test", "manifest.test"); err != nil {
t.Fatal(err)
}
if _, ok := db.ResolveToken(second.User, second.Token, oltp.ScopeSync); ok {
t.Errorf("FAIL: the checkout token outlived its task")
}
for name, c := range map[string][2]string{
"another repository's run": {"other", "manifest.test"},
"a run nobody recorded": {"test", "forged.test"},
"a run that is over": {"test", "done.test"},
"a task that is no task": {"test", "nodot"},
"a task with no job": {"test", "manifest."},
} {
if got, err := d.DeliverCheckoutToken(c[0], c[1]); !errors.Is(err, ErrSecretRefused) || got.Token != "" {
t.Errorf("FAIL: %s got %+v, %v; want a refusal and no token", name, got, err)
}
}
if _, err := (Deps{}).DeliverCheckoutToken("test", "manifest.test"); err == nil {
t.Errorf("FAIL: a forge with no run records delivered a token")
}
if !t.Failed() {
t.Log("ok")
}
}