-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
455 lines (420 loc) · 9.81 KB
/
store.go
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package store
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/tidwall/sjson"
"github.com/verifa/horizon/pkg/auth"
"github.com/verifa/horizon/pkg/hz"
)
const (
// Format: HZ.<internal/api>.store.<command>.<group>.<kind>.<account>.<name>
subjectInternalStore = "HZ.internal.store.*.*.*.*.*.*"
subjectAPIStore = "HZ.api.store.*.*.*.*.*.*"
subjectIndexCommand = 3
subjectIndexGroup = 4
subjectIndexVersion = 5
subjectIndexKind = 6
subjectIndexAccount = 7
subjectIndexName = 8
subjectLength = 9
)
type StoreCommand string
const (
StoreCommandCreate StoreCommand = "create"
StoreCommandApply StoreCommand = "apply"
StoreCommandGet StoreCommand = "get"
StoreCommandList StoreCommand = "list"
StoreCommandDelete StoreCommand = "delete"
)
func (c StoreCommand) String() string {
return string(c)
}
type StoreOption func(*storeOptions)
func WithMutexTTL(ttl time.Duration) StoreOption {
return func(o *storeOptions) {
o.mutexTTL = ttl
}
}
func WithStopTimeout(timeout time.Duration) StoreOption {
return func(o *storeOptions) {
o.stopTimeout = timeout
}
}
var defaultStoreOptions = storeOptions{
mutexTTL: time.Minute,
stopTimeout: time.Minute,
}
type storeOptions struct {
mutexTTL time.Duration
stopTimeout time.Duration
}
func StartStore(
ctx context.Context,
conn *nats.Conn,
auth *auth.Auth,
opts ...StoreOption,
) (*Store, error) {
store := Store{
Conn: conn,
Auth: auth,
}
if err := store.Start(ctx, conn, auth, opts...); err != nil {
return nil, fmt.Errorf("starting store: %w", err)
}
return &store, nil
}
type Store struct {
Conn *nats.Conn
Auth *auth.Auth
js jetstream.JetStream
kv jetstream.KeyValue
mutex jetstream.KeyValue
gc *GarbageCollector
subs []*nats.Subscription
stopTimeout time.Duration
wg sync.WaitGroup
}
func (s *Store) Start(
ctx context.Context,
conn *nats.Conn,
auth *auth.Auth,
opts ...StoreOption,
) error {
opt := defaultStoreOptions
for _, o := range opts {
o(&opt)
}
s.stopTimeout = opt.stopTimeout
js, err := jetstream.New(conn)
if err != nil {
return fmt.Errorf("new jetstream: %w", err)
}
s.js = js
kv, err := js.KeyValue(ctx, hz.BucketObjects)
if err != nil {
return fmt.Errorf(
"conntecting to objects kv bucket %q: %w",
hz.BucketObjects,
err,
)
}
s.kv = kv
mutex, err := js.KeyValue(ctx, hz.BucketMutex)
if err != nil {
return fmt.Errorf(
"connecting to mutex kv bucket %q: %w",
hz.BucketMutex,
err,
)
}
s.mutex = mutex
{
sub, err := conn.QueueSubscribe(
subjectInternalStore,
"store",
func(msg *nats.Msg) {
slog.Info("received store message", "subject", msg.Subject)
go s.handleInternalMsg(ctx, msg)
},
)
if err != nil {
return fmt.Errorf("subscribing store: %w", err)
}
s.subs = append(s.subs, sub)
}
{
sub, err := conn.QueueSubscribe(
subjectAPIStore,
"store",
func(msg *nats.Msg) {
slog.Info("received store message", "subject", msg.Subject)
go s.handleAPIMsg(ctx, msg)
},
)
if err != nil {
return fmt.Errorf("subscribing store: %w", err)
}
s.subs = append(s.subs, sub)
}
// Start garbage collector.
gc := &GarbageCollector{
Conn: conn,
KV: kv,
}
if err := gc.Start(ctx); err != nil {
return fmt.Errorf("start garbage collector: %w", err)
}
s.gc = gc
return nil
}
func (s *Store) Close() error {
var errs error
for _, sub := range s.subs {
if err := sub.Unsubscribe(); err != nil {
errs = errors.Join(errs, err)
}
}
s.gc.Stop()
// Wait for all store operations to finish, or timeout.
if s.stopWaitTimeout() {
errs = errors.Join(
errs,
fmt.Errorf(
"timeout after %s waiting for store operations to finish",
s.stopTimeout,
),
)
}
return errs
}
func (s *Store) stopWaitTimeout() bool {
done := make(chan struct{})
go func() {
defer close(done)
s.wg.Wait()
}()
tickDuration := time.Second * 10
ticker := time.NewTicker(tickDuration)
elapsedTime := time.Duration(0)
for {
elapsedTime += tickDuration
select {
case <-ticker.C:
slog.Info(
"waiting for reconcile loops to finish",
"elapsed",
elapsedTime,
"timeout",
s.stopTimeout,
)
case <-done:
return false // completed normally
case <-time.After(s.stopTimeout):
return true // timed out
}
}
}
func (s *Store) handleAPIMsg(ctx context.Context, msg *nats.Msg) {
s.wg.Add(1)
defer s.wg.Done()
// Parse subject to get details.
parts := strings.Split(msg.Subject, ".")
if len(parts) != subjectLength {
_ = hz.RespondError(msg, &hz.Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("invalid subject: %q", msg.Subject),
})
return
}
cmd := StoreCommand(parts[subjectIndexCommand])
key := hz.ObjectKey{
Group: parts[subjectIndexGroup],
Version: parts[subjectIndexVersion],
Kind: parts[subjectIndexKind],
Account: parts[subjectIndexAccount],
Name: parts[subjectIndexName],
}
req := auth.CheckRequest{
Session: msg.Header.Get(hz.HeaderAuthorization),
Object: key,
}
switch cmd {
case StoreCommandList:
// List is a bit special. We do not check for permissions to a specific
// object, instead we want to list the objects which we can access.
// The internal msg handler will do the actual listing and honour the
// session.
// Just forward it there...
// Check session is valid, at the very least.
session := msg.Header.Get(hz.HeaderAuthorization)
_, err := s.Auth.Sessions.Get(ctx, session)
if err != nil {
_ = hz.RespondError(msg, err)
return
}
s.handleInternalMsg(ctx, msg)
return
case StoreCommandGet:
req.Verb = auth.VerbRead
case StoreCommandCreate:
req.Verb = auth.VerbCreate
case StoreCommandApply:
// This requires checking if it's a create or edit operation.
_, err := s.get(ctx, key)
if errors.Is(err, hz.ErrNotFound) {
req.Verb = auth.VerbCreate
} else {
req.Verb = auth.VerbUpdate
}
case StoreCommandDelete:
req.Verb = auth.VerbDelete
default:
_ = hz.RespondError(msg, &hz.Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("invalid command: %q", cmd),
})
return
}
ok, err := s.Auth.Check(ctx, req)
if err != nil {
_ = hz.RespondError(msg, err)
return
}
if !ok {
_ = hz.RespondError(msg, &hz.Error{
Status: http.StatusForbidden,
Message: "forbidden",
})
return
}
s.handleInternalMsg(ctx, msg)
}
// handleInternalMsg handles messages for the internal (unprotected) nats
// subjects.
// Even though it is unprotected, some commands (like list) still honour the
// authz for a user session, if provided.
func (s *Store) handleInternalMsg(ctx context.Context, msg *nats.Msg) {
s.wg.Add(1)
defer s.wg.Done()
// Parse subject to get details.
parts := strings.Split(msg.Subject, ".")
if len(parts) != subjectLength {
_ = hz.RespondError(msg, &hz.Error{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("invalid subject: %q", msg.Subject),
})
return
}
cmd := StoreCommand(parts[subjectIndexCommand])
key := hz.ObjectKey{
Group: parts[subjectIndexGroup],
Version: parts[subjectIndexVersion],
Kind: parts[subjectIndexKind],
Account: parts[subjectIndexAccount],
Name: parts[subjectIndexName],
}
switch cmd {
case StoreCommandCreate:
req := CreateRequest{
Key: key,
Data: msg.Data,
}
if err := s.Create(ctx, req); err != nil {
_ = hz.RespondError(msg, err)
return
}
_ = hz.RespondStatus(msg, http.StatusCreated, nil)
return
case StoreCommandApply:
manager := msg.Header.Get(hz.HeaderApplyFieldManager)
forceStr := msg.Header.Get(hz.HeaderApplyForceConflicts)
force, err := strconv.ParseBool(forceStr)
if err != nil {
_ = hz.RespondError(
msg,
&hz.Error{
Status: http.StatusBadRequest,
Message: "invalid header " + hz.HeaderApplyForceConflicts,
},
)
return
}
if manager == "" {
_ = hz.RespondError(
msg,
&hz.Error{
Status: http.StatusBadRequest,
Message: "missing field manager",
},
)
return
}
req := ApplyRequest{
Data: msg.Data,
Manager: manager,
Key: key,
Force: force,
}
status, err := s.Apply(ctx, req)
if err != nil {
_ = hz.RespondError(msg, err)
return
}
_ = hz.RespondStatus(msg, status, nil)
return
case StoreCommandGet:
req := GetRequest{
Key: key,
}
resp, err := s.Get(ctx, req)
if err != nil {
_ = hz.RespondError(msg, err)
return
}
_ = hz.RespondOK(msg, resp)
return
case StoreCommandList:
// Logic: the auth rbac does not know which objects exist.
// Therefore, we cannot ask it which objects we can list.
// Therefore, list all the actual objects that match the supplied key,
// and then filter them with rbac.
req := ListRequest{
Key: key,
}
resp, err := s.List(ctx, req)
if err != nil {
_ = hz.RespondError(msg, err)
return
}
session := msg.Header.Get(hz.HeaderAuthorization)
if session != "" {
// Filter the response with the rbac.
if err := s.Auth.List(ctx, auth.ListRequest{
Session: msg.Header.Get(hz.HeaderAuthorization),
ObjectList: resp,
}); err != nil {
_ = hz.RespondError(msg, err)
return
}
}
data, err := json.Marshal(resp)
if err != nil {
_ = hz.RespondError(msg, &hz.Error{
Status: http.StatusInternalServerError,
Message: "marshalling list response: " + err.Error(),
})
return
}
_ = hz.RespondOK(msg, data)
return
case StoreCommandDelete:
req := DeleteRequest{
Key: key,
}
if err := s.Delete(ctx, req); err != nil {
_ = hz.RespondError(msg, err)
return
}
_ = hz.RespondOK(msg, nil)
return
default:
_ = hz.RespondError(msg, &hz.Error{
Status: http.StatusBadRequest,
Message: "invalid command",
})
return
}
}
func removeReadOnlyFields(data []byte) ([]byte, error) {
return sjson.DeleteBytes(data, "metadata.revision")
}