-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathbolt.go
More file actions
401 lines (320 loc) · 9.72 KB
/
Copy pathbolt.go
File metadata and controls
401 lines (320 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package mercure
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"log/slog"
"math/rand"
"os"
"path/filepath"
"sync"
"time"
bolt "go.etcd.io/bbolt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
const BoltDefaultCleanupFrequency = 0.3
const defaultBoltBucketName = "updates"
// maxHistoryScan caps how many history events a single subscriber
// reconnection can force the transport to walk before giving up on
// finding the requested Last-Event-ID. The cap is a denial-of-service
// guard: without it, an attacker sending an ancient or non-existent
// Last-Event-ID forces an O(history-size) scan on every request.
const maxHistoryScan = 10000
// BoltTransport implements the TransportInterface using the Bolt database.
type BoltTransport struct {
sync.RWMutex
subscribers *SubscriberList
logger *slog.Logger
db *bolt.DB
bucketName string
size uint64
cleanupFrequency float64
closed chan struct{}
closedOnce sync.Once
lastSeq uint64
lastEventID string
}
// NewBoltTransport creates a new BoltTransport.
func NewBoltTransport(
subscriberList *SubscriberList,
logger *slog.Logger,
path string,
bucketName string,
size uint64,
cleanupFrequency float64,
) (*BoltTransport, error) {
if path == "" {
path = "bolt.db"
}
if bucketName == "" {
bucketName = defaultBoltBucketName
}
if dir := filepath.Dir(path); dir != "" && dir != "." {
// Path comes from operator config (Caddyfile or env), not HTTP input.
if err := os.MkdirAll(dir, 0o700); err != nil { //nolint:gosec
return nil, &TransportError{err: fmt.Errorf("creating bolt data directory %q: %w", dir, err)}
}
}
db, err := bolt.Open(path, 0o600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, &TransportError{err: err}
}
lastEventID, err := getDBLastEventID(db, bucketName)
if err != nil {
return nil, &TransportError{err: err}
}
return &BoltTransport{
logger: logger,
db: db,
bucketName: bucketName,
size: size,
cleanupFrequency: cleanupFrequency,
subscribers: subscriberList,
closed: make(chan struct{}),
lastEventID: lastEventID,
}, nil
}
func getDBLastEventID(db *bolt.DB, bucketName string) (string, error) {
lastEventID := EarliestLastEventID
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
if b == nil {
return nil // No data
}
if k, _ := b.Cursor().Last(); k != nil {
lastEventID = string(k[8:])
}
return nil
})
if err != nil {
return "", fmt.Errorf("unable to get lastEventID from BoltDB: %w", err)
}
return lastEventID, nil
}
// Dispatch dispatches an update to all subscribers and persists it in Bolt DB.
func (t *BoltTransport) Dispatch(ctx context.Context, update *Update) error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
update.AssignUUID()
updateJSON, err := json.Marshal(*update)
if err != nil {
return fmt.Errorf("error when marshaling update: %w", err)
}
// We cannot use RLock() because Bolt allows only one read-write transaction at a time
t.Lock()
defer t.Unlock()
if err := t.persist(update.ID, updateJSON); err != nil {
return err
}
for _, s := range t.subscribers.MatchAny(update) {
s.Dispatch(ctx, update, false)
}
return nil
}
// AddSubscriber adds a new subscriber to the transport.
func (t *BoltTransport) AddSubscriber(ctx context.Context, s *LocalSubscriber) error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
t.Lock()
t.subscribers.Add(s)
toSeq := t.lastSeq
t.Unlock()
if s.RequestLastEventID != "" {
if err := t.dispatchHistory(ctx, s, toSeq); err != nil {
return err
}
}
s.Ready(ctx)
return nil
}
// RemoveSubscriber removes a new subscriber from the transport.
func (t *BoltTransport) RemoveSubscriber(_ context.Context, s *LocalSubscriber) error {
select {
case <-t.closed:
return ErrClosedTransport
default:
}
t.Lock()
defer t.Unlock()
t.subscribers.Remove(s)
return nil
}
// GetSubscribers get the list of active subscribers.
func (t *BoltTransport) GetSubscribers(_ context.Context) (string, []*Subscriber, error) {
t.RLock()
defer t.RUnlock()
return t.lastEventID, getSubscribers(t.subscribers), nil
}
// Close closes the Transport.
func (t *BoltTransport) Close(_ context.Context) (err error) {
t.closedOnce.Do(func() {
close(t.closed)
t.Lock()
defer t.Unlock()
t.subscribers.Walk(0, func(s *LocalSubscriber) bool {
s.Disconnect()
return true
})
err = t.db.Close()
})
if err == nil {
return nil
}
return fmt.Errorf("unable to close Bolt DB: %w", err)
}
// pastSeqBound reports whether the BoltDB key k was written strictly after
// the sequence snapshot toSeq, and therefore falls outside the subscriber's
// history window. Events whose seq equals toSeq are the most recent ones
// observed at subscription time and are still considered part of history.
// toSeq == 0 means the bucket was empty at subscription time, so any key
// (all with seq >= 1) is "past the bound".
func pastSeqBound(k []byte, toSeq uint64) bool {
return binary.BigEndian.Uint64(k[:8]) > toSeq
}
//nolint:gocognit,funlen
func (t *BoltTransport) dispatchHistory(ctx context.Context, s *LocalSubscriber, toSeq uint64) error {
ctx, span := startSpan(ctx, "mercure.transport.history",
trace.WithAttributes(
attribute.String("mercure.transport", "bolt"),
attribute.String("mercure.subscriber.id", s.ID),
attribute.String("mercure.last_event_id.requested", s.RequestLastEventID),
))
defer span.End()
err := t.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(t.bucketName))
if b == nil {
s.HistoryDispatched(EarliestLastEventID)
return nil // No data
}
c := b.Cursor()
responseLastEventID := EarliestLastEventID
afterFromID := s.RequestLastEventID == EarliestLastEventID
scanned := 0
for k, v := c.First(); k != nil; k, v = c.Next() {
// Keys written after the subscribe snapshot (concurrent Dispatch
// between subscriber registration and this read transaction)
// must not leak into the response header or be re-delivered
// alongside the live dispatch queue — check the bound first.
if pastSeqBound(k, toSeq) {
break
}
if !afterFromID {
// DoS guard: cap the search-for-requested-ID phase only.
// Once afterFromID is true the loop is dispatching legitimate
// authorized history and must not be truncated.
if scanned >= maxHistoryScan {
break
}
scanned++
id := string(k[8:])
if id == s.RequestLastEventID {
afterFromID = true
// The subscriber already knows this id; echoing it
// is not a disclosure.
responseLastEventID = s.RequestLastEventID
continue
}
// Only disclose the id of an event the subscriber is
// authorized to read. We must deserialize to evaluate
// Match against the update's topics and Private flag.
var update *Update
if err := json.Unmarshal(v, &update); err != nil {
// Skip silently — do not disclose this id.
continue
}
if s.Match(update) {
responseLastEventID = id
}
continue
}
var update *Update
if err := json.Unmarshal(v, &update); err != nil {
s.HistoryDispatched(responseLastEventID)
err := fmt.Errorf("unable to unmarshal update: %w", err)
if t.logger.Enabled(ctx, slog.LevelError) {
t.logger.LogAttrs(ctx, slog.LevelError, "Unable to unmarshal update coming from the Bolt DB", slog.Any("update", update), slog.Any("error", err))
}
return err
}
if s.Match(update) && !s.Dispatch(ctx, update, true) {
s.HistoryDispatched(responseLastEventID)
return nil
}
}
s.HistoryDispatched(responseLastEventID)
if !afterFromID {
if t.logger.Enabled(ctx, slog.LevelInfo) {
t.logger.LogAttrs(ctx, slog.LevelInfo, "Can't find requested LastEventID")
}
}
return nil
})
if err != nil {
err = fmt.Errorf("unable to retrieve history from BoltDB: %w", err)
recordSpanError(span, err)
return err
}
return nil
}
// persist stores update in the database.
func (t *BoltTransport) persist(updateID string, updateJSON []byte) error {
if err := t.db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(t.bucketName))
if err != nil {
return fmt.Errorf("error when creating Bolt DB bucket: %w", err)
}
seq, err := bucket.NextSequence()
if err != nil {
return fmt.Errorf("error when generating Bolt DB sequence: %w", err)
}
prefix := make([]byte, 8)
binary.BigEndian.PutUint64(prefix, seq)
// The sequence value is prepended to the update id to create an ordered list
key := bytes.Join([][]byte{prefix, []byte(updateID)}, []byte{})
// The DB is append-only
bucket.FillPercent = 1
t.lastSeq = seq
t.lastEventID = updateID
if err := bucket.Put(key, updateJSON); err != nil {
return fmt.Errorf("unable to put value in Bolt DB: %w", err)
}
return t.cleanup(bucket, seq)
}); err != nil {
return fmt.Errorf("bolt error: %w", err)
}
return nil
}
// cleanup removes entries in the history above the size limit, triggered probabilistically.
func (t *BoltTransport) cleanup(bucket *bolt.Bucket, lastID uint64) error {
if t.size == 0 ||
t.cleanupFrequency == 0 ||
t.size >= lastID ||
(t.cleanupFrequency != 1 && rand.Float64() < t.cleanupFrequency) { //nolint:gosec
return nil
}
removeUntil := lastID - t.size
c := bucket.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
if binary.BigEndian.Uint64(k[:8]) > removeUntil {
break
}
if err := bucket.Delete(k); err != nil {
return fmt.Errorf("unable to delete value in Bolt DB: %w", err)
}
}
return nil
}
// Interface guards.
var (
_ Transport = (*BoltTransport)(nil)
_ TransportSubscribers = (*BoltTransport)(nil)
)