vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Replace the runner allowlist with a runner role
648cd81bde97042f682a528ebb9ce6353b95bf3f · Keenan Nemetz · 1d ago · parent ea92d754
forge/access/runners_test.go +61−32
| @@ -1,25 +1,28 @@ | |||
| 1 | 1 | package access | |
| 2 | 2 | ||
| 3 | // Who may report a CI result is an allowlist, not a role: it answers "may this | ||
| 4 | // account's build be believed about this repository", not "may this account push to | ||
| 5 | // it". These tests pin the two halves of that rule — the list, and the admins who are | ||
| 6 | // implicit in it. | ||
| 7 | import "testing" | ||
| 3 | // Who may report a CI result is the runner role: it answers "may this account's | ||
| 4 | // build be believed about this repository", not "may this account push to it". | ||
| 5 | // Maintainers and admins sit above it, so they count without an explicit entry. | ||
| 6 | // These tests pin the role and the migration from the retired allowlist. | ||
| 7 | import ( | ||
| 8 | "bytes" | ||
| 9 | "testing" | ||
| 10 | ) | ||
| 8 | 11 | ||
| 9 | func TestOnlyRunnersAndAdminsCount(t *testing.T) { | ||
| 12 | func TestRunnerRoleAndThoseAboveItCount(t *testing.T) { | ||
| 10 | 13 | t.Parallel() | |
| 11 | 14 | a := Empty("owner-id") | |
| 12 | a.Entries["writer-id"] = "maintain" | ||
| 15 | a.Entries["maintainer-id"] = "maintain" | ||
| 16 | a.Entries["runner-id"] = "runner" | ||
| 13 | 17 | a.Entries["reader-id"] = "read" | |
| 14 | a.SetRunners([]string{"ci-runner", "writer-id"}) | ||
| 15 | 18 | for _, tc := range []struct { | |
| 16 | 19 | account string | |
| 17 | 20 | want bool | |
| 18 | 21 | why string | |
| 19 | 22 | }{ | |
| 20 | {"owner-id", true, "an admin can rewrite this list, so restricting them here is theatre"}, | ||
| 21 | {"writer-id", true, "named as a runner"}, | ||
| 22 | {"ci-runner", true, "named as a runner, with no role at all"}, | ||
| 23 | {"owner-id", true, "an admin is above the runner role"}, | ||
| 24 | {"maintainer-id", true, "a maintainer is above the runner role"}, | ||
| 25 | {"runner-id", true, "the runner role is the grant"}, | ||
| 23 | 26 | {"reader-id", false, "read access is not trust in a build"}, | |
| 24 | 27 | {"stranger", false, "nobody"}, | |
| 25 | 28 | {"", false, "an unattributed result is answerable to nobody"}, | |
| @@ -30,42 +33,68 @@ func TestOnlyRunnersAndAdminsCount(t *testing.T) { | |||
| 30 | 33 | } | |
| 31 | 34 | } | |
| 32 | 35 | ||
| 33 | // An empty list is not "nobody can report": it is "nobody beyond the admins", and the | ||
| 34 | // distinction matters because every repository starts with an empty list. | ||
| 35 | func TestAnEmptyRunnerListStillLetsAdminsReport(t *testing.T) { | ||
| 36 | // A new repository has no explicit entries, but its owner is an admin and admins | ||
| 37 | // are runners. The rule is about the role, not about the row being present. | ||
| 38 | func TestAnEmptyRuleSetStillLetsMaintainersAndAdminsReport(t *testing.T) { | ||
| 36 | 39 | t.Parallel() | |
| 37 | 40 | a := Empty("owner-id") | |
| 38 | 41 | if !a.CanReport("owner-id") { | |
| 39 | 42 | t.Fatal("the owner of a new repository cannot report CI results") | |
| 40 | 43 | } | |
| 41 | if a.CanReport("writer-id") { | ||
| 42 | t.Fatal("a stranger's result counted") | ||
| 44 | a.Entries["maintainer"] = "maintain" | ||
| 45 | if !a.CanReport("maintainer") { | ||
| 46 | t.Fatal("a maintainer's result did not count") | ||
| 47 | } | ||
| 48 | a.Entries["reader"] = "read" | ||
| 49 | if a.CanReport("reader") { | ||
| 50 | t.Fatal("a read-only account's result counted") | ||
| 43 | 51 | } | |
| 44 | 52 | } | |
| 45 | func TestRunnerListIsNormalised(t *testing.T) { | ||
| 53 | |||
| 54 | // The retired allowlist is read once and folded into the role. An account named | ||
| 55 | // there becomes a runner; one that already holds a higher role keeps it, because | ||
| 56 | // the migration must never lower a permission. | ||
| 57 | func TestLegacyRunnerAllowlistBecomesTheRole(t *testing.T) { | ||
| 46 | 58 | t.Parallel() | |
| 47 | a := Empty("owner-id") | ||
| 48 | a.SetRunners([]string{"b", " a ", "a", "", "c"}) | ||
| 49 | if len(a.Runners) != 3 || a.Runners[0] != "a" || a.Runners[1] != "b" || a.Runners[2] != "c" { | ||
| 50 | t.Fatalf("runners = %#v, want a b c with no blanks or duplicates", a.Runners) | ||
| 59 | parsed, err := Parse([]byte(`{"version":1,"default":"none","entries":{"lower":"read","higher":"maintain"},"runners":["lower","higher","new-runner","new-runner",""]}`)) | ||
| 60 | if err != nil { | ||
| 61 | t.Fatal(err) | ||
| 62 | } | ||
| 63 | if got := parsed.Entries["lower"]; got != "runner" { | ||
| 64 | t.Fatalf("allowlisted read entry = %q, want runner", got) | ||
| 65 | } | ||
| 66 | if got := parsed.Entries["higher"]; got != "maintain" { | ||
| 67 | t.Fatalf("allowlisted maintain entry = %q, want maintain", got) | ||
| 51 | 68 | } | |
| 52 | // The same rule on the way in from disk, so a hand-edited file cannot leave a | ||
| 53 | // list that reads differently from how it behaves. | ||
| 54 | parsed, err := Parse([]byte(`{"version":1,"default":"none","runners":["b","a","b",""]}`)) | ||
| 69 | if got := parsed.Entries["new-runner"]; got != "runner" { | ||
| 70 | t.Fatalf("allowlisted account = %q, want runner", got) | ||
| 71 | } | ||
| 72 | if !parsed.CanReport("lower") || !parsed.CanReport("higher") || !parsed.CanReport("new-runner") { | ||
| 73 | t.Fatalf("Migrated roles do not report: %#v", parsed.Entries) | ||
| 74 | } | ||
| 75 | // The retired field is not written back. The next read sees only roles. | ||
| 76 | data, err := parsed.Bytes() | ||
| 77 | if err != nil { | ||
| 78 | t.Fatal(err) | ||
| 79 | } | ||
| 80 | if bytes.Contains(data, []byte(`"runners"`)) { | ||
| 81 | t.Fatalf("the retired allowlist was written back:\n%s", data) | ||
| 82 | } | ||
| 83 | back, err := Parse(data) | ||
| 55 | 84 | if err != nil { | |
| 56 | 85 | t.Fatal(err) | |
| 57 | 86 | } | |
| 58 | if len(parsed.Runners) != 2 || parsed.Runners[0] != "a" || parsed.Runners[1] != "b" { | ||
| 59 | t.Fatalf("parsed runners = %#v", parsed.Runners) | ||
| 87 | if back.Entries["lower"] != "runner" || back.Entries["higher"] != "maintain" { | ||
| 88 | t.Fatalf("roles did not survive a round trip: %#v", back.Entries) | ||
| 60 | 89 | } | |
| 61 | 90 | } | |
| 62 | 91 | ||
| 63 | // The list has to survive a round trip through the ref it is stored on, since that is | ||
| 64 | // the only copy. | ||
| 65 | func TestRunnerListRoundTrips(t *testing.T) { | ||
| 92 | // A rule set with the new role has to survive the same round trip: it is the only | ||
| 93 | // copy of the repository's rules. | ||
| 94 | func TestRunnerRoleRoundTrips(t *testing.T) { | ||
| 66 | 95 | t.Parallel() | |
| 67 | 96 | a := Empty("owner-id") | |
| 68 | a.SetRunners([]string{"ci-runner"}) | ||
| 97 | a.Entries["ci-runner"] = "runner" | ||
| 69 | 98 | a.Protected = []string{"refs/heads/main"} | |
| 70 | 99 | data, err := a.Bytes() | |
| 71 | 100 | if err != nil { | |
| @@ -75,7 +104,7 @@ func TestRunnerListRoundTrips(t *testing.T) { | |||
| 75 | 104 | if err != nil { | |
| 76 | 105 | t.Fatal(err) | |
| 77 | 106 | } | |
| 78 | if len(back.Runners) != 1 || back.Runners[0] != "ci-runner" || !back.CanReport("ci-runner") { | ||
| 79 | t.Fatalf("runners did not survive: %#v", back.Runners) | ||
| 107 | if back.Entries["ci-runner"] != "runner" || !back.CanReport("ci-runner") { | ||
| 108 | t.Fatalf("the runner role did not survive: %#v", back.Entries) | ||
| 80 | 109 | } | |
| 81 | 110 | } | |