gunk-dev/the-valley

policy_test.go

sourcef0de1b6b5b5dmain's published tip history
8.0 KiB
1package main
2
3import (
4 "errors"
5 "io"
6 "os"
7 "path/filepath"
8 "testing"
9
10 "the-valley/web/export"
11)
12
13// The authorization counter reads no clock and survives the process: each
14// publisher over one state directory allocates a sequence greater than
15// every one allocated before, a floor from the receiver raises it, and a
16// counter that cannot be read is an error, never a fresh start at one.
17func TestTheAuthorizationSequenceIsDurable(t *testing.T) {
18 state := t.TempDir()
19 next := func(floor int64) (int64, error) {
20 p := &publisher{cfg: config{state: state}, stdout: io.Discard, stderr: io.Discard}
21 return p.nextSequence(floor)
22 }
23 for i, c := range []struct{ floor, want int64 }{{0, 1}, {0, 2}, {10, 11}, {3, 12}, {0, 13}} {
24 got, err := next(c.floor)
25 if err != nil || got != c.want {
26 t.Fatalf("allocation %d with floor %d gave %d, %v; want %d", i+1, c.floor, got, err, c.want)
27 }
28 }
29 data, err := os.ReadFile(filepath.Join(state, authorizationSequenceFile))
30 if err != nil || string(data) != "13\n" {
31 t.Errorf("the counter holds %q, %v; want 13", data, err)
32 }
33 for _, bad := range []string{"", "x\n", "-4\n"} {
34 if err := os.WriteFile(filepath.Join(state, authorizationSequenceFile), []byte(bad), 0o600); err != nil {
35 t.Fatal(err)
36 }
37 if got, err := next(0); err == nil {
38 t.Errorf("a counter holding %q gave sequence %d", bad, got)
39 }
40 }
41}
42
43// An incomplete check keeps a project's access only from the authorization
44// the receiver last said it stored, and only while the receiver still
45// holds it. With app's policy unreadable, a pass keeps app, naming that
46// authorization as its basis. Then app's own policy withdraws anyone, and
47// the receiver stores the withdrawal. The publisher's state is then
48// restored from a copy taken before the withdrawal, and app's policy is
49// unreadable again. The restored record still lets anyone read app, but
50// the receiver refuses the authorization that keeps it, as replayed, or as
51// stale-basis when the restored counter is ahead, and the retry keeps
52// nothing it could not check.
53func TestARestoredStateRestoresNoWithdrawnAccess(t *testing.T) {
54 v := newValley(t)
55 rv := newReceiver(t, v)
56 app := filepath.Join(v.srv, "app.git")
57 stored := func() *export.Authorization {
58 t.Helper()
59 a, _, err := rv.store.Authorization()
60 if err != nil {
61 t.Fatal(err)
62 }
63 return a
64 }
65 unreadable := func(f func()) (string, error) {
66 t.Helper()
67 if err := os.Rename(app, app+".moved"); err != nil {
68 t.Fatal(err)
69 }
70 defer os.Rename(app+".moved", app)
71 _, stderr, err := v.tryPublish(nil, rv.flags()...)
72 if f != nil {
73 f()
74 }
75 return stderr, err
76 }
77
78 v.publish(rv.flags()...)
79 first := stored()
80 if !first.Allows(export.Anyone, "app", identities["app"]) {
81 t.Fatalf("the first authorization names %+v, want app", first.Projects)
82 }
83 backup := filepath.Join(t.TempDir(), "state")
84 v.run("", "cp", "-a", v.state, backup)
85
86 if _, err := unreadable(nil); !errors.Is(err, errIncomplete) {
87 t.Fatalf("a pass with app unreadable gave %v, want an incomplete check", err)
88 }
89 if kept := stored(); !kept.Allows(export.Anyone, "app", identities["app"]) || kept.Basis != first.Sequence {
90 t.Errorf("with app unreadable the receiver stored %+v with basis %d; want app kept on basis %d", kept.Projects, kept.Basis, first.Sequence)
91 }
92
93 v.declare("app", policy("app", "operator"))
94 v.publish(rv.flags()...)
95 if withdrawn := stored(); withdrawn.Allows(export.Anyone, "app", identities["app"]) {
96 t.Fatalf("the withdrawal stored %+v, which names app", withdrawn.Projects)
97 }
98
99 for _, c := range []struct {
100 name string
101 counter string // the restored counter, or "" to leave the copy's
102 refused export.Code
103 }{
104 {"behind", "", export.CodeReplayed},
105 {"ahead", "1000\n", export.CodeStaleBasis},
106 } {
107 t.Run(c.name, func(t *testing.T) {
108 if err := os.RemoveAll(v.state); err != nil {
109 t.Fatal(err)
110 }
111 v.run("", "cp", "-a", backup, v.state)
112 if c.counter != "" {
113 v.write(filepath.Join(v.state, authorizationSequenceFile), c.counter)
114 }
115 before := stored().Sequence
116 stderr, err := unreadable(nil)
117 if !errors.Is(err, errIncomplete) {
118 t.Fatalf("the restored pass gave %v, want an incomplete check", err)
119 }
120 mustSay(t, "the restored pass", stderr, "refused")
121 mustSay(t, "the restored pass", stderr, "as "+string(c.refused))
122 got := stored()
123 if got.Sequence <= before || got.Allows(export.Anyone, "app", identities["app"]) || got.Basis != 0 {
124 t.Errorf("after the restored pass the receiver holds %d naming %+v on basis %d; want a newer one that keeps nothing",
125 got.Sequence, got.Projects, got.Basis)
126 }
127 })
128 }
129}
130
131// A failure while the counter is written, at the file's sync, its rename
132// or the directory's sync, returns no sequence, and leaves no temporary
133// file behind. No later allocation hands out a sequence at or below one
134// handed out before: a failure at the directory's sync may have written
135// the counter, and the next allocation counts from it.
136func TestTheAuthorizationCounterSurvivesFailedWrites(t *testing.T) {
137 state := t.TempDir()
138 next := func() (int64, error) {
139 p := &publisher{cfg: config{state: state}, stdout: io.Discard, stderr: io.Discard}
140 return p.nextSequence(0)
141 }
142 handed := int64(0)
143 allocate := func() {
144 t.Helper()
145 seq, err := next()
146 if err != nil {
147 t.Fatal(err)
148 }
149 if seq <= handed {
150 t.Fatalf("allocated %d after %d", seq, handed)
151 }
152 handed = seq
153 }
154 allocate()
155 failed := errors.New("injected")
156 realSync, realRename := syncFile, renameFile
157 defer func() { syncFile, renameFile = realSync, realRename }()
158 dirSync := func(f *os.File) error {
159 if info, err := f.Stat(); err == nil && info.IsDir() {
160 return failed
161 }
162 return realSync(f)
163 }
164 for _, c := range []struct {
165 name string
166 sync func(*os.File) error
167 rename func(string, string) error
168 }{
169 {"the file's sync", func(*os.File) error { return failed }, realRename},
170 {"the rename", realSync, func(string, string) error { return failed }},
171 {"the directory's sync", dirSync, realRename},
172 } {
173 syncFile, renameFile = c.sync, c.rename
174 if seq, err := next(); !errors.Is(err, failed) {
175 t.Errorf("a failure at %s gave sequence %d, %v", c.name, seq, err)
176 }
177 syncFile, renameFile = realSync, realRename
178 entries, err := os.ReadDir(state)
179 if err != nil {
180 t.Fatal(err)
181 }
182 if len(entries) != 1 || entries[0].Name() != authorizationSequenceFile {
183 t.Errorf("after a failure at %s the state directory holds %v", c.name, entries)
184 }
185 allocate()
186 }
187}
188
189// A receiver that lost its state, as when its volume is replaced, holds no
190// authorization. With app's policy unreadable, the pass keeps app's access
191// on the basis of the authorization the receiver acknowledged before, and
192// the receiver refuses it as stale-basis. The retry keeps nothing it could
193// not check, and publishes the grant it could read: anyone may read site.
194func TestAReceiverThatLostItsStateGetsTheReadableGrants(t *testing.T) {
195 v := newValley(t)
196 rv := newReceiver(t, v)
197 v.declare("site", policy("site", "anyone"))
198 v.publish(rv.flags()...)
199 before, _, err := rv.store.Authorization()
200 if err != nil {
201 t.Fatal(err)
202 }
203 if !before.Allows(export.Anyone, "app", identities["app"]) || !before.Allows(export.Anyone, "site", identities["site"]) {
204 t.Fatalf("the first authorization names %+v, want app and site", before.Projects)
205 }
206
207 rv.replaceStore()
208 app := filepath.Join(v.srv, "app.git")
209 if err := os.Rename(app, app+".moved"); err != nil {
210 t.Fatal(err)
211 }
212 defer os.Rename(app+".moved", app)
213 _, stderr, err := v.tryPublish(nil, rv.flags()...)
214 if !errors.Is(err, errIncomplete) {
215 t.Fatalf("the pass gave %v, want an incomplete check", err)
216 }
217 mustSay(t, "the pass to a receiver that lost its state", stderr, "as "+string(export.CodeStaleBasis))
218 got, _, err := rv.store.Authorization()
219 if err != nil {
220 t.Fatalf("the receiver that lost its state stored no authorization: %v", err)
221 }
222 if !got.Allows(export.Anyone, "site", identities["site"]) || got.Allows(export.Anyone, "app", identities["app"]) || got.Basis != 0 {
223 t.Errorf("the receiver that lost its state holds %+v on basis %d; want site alone, on no basis", got.Projects, got.Basis)
224 }
225}