-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathoplog.go
469 lines (442 loc) · 15.4 KB
/
oplog.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package oplog
import (
"bytes"
"context"
"fmt"
"io"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/oplog/store"
"github.com/hashicorp/go-dbw"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/hashicorp/go-kms-wrapping/v2/extras/structwrapping"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
// Version of oplog entries (among other things, it's used to manage upgrade
// compatibility when replicating)
// v1: initial version
// v2: adds the new Message.Opts
const Version = "v2"
// Message wraps a proto.Message with some other bits like operation type,
// paths and options.
type Message struct {
proto.Message
TypeName string
OpType OpType
FieldMaskPaths []string
SetToNullPaths []string
Opts []dbw.Option
}
// Entry represents an oplog entry
type Entry struct {
*store.Entry
Cipherer wrapping.Wrapper `gorm:"-"`
Ticketer Ticketer `gorm:"-"`
}
// Metadata provides meta information about the Entry
type Metadata map[string][]string
// NewEntry creates a new Entry
func NewEntry(ctx context.Context, aggregateName string, metadata Metadata, cipherer wrapping.Wrapper, ticketer Ticketer) (*Entry, error) {
const op = "oplog.NewEntry"
entry := Entry{
Entry: &store.Entry{
AggregateName: aggregateName,
Version: Version,
},
Cipherer: cipherer,
Ticketer: ticketer,
}
if len(metadata) > 0 {
storeMD := []*store.Metadata{}
for k, v := range metadata {
if len(v) > 0 {
for _, vv := range v {
storeMD = append(storeMD, &store.Metadata{Key: k, Value: vv})
}
continue
}
// this metadata just has a key with no values
storeMD = append(storeMD, &store.Metadata{Key: k})
}
entry.Metadata = storeMD
}
if err := entry.validate(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return &entry, nil
}
func (e *Entry) validate(ctx context.Context) error {
const op = "oplog.(Entry).validate"
if e.Cipherer == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil cipherer")
}
if e.Ticketer == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil ticketer")
}
if e.Entry == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil entry")
}
if e.Entry.Version == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing entry version")
}
if e.Entry.AggregateName == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing entry aggregate name")
}
return nil
}
// UnmarshalData the data attribute from []byte (treated as a FIFO QueueBuffer) to a []proto.Message
func (e *Entry) UnmarshalData(ctx context.Context, types *TypeCatalog) ([]Message, error) {
const op = "oplog.(Entry).UnmarshalData"
if types == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil type catalog")
}
if len(e.Data) == 0 {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing data")
}
msgs := []Message{}
queue := Queue{
Buffer: *bytes.NewBuffer(e.Data),
Catalog: types,
}
for {
item, err := queue.remove(ctx)
if err == io.EOF {
break
}
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("error removing item from queue"))
}
name, err := types.GetTypeName(ctx, item.msg)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
dbwOpts, err := convertToDbwOpts(ctx, item.operationOptions)
if err != nil {
}
msgs = append(msgs, Message{
Message: item.msg,
TypeName: name,
OpType: item.opType,
FieldMaskPaths: item.fieldMask,
SetToNullPaths: item.setToNullPaths,
Opts: dbwOpts,
})
}
return msgs, nil
}
func convertToDbwOpts(ctx context.Context, opts *OperationOptions) ([]dbw.Option, error) {
const op = "oplog.convertToDbwOpts"
if opts == nil {
return []dbw.Option{}, nil
}
dbwOpts := []dbw.Option{
dbw.WithSkipVetForWrite(opts.GetWithSkipVetForWrite()),
}
if opts.GetWithVersion() != nil {
dbwOpts = append(dbwOpts, dbw.WithVersion(&opts.GetWithVersion().Value))
}
if opts.GetWithOnConflict() != nil {
c := &dbw.OnConflict{}
switch targetType := opts.GetWithOnConflict().GetTarget().(type) {
case *WithOnConflict_Columns:
c.Target = dbw.Columns(opts.GetWithOnConflict().GetColumns().GetNames())
case *WithOnConflict_Constraint:
c.Target = dbw.Constraint(opts.GetWithOnConflict().GetConstraint())
default:
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("not a supported target type: %T", targetType))
}
switch actionType := opts.GetWithOnConflict().GetAction().(type) {
case *WithOnConflict_DoNothing:
c.Action = dbw.DoNothing(opts.GetWithOnConflict().GetDoNothing())
case *WithOnConflict_UpdateAll:
c.Action = dbw.UpdateAll(opts.GetWithOnConflict().GetUpdateAll())
case *WithOnConflict_ColumnValues:
cvAction := make([]dbw.ColumnValue, 0, len(actionType.ColumnValues.GetValues()))
for _, cv := range actionType.ColumnValues.GetValues() {
newColVal := dbw.ColumnValue{
Column: cv.GetName(),
}
switch pbVal := cv.Value.(type) {
case *ColumnValue_Raw:
newColVal.Value = pbVal.Raw.AsInterface()
case *ColumnValue_ExprValue:
expr := dbw.ExprValue{
Sql: pbVal.ExprValue.GetSql(),
}
args := make([]interface{}, 0, len(pbVal.ExprValue.GetArgs()))
for _, a := range pbVal.ExprValue.GetArgs() {
args = append(args, a.AsInterface())
}
newColVal.Value = expr
case *ColumnValue_Column:
newColVal.Value = dbw.Column{
Name: pbVal.Column.GetName(),
Table: pbVal.Column.GetTable(),
}
default:
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("not a supported column value type: %T", pbVal))
}
cvAction = append(cvAction, newColVal)
}
c.Action = cvAction
default:
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("not a supported action type: %T", actionType))
}
dbwOpts = append(dbwOpts, dbw.WithOnConflict(c))
}
if opts.GetWithWhereClause() != "" {
sql := opts.GetWithWhereClause()
args := make([]interface{}, 0, len(opts.GetWithWhereClauseArgs()))
for _, a := range opts.GetWithWhereClauseArgs() {
args = append(args, a.AsInterface())
}
dbwOpts = append(dbwOpts, dbw.WithWhere(sql, args...))
}
return dbwOpts, nil
}
// convertFromDbwOpts converts dbw options to an OperationalOptions for an Entry
func convertFromDbwOpts(ctx context.Context, opts dbw.Options) (*OperationOptions, error) {
const op = "oplog.convertFromDbwOptions"
pbOpts := &OperationOptions{
WithSkipVetForWrite: opts.WithSkipVetForWrite,
WithWhereClause: opts.WithWhereClause,
}
if opts.WithVersion != nil {
pbOpts.WithVersion = &wrappers.UInt32Value{Value: *opts.WithVersion}
}
clauseValues := make([]*structpb.Value, 0, len(opts.WithWhereClauseArgs))
for _, arg := range opts.WithWhereClauseArgs {
v, err := structpb.NewValue(arg)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
clauseValues = append(clauseValues, v)
}
pbOpts.WithWhereClauseArgs = clauseValues
if opts.WithOnConflict != nil {
c := &WithOnConflict{}
switch target := opts.WithOnConflict.Target.(type) {
case dbw.Constraint:
c.Target = &WithOnConflict_Constraint{string(target)}
case dbw.Columns:
c.Target = &WithOnConflict_Columns{
&Columns{
Names: []string(target),
},
}
default:
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("not a supported target type: %T", target))
}
switch action := opts.WithOnConflict.Action.(type) {
case dbw.DoNothing:
c.Action = &WithOnConflict_DoNothing{bool(action)}
case dbw.UpdateAll:
c.Action = &WithOnConflict_UpdateAll{bool(action)}
case []dbw.ColumnValue:
colVals := make([]*ColumnValue, 0, len(action))
for _, cv := range action {
pbColVal := &ColumnValue{
Name: cv.Column,
}
switch cvVal := cv.Value.(type) {
case dbw.ExprValue:
args := make([]*structpb.Value, 0, len(cvVal.Vars))
for _, vv := range cvVal.Vars {
pbVar, err := structpb.NewValue(vv)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create expr arg"))
}
args = append(args, pbVar)
}
pbColVal.Value = &ColumnValue_ExprValue{&ExprValue{
Sql: cvVal.Sql,
Args: args,
}}
case dbw.Column:
pbColVal.Value = &ColumnValue_Column{
&Column{
Name: cvVal.Name,
Table: cvVal.Table,
},
}
default:
exprVal, err := structpb.NewValue(cv.Value)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create column value"))
}
pbColVal.Value = &ColumnValue_Raw{exprVal}
}
colVals = append(colVals, pbColVal)
}
c.Action = &WithOnConflict_ColumnValues{ColumnValues: &ColumnValues{Values: colVals}}
default:
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("not a valid supported action: %T", action))
}
pbOpts.WithOnConflict = c
}
return pbOpts, nil
}
// WriteEntryWith the []proto.Message marshaled into the entry data as a FIFO QueueBuffer
// if Cipherer != nil then the data is authentication encrypted
func (e *Entry) WriteEntryWith(ctx context.Context, tx *Writer, ticket *store.Ticket, msgs ...*Message) error {
const op = "oplog.(Entry).WriteEntryWith"
if tx == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil writer")
}
if err := e.validate(ctx); err != nil {
return errors.Wrap(ctx, err, op)
}
if ticket == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil ticket")
}
if ticket.Version == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing ticket version")
}
queue := Queue{}
for _, m := range msgs {
if m == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil message")
}
if err := queue.add(ctx, m.Message, m.TypeName, m.OpType, WithOperationOptions(m.Opts...), WithFieldMaskPaths(m.FieldMaskPaths), WithSetToNullPaths(m.SetToNullPaths)); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error adding message to queue"))
}
}
e.Data = append(e.Data, queue.Bytes()...)
if e.Cipherer != nil {
if err := e.encryptData(ctx); err != nil {
return errors.Wrap(ctx, err, op)
}
}
rw := dbw.New(tx.DB)
if err := rw.Create(ctx, e); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error writing data to storage"))
}
if err := e.Ticketer.Redeem(ctx, ticket); err != nil {
return errors.Wrap(ctx, err, op)
}
return nil
}
// Write the entry as is with whatever it has for e.Data marshaled into a FIFO QueueBuffer
// Cipherer != nil then the data is authentication encrypted
func (e *Entry) Write(ctx context.Context, tx *Writer, ticket *store.Ticket) error {
const op = "oplog.(Entry).Write"
if err := e.validate(ctx); err != nil {
return errors.Wrap(ctx, err, op)
}
if ticket == nil {
return errors.New(ctx, errors.InvalidParameter, op, "nil ticket")
}
if ticket.Version == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing ticket version")
}
if e.Cipherer != nil {
if err := e.encryptData(ctx); err != nil {
return errors.Wrap(ctx, err, op)
}
}
rw := dbw.New(tx.DB)
if err := rw.Create(ctx, e); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("error writing data to storage"))
}
if err := e.Ticketer.Redeem(ctx, ticket); err != nil {
return errors.Wrap(ctx, err, op)
}
return nil
}
// encryptData the entry's data using its Cipherer (wrapping.Wrapper)
func (e *Entry) encryptData(ctx context.Context) error {
const op = "oplog.(Entry).EncryptData"
// structwrapping doesn't support embedding, so we'll pass in the store.Entry directly
if err := structwrapping.WrapStruct(ctx, e.Cipherer, e.Entry, nil); err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
}
return nil
}
// DecryptData will decrypt the entry's data using its Cipherer (wrapping.Wrapper)
func (e *Entry) DecryptData(ctx context.Context) error {
const op = "oplog.(Entry).DecryptData"
// structwrapping doesn't support embedding, so we'll pass in the store.Entry directly
if err := structwrapping.UnwrapStruct(ctx, e.Cipherer, e.Entry, nil); err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
}
return nil
}
// Replay provides the ability to replay an entry. you must initialize any new tables ending with the tableSuffix before
// calling Replay, otherwise you'll get "a table doesn't exist" error.
func (e *Entry) Replay(ctx context.Context, tx *Writer, types *TypeCatalog, tableSuffix string) error {
const op = "oplog.(Entry).Replay"
msgs, err := e.UnmarshalData(ctx, types)
if err != nil {
return errors.Wrap(ctx, err, op)
}
for _, m := range msgs {
em, ok := m.Message.(ReplayableMessage)
if !ok {
return errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("%T is not a replayable message", m.Message))
}
origTableName := em.TableName()
defer em.SetTableName(origTableName)
/*
how replay will be implemented for snapshots is still very much under discussion.
when we go to implement snapshots we may very well need to refactor
this create table choice... there are many issues with doing the
"create" in this manner:
* the perms needed to create a table and possible security issues
* the fk references would be to the original tables, not the new
replay tables.
* we need a way to create a replay table that contains the existing
named constraints. Currently, on conflicts using a constraint
target won't work.
It may be a better choice to just create separate schemas for replay
named blue and green since we need at min of two replay tables
definitions. if we went with separate schemas they could be create
with a boundary cli cmd that had appropriate privs (reducing
security issues) and the separate schemas wouldn't have the fk
reference issues mentioned above.
*/
replayTable := origTableName + tableSuffix
hasTable, err := tx.hasTable(ctx, replayTable)
if err != nil {
return errors.Wrap(ctx, err, op)
}
if !hasTable {
if err := tx.createTableLike(ctx, origTableName, replayTable); err != nil {
return errors.Wrap(ctx, err, op)
}
}
rw := dbw.New(tx.DB)
m.Opts = append(m.Opts, dbw.WithTable(replayTable))
switch m.OpType {
case OpType_OP_TYPE_CREATE:
if err := rw.Create(ctx, m.Message, m.Opts...); err != nil {
return errors.Wrap(ctx, err, op)
}
case OpType_OP_TYPE_CREATE_ITEMS:
// TODO: jimlambrt 12/2021 -> while this will work for
// CreateItems(...) it's hardly efficient. We'll need to refactor
// oplog quite a bit to support a multi-message operation.
if err := rw.CreateItems(ctx, []interface{}{m.Message}, m.Opts...); err != nil {
return errors.Wrap(ctx, err, op)
}
case OpType_OP_TYPE_UPDATE:
if _, err := rw.Update(ctx, m.Message, m.FieldMaskPaths, m.SetToNullPaths, m.Opts...); err != nil {
return errors.Wrap(ctx, err, op)
}
case OpType_OP_TYPE_DELETE:
if _, err := rw.Delete(ctx, m.Message, m.Opts...); err != nil {
return errors.Wrap(ctx, err, op)
}
case OpType_OP_TYPE_DELETE_ITEMS:
// TODO: jimlambrt 12/2021 -> while this will work for
// DeleteItems(...) it's hardly efficient. We'll need to refactor
// oplog quite a bit to support a multi-message operation.
if _, err := rw.DeleteItems(ctx, []interface{}{m.Message}, m.Opts...); err != nil {
return errors.Wrap(ctx, err, op)
}
default:
return errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("invalid operation %T", m.OpType))
}
}
return nil
}