e2e/codescan_test.go

545ba0bcdea3b534903bd78c918c0c025a97eda7
gitbay/e2e/codescan_test.go

117 lines · 5366 bytes

2 symbols in this file
  1package e2e
  2
  3import (
  4	"os"
  5	"path/filepath"
  6	"strings"
  7	"testing"
  8)
  9
 10// The instance's code scan end to end: enable, queue, a real runner runs
 11// the configured steps and uploads their SARIF, and the findings land in
 12// an issue. A results directory committed to the repository is cleared
 13// before the steps run, so it is never uploaded as the scan's (#340).
 14func TestSecurityScan(t *testing.T) {
 15	t.Parallel()
 16	inst := startInstanceWith(t, `[security]
 17image = "localhost/scan:1"
 18steps = ['mkdir -p "$GITBAY_RESULTS"', 'cp scan.sarif "$GITBAY_RESULTS/scan.sarif"']
 19`)
 20	inst.runner = buildRunner(t)
 21	aliceKey := inst.newKey(t, "alice")
 22	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
 23	runnerKey := inst.newKey(t, "ci")
 24	inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
 25	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
 26		t.Fatalf("repo create: %s", errOut)
 27	}
 28	work := t.TempDir()
 29	env := inst.gitEnv(aliceKey)
 30	mustGit(t, work, env, "clone", "-q", inst.sshURL("alice/app"), "w")
 31	dir := filepath.Join(work, "w")
 32	os.MkdirAll(filepath.Join(dir, ".gitbay-results"), 0o755)
 33	os.WriteFile(filepath.Join(dir, "scan.sarif"), []byte(`{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"opengrep"}},
 34		"results":[{"ruleId":"python.exec-used","level":"error","message":{"text":"exec on input"},
 35		"locations":[{"physicalLocation":{"artifactLocation":{"uri":"app.py"},"region":{"startLine":7}}}]}]}]}`), 0o644)
 36	os.WriteFile(filepath.Join(dir, ".gitbay-results", "planted.sarif"), []byte(`{"runs":[{"tool":{"driver":{"name":"planted"}},
 37		"results":[{"ruleId":"planted.rule","message":{"text":"planted"}}]}]}`), 0o644)
 38	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 39	mustGit(t, dir, env, "add", ".")
 40	mustGit(t, dir, env, "commit", "-q", "-m", "base")
 41	mustGit(t, dir, env, "push", "-q", "origin", "main")
 42
 43	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "enable", "alice/app"); code != 0 {
 44		t.Fatalf("security enable: %s", errOut)
 45	}
 46	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "run", "alice/app"); code != 0 {
 47		t.Fatalf("security run: %s", errOut)
 48	}
 49	if out := inst.runnerOnce(t, runnerKey); !strings.Contains(out, "success") {
 50		t.Fatalf("runner:\n%s", out)
 51	}
 52	out, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "status", "alice/app", "--json")
 53	if code != 0 || !strings.Contains(out, `"rule":"python.exec-used"`) || strings.Contains(out, "planted") {
 54		t.Fatalf("status: %d %s\n%s", code, errOut, out)
 55	}
 56	out, _, _ = inst.ssh(t, aliceKey, "", "issue", "list", "alice/app")
 57	if !strings.Contains(out, "Code scanning findings") {
 58		t.Fatalf("no findings issue:\n%s", out)
 59	}
 60	// The scan sets no commit status.
 61	sha := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
 62	if out, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha); strings.Contains(out, "gitbay-security") {
 63		t.Fatalf("scan set a status:\n%s", out)
 64	}
 65}
 66
 67// A runner attached for the code scan only leaves the repository's own
 68// jobs to its other runner: the shape of a repository whose job must run
 69// somewhere the scan image is not.
 70func TestRunnerAttachedForJobs(t *testing.T) {
 71	t.Parallel()
 72	inst := startInstanceWith(t, `[security]
 73image = "localhost/scan:1"
 74steps = ['mkdir -p "$GITBAY_RESULTS"', 'cp empty.sarif "$GITBAY_RESULTS/x.sarif"']
 75`)
 76	inst.runner = buildRunner(t)
 77	aliceKey := inst.newKey(t, "alice")
 78	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
 79	scanKey := inst.newKey(t, "scanner")
 80	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
 81		t.Fatalf("repo create: %s", errOut)
 82	}
 83	pub, err := os.ReadFile(scanKey + ".pub")
 84	if err != nil {
 85		t.Fatal(err)
 86	}
 87	if _, errOut, code := inst.ssh(t, aliceKey, string(pub), "repo", "runner", "add", "alice/app", "--jobs", "gitbay-security"); code != 0 {
 88		t.Fatalf("runner add --jobs: %s", errOut)
 89	}
 90	work := t.TempDir()
 91	env := inst.gitEnv(aliceKey)
 92	mustGit(t, work, env, "clone", "-q", inst.sshURL("alice/app"), "w")
 93	dir := filepath.Join(work, "w")
 94	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
 95	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte("jobs:\n  pull:\n    steps:\n      - echo pulled\n"), 0o644)
 96	os.WriteFile(filepath.Join(dir, "empty.sarif"), []byte(`{"version":"2.1.0","runs":[]}`), 0o644)
 97	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 98	mustGit(t, dir, env, "add", ".")
 99	mustGit(t, dir, env, "commit", "-q", "-m", "ci")
100	mustGit(t, dir, env, "push", "-q", "origin", "main")
101	inst.ssh(t, aliceKey, "", "repo", "security", "enable", "alice/app")
102	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "run", "alice/app"); code != 0 {
103		t.Fatalf("security run: %s", errOut)
104	}
105
106	// The pull build is older, but this runner may only take the scan.
107	if out := inst.runnerOnce(t, scanKey); !strings.Contains(out, "gitbay-security") || strings.Contains(out, " pull ") {
108		t.Fatalf("runner:\n%s", out)
109	}
110	if out := inst.runnerOnce(t, scanKey); strings.Contains(out, "pull") {
111		t.Fatalf("the scan runner claimed the pull job:\n%s", out)
112	}
113	out, _, _ := inst.ssh(t, aliceKey, "", "build", "list", "alice/app")
114	if !strings.Contains(out, "pull\tpending") {
115		t.Fatalf("pull was not left pending:\n%s", out)
116	}
117}