gunk-dev/the-valley

authorize.go

source62fa289e043emain'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 a
112// basis other than the stored one's sequence as stale-basis, and then
113// replaces the stored authorization durably. Once Authorize returns
114// nil, every request a server reads the authorization for is held to it.
115//
116// Authorize deletes nothing. Prune deletes the repositories it withdrew.
117func (s *Store) Authorize(ctx context.Context, a *Authorization, signed []byte) error {
118 if s.readOnly {
119 return errReadOnly
120 }
121 unlock, err := s.lockAuthorization(ctx)
122 if err != nil {
123 return err
124 }
125 defer unlock()
126 hw, err := s.AuthorizationHighWater()
127 if err != nil {
128 return err
129 }
130 if err := a.admissible(hw); err != nil {
131 return err
132 }
133 if err := ctx.Err(); err != nil {
134 return err
135 }
136 return writeDurably(s.path("authorization"), signed)
137}
138
139// Missing are the projects of the active export that the stored
140// authorization names and whose repositories the active generation no
141// longer holds, in the export's order.
142func (s *Store) Missing() ([]string, error) {
143 named, err := s.named()
144 if err != nil {
145 return nil, err
146 }
147 missing := []string{}
148 g, release, err := s.Open()
149 defer release()
150 if errors.Is(err, ErrNoExport) {
151 return missing, nil
152 }
153 if err != nil {
154 return nil, err
155 }
156 e, err := g.Export()
157 if err != nil {
158 return nil, err
159 }
160 for _, p := range e.Projects {
161 if repo, _ := g.Repo(p.Name); named(p.Name, p.Identity) && !exists(repo) {
162 missing = append(missing, p.Name)
163 }
164 }
165 return missing, nil
166}
167
168// Prune deletes from the active generation the repository of each project
169// the stored authorization does not name, once no reader holds the
170// generation. It takes the activation lock, waiting for it until ctx is
171// done. While a reader holds the generation it deletes nothing and returns
172// ErrHeld, and the caller tries again later. With no stored authorization
173// it deletes nothing: a server serves nothing without one, and a later
174// authorization may name what the generation holds. export.json stays as
175// it was signed, so it keeps the withdrawn projects' plan text until a
176// later activation replaces the generation.
177func (s *Store) Prune(ctx context.Context) error {
178 if s.readOnly {
179 return errReadOnly
180 }
181 unlock, err := s.lock(ctx)
182 if err != nil {
183 return err
184 }
185 defer unlock()
186 return s.prune(ctx)
187}
188
189// prune is Prune, with the activation lock held.
190func (s *Store) prune(ctx context.Context) error {
191 a, _, err := s.Authorization()
192 if errors.Is(err, ErrNoAuthorization) {
193 return nil
194 }
195 if err != nil {
196 return err
197 }
198 seq, ok, err := s.active()
199 if err != nil || !ok {
200 return err
201 }
202 g := Generation{Sequence: seq, Dir: s.generationDir(seq)}
203 e, err := g.Export()
204 if err != nil {
205 return err
206 }
207 var withdrawn []string
208 for _, p := range e.Projects {
209 if repo, _ := g.Repo(p.Name); !a.Names(p.Name, p.Identity) && exists(repo) {
210 withdrawn = append(withdrawn, repo)
211 }
212 }
213 if len(withdrawn) == 0 {
214 return nil
215 }
216 f, err := os.Open(g.Dir)
217 if err != nil {
218 return err
219 }
220 defer f.Close()
221 if err := flock(f, syscall.LOCK_EX|syscall.LOCK_NB); errors.Is(err, syscall.EWOULDBLOCK) {
222 return ErrHeld
223 } else if err != nil {
224 return err
225 }
226 // Holding the generation exclusively, no reader is in it, and one
227 // that opens it next finds the repositories gone.
228 var trash []string
229 for _, repo := range withdrawn {
230 to := s.path("trash", "withdrawn-"+filepath.Base(repo)+"-"+random())
231 if err := rename(repo, to); err != nil {
232 return err
233 }
234 trash = append(trash, to)
235 }
236 err = syncPath(filepath.Join(g.Dir, "repos"))
237 f.Close()
238 for _, t := range trash {
239 err = errors.Join(err, removeAll(ctx, t))
240 }
241 return err
242}