gunk-dev/the-valley

policy_test.go

source4cbb5ec9c5dbmain's published tip history
12.7 KiB
1package main
2
3import (
4 "errors"
5 "strings"
6 "testing"
7
8 "the-valley/approval"
9 "the-valley/change"
10 "the-valley/knot"
11)
12
13// fakeRepository holds no objects.
14type fakeRepository struct{}
15
16func (f fakeRepository) symbolic([]string) (map[string]bool, error) { return nil, nil }
17func (f fakeRepository) notes(string) (map[string][]byte, error) {
18 return nil, errors.New("holds no objects")
19}
20func (f fakeRepository) record(string) (fileCommit, error) {
21 return fileCommit{}, errors.New("holds no objects")
22}
23func (f fakeRepository) report(string) (fileCommit, error) {
24 return fileCommit{}, errors.New("holds no objects")
25}
26func (f fakeRepository) approval(string) ([]byte, error) {
27 return nil, errors.New("holds no objects")
28}
29func (f fakeRepository) hasRecord(string) (bool, error) {
30 return false, errors.New("holds no refs")
31}
32func (f fakeRepository) ref(string) (string, bool, error) { return "", false, nil }
33func (f fakeRepository) kind(string) (string, error) { return "", errors.New("holds no objects") }
34func (f fakeRepository) records() (int, error) { return 0, nil }
35func (f fakeRepository) checkRequests(string) (int, error) { return 0, nil }
36func (f fakeRepository) graph() (graphReader, error) { return nil, errors.New("holds no objects") }
37
38// Every rule that applies to a ref has to allow the write, and none of
39// them can be satisfied by another. Each case here is a declaration that
40// tries to stand in for a rule it is not: a writer list covering a
41// namespace whose rule is a grant or a prohibition, a grant covering one
42// whose rule is fixed, and a ref name standing for another ref.
43func TestNoRuleStandsInForAnother(t *testing.T) {
44 const zero = "0000000000000000000000000000000000000000"
45 const some = "1111111111111111111111111111111111111111"
46 p := policy{
47 project: "everything",
48 push: pushPolicy{
49 // Every ref the project has is protected, and the writer is
50 // also opened every ref by a grant.
51 Refs: []string{"refs/*"},
52 Writers: []string{"writer"},
53 Grants: map[string]refGrant{
54 "everything": {Refs: []string{"refs/*"}, Writers: []string{"writer", "opened"}},
55 },
56 },
57 grants: grants{"requester": {"request": true}},
58 repo: fakeRepository{},
59 symrefs: map[string]bool{"refs/heads/alias": true},
60 }
61 for _, c := range []struct {
62 who, ref string
63 allowed bool
64 says string
65 }{
66 // Prohibited outright, to the writer the grant and the protection
67 // both name.
68 {"writer", "refs/replace/" + some, false, "refs/replace/ is closed to every push"},
69 {"writer", "refs/notes/commits", false, "refs/notes/ is closed to every push"},
70 {"writer", "refs/the-valley/integration-outcomes/main/c", false, "written only by the machinery"},
71 {"writer", "refs/the-valley/integration-landings/main/" + some, false, "written only by the machinery"},
72 // A symbolic ref is refused whoever writes it.
73 {"writer", "refs/heads/alias", false, "symbolic ref"},
74 // The approve grant is needed whatever else is declared, and
75 // protection is needed on top of it.
76 {"writer", "refs/the-valley/integration-requests/main/c", false, "approve grant (also accepted as request)"},
77 {"requester", "refs/the-valley/integration-requests/main/c", false, "a protected ref"},
78 // A tag takes a grant, and protection on top: being opened the
79 // ref does not make a principal its writer.
80 {"writer", "refs/tags/v1", true, ""},
81 {"opened", "refs/tags/v1", false, "a protected ref"},
82 {"requester", "refs/tags/v1", false, "opens only through a named grant"},
83 // An attestation named for a digest alone is refused before
84 // anything is read: it would block every ref beneath it.
85 {"writer", "refs/the-valley/attestations/" + strings.Repeat("a", 64), false, "<digest>/<key hash>"},
86 // The plan graph is one ref, matched exactly. A ref that only
87 // begins with its name is in the closed namespace, and the graph
88 // takes no write from a key that names no principal, before
89 // anything is read.
90 {"writer", knot.Ref + "-x", false, "written only by the machinery"},
91 {"writer", knot.Ref + "/x", false, "written only by the machinery"},
92 {"", knot.Ref, false, "takes writes only from a named principal"},
93 } {
94 got := p.decide(c.who, update{old: zero, new: some, ref: c.ref})
95 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
96 t.Errorf("%s writing %s: got %q, want allowed=%v saying %q", c.who, c.ref, got, c.allowed, c.says)
97 }
98 }
99}
100
101// The plan graph is never deleted, by anyone, before anything is read.
102func TestNoPushDeletesThePlanGraph(t *testing.T) {
103 p := policy{project: "p", repo: fakeRepository{}}
104 got := p.decide("writer", update{old: "1111111111111111111111111111111111111111", new: "0000000000000000000000000000000000000000", ref: knot.Ref})
105 if !strings.Contains(got, "may not delete refs/the-valley/knots") || !strings.Contains(got, "never deleted") {
106 t.Errorf("a delete of the graph: %q", got)
107 }
108}
109
110// refs/the-valley/flow/data, matched exactly, opens only through a named
111// grant of the project. Every neighbouring ref, case variants included,
112// stays in the closed machinery namespace even under a broad grant,
113// lookalikes stay refused, and holding the grant confers no other power.
114func TestFlowDataRef(t *testing.T) {
115 const zero = "0000000000000000000000000000000000000000"
116 const one = "1111111111111111111111111111111111111111"
117 const two = "2222222222222222222222222222222222222222"
118 id := strings.Repeat("k", 32)
119
120 // An exact grant on a project whose main is protected with no writers.
121 exact := policy{
122 project: "app",
123 push: pushPolicy{
124 Refs: []string{"refs/heads/main"},
125 Grants: map[string]refGrant{
126 "flow": {Refs: []string{flowDataRef}, Writers: []string{"agent"}},
127 },
128 },
129 repo: fakeRepository{},
130 }
131 // The same project with no grants at all.
132 noGrant := policy{
133 project: "app",
134 push: pushPolicy{Refs: []string{"refs/heads/main"}},
135 repo: fakeRepository{},
136 }
137 // A project where the ref is symbolic.
138 symref := policy{
139 project: "app",
140 push: exact.push,
141 repo: fakeRepository{},
142 symrefs: map[string]bool{flowDataRef: true},
143 }
144 // Protection covering refs/the-valley/flow/data adds a writer check on top.
145 protected := policy{
146 project: "app",
147 push: pushPolicy{
148 Refs: []string{"refs/*"},
149 Writers: []string{"writer"},
150 Grants: map[string]refGrant{
151 "flow": {Refs: []string{flowDataRef}, Writers: []string{"agent", "writer"}},
152 },
153 },
154 repo: fakeRepository{},
155 }
156
157 for _, c := range []struct {
158 name string
159 p policy
160 who string
161 u update
162 allowed bool
163 says string
164 }{
165 // Granted principal creates, updates (including non-fast-forward), and deletes the exact ref.
166 {"exact grant create", exact, "agent", update{zero, one, flowDataRef}, true, ""},
167 {"exact grant update", exact, "agent", update{one, two, flowDataRef}, true, ""},
168 {"exact grant delete", exact, "agent", update{one, zero, flowDataRef}, true, ""},
169
170 // Symbolic ref is still refused even for a grant holder.
171 {"symbolic ref refused", symref, "agent", update{one, two, flowDataRef}, false, "symbolic ref"},
172
173 // Ungranted and untagged principals are refused with the named-grant wording.
174 {"ungranted principal with grant present", exact, "other", update{zero, one, flowDataRef}, false,
175 "refs/the-valley/flow/ opens only through a named grant, and the grant flow of app opens this ref to agent only"},
176 {"untagged key with grant present", exact, "", update{zero, one, flowDataRef}, false,
177 "<untagged key> may not write refs/the-valley/flow/data — refs/the-valley/flow/ opens only through a named grant, and the grant flow of app opens this ref to agent only"},
178 {"named principal with no grant", noGrant, "agent", update{zero, one, flowDataRef}, false,
179 "refs/the-valley/flow/ opens only through a named grant, and no grant of app opens this ref"},
180 {"untagged key with no grant", noGrant, "", update{zero, one, flowDataRef}, false,
181 "<untagged key> may not write refs/the-valley/flow/data — refs/the-valley/flow/ opens only through a named grant, and no grant of app opens this ref"},
182
183 // Holding the flow grant confers nothing else.
184 {"flow grant does not open protected main", exact, "agent", update{zero, one, "refs/heads/main"}, false, "a protected ref of app"},
185 {"flow grant does not open requests", exact, "agent", update{zero, one, requestPrefix + "main/c"}, false, approveGrant},
186 {"flow grant does not open approvals", exact, "agent", update{zero, one, approval.RefPrefix + "main/c-" + id}, false, approveGrant},
187 {"flow grant does not open restack reports", exact, "agent", update{zero, one, change.RestackPrefix + "main/" + id}, false, "restack grant"},
188 {"flow grant does not delete change records", exact, "agent", update{one, zero, change.RefPrefix + "main/" + id}, false, approveGrant},
189 {"flow grant does not open tags", exact, "agent", update{zero, one, "refs/tags/v1"}, false, "refs/tags/ opens only through a named grant"},
190
191 // Protection adds on top of the grant.
192 {"protected flow ref refuses non-writer grant holder", protected, "agent", update{zero, one, flowDataRef}, false, "a protected ref of app"},
193 {"protected flow ref allows writer grant holder", protected, "writer", update{zero, one, flowDataRef}, true, ""},
194 } {
195 got := c.p.decide(c.who, c.u)
196 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
197 t.Errorf("%s (%s on %s): got %q, want allowed=%v saying %q", c.name, c.who, c.u.ref, got, c.allowed, c.says)
198 }
199 }
200
201 // A broad grant opens the exact ref and no other ref under
202 // refs/the-valley/. refs/* is the broad pattern a host can declare;
203 // the schema refuses refs/the-valley/*, and the hook holds without it.
204 const machinery = "written only by the machinery on the host, and no push may write it"
205 for _, pattern := range []string{"refs/*", "refs/the-valley/*"} {
206 broad := policy{
207 project: "app",
208 push: pushPolicy{
209 Refs: []string{"refs/heads/main"},
210 Grants: map[string]refGrant{
211 "broad": {Refs: []string{pattern}, Writers: []string{"broad"}},
212 },
213 },
214 repo: fakeRepository{},
215 }
216 for _, c := range []struct {
217 name string
218 u update
219 allowed bool
220 says string
221 }{
222 {"opens exact ref", update{zero, one, flowDataRef}, true, ""},
223 {"refused on prefix slash", update{zero, one, "refs/the-valley/flow/"}, false, machinery},
224 {"refused on prefix bare", update{zero, one, "refs/the-valley/flow"}, false, machinery},
225 {"refused on sibling", update{zero, one, "refs/the-valley/flow/other"}, false, machinery},
226 {"refused on child", update{zero, one, "refs/the-valley/flow/data/x"}, false, machinery},
227 {"refused on suffixed name", update{zero, one, "refs/the-valley/flow/data2"}, false, machinery},
228 {"refused on dashed suffix", update{zero, one, "refs/the-valley/flow/data-x"}, false, machinery},
229 {"refused on case variant of flow", update{zero, one, "refs/the-valley/Flow/data"}, false, machinery},
230 {"refused on case variant of data", update{zero, one, "refs/the-valley/flow/Data"}, false, machinery},
231 {"refused on outcomes", update{zero, one, "refs/the-valley/integration-outcomes/main/c"}, false, machinery},
232 {"refused on signers", update{zero, one, "refs/the-valley/signers"}, false, machinery},
233
234 // Nor does it bypass the other fixed namespaces under refs/the-valley/.
235 {"does not open requests", update{zero, one, requestPrefix + "main/c"}, false, approveGrant},
236 {"does not open approvals", update{zero, one, approval.RefPrefix + "main/c-" + id}, false, approveGrant},
237 {"does not open restack reports", update{zero, one, change.RestackPrefix + "main/" + id}, false, "restack grant"},
238 {"does not delete knots", update{one, zero, knot.Ref}, false, "never deleted"},
239
240 // Lookalikes of refs/the-valley/flow/data stay refused on create and update.
241 {"lookalike under refs/heads/", update{zero, one, "refs/heads/" + flowDataRef}, false, "no push may create or update a ref named refs/heads/ followed by a full ref name"},
242 {"lookalike under refs/tags/", update{zero, one, "refs/tags/" + flowDataRef}, false, "no push may create or update a ref named refs/tags/ followed by a full ref name"},
243 {"lookalike under refs/", update{zero, one, "refs/" + flowDataRef}, false, "no push may create or update a ref named refs/ followed by a full ref name"},
244 {"lookalike under refs/remotes/", update{zero, one, "refs/remotes/" + flowDataRef}, false, "no push may create or update a ref named refs/remotes/ followed by a full ref name"},
245 {"lookalike under refs/remotes/ with /HEAD", update{zero, one, "refs/remotes/" + flowDataRef + "/HEAD"}, false, "no push may create or update a ref named refs/remotes/ followed by a full ref name"},
246 } {
247 got := broad.decide("broad", c.u)
248 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
249 t.Errorf("%s grant %s (on %s): got %q, want allowed=%v saying %q", pattern, c.name, c.u.ref, got, c.allowed, c.says)
250 }
251 }
252 }
253}