-
Notifications
You must be signed in to change notification settings - Fork 479
/
etcd.go
429 lines (369 loc) · 11.5 KB
/
etcd.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
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package etcd
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/dapr/components-contrib/internal/utils"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/state"
stateutils "github.com/dapr/components-contrib/state/utils"
"github.com/dapr/kit/logger"
"github.com/dapr/kit/ptr"
)
// Etcd is a state store implementation for Etcd.
type Etcd struct {
state.BulkStore
client *clientv3.Client
keyPrefixPath string
features []state.Feature
logger logger.Logger
}
type etcdConfig struct {
Endpoints string `json:"endpoints"`
KeyPrefixPath string `json:"keyPrefixPath"`
// TLS
TLSEnable string `json:"tlsEnable"`
CA string `json:"ca"`
Cert string `json:"cert"`
Key string `json:"key"`
}
// NewEtcdStateStore returns a new etcd state store.
func NewEtcdStateStore(logger logger.Logger) state.Store {
s := &Etcd{
logger: logger,
features: []state.Feature{state.FeatureETag, state.FeatureTransactional},
}
s.BulkStore = state.NewDefaultBulkStore(s)
return s
}
// Init does metadata and config parsing and initializes the
// Etcd client.
func (e *Etcd) Init(_ context.Context, metadata state.Metadata) error {
etcdConfig, err := metadataToConfig(metadata.Properties)
if err != nil {
return fmt.Errorf("couldn't convert metadata properties: %w", err)
}
e.client, err = e.ParseClientFromConfig(etcdConfig)
if err != nil {
return fmt.Errorf("initializing etcd client: %w", err)
}
e.keyPrefixPath = etcdConfig.KeyPrefixPath
return nil
}
func (e *Etcd) ParseClientFromConfig(etcdConfig *etcdConfig) (*clientv3.Client, error) {
endpoints := strings.Split(etcdConfig.Endpoints, ",")
if len(endpoints) == 0 || endpoints[0] == "" {
return nil, errors.New("endpoints required")
}
var tlsConfig *tls.Config
if utils.IsTruthy(etcdConfig.TLSEnable) {
if etcdConfig.Cert != "" && etcdConfig.Key != "" && etcdConfig.CA != "" {
var err error
tlsConfig, err = NewTLSConfig(etcdConfig.Cert, etcdConfig.Key, etcdConfig.CA)
if err != nil {
return nil, fmt.Errorf("tls authentication error: %w", err)
}
} else {
return nil, errors.New("tls authentication information is incomplete")
}
}
config := clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
TLS: tlsConfig,
}
client, err := clientv3.New(config)
if err != nil {
return nil, err
}
return client, nil
}
// Features returns the features available in this state store.
func (e *Etcd) Features() []state.Feature {
return e.features
}
func metadataToConfig(connInfo map[string]string) (*etcdConfig, error) {
m := &etcdConfig{}
err := metadata.DecodeMetadata(connInfo, m)
return m, err
}
// Get retrieves a Etcd KV item.
func (e *Etcd) Get(ctx context.Context, req *state.GetRequest) (*state.GetResponse, error) {
keyWithPath := e.keyPrefixPath + "/" + req.Key
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
resp, err := e.client.Get(ctx, keyWithPath)
if err != nil {
return nil, fmt.Errorf("couldn't get key %s: %w", keyWithPath, err)
}
if resp == nil || len(resp.Kvs) == 0 {
return &state.GetResponse{}, nil
}
return &state.GetResponse{
Data: resp.Kvs[0].Value,
ETag: ptr.Of(strconv.Itoa(int(resp.Kvs[0].ModRevision))),
}, nil
}
// Set saves a Etcd KV item.
func (e *Etcd) Set(ctx context.Context, req *state.SetRequest) error {
ttlInSeconds, err := e.doSetValidateParameters(req)
if err != nil {
return err
}
keyWithPath := e.keyPrefixPath + "/" + req.Key
err = e.doValidateEtag(keyWithPath, req.ETag, req.Options.Concurrency)
if err != nil {
return err
}
reqVal, err := stateutils.Marshal(req.Value, json.Marshal)
if err != nil {
return err
}
return e.doSet(ctx, keyWithPath, string(reqVal), req.ETag, ttlInSeconds)
}
func (e *Etcd) doSet(ctx context.Context, key, reqVal string, etag *string, ttlInSeconds int64) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if ttlInSeconds > 0 {
resp, err := e.client.Grant(ctx, ttlInSeconds)
if err != nil {
return fmt.Errorf("couldn't grant lease %s: %w", key, err)
}
if etag != nil {
etag, _ := strconv.ParseInt(*etag, 10, 64)
_, err = e.client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", etag)).
Then(clientv3.OpPut(key, reqVal, clientv3.WithLease(resp.ID))).
Commit()
} else {
_, err = e.client.Put(ctx, key, reqVal, clientv3.WithLease(resp.ID))
}
if err != nil {
return fmt.Errorf("couldn't set key %s: %w", key, err)
}
} else {
var err error
if etag != nil {
etag, _ := strconv.ParseInt(*etag, 10, 64)
_, err = e.client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", etag)).
Then(clientv3.OpPut(key, reqVal)).
Commit()
} else {
_, err = e.client.Put(ctx, key, reqVal)
}
if err != nil {
return fmt.Errorf("couldn't set key %s: %w", key, err)
}
}
return nil
}
func (e *Etcd) doSetValidateParameters(req *state.SetRequest) (int64, error) {
err := state.CheckRequestOptions(req.Options)
if err != nil {
return 0, err
}
var ttlVal int64
ttlInSeconds, err := stateutils.ParseTTL(req.Metadata)
if err != nil {
return 0, err
}
if ttlInSeconds != nil {
ttlVal = int64(*ttlInSeconds)
}
return ttlVal, nil
}
// Delete performes a Etcd KV delete operation.
func (e *Etcd) Delete(ctx context.Context, req *state.DeleteRequest) error {
if err := state.CheckRequestOptions(req.Options); err != nil {
return err
}
keyWithPath := e.keyPrefixPath + "/" + req.Key
if err := e.doValidateEtag(keyWithPath, req.ETag, req.Options.Concurrency); err != nil {
return err
}
return e.doDelete(ctx, keyWithPath, req.ETag)
}
func (e *Etcd) doDelete(ctx context.Context, key string, etag *string) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var err error
if etag != nil {
etag, _ := strconv.ParseInt(*etag, 10, 64)
_, err = e.client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", etag)).
Then(clientv3.OpDelete(key)).
Commit()
} else {
_, err = e.client.Delete(ctx, key)
}
if err != nil {
return fmt.Errorf("couldn't delete key %s: %w", key, err)
}
return nil
}
func (e *Etcd) doValidateEtag(key string, etag *string, concurrency string) error {
hasEtag := etag != nil && *etag != ""
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if concurrency == state.FirstWrite && !hasEtag {
item, err := e.client.Get(ctx, key)
if err != nil {
return fmt.Errorf("couldn't get key %s: %w", key, err)
} else if item != nil && len(item.Kvs) != 0 {
return state.NewETagError(state.ETagMismatch, errors.New("item already exists and no etag was passed"))
} else {
return nil
}
} else if hasEtag {
item, err := e.client.Get(ctx, key)
if err != nil {
return fmt.Errorf("couldn't get key %s: %w", key, err)
}
if item == nil || len(item.Kvs) == 0 {
return state.NewETagError(state.ETagMismatch, fmt.Errorf("state not exist or expired for key=%s", key))
}
currentEtag := strconv.Itoa(int(item.Kvs[0].ModRevision))
if currentEtag != *etag {
return state.NewETagError(state.ETagMismatch, fmt.Errorf(
"state etag not match for key=%s: current=%s, expect=%s", key, currentEtag, *etag))
}
}
return nil
}
func (e *Etcd) GetComponentMetadata() map[string]string {
metadataStruct := etcdConfig{}
metadataInfo := map[string]string{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.StateStoreType)
return metadataInfo
}
func (e *Etcd) Close() error {
if e.client == nil {
return nil
}
return e.client.Close()
}
func (e *Etcd) Ping() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := e.client.Get(ctx, "health"); err != nil {
return fmt.Errorf("etcd store: error connecting to etcd at %s: %w", e.client.Endpoints(), err)
}
return nil
}
// Multi performs a transactional operation. succeeds only if all operations succeed, and fails if one or more operations fail.
func (e *Etcd) Multi(ctx context.Context, request *state.TransactionalStateRequest) error {
if len(request.Operations) == 0 {
return nil
}
ops := make([]clientv3.Op, 0, len(request.Operations))
for _, o := range request.Operations {
switch req := o.(type) {
case state.SetRequest:
ttlInSeconds, err := e.doSetValidateParameters(&req)
if err != nil {
return err
}
keyWithPath := e.keyPrefixPath + "/" + req.Key
err = e.doValidateEtag(keyWithPath, req.ETag, req.Options.Concurrency)
if err != nil {
return err
}
reqVal, err := stateutils.Marshal(req.Value, json.Marshal)
if err != nil {
return err
}
var cmp clientv3.Cmp
if req.HasETag() {
etag, _ := strconv.ParseInt(*req.ETag, 10, 64)
cmp = clientv3.Compare(clientv3.ModRevision(keyWithPath), "=", etag)
}
if ttlInSeconds > 0 {
resp, err := e.client.Grant(ctx, ttlInSeconds)
if err != nil {
return fmt.Errorf("couldn't grant lease %s: %w", keyWithPath, err)
}
put := clientv3.OpPut(keyWithPath, string(reqVal), clientv3.WithLease(resp.ID))
if req.HasETag() {
ops = append(ops, clientv3.OpTxn([]clientv3.Cmp{cmp}, []clientv3.Op{put}, nil))
} else {
ops = append(ops, clientv3.OpTxn(nil, []clientv3.Op{put}, nil))
}
} else {
put := clientv3.OpPut(keyWithPath, string(reqVal))
if req.HasETag() {
ops = append(ops, clientv3.OpTxn([]clientv3.Cmp{cmp}, []clientv3.Op{put}, nil))
} else {
ops = append(ops, clientv3.OpTxn(nil, []clientv3.Op{put}, nil))
}
}
case state.DeleteRequest:
if err := state.CheckRequestOptions(req.Options); err != nil {
return err
}
keyWithPath := e.keyPrefixPath + "/" + req.Key
err := e.doValidateEtag(keyWithPath, req.ETag, req.Options.Concurrency)
if err != nil {
return err
}
del := clientv3.OpDelete(keyWithPath)
if req.HasETag() {
etag, _ := strconv.ParseInt(*req.ETag, 10, 64)
cmp := clientv3.Compare(clientv3.ModRevision(keyWithPath), "=", etag)
ops = append(ops, clientv3.OpTxn([]clientv3.Cmp{cmp}, []clientv3.Op{del}, nil))
} else {
ops = append(ops, clientv3.OpTxn(nil, []clientv3.Op{del}, nil))
}
}
}
_, err := e.client.Txn(ctx).Then(ops...).Commit()
if err != nil {
return err
}
return nil
}
func NewTLSConfig(clientCert, clientKey, caCert string) (*tls.Config, error) {
valid := false
config := &tls.Config{
MinVersion: tls.VersionTLS12,
}
if clientCert != "" && clientKey != "" {
key := []byte(clientKey)
cert, err := tls.X509KeyPair([]byte(clientCert), key)
if err != nil {
return nil, fmt.Errorf("error parse X509KeyPair: %w", err)
}
config.Certificates = []tls.Certificate{cert}
valid = true
}
if caCert != "" {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
config.RootCAs = caCertPool
valid = true
}
if !valid {
config = nil
}
return config, nil
}