Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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

server/integration_test.go +59−120

@@ -445,6 +445,32 @@ func (f *fixture) addWriter(t *testing.T) string {
445 445 return priv
446 446 }
447 447
448 // addBot creates a bot account with one repository role and returns its private
449 // key. The role is a repository entry, not an instance role, so the test exercises
450 // the same per-repository grant an operator gives a runner.
451 func (f *fixture) addBot(t *testing.T, name string, role access.Role) string {
452 t.Helper()
453 pub, priv := keypair(t, filepath.Join(f.cfg.DataDir, "id_"+name))
454 acct, err := f.srv.Identity.CreateBot(name, access.None, pub, "test")
455 if err != nil {
456 t.Fatal(err)
457 }
458 m := mustRepo(t, f)
459 a, err := m.ACL()
460 if err != nil {
461 t.Fatal(err)
462 }
463 if role == access.None {
464 delete(a.Entries, acct.Name)
465 } else {
466 a.Entries[acct.Name] = role.String()
467 }
468 if err := m.SetACL(a, "test"); err != nil {
469 t.Fatal(err)
470 }
471 return priv
472 }
473
448 474 // accountName checks that a fixture account is active and returns its name, the
449 475 // identifier ACLs and approvals store.
450 476 func (f *fixture) accountName(name string) string {
@@ -3826,15 +3852,24 @@ func TestProtectedBranchRequiresGreenChecks(t *testing.T) {
3826 3852 }
3827 3853 }
3828 3854
3829 // TestOnlyAllowedRunnersGreenLightABranch: a green run counts only if the host let
3830 // the account that reported it. A required check is a claim about a commit, and
3831 // without this rule any writer could satisfy it by running CI on their own machine.
3855 // TestOnlyAllowedRunnersGreenLightABranch: a green run counts only if the
3856 // repository gives the account that reported it the runner role. A required check
3857 // is a claim about a commit, and without this rule any reader could satisfy it by
3858 // running CI on their own machine.
3832 3859 func TestOnlyAllowedRunnersGreenLightABranch(t *testing.T) {
3833 3860 t.Parallel()
3834 3861 f := newFixtureWith(t, func(c *config.Config) { c.LeaseSeconds = 2 })
3835 3862 writerKey := f.addWriter(t)
3836 wilbur := f.accountName("wilbur")
3837 3863 f.publish(f.repo)
3864
3865 // A build account with read access and no runner role. It can clone a public
3866 // repository, but it cannot lease work and its result does not count.
3867 ciPub, ciKey := keypair(t, filepath.Join(f.cfg.DataDir, "id_ci"))
3868 if _, err := f.srv.Identity.Create("ci", ciPub, "test"); err != nil {
3869 t.Fatal(err)
3870 }
3871 ci := f.accountName("ci")
3872
3838 3873 if _, errs, err := f.git(workRepo(t, map[string]string{"a.md": "a\n"}),
3839 3874 "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
3840 3875 t.Fatalf("initial main: %s", errs)
@@ -3854,149 +3889,53 @@ func TestOnlyAllowedRunnersGreenLightABranch(t *testing.T) {
3854 3889
3855 3890 // Not a runner yet: the job is not even leased, because running a job executes
3856 3891 // the repository's code. The push stays queued rather than silently green.
3857 f.runOnceAs(t, writerKey)
3892 f.runOnceAs(t, ciKey)
3858 3893 if got := f.waitForRun(t, 10*time.Second, func(r *runs.Run) bool { return r.SHA == head }); got.Status != runs.StatusQueued {
3859 3894 t.Fatalf("a non-runner was given work: %+v", got)
3860 3895 }
3861 3896
3862 // Allow the account, through the command a host admin would use.
3863 out, errs, err := f.cli("", "acl", "runners", "add", f.repo, "wilbur", "--config", f.cfgFile(t))
3897 // Grant the role, through the command a host admin would use.
3898 out, errs, err := f.cli("", "acl", "set-role", f.repo, "ci", "runner", "--config", f.cfgFile(t))
3864 3899 if err != nil {
3865 t.Fatalf("acl runners: %v\n%s", err, errs)
3900 t.Fatalf("acl set-role: %v\n%s", err, errs)
3866 3901 }
3867 if !strings.Contains(out, "wilbur") {
3868 t.Fatalf("acl runners did not name the account:\n%s", out)
3902 if !strings.Contains(out, "ci") {
3903 t.Fatalf("acl set-role did not name the account:\n%s", out)
3869 3904 }
3870 f.runOnceAs(t, writerKey)
3905 f.runOnceAs(t, ciKey)
3871 3906 done := f.waitForRun(t, 20*time.Second, func(r *runs.Run) bool {
3872 3907 return r.SHA == head && r.Status == runs.StatusSuccess
3873 3908 })
3874 3909 att, err := f.srv.Runs.Attestation(done.ID)
3875 if err != nil || att == nil || att.Signer != wilbur {
3876 t.Fatalf("the run is not attributed to wilbur: %+v, %v", att, err)
3910 if err != nil || att == nil || att.Signer != ci {
3911 t.Fatalf("the run is not attributed to ci: %+v, %v", att, err)
3877 3912 }
3878 3913
3879 // Revoking trust is what the rule is for: the same green result stops counting,
3880 // and the refusal says who reported it and what to do about it.
3881 a, _ = m.ACL()
3882 a.SetRunners(nil)
3883 if err := m.SetACL(a, "test"); err != nil {
3884 t.Fatal(err)
3914 // Revoking the role is what the rule is for: the same green result stops
3915 // counting, and the refusal says who reported it and what to do about it.
3916 if _, errs, err := f.cli("", "acl", "set-role", f.repo, "ci", "read", "--config", f.cfgFile(t)); err != nil {
3917 t.Fatalf("revoke runner role: %v\n%s", err, errs)
3885 3918 }
3886 3919 f.openAndApprove(t, head, "wilbur")
3887 3920 _, errs, err = f.gitWithKey(writerKey, feature, "push", f.sshURL(f.repo), "HEAD:refs/heads/main")
3888 3921 if err == nil {
3889 t.Fatal("a result from a revoked runner landed on a protected branch")
3922 t.Fatal("a result from an account without the runner role landed on a protected branch")
3890 3923 }
3891 if !strings.Contains(errs, "is not a runner for this repository") || !strings.Contains(errs, "acl runners") {
3924 if !strings.Contains(errs, "is not a runner for this repository") || !strings.Contains(errs, "set-role") || !strings.Contains(errs, "runner") {
3892 3925 t.Fatalf("the refusal does not say why:\n%s", errs)
3893 3926 }
3894 3927 // The page explains the same thing where somebody would look for it.
3895 3928 if _, page := f.get("/" + f.repo + "/runs/" + done.ID); !strings.Contains(page, "not a check") ||
3896 !strings.Contains(page, "wilbur is not a runner for this repository") {
3929 !strings.Contains(page, "ci is not a runner for this repository") {
3897 3930 t.Fatalf("the run page does not say the result does not count:\n%s", page)
3898 3931 }
3899 3932
3900 // Allowing it again is enough: nothing else changed, and nothing was lost.
3901 if _, errs, err := f.cli("", "acl", "runners", "add", f.repo, "wilbur", "--config", f.cfgFile(t)); err != nil {
3902 t.Fatalf("acl runners: %v\n%s", err, errs)
3933 // Granting it again is enough: nothing else changed, and nothing was lost.
3934 if _, errs, err := f.cli("", "acl", "set-role", f.repo, "ci", "runner", "--config", f.cfgFile(t)); err != nil {
3935 t.Fatalf("regrant runner role: %v\n%s", err, errs)
3903 3936 }
3904 3937 if _, errs, err := f.gitWithKey(writerKey, feature, "push", f.sshURL(f.repo), "HEAD:refs/heads/main"); err != nil {
3905 t.Fatalf("an allowed runner's result was refused: %s", errs)
3906 }
3907 }
3908
3909 // TestACLRunnersEditsTheList: add, set, remove and clear are separate verbs,
3910 // because replacing a list to add a sixth name is how a list gets shortened by mistake.
3911 func TestACLRunnersEditsTheList(t *testing.T) {
3912 t.Parallel()
3913 f := newFixture(t)
3914 f.addWriter(t)
3915 f.publish(f.repo)
3916 cfg := f.cfgFile(t)
3917 runners := func() []string {
3918 t.Helper()
3919 a, err := mustRepo(t, f).ACL()
3920 if err != nil {
3921 t.Fatal(err)
3922 }
3923 return a.Runners
3924 }
3925
3926 if out, errs, err := f.cli("", "acl", "runners", "add", f.repo, "wilbur", "--config", cfg); err != nil {
3927 t.Fatalf("add: %v\n%s", err, errs)
3928 } else if !strings.Contains(out, "wilbur") {
3929 t.Fatalf("add did not report the account:\n%s", out)
3930 }
3931 if got := runners(); len(got) != 1 || got[0] != f.accountName("wilbur") {
3932 t.Fatalf("runners after add wilbur = %#v", got)
3933 }
3934
3935 // Adding again is a no-op rather than a duplicate in the file.
3936 if _, errs, err := f.cli("", "acl", "runners", "add", f.repo, "wilbur", "--config", cfg); err != nil {
3937 t.Fatalf("second add: %v\n%s", err, errs)
3938 }
3939 if got := runners(); len(got) != 1 {
3940 t.Fatalf("a repeated add duplicated the account: %#v", got)
3941 }
3942
3943 // The owner is an admin, so they already count; naming them is legal but adds
3944 // nothing, and an empty list is rendered as what it means rather than as "none".
3945 if out, _, err := f.cli("", "acl", "runners", "list", f.repo, "--config", cfg); err != nil {
3946 t.Fatalf("show: %v", err)
3947 } else if !strings.Contains(out, "wilbur") {
3948 t.Fatalf("show did not list the runner:\n%s", out)
3949 }
3950
3951 // set replaces the list: a repo with one runner stays a repo with one.
3952 if _, errs, err := f.cli("", "acl", "runners", "set", f.repo, "keenan", "--config", cfg); err != nil {
3953 t.Fatalf("replace: %v\n%s", err, errs)
3954 }
3955 if got := runners(); len(got) != 1 || got[0] != f.ownerID {
3956 t.Fatalf("runners after replacement = %#v", got)
3957 }
3958
3959 // Removing somebody who is not on the list is an error, the way `git config
3960 // --unset` refuses a key that is not set: a revocation that never happened
3961 // should not look like one that did.
3962 if _, errs, err := f.cli("", "acl", "runners", "remove", f.repo, "wilbur", "--config", cfg); err == nil {
3963 t.Fatalf("removing a non-runner was accepted:\n%s", errs)
3964 } else if !strings.Contains(errs, "is not a runner for") {
3965 t.Fatalf("the refusal does not say what happened:\n%s", errs)
3966 }
3967 if got := runners(); len(got) != 1 {
3968 t.Fatalf("a refused removal changed the list: %#v", got)
3969 }
3970
3971 // Removal is a subcommand, not a whole-list replacement.
3972 if _, errs, err := f.cli("", "acl", "runners", "remove", f.repo, "keenan", "--config", cfg); err != nil {
3973 t.Fatalf("remove: %v\n%s", err, errs)
3974 }
3975 if got := runners(); len(got) != 0 {
3976 t.Fatalf("runners after remove = %#v", got)
3977 }
3978
3979 // And clear returns to admins-only, which is where a new repository starts.
3980 if _, errs, err := f.cli("", "acl", "runners", "add", f.repo, "wilbur", "--config", cfg); err != nil {
3981 t.Fatalf("re-add: %v\n%s", err, errs)
3982 }
3983 if _, errs, err := f.cli("", "acl", "runners", "clear", f.repo, "--config", cfg); err != nil {
3984 t.Fatalf("clear: %v\n%s", err, errs)
3985 }
3986 if got := runners(); len(got) != 0 {
3987 t.Fatalf("runners after clear = %#v", got)
3988 }
3989 // Modes are subcommands now; the old flag form is gone rather than accepted
3990 // silently with different semantics.
3991 if _, errs, err := f.cli("", "acl", "runners", "--add", f.repo, "wilbur", "--config", cfg); err == nil {
3992 t.Fatalf("the old --add form was accepted:\n%s", errs)
3993 }
3994
3995 // An unknown account is refused rather than written as a typo that silently
3996 // permits nobody.
3997 _, errs, err := f.cli("", "acl", "runners", "add", f.repo, "nobody", "--config", cfg)
3998 if err == nil {
3999 t.Fatalf("an unknown account was accepted:\n%s", errs)
3938 t.Fatalf("a run from the runner role was refused: %s", errs)
4000 3939 }
4001 3940 }
4002 3941