gunk-dev/the-valley

authorize.go

sourceed42f27353e2main's published tip history
6.6 KiB
1package export
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "io/fs"
9 "os"
10 "path/filepath"
11 "syscall"
12)
13
14// Holdings are the active export, by its sequence and its digest, or nil
15// for none, and the source tip of each of its projects whose repository
16// the active generation still holds.
17func (s *Store) Holdings() (*Active, map[string]string, error) {
18 held := map[string]string{}
19 g, release, err := s.Open()
20 defer release()
21 switch {
22 case errors.Is(err, ErrNoExport):
23 return nil, held, nil
24 case err != nil:
25 return nil, nil, err
26 }
27 e, err := g.Export()
28 if err != nil {
29 return nil, nil, err
30 }
31 digest, err := e.Digest()
32 if err != nil {
33 return nil, nil, err
34 }
35 for _, p := range e.Projects {
36 if repo, _ := g.Repo(p.Name); exists(repo) {
37 held[p.Name] = p.SourceTip
38 }
39 }
40 return &Active{Sequence: g.Sequence, Digest: digest}, held, nil
41}
42
43// named reports, by the stored authorization, whether a project and
44// identity are named, with any reader. With no stored authorization it
45// names nothing.
46func (s *Store) named() (func(name, identity string) bool, error) {
47 a, _, err := s.Authorization()
48 if errors.Is(err, ErrNoAuthorization) {
49 return func(string, string) bool { return false }, nil
50 }
51 if err != nil {
52 return nil, err
53 }
54 return a.Names, nil
55}
56
57// ReadAuthorization is the stored authorization, as the signed note the
58// receiver admitted, read whole and at most MaxAuthorization bytes. Its
59// error is ErrNoAuthorization before the first is stored.
60func (s *Store) ReadAuthorization() ([]byte, error) {
61 f, err := os.Open(s.path("authorization"))
62 if errors.Is(err, fs.ErrNotExist) {
63 return nil, ErrNoAuthorization
64 }
65 if err != nil {
66 return nil, err
67 }
68 defer f.Close()
69 data, err := io.ReadAll(io.LimitReader(f, MaxAuthorization+1))
70 if err != nil {
71 return nil, err
72 }
73 if len(data) > MaxAuthorization {
74 return nil, fmt.Errorf("%s is larger than %d bytes", s.path("authorization"), MaxAuthorization)
75 }
76 return data, nil
77}
78
79// Authorization is the stored authorization, read and parsed, with the
80// signed note it was read from.
81func (s *Store) Authorization() (*Authorization, []byte, error) {
82 signed, err := s.ReadAuthorization()
83 if err != nil {
84 return nil, nil, err
85 }
86 a, err := ReadStoredAuthorization(signed)
87 if err != nil {
88 return nil, nil, fmt.Errorf("%s: %w", s.path("authorization"), err)
89 }
90 return a, signed, nil
91}
92
93// AuthorizationHighWater is the stored authorization's sequence, 0 for
94// none. A stored authorization that cannot be read is an error, never 0,
95// so no replay is admitted over it.
96func (s *Store) AuthorizationHighWater() (int64, error) {
97 a, _, err := s.Authorization()
98 if errors.Is(err, ErrNoAuthorization) {
99 return 0, nil
100 }
101 if err != nil {
102 return 0, err
103 }
104 return a.Sequence, nil
105}
106
107// Authorize stores an authorization VerifyAuthorization admitted, whose
108// signed note is signed. It takes the authorization lock, waiting for it
109// until ctx is done, and never the activation lock, so a stalled upload,
110// commit or repository check does not hold it up. Under the lock it
111// refuses a sequence not greater than the stored one's as replayed, and
112// then replaces the stored authorization durably. Once Authorize returns
113// nil, every request a server reads the authorization for is held to it.
114//
115// Authorize deletes nothing. Prune deletes the repositories it withdrew.
116func (s *Store) Authorize(ctx context.Context, a *Authorization, signed []byte) error {
117 if s.readOnly {
118 return errReadOnly
119 }
120 unlock, err := s.lockAuthorization(ctx)
121 if err != nil {
122 return err
123 }
124 defer unlock()
125 hw, err := s.AuthorizationHighWater()
126 if err != nil {
127 return err
128 }
129 if a.Sequence <= hw {
130 e := refuse(CodeReplayed, "the authorization's sequence %d is not greater than %d, the stored authorization's", a.Sequence, hw)
131 e.HighWater = hw
132 return e
133 }
134 if err := ctx.Err(); err != nil {
135 return err
136 }
137 return writeDurably(s.path("authorization"), signed)
138}
139
140// Missing are the projects of the active export that the stored
141// authorization names and whose repositories the active generation no
142// longer holds, in the export's order.
143func (s *Store) Missing() ([]string, error) {
144 named, err := s.named()
145 if err != nil {
146 return nil, err
147 }
148 missing := []string{}
149 g, release, err := s.Open()
150 defer release()
151 if errors.Is(err, ErrNoExport) {
152 return missing, nil
153 }
154 if err != nil {
155 return nil, err
156 }
157 e, err := g.Export()
158 if err != nil {
159 return nil, err
160 }
161 for _, p := range e.Projects {
162 if repo, _ := g.Repo(p.Name); named(p.Name, p.Identity) && !exists(repo) {
163 missing = append(missing, p.Name)
164 }
165 }
166 return missing, nil
167}
168
169// Prune deletes from the active generation the repository of each project
170// the stored authorization does not name, once no reader holds the
171// generation. It takes the activation lock, waiting for it until ctx is
172// done. While a reader holds the generation it deletes nothing and returns
173// ErrHeld, and the caller tries again later. With no stored authorization
174// it deletes nothing: a server serves nothing without one, and a later
175// authorization may name what the generation holds. export.json stays as
176// it was signed, so it keeps the withdrawn projects' plan text until a
177// later activation replaces the generation.
178func (s *Store) Prune(ctx context.Context) error {
179 if s.readOnly {
180 return errReadOnly
181 }
182 unlock, err := s.lock(ctx)
183 if err != nil {
184 return err
185 }
186 defer unlock()
187 return s.prune(ctx)
188}
189
190// prune is Prune, with the activation lock held.
191func (s *Store) prune(ctx context.Context) error {
192 a, _, err := s.Authorization()
193 if errors.Is(err, ErrNoAuthorization) {
194 return nil
195 }
196 if err != nil {
197 return err
198 }
199 seq, ok, err := s.active()
200 if err != nil || !ok {
201 return err
202 }
203 g := Generation{Sequence: seq, Dir: s.generationDir(seq)}
204 e, err := g.Export()
205 if err != nil {
206 return err
207 }
208 var withdrawn []string
209 for _, p := range e.Projects {
210 if repo, _ := g.Repo(p.Name); !a.Names(p.Name, p.Identity) && exists(repo) {
211 withdrawn = append(withdrawn, repo)
212 }
213 }
214 if len(withdrawn) == 0 {
215 return nil
216 }
217 f, err := os.Open(g.Dir)
218 if err != nil {
219 return err
220 }
221 defer f.Close()
222 if err := flock(f, syscall.LOCK_EX|syscall.LOCK_NB); errors.Is(err, syscall.EWOULDBLOCK) {
223 return ErrHeld
224 } else if err != nil {
225 return err
226 }
227 // Holding the generation exclusively, no reader is in it, and one
228 // that opens it next finds the repositories gone.
229 var trash []string
230 for _, repo := range withdrawn {
231 to := s.path("trash", "withdrawn-"+filepath.Base(repo)+"-"+random())
232 if err := rename(repo, to); err != nil {
233 return err
234 }
235 trash = append(trash, to)
236 }
237 err = syncPath(filepath.Join(g.Dir, "repos"))
238 f.Close()
239 for _, t := range trash {
240 err = errors.Join(err, removeAll(ctx, t))
241 }
242 return err
243}