-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
etcd.go
694 lines (646 loc) · 20.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
Copyright 2015-2019 Gravitational, Inc.
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 etcdbk implements Etcd powered backend
package etcdbk
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"io/ioutil"
"sort"
"strings"
"time"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
"go.etcd.io/etcd/mvcc/mvccpb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
writeRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "etcd_backend_write_requests",
Help: "Number of wrtie requests to the database",
},
)
readRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "etcd_backend_read_requests",
Help: "Number of read requests to the database",
},
)
batchReadRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "etcd_backend_batch_read_requests",
Help: "Number of read requests to the database",
},
)
txRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "etcd_backend_tx_requests",
Help: "Number of transaction requests to the database",
},
)
writeLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "etcd_backend_write_seconds",
Help: "Latency for etcd write operations",
// lowest bucket start of upper bound 0.001 sec (1 ms) with factor 2
// highest bucket start of 0.001 sec * 2^15 == 32.768 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
},
)
txLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "etcd_backend_tx_seconds",
Help: "Latency for etcd transaction operations",
// lowest bucket start of upper bound 0.001 sec (1 ms) with factor 2
// highest bucket start of 0.001 sec * 2^15 == 32.768 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
},
)
batchReadLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "etcd_backend_batch_read_seconds",
Help: "Latency for etcd read operations",
// lowest bucket start of upper bound 0.001 sec (1 ms) with factor 2
// highest bucket start of 0.001 sec * 2^15 == 32.768 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
},
)
readLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "etcd_backend_read_seconds",
Help: "Latency for etcd read operations",
// lowest bucket start of upper bound 0.001 sec (1 ms) with factor 2
// highest bucket start of 0.001 sec * 2^15 == 32.768 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
},
)
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(writeLatencies)
prometheus.MustRegister(txLatencies)
prometheus.MustRegister(batchReadLatencies)
prometheus.MustRegister(readLatencies)
prometheus.MustRegister(writeRequests)
prometheus.MustRegister(txRequests)
prometheus.MustRegister(batchReadRequests)
prometheus.MustRegister(readRequests)
}
const (
// keyPrefix is a prefix that is added to every etcd key
// for backwards compatibility
keyPrefix = "/teleport"
)
type EtcdBackend struct {
nodes []string
*log.Entry
cfg *Config
etcdKey string
client *clientv3.Client
cancelC chan bool
stopC chan bool
clock clockwork.Clock
buf *backend.CircularBuffer
ctx context.Context
cancel context.CancelFunc
watchStarted context.Context
signalWatchStart context.CancelFunc
}
// Config represents JSON config for etcd backend
type Config struct {
// Nodes is a list of nodes
Nodes []string `json:"peers,omitempty"`
// Key is an optional prefix for etcd
Key string `json:"prefix,omitempty"`
// TLSKeyFile is a private key, implies mTLS client authentication
TLSKeyFile string `json:"tls_key_file,omitempty"`
// TLSCertFile is a client certificate implies mTLS client authentication
TLSCertFile string `json:"tls_cert_file,omitempty"`
// TLSCAFile is a trusted certificate authority certificate
TLSCAFile string `json:"tls_ca_file,omitempty"`
// Insecure turns off TLS
Insecure bool `json:"insecure,omitempty"`
// BufferSize is a default buffer size
// used to pull events
BufferSize int `json:"buffer_size,omitempty"`
// DialTimeout specifies dial timeout
DialTimeout time.Duration `json:"dial_timeout,omitempty"`
// Username is an optional username for HTTPS basic authentication
Username string `json:"username,omitempty"`
// Password is initalized from password file, and is not read from the config
Password string `json:"-"`
// PasswordFile is an optional password file for HTTPS basic authentication,
// expects path to a file
PasswordFile string `json:"password_file,omitempty"`
}
// GetName returns the name of etcd backend as it appears in 'storage/type' section
// in Teleport YAML file. This function is a part of backend API
func GetName() string {
return "etcd"
}
// keep this here to test interface conformance
var _ backend.Backend = &EtcdBackend{}
// New returns new instance of Etcd-powered backend
func New(ctx context.Context, params backend.Params) (*EtcdBackend, error) {
var err error
if params == nil {
return nil, trace.BadParameter("missing etcd configuration")
}
// convert generic backend parameters structure to etcd config:
var cfg *Config
if err = utils.ObjectToStruct(params, &cfg); err != nil {
return nil, trace.BadParameter("invalid etcd configuration: %v", err)
}
if err = cfg.Validate(); err != nil {
return nil, trace.Wrap(err)
}
buf, err := backend.NewCircularBuffer(ctx, cfg.BufferSize)
if err != nil {
return nil, trace.Wrap(err)
}
closeCtx, cancel := context.WithCancel(ctx)
watchStarted, signalWatchStart := context.WithCancel(ctx)
b := &EtcdBackend{
Entry: log.WithFields(log.Fields{trace.Component: GetName()}),
cfg: cfg,
nodes: cfg.Nodes,
cancelC: make(chan bool, 1),
stopC: make(chan bool, 1),
clock: clockwork.NewRealClock(),
cancel: cancel,
ctx: closeCtx,
watchStarted: watchStarted,
signalWatchStart: signalWatchStart,
buf: buf,
}
if err = b.reconnect(); err != nil {
return nil, trace.Wrap(err)
}
// Wrap backend in a input sanitizer and return it.
return b, nil
}
// Validate checks if all the parameters are present/valid
func (cfg *Config) Validate() error {
if len(cfg.Key) == 0 {
return trace.BadParameter(`etcd: missing "prefix" parameter`)
}
if len(cfg.Nodes) == 0 {
return trace.BadParameter(`etcd: missing "peers" parameter`)
}
if cfg.Insecure == false {
if cfg.TLSCAFile == "" {
return trace.BadParameter(`etcd: missing "tls_ca_file" parameter`)
}
}
if cfg.BufferSize == 0 {
cfg.BufferSize = backend.DefaultBufferSize
}
if cfg.DialTimeout == 0 {
cfg.DialTimeout = defaults.DefaultDialTimeout
}
if cfg.PasswordFile != "" {
out, err := ioutil.ReadFile(cfg.PasswordFile)
if err != nil {
return trace.ConvertSystemError(err)
}
// trim newlines as passwords in files tend to have newlines
cfg.Password = strings.TrimSpace(string(out))
}
return nil
}
func (b *EtcdBackend) Clock() clockwork.Clock {
return b.clock
}
func (b *EtcdBackend) Close() error {
b.cancel()
b.buf.Close()
return b.client.Close()
}
// CloseWatchers closes all the watchers
// without closing the backend
func (b *EtcdBackend) CloseWatchers() {
b.buf.Reset()
}
func (b *EtcdBackend) reconnect() error {
tlsConfig := utils.TLSConfig(nil)
if b.cfg.TLSCertFile != "" {
clientCertPEM, err := ioutil.ReadFile(b.cfg.TLSCertFile)
if err != nil {
return trace.ConvertSystemError(err)
}
clientKeyPEM, err := ioutil.ReadFile(b.cfg.TLSKeyFile)
if err != nil {
return trace.ConvertSystemError(err)
}
tlsCert, err := tls.X509KeyPair(clientCertPEM, clientKeyPEM)
if err != nil {
return trace.BadParameter("failed to parse private key: %v", err)
}
tlsConfig.Certificates = []tls.Certificate{tlsCert}
}
var caCertPEM []byte
if b.cfg.TLSCAFile != "" {
var err error
caCertPEM, err = ioutil.ReadFile(b.cfg.TLSCAFile)
if err != nil {
return trace.ConvertSystemError(err)
}
}
certPool := x509.NewCertPool()
parsedCert, err := tlsca.ParseCertificatePEM(caCertPEM)
if err != nil {
return trace.Wrap(err, "failed to parse CA certificate")
}
certPool.AddCert(parsedCert)
tlsConfig.RootCAs = certPool
tlsConfig.ClientCAs = certPool
clt, err := clientv3.New(clientv3.Config{
Endpoints: b.nodes,
TLS: tlsConfig,
DialTimeout: b.cfg.DialTimeout,
Username: b.cfg.Username,
Password: b.cfg.Password,
})
if err != nil {
return trace.Wrap(err)
}
b.client = clt
go b.asyncWatch()
return nil
}
func (b *EtcdBackend) asyncWatch() {
err := b.watchEvents()
b.Debugf("Watch exited: %v.", err)
}
func (b *EtcdBackend) watchEvents() error {
start:
eventsC := b.client.Watch(b.ctx, keyPrefix, clientv3.WithPrefix())
b.signalWatchStart()
for {
select {
case e, ok := <-eventsC:
if e.Canceled || !ok {
b.Debugf("Watch channel has closed.")
goto start
}
out := make([]backend.Event, 0, len(e.Events))
for i := range e.Events {
event, err := b.fromEvent(b.ctx, *e.Events[i])
if err != nil {
b.Errorf("Failed to unmarshal event: %v %v.", err, *e.Events[i])
} else {
out = append(out, *event)
}
}
b.buf.PushBatch(out)
case <-b.ctx.Done():
return trace.ConnectionProblem(b.ctx.Err(), "context is closing")
}
}
}
// NewWatcher returns a new event watcher
func (b *EtcdBackend) NewWatcher(ctx context.Context, watch backend.Watch) (backend.Watcher, error) {
select {
case <-b.watchStarted.Done():
case <-ctx.Done():
return nil, trace.ConnectionProblem(ctx.Err(), "context is closing")
}
return b.buf.NewWatcher(ctx, watch)
}
// GetRange returns query range
func (b *EtcdBackend) GetRange(ctx context.Context, startKey, endKey []byte, limit int) (*backend.GetResult, error) {
if len(startKey) == 0 {
return nil, trace.BadParameter("missing parameter startKey")
}
if len(endKey) == 0 {
return nil, trace.BadParameter("missing parameter endKey")
}
opts := []clientv3.OpOption{clientv3.WithSerializable(), clientv3.WithRange(prependPrefix(endKey))}
if limit > 0 {
opts = append(opts, clientv3.WithLimit(int64(limit)))
}
start := b.clock.Now()
re, err := b.client.Get(ctx, prependPrefix(startKey), opts...)
batchReadLatencies.Observe(time.Since(start).Seconds())
batchReadRequests.Inc()
if err := convertErr(err); err != nil {
return nil, trace.Wrap(err)
}
items := make([]backend.Item, 0, len(re.Kvs))
for _, kv := range re.Kvs {
value, err := unmarshal(kv.Value)
if err != nil {
return nil, trace.Wrap(err)
}
items = append(items, backend.Item{
Key: trimPrefix(kv.Key),
Value: value,
ID: kv.ModRevision,
LeaseID: kv.Lease,
})
}
sort.Sort(backend.Items(items))
return &backend.GetResult{Items: items}, nil
}
// Create creates item if it does not exist
func (b *EtcdBackend) Create(ctx context.Context, item backend.Item) (*backend.Lease, error) {
var opts []clientv3.OpOption
var lease backend.Lease
if !item.Expires.IsZero() {
if err := b.setupLease(ctx, item, &lease, &opts); err != nil {
return nil, trace.Wrap(err)
}
}
start := b.clock.Now()
re, err := b.client.Txn(ctx).
If(clientv3.Compare(clientv3.CreateRevision(prependPrefix(item.Key)), "=", 0)).
Then(clientv3.OpPut(prependPrefix(item.Key), base64.StdEncoding.EncodeToString(item.Value), opts...)).
Commit()
txLatencies.Observe(time.Since(start).Seconds())
txRequests.Inc()
if err != nil {
return nil, trace.Wrap(convertErr(err))
}
if !re.Succeeded {
return nil, trace.AlreadyExists("%q already exists", string(item.Key))
}
return &lease, nil
}
// Update updates value in the backend
func (b *EtcdBackend) Update(ctx context.Context, item backend.Item) (*backend.Lease, error) {
var opts []clientv3.OpOption
var lease backend.Lease
if !item.Expires.IsZero() {
if err := b.setupLease(ctx, item, &lease, &opts); err != nil {
return nil, trace.Wrap(err)
}
}
start := b.clock.Now()
re, err := b.client.Txn(ctx).
If(clientv3.Compare(clientv3.CreateRevision(prependPrefix(item.Key)), "!=", 0)).
Then(clientv3.OpPut(prependPrefix(item.Key), base64.StdEncoding.EncodeToString(item.Value), opts...)).
Commit()
txLatencies.Observe(time.Since(start).Seconds())
txRequests.Inc()
if err != nil {
return nil, trace.Wrap(convertErr(err))
}
if !re.Succeeded {
return nil, trace.NotFound("%q is not found", string(item.Key))
}
return &lease, nil
}
// CompareAndSwap compares item with existing item
// and replaces is with replaceWith item
func (b *EtcdBackend) CompareAndSwap(ctx context.Context, expected backend.Item, replaceWith backend.Item) (*backend.Lease, error) {
if len(expected.Key) == 0 {
return nil, trace.BadParameter("missing parameter Key")
}
if len(replaceWith.Key) == 0 {
return nil, trace.BadParameter("missing parameter Key")
}
if bytes.Compare(expected.Key, replaceWith.Key) != 0 {
return nil, trace.BadParameter("expected and replaceWith keys should match")
}
var opts []clientv3.OpOption
var lease backend.Lease
if !replaceWith.Expires.IsZero() {
if err := b.setupLease(ctx, replaceWith, &lease, &opts); err != nil {
return nil, trace.Wrap(err)
}
}
encodedPrev := base64.StdEncoding.EncodeToString(expected.Value)
start := b.clock.Now()
re, err := b.client.Txn(ctx).
If(clientv3.Compare(clientv3.Value(prependPrefix(expected.Key)), "=", encodedPrev)).
Then(clientv3.OpPut(prependPrefix(expected.Key), base64.StdEncoding.EncodeToString(replaceWith.Value), opts...)).
Commit()
txLatencies.Observe(time.Since(start).Seconds())
txRequests.Inc()
if err != nil {
err = convertErr(err)
if trace.IsNotFound(err) {
return nil, trace.CompareFailed(err.Error())
}
}
if !re.Succeeded {
return nil, trace.CompareFailed("key %q did not match expected value", string(expected.Key))
}
return &lease, nil
}
// Put puts value into backend (creates if it does not
// exists, updates it otherwise)
func (b *EtcdBackend) Put(ctx context.Context, item backend.Item) (*backend.Lease, error) {
var opts []clientv3.OpOption
var lease backend.Lease
if !item.Expires.IsZero() {
if err := b.setupLease(ctx, item, &lease, &opts); err != nil {
return nil, trace.Wrap(err)
}
}
start := b.clock.Now()
_, err := b.client.Put(
ctx,
prependPrefix(item.Key),
base64.StdEncoding.EncodeToString(item.Value),
opts...)
writeLatencies.Observe(time.Since(start).Seconds())
writeRequests.Inc()
if err != nil {
return nil, convertErr(err)
}
return &lease, nil
}
// KeepAlive updates TTL on the lease ID
func (b *EtcdBackend) KeepAlive(ctx context.Context, lease backend.Lease, expires time.Time) error {
if lease.ID == 0 {
return trace.BadParameter("lease is not specified")
}
re, err := b.client.Get(ctx, prependPrefix(lease.Key), clientv3.WithSerializable(), clientv3.WithKeysOnly())
if err != nil {
return convertErr(err)
}
if len(re.Kvs) == 0 {
return trace.NotFound("item %q is not found", string(lease.Key))
}
// instead of keep-alive on the old lease, setup a new lease
// because we would like the event to be generated
// which does not happen in case of lease keep-alive
var opts []clientv3.OpOption
var newLease backend.Lease
if err := b.setupLease(ctx, backend.Item{Expires: expires}, &newLease, &opts); err != nil {
return trace.Wrap(err)
}
opts = append(opts, clientv3.WithIgnoreValue())
kv := re.Kvs[0]
_, err = b.client.Put(ctx, string(kv.Key), "", opts...)
return convertErr(err)
}
// Get returns a single item or not found error
func (b *EtcdBackend) Get(ctx context.Context, key []byte) (*backend.Item, error) {
re, err := b.client.Get(ctx, prependPrefix(key), clientv3.WithSerializable())
if err != nil {
return nil, convertErr(err)
}
if len(re.Kvs) == 0 {
return nil, trace.NotFound("item %q is not found", string(key))
}
kv := re.Kvs[0]
bytes, err := unmarshal(kv.Value)
if err != nil {
return nil, trace.Wrap(err)
}
return &backend.Item{Key: key, Value: bytes, ID: kv.ModRevision, LeaseID: kv.Lease}, nil
}
// Delete deletes item by key
func (b *EtcdBackend) Delete(ctx context.Context, key []byte) error {
start := b.clock.Now()
re, err := b.client.Delete(ctx, prependPrefix(key))
writeLatencies.Observe(time.Since(start).Seconds())
writeRequests.Inc()
if err != nil {
return trace.Wrap(convertErr(err))
}
if re.Deleted == 0 {
return trace.NotFound("%q is not found", key)
}
return nil
}
// DeleteRange deletes range of items with keys between startKey and endKey
func (b *EtcdBackend) DeleteRange(ctx context.Context, startKey, endKey []byte) error {
if len(startKey) == 0 {
return trace.BadParameter("missing parameter startKey")
}
if len(endKey) == 0 {
return trace.BadParameter("missing parameter endKey")
}
start := b.clock.Now()
_, err := b.client.Delete(ctx, prependPrefix(startKey), clientv3.WithRange(prependPrefix(endKey)))
writeLatencies.Observe(time.Since(start).Seconds())
writeRequests.Inc()
if err != nil {
return trace.Wrap(convertErr(err))
}
return nil
}
func (b *EtcdBackend) setupLease(ctx context.Context, item backend.Item, lease *backend.Lease, opts *[]clientv3.OpOption) error {
ttl := b.ttl(item.Expires)
elease, err := b.client.Grant(ctx, seconds(ttl))
if err != nil {
return convertErr(err)
}
*opts = []clientv3.OpOption{clientv3.WithLease(elease.ID)}
lease.ID = int64(elease.ID)
lease.Key = item.Key
return nil
}
func (b *EtcdBackend) ttl(expires time.Time) time.Duration {
return backend.TTL(b.clock, expires)
}
func (b *EtcdBackend) fromEvent(ctx context.Context, e clientv3.Event) (*backend.Event, error) {
event := &backend.Event{
Type: fromType(e.Type),
Item: backend.Item{
Key: trimPrefix(e.Kv.Key),
ID: e.Kv.ModRevision,
},
}
if event.Type == backend.OpDelete {
return event, nil
}
// get the new expiration date if it was updated
if e.Kv.Lease != 0 {
re, err := b.client.TimeToLive(ctx, clientv3.LeaseID(e.Kv.Lease))
if err != nil {
return nil, convertErr(err)
}
event.Item.Expires = b.clock.Now().UTC().Add(time.Second * time.Duration(re.TTL))
}
value, err := unmarshal(e.Kv.Value)
if err != nil {
return nil, trace.Wrap(err)
}
event.Item.Value = value
return event, nil
}
// seconds converts duration to seconds, rounds up to 1 second
func seconds(ttl time.Duration) int64 {
i := int64(ttl / time.Second)
if i <= 0 {
i = 1
}
return i
}
func unmarshal(value []byte) ([]byte, error) {
if len(value) == 0 {
return nil, trace.BadParameter("missing value")
}
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(value)))
n, err := base64.StdEncoding.Decode(dbuf, value)
return dbuf[:n], err
}
func convertErr(err error) error {
if err == nil {
return nil
}
if err == context.Canceled {
return trace.ConnectionProblem(err, "operation has been cancelled")
} else if err == context.DeadlineExceeded {
return trace.ConnectionProblem(err, "operation has timed out")
} else if err == rpctypes.ErrEmptyKey {
return trace.BadParameter(err.Error())
} else if ev, ok := status.FromError(err); ok {
switch ev.Code() {
// server-side context might have timed-out first (due to clock skew)
// while original client-side context is not timed-out yet
case codes.DeadlineExceeded:
return trace.ConnectionProblem(err, "operation has timed out")
case codes.NotFound:
return trace.NotFound(err.Error())
case codes.AlreadyExists:
return trace.AlreadyExists(err.Error())
case codes.FailedPrecondition:
return trace.CompareFailed(err.Error())
default:
return trace.BadParameter(err.Error())
}
}
return trace.ConnectionProblem(err, err.Error())
}
func fromType(eventType mvccpb.Event_EventType) backend.OpType {
switch eventType {
case mvccpb.PUT:
return backend.OpPut
default:
return backend.OpDelete
}
}
func trimPrefix(in []byte) []byte {
return bytes.TrimPrefix(in, []byte(keyPrefix))
}
func prependPrefix(in []byte) string {
return keyPrefix + string(in)
}