e2e/codescan_test.go
65 lines · 3028 bytes
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}