-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
158 lines (140 loc) · 3.58 KB
/
kv.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
package etcdcrypto
import (
"github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"golang.org/x/net/context"
)
type kvCipher struct {
clientv3.KV
Cipher
}
func NewKV(kv clientv3.KV, cipher Cipher) clientv3.KV {
return &kvCipher{kv, cipher}
}
func (kv *kvCipher) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
r, err := kv.Do(ctx, clientv3.OpPut(key, val, opts...))
if err != nil {
return nil, err
}
return r.Put(), nil
}
func (kv *kvCipher) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
r, err := kv.Do(ctx, clientv3.OpGet(key, opts...))
if err != nil {
return nil, err
}
return r.Get(), nil
}
func (kv *kvCipher) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
r, err := kv.Do(ctx, clientv3.OpDelete(key, opts...))
if err != nil {
return nil, err
}
return r.Del(), nil
}
func (kv *kvCipher) Do(ctx context.Context, op clientv3.Op) (clientv3.OpResponse, error) {
r, err := kv.KV.Do(ctx, kv.encOp(op))
if err != nil {
return r, err
}
switch {
case r.Get() != nil:
kv.decGetResponse(r.Get())
case r.Put() != nil:
kv.decPutResponse(r.Put())
case r.Del() != nil:
kv.decDeleteResponse(r.Del())
}
return r, nil
}
type txnCipher struct {
clientv3.Txn
kv *kvCipher
}
func (kv *kvCipher) Txn(ctx context.Context) clientv3.Txn {
return &txnCipher{kv.KV.Txn(ctx), kv}
}
func (txn *txnCipher) If(cs ...clientv3.Cmp) clientv3.Txn {
newCmps := make([]clientv3.Cmp, len(cs))
for i := range cs {
newCmps[i] = cs[i]
}
txn.Txn = txn.Txn.If(newCmps...)
return txn
}
func (txn *txnCipher) Then(ops ...clientv3.Op) clientv3.Txn {
newOps := make([]clientv3.Op, len(ops))
for i := range ops {
newOps[i] = txn.kv.encOp(ops[i])
}
txn.Txn = txn.Txn.Then(newOps...)
return txn
}
func (txn *txnCipher) Else(ops ...clientv3.Op) clientv3.Txn {
newOps := make([]clientv3.Op, len(ops))
for i := range ops {
newOps[i] = txn.kv.encOp(ops[i])
}
txn.Txn = txn.Txn.Else(newOps...)
return txn
}
func (txn *txnCipher) Commit() (*clientv3.TxnResponse, error) {
resp, err := txn.Txn.Commit()
if err != nil {
return nil, err
}
txn.kv.decTxnResponse(resp)
return resp, nil
}
func (kv *kvCipher) decGetResponse(resp *clientv3.GetResponse) {
for i := range resp.Kvs {
v, err := kv.Decrypt(resp.Kvs[i].Value)
if err != nil {
panic(err)
}
resp.Kvs[i].Value = v
}
}
func (kv *kvCipher) decPutResponse(resp *clientv3.PutResponse) {
if resp.PrevKv != nil {
v, err := kv.Decrypt(resp.PrevKv.Value)
if err != nil {
panic(err)
}
resp.PrevKv.Value = v
}
}
func (kv *kvCipher) decDeleteResponse(resp *clientv3.DeleteResponse) {
for i := range resp.PrevKvs {
v, err := kv.Decrypt(resp.PrevKvs[i].Value)
if err != nil {
panic(err)
}
resp.PrevKvs[i].Value = v
}
}
func (kv *kvCipher) decTxnResponse(resp *clientv3.TxnResponse) {
for _, r := range resp.Responses {
switch tv := r.Response.(type) {
case *pb.ResponseOp_ResponseRange:
if tv.ResponseRange != nil {
kv.decGetResponse((*clientv3.GetResponse)(tv.ResponseRange))
}
case *pb.ResponseOp_ResponsePut:
if tv.ResponsePut != nil {
kv.decPutResponse((*clientv3.PutResponse)(tv.ResponsePut))
}
case *pb.ResponseOp_ResponseDeleteRange:
if tv.ResponseDeleteRange != nil {
kv.decDeleteResponse((*clientv3.DeleteResponse)(tv.ResponseDeleteRange))
}
default:
}
}
}
func (kv *kvCipher) encOp(op clientv3.Op) clientv3.Op {
if b := op.ValueBytes(); b != nil {
op.WithValueBytes(kv.Encrypt(b))
}
return op
}