| 1 | // valleyhook is the pre-receive hook of a protected project on a valley |
| 2 | // host: the whole of what a push may write there. The NixOS module |
| 3 | // (nix/valley-host.nix) installs a two-line script as the hook, and that |
| 4 | // script only hands this program the pushing principal, the project's |
| 5 | // declared protection, and the grants files. Every decision about a ref is |
| 6 | // made here, so the policy lives in one place. |
| 7 | package main |
| 8 | |
| 9 | import ( |
| 10 | "bufio" |
| 11 | "encoding/json" |
| 12 | "flag" |
| 13 | "fmt" |
| 14 | "io" |
| 15 | "os" |
| 16 | "strings" |
| 17 | ) |
| 18 | |
| 19 | const usage = `usage: valleyhook pre-receive [flags] < ref updates |
| 20 | |
| 21 | The pre-receive hook of a protected project on a valley host. git hands it |
| 22 | one line per ref a push would update: the old id, the new id and the |
| 23 | refname. It accepts the push only if the pushing principal may make every |
| 24 | one of those writes. One refusal refuses the whole push, because a |
| 25 | pre-receive hook answers for the push and not for each ref. |
| 26 | |
| 27 | Who may write a ref, decided by the first rule that matches it: |
| 28 | |
| 29 | refs/replace/* nobody. A replacement ref makes one object |
| 30 | stand in for another wherever git looks it |
| 31 | up, so it would change what every reader of |
| 32 | the repository sees. |
| 33 | refs/the-valley/attestations/* |
| 34 | anyone, and only to create one. An |
| 35 | attestation is a record, and a record that |
| 36 | can be rewritten or dropped is not one. |
| 37 | a protected ref the project's declared writers. |
| 38 | refs/the-valley/integration-requests/* |
| 39 | principals holding the request grant. They |
| 40 | may file, replace and withdraw a request. |
| 41 | a ref an allow entry opens the principals that entry names. |
| 42 | refs/heads/* anyone: these are topic branches. |
| 43 | anything else nobody. Tags, notes and every other namespace |
| 44 | stay closed until the project's protection |
| 45 | opens a pattern of them by name. |
| 46 | |
| 47 | A delete is a write, and is decided like one. |
| 48 | |
| 49 | The integrator's own writes are not pushes. A controller updates the refs |
| 50 | it writes on the host, and no push hook sees a local write. |
| 51 | |
| 52 | A protection file or a grants file that cannot be read refuses the whole |
| 53 | push. The hook fails closed: a policy it cannot read is not a policy it |
| 54 | can apply. |
| 55 | |
| 56 | flags: |
| 57 | --project NAME the project, named in what a refusal says |
| 58 | --protection FILE the project's protection block, as the host |
| 59 | declaration exports it (JSON) |
| 60 | --grants FILE grants, one "<grant> <principal>" per line. Repeat |
| 61 | the flag to read more than one file; what they grant |
| 62 | adds up. A file that does not exist grants nothing. |
| 63 | --principal NAME the pushing principal; empty for a key that names |
| 64 | none |
| 65 | ` |
| 66 | |
| 67 | func main() { |
| 68 | if len(os.Args) < 2 { |
| 69 | fmt.Fprint(os.Stderr, usage) |
| 70 | os.Exit(2) |
| 71 | } |
| 72 | switch os.Args[1] { |
| 73 | case "pre-receive": |
| 74 | os.Exit(preReceive(os.Args[2:], os.Stdin, os.Stderr)) |
| 75 | case "help", "-h", "--help": |
| 76 | fmt.Print(usage) |
| 77 | default: |
| 78 | fmt.Fprintf(os.Stderr, "valleyhook: unknown command %q\n\n%s", os.Args[1], usage) |
| 79 | os.Exit(2) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // preReceive runs the hook over the updates on stdin and returns its exit |
| 84 | // status: 0 accepts the push, anything else refuses it. |
| 85 | func preReceive(args []string, stdin io.Reader, stderr io.Writer) int { |
| 86 | fs := flag.NewFlagSet("pre-receive", flag.ContinueOnError) |
| 87 | fs.SetOutput(stderr) |
| 88 | fs.Usage = func() { fmt.Fprint(stderr, usage) } |
| 89 | project := fs.String("project", "", "the project, named in what a refusal says") |
| 90 | protectionFile := fs.String("protection", "", "the project's protection block (JSON)") |
| 91 | principal := fs.String("principal", "", "the pushing principal") |
| 92 | var grantFiles []string |
| 93 | fs.Func("grants", "a grants file; repeatable", func(path string) error { |
| 94 | grantFiles = append(grantFiles, path) |
| 95 | return nil |
| 96 | }) |
| 97 | if err := fs.Parse(args); err != nil { |
| 98 | return 2 |
| 99 | } |
| 100 | if *project == "" || *protectionFile == "" { |
| 101 | fmt.Fprintf(stderr, "valleyhook: --project and --protection are required\n") |
| 102 | return 2 |
| 103 | } |
| 104 | |
| 105 | // Everything the decision needs is read before any ref is looked at, |
| 106 | // so a policy that cannot be read refuses the push as a whole rather |
| 107 | // than part of the way through it. |
| 108 | p := policy{project: *project, grants: grants{}} |
| 109 | if err := readProtection(*protectionFile, &p.protection); err != nil { |
| 110 | return failClosed(stderr, err) |
| 111 | } |
| 112 | for _, path := range grantFiles { |
| 113 | if err := p.grants.read(path); err != nil { |
| 114 | return failClosed(stderr, err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | refused := false |
| 119 | lines := bufio.NewScanner(stdin) |
| 120 | for lines.Scan() { |
| 121 | if strings.TrimSpace(lines.Text()) == "" { |
| 122 | continue |
| 123 | } |
| 124 | fields := strings.Fields(lines.Text()) |
| 125 | if len(fields) != 3 { |
| 126 | return failClosed(stderr, fmt.Errorf("git sent %q, which is not an old id, a new id and a refname", lines.Text())) |
| 127 | } |
| 128 | u := update{old: fields[0], new: fields[1], ref: fields[2]} |
| 129 | if refusal := p.decide(*principal, u); refusal != "" { |
| 130 | fmt.Fprintln(stderr, refusal) |
| 131 | refused = true |
| 132 | } |
| 133 | } |
| 134 | if err := lines.Err(); err != nil { |
| 135 | return failClosed(stderr, err) |
| 136 | } |
| 137 | if refused { |
| 138 | return 1 |
| 139 | } |
| 140 | return 0 |
| 141 | } |
| 142 | |
| 143 | // failClosed refuses the whole push for a reason that is not about any one |
| 144 | // ref. |
| 145 | func failClosed(stderr io.Writer, err error) int { |
| 146 | fmt.Fprintf(stderr, "valley: the push is refused because its policy could not be read: %v\n", err) |
| 147 | return 1 |
| 148 | } |
| 149 | |
| 150 | // readProtection reads the project's protection block. A field this |
| 151 | // program does not know refuses the read: the block is exported from the |
| 152 | // schema this program ships beside, so an unknown field means the two have |
| 153 | // drifted apart, and the field could be a restriction the hook would |
| 154 | // otherwise skip. |
| 155 | func readProtection(path string, p *protection) error { |
| 156 | f, err := os.Open(path) |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | defer f.Close() |
| 161 | dec := json.NewDecoder(f) |
| 162 | dec.DisallowUnknownFields() |
| 163 | if err := dec.Decode(p); err != nil { |
| 164 | return fmt.Errorf("%s: %w", path, err) |
| 165 | } |
| 166 | return nil |
| 167 | } |