gunk-dev/the-valley

policy.go

sourceef0e6a83672emain's published tip history
6.0 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6)
7
8// protection is a project's protection block, as the host declaration
9// exports it (schema/valley.cue, #Protection).
10type protection struct {
11 // Refs are the protected ref patterns. Only Writers may write a ref
12 // one of them matches.
13 Refs []string `json:"refs"`
14 Writers []string `json:"writers"`
15
16 // Allow opens refs the default rules close, each to the principals it
17 // names.
18 Allow []opening `json:"allow"`
19}
20
21// opening is one allow entry: ref patterns, and who may write them.
22type opening struct {
23 Refs []string `json:"refs"`
24 Writers []string `json:"writers"`
25}
26
27// The namespaces whose rule is fixed rather than declared.
28const (
29 replacePrefix = "refs/replace/"
30 attestationPrefix = "refs/the-valley/attestations/"
31 requestPrefix = "refs/the-valley/integration-requests/"
32 branchPrefix = "refs/heads/"
33
34 // grantRequest is the grant that opens the integration-request
35 // namespace (dcr-e544f20).
36 grantRequest = "request"
37)
38
39// policy is everything one push is judged against.
40type policy struct {
41 project string
42 protection protection
43 grants grants
44}
45
46// update is one line of what git hands a pre-receive hook.
47type update struct {
48 old, new, ref string
49}
50
51// decide returns "" when principal may make the write, and otherwise the
52// sentence that refuses it. An empty principal is a key that names none:
53// it is nobody's writer and holds no grant, so it writes only what anyone
54// may.
55//
56// The rules are tried in order and the first one whose namespace matches
57// decides. The order is what keeps a declaration from opening more than it
58// says: replacement refs are refused before anything else is consulted, a
59// protected ref stays its writers' alone, and an allow entry is consulted
60// only for refs the fixed rules have not already answered for.
61func (p policy) decide(principal string, u update) string {
62 who := principal
63 if who == "" {
64 who = "<untagged key>"
65 }
66 refuse := func(verb, why string) string {
67 return fmt.Sprintf("valley: %s may not %s %s — %s", who, verb, u.ref, why)
68 }
69
70 switch {
71 case strings.HasPrefix(u.ref, replacePrefix):
72 return refuse("write", "refs/replace/ is closed to every push: a replacement ref makes one object stand in for another wherever git looks it up, so it would change what every reader of the repository sees")
73
74 case strings.HasPrefix(u.ref, attestationPrefix):
75 // An all-zero old id is a creation, at any hash length.
76 if !isZero(u.old) {
77 return refuse("update", "attestation refs are create-only")
78 }
79 return ""
80 }
81
82 if p.protection.protects(u.ref) {
83 if principal != "" && contains(p.protection.Writers, principal) {
84 return ""
85 }
86 return refuse("write", fmt.Sprintf("a protected ref of %s (%s)", p.project, p.protection.whoMay()))
87 }
88
89 if strings.HasPrefix(u.ref, requestPrefix) {
90 if principal != "" && p.grants.holds(principal, grantRequest) {
91 return ""
92 }
93 holder := who + " holds none"
94 if principal == "" {
95 holder = "a key that names no principal holds no grant"
96 }
97 return refuse("write", fmt.Sprintf("%s takes writes only from a principal holding the %s grant, and %s", requestPrefix, grantRequest, holder))
98 }
99
100 opened := p.protection.openedTo(u.ref)
101 if principal != "" && contains(opened, principal) {
102 return ""
103 }
104 if strings.HasPrefix(u.ref, branchPrefix) {
105 return ""
106 }
107
108 closed := namespace(u.ref)
109 if len(opened) > 0 {
110 return refuse("write", fmt.Sprintf("%s is closed to pushes, and the protection of %s opens this ref to %s only", closed, p.project, strings.Join(opened, ", ")))
111 }
112 return refuse("write", fmt.Sprintf("%s is closed to pushes, and no allow entry in the protection of %s opens this ref", closed, p.project))
113}
114
115// protects reports whether ref is one of the project's protected refs.
116func (p protection) protects(ref string) bool {
117 for _, pattern := range p.Refs {
118 if match(pattern, ref) {
119 return true
120 }
121 }
122 return false
123}
124
125// whoMay is what a refusal says about who may write a protected ref
126// instead. With no writer declared there is no one, so it says how a
127// change lands.
128func (p protection) whoMay() string {
129 if len(p.Writers) == 0 {
130 return "no writer is declared — changes land by integration request"
131 }
132 return "writers: " + strings.Join(p.Writers, ", ")
133}
134
135// openedTo lists the principals the allow entries open ref to, in the
136// order they are declared.
137func (p protection) openedTo(ref string) []string {
138 var who []string
139 for _, o := range p.Allow {
140 for _, pattern := range o.Refs {
141 if match(pattern, ref) {
142 for _, w := range o.Writers {
143 if !contains(who, w) {
144 who = append(who, w)
145 }
146 }
147 break
148 }
149 }
150 }
151 return who
152}
153
154// match reports whether ref matches a declared pattern. `*` matches any
155// run of characters, path separators included, which is how the patterns
156// are documented (schema/valley.cue). Every other character matches
157// itself: the schema refuses the characters a shell glob would read
158// specially, because git refuses them in a refname too.
159func match(pattern, ref string) bool {
160 p, r := 0, 0
161 star, resume := -1, 0
162 for r < len(ref) {
163 switch {
164 case p < len(pattern) && pattern[p] == '*':
165 star, resume = p, r
166 p++
167 case p < len(pattern) && pattern[p] == ref[r]:
168 p++
169 r++
170 case star >= 0:
171 // Let the last star take one more character, and try the
172 // rest of the pattern again from there.
173 resume++
174 p, r = star+1, resume
175 default:
176 return false
177 }
178 }
179 for p < len(pattern) && pattern[p] == '*' {
180 p++
181 }
182 return p == len(pattern)
183}
184
185// namespace is the part of a refname a refusal names as closed:
186// refs/<kind>/, or refs/the-valley/<kind>/ for the valley's own refs.
187func namespace(ref string) string {
188 parts := strings.Split(ref, "/")
189 n := 2
190 if len(parts) > 1 && parts[1] == "the-valley" {
191 n = 3
192 }
193 if len(parts) <= n {
194 return ref
195 }
196 return strings.Join(parts[:n], "/") + "/"
197}
198
199func isZero(id string) bool {
200 return id != "" && strings.Trim(id, "0") == ""
201}
202
203func contains(list []string, s string) bool {
204 for _, x := range list {
205 if x == s {
206 return true
207 }
208 }
209 return false
210}