-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathkv_audit.go
More file actions
168 lines (144 loc) · 4.17 KB
/
Copy pathkv_audit.go
File metadata and controls
168 lines (144 loc) · 4.17 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
package kv
import (
"bufio"
"context"
"encoding/binary"
"fmt"
"os"
"github.com/cockroachdb/errors"
"go.etcd.io/etcd/api/v3/mvccpb"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"github.com/milvus-io/birdwatcher/models"
)
// implementation assertion
var _ MetaKV = (*FileAuditKV)(nil)
type FileAuditKV struct {
cli MetaKV
file *os.File
}
// NewFileAuditKV creates a file auditing log kv.
func NewFileAuditKV(kv MetaKV, file *os.File) *FileAuditKV {
return &FileAuditKV{
cli: kv,
file: file,
}
}
func (c *FileAuditKV) Load(ctx context.Context, key string, opts ...LoadOption) (string, error) {
return c.cli.Load(ctx, key, opts...)
}
func (c *FileAuditKV) LoadWithPrefix(ctx context.Context, key string, opts ...LoadOption) ([]string, []string, error) {
return c.cli.LoadWithPrefix(ctx, key, opts...)
}
func (c *FileAuditKV) Save(ctx context.Context, key, value string) error {
c.writeHeader(models.AuditOpType_OpPut, 2)
err := c.cli.Save(ctx, key, value)
if err == nil {
c.writeHeader(models.AuditOpType_OpPutBefore, 1)
c.writeKeyValue(key, value)
}
c.writeHeader(models.AuditOpType_OpPutAfter, 1)
return err
}
func (c *FileAuditKV) MultiSave(ctx context.Context, keys, values []string) error {
if len(keys) != len(values) {
return errors.Newf("keys and values length mismatch, keys: %d, values: %d", len(keys), len(values))
}
c.writeHeader(models.AuditOpType_OpPut, int32(len(keys)))
err := c.cli.MultiSave(ctx, keys, values)
if err == nil {
c.writeHeader(models.AuditOpType_OpPutBefore, int32(len(keys)))
for i, key := range keys {
c.writeKeyValue(key, values[i])
}
}
c.writeHeader(models.AuditOpType_OpPutAfter, int32(len(keys)))
return err
}
func (c *FileAuditKV) Remove(ctx context.Context, key string) error {
fmt.Println("audit delete", key)
val, err := c.cli.Load(ctx, key)
if err != nil {
return err
}
err = c.cli.Remove(ctx, key)
if err != nil {
return err
}
c.writeHeader(models.AuditOpType_OpDel, 1)
c.writeKeyValue(key, val)
return nil
}
func (c *FileAuditKV) RemoveWithPrefix(ctx context.Context, key string) error {
fmt.Println("audit delete with prefix", key)
keys, values, err := c.cli.LoadWithPrefix(ctx, key)
if err != nil {
return err
}
err = c.cli.RemoveWithPrefix(ctx, key)
if err != nil {
return err
}
c.writeHeader(models.AuditOpType_OpDel, 1)
for i, key := range keys {
val := values[i]
c.writeKeyValue(key, val)
}
return nil
}
func (c *FileAuditKV) removeWithPrevKV(ctx context.Context, key string) (*mvccpb.KeyValue, error) {
return c.cli.removeWithPrevKV(ctx, key)
}
func (c *FileAuditKV) removeWithPrefixAndPrevKV(ctx context.Context, prefix string) ([]*mvccpb.KeyValue, error) {
return c.cli.removeWithPrefixAndPrevKV(ctx, prefix)
}
func (c *FileAuditKV) GetAllRootPath(ctx context.Context) ([]string, error) {
return c.cli.GetAllRootPath(ctx)
}
func (c *FileAuditKV) Close() {
c.cli.Close()
}
func (c *FileAuditKV) BackupKV(base, prefix string, w *bufio.Writer, ignoreRevision bool, batchSize int64) error {
return c.cli.BackupKV(base, prefix, w, ignoreRevision, batchSize)
}
func (c *FileAuditKV) writeHeader(op models.AuditOpType, entriesNum int32) {
header := &models.AuditHeader{
Version: 1,
OpType: int32(op),
EntriesNum: entriesNum,
}
bs, _ := proto.Marshal(header)
c.writeData(bs)
}
func (c *FileAuditKV) writeLogKV(kv *mvccpb.KeyValue) {
if kv == (*mvccpb.KeyValue)(nil) {
return
}
bs, _ := proto.Marshal(protoadapt.MessageV2Of(kv))
c.writeData(bs)
}
func (c *FileAuditKV) writeKeyValue(key, value string) {
kv := &mvccpb.KeyValue{
Key: []byte(key),
Value: []byte(value),
}
c.writeLogKV(kv)
}
func (c *FileAuditKV) writeData(data []byte) {
lb := make([]byte, 8)
binary.LittleEndian.PutUint64(lb, uint64(len(data)))
_, err := c.file.Write(lb)
if err != nil {
fmt.Println("failed to write audit header", err.Error())
return
}
if len(data) > 0 {
_, err = c.file.Write(data)
if err != nil {
fmt.Println("failed to write audit log", err.Error())
}
}
}
func (c *FileAuditKV) WalkWithPrefix(ctx context.Context, prefix string, paginationSize int, fn func([]byte, []byte) error) error {
return c.cli.WalkWithPrefix(ctx, prefix, paginationSize, fn)
}