gunk-dev/the-valley

pushpolicy.go

sourcec14b0e68e9c3main's published tip history
3.8 KiB
1// Package pushpolicy reads a project's push policy: what the host
2// declaration (schema/valley.cue) says about pushes to one project,
3// exported as JSON by the host module (nix/valley-host.nix). The
4// pre-receive hook reads it to decide every push, and a reader that
5// shares this package reads the file exactly as the hook does.
6//
7// The package depends on the standard library and exactjson alone.
8package pushpolicy
9
10import (
11 "fmt"
12 "os"
13
14 "the-valley/exactjson"
15)
16
17// Policy is a project's push policy: the protected refs and their writers
18// from its protection block, its named ref grants, and whether it retains
19// its change records. A project that declares no protection block
20// protects nothing.
21type Policy struct {
22 // Refs are the protected ref patterns. A ref one of them matches takes
23 // a push only from one of Writers, on top of whatever else its
24 // namespace requires.
25 Refs []string `json:"refs"`
26 Writers []string `json:"writers"`
27
28 // Grants open refs the namespace rules close, each to the principals
29 // it names. Tags open only this way.
30 Grants map[string]Grant `json:"grants"`
31
32 // RetainChangeRecords keeps every change record the project holds: no
33 // push deletes one, whatever grant the pusher holds. A record's ref
34 // then holds the change's whole history, so a reader can tell a
35 // change that stayed ready from one withdrawn or unreadied and readied
36 // again. Left out, it is false.
37 RetainChangeRecords bool `json:"retainChangeRecords"`
38}
39
40// Grant is one named grant: ref patterns, and who may write them.
41type Grant struct {
42 Refs []string `json:"refs"`
43 Writers []string `json:"writers"`
44}
45
46// Read reads the push policy at path. A field this package does not know
47// refuses the read: the policy is exported from the schema this package
48// ships beside, so an unknown field means the two have drifted apart, and
49// the field could be a restriction a reader would otherwise skip.
50//
51// Every key must spell its field exactly, and no key may appear twice in
52// one object. encoding/json alone matches a key to a field without regard
53// to case. A host declaration that reopens a schema definition could then
54// name a writer under a field spelled another way, beside the writers a
55// reviewer reads.
56func Read(path string) (Policy, error) {
57 var p Policy
58 data, err := os.ReadFile(path)
59 if err != nil {
60 return p, err
61 }
62 if err := exactjson.Decode(data, &p); err != nil {
63 return Policy{}, fmt.Errorf("%s: %w", path, err)
64 }
65 return p, nil
66}
67
68// Protects reports whether ref, a full ref name, is one of the project's
69// protected refs.
70func (p Policy) Protects(ref string) bool {
71 for _, pattern := range p.Refs {
72 if Match(pattern, ref) {
73 return true
74 }
75 }
76 return false
77}
78
79// Sealed reports whether ref, a full ref name, is protected and no push
80// may write it: the policy declares no writer, so the ref moves only by a
81// write made on the host.
82func (p Policy) Sealed(ref string) bool {
83 return p.Protects(ref) && len(p.Writers) == 0
84}
85
86// Match reports whether ref matches a declared pattern. `*` matches any
87// run of characters, path separators included, which is how the patterns
88// are documented (schema/valley.cue). Every other character matches
89// itself: the schema refuses the characters a shell glob would read
90// specially, because git refuses them in a refname too.
91func Match(pattern, ref string) bool {
92 p, r := 0, 0
93 star, resume := -1, 0
94 for r < len(ref) {
95 switch {
96 case p < len(pattern) && pattern[p] == '*':
97 star, resume = p, r
98 p++
99 case p < len(pattern) && pattern[p] == ref[r]:
100 p++
101 r++
102 case star >= 0:
103 // Let the last star take one more character, and try the
104 // rest of the pattern again from there.
105 resume++
106 p, r = star+1, resume
107 default:
108 return false
109 }
110 }
111 for p < len(pattern) && pattern[p] == '*' {
112 p++
113 }
114 return p == len(pattern)
115}