/
config.go
135 lines (117 loc) 路 3.95 KB
/
config.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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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 etcdv3
import (
"crypto/tls"
"crypto/x509"
"os"
"strings"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/tlsutil"
)
// Config represents a part of the etcd configuration that can be
// loaded from a file. Usually, the Config is next transformed into
// ClientConfig using ConfigToClient() function for use with the coreos/etcd
// package.
type Config struct {
Endpoints []string `json:"endpoints"`
DialTimeout time.Duration `json:"dial-timeout"`
OpTimeout time.Duration `json:"operation-timeout"`
InsecureTransport bool `json:"insecure-transport"`
InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify"`
Certfile string `json:"cert-file"`
Keyfile string `json:"key-file"`
CAfile string `json:"ca-file"`
AutoCompact time.Duration `json:"auto-compact"`
ReconnectResync bool `json:"resync-after-reconnect"`
}
// ClientConfig extends clientv3.Config with configuration options introduced
// by this package.
type ClientConfig struct {
*clientv3.Config
// OpTimeout is the maximum amount of time the client will wait for a pending
// operation before timing out.
OpTimeout time.Duration
}
const (
// defaultDialTimeout defines the default timeout for connecting to etcd.
defaultDialTimeout = 1 * time.Second
// defaultOpTimeout defines the default timeout for any request-reply etcd operation.
defaultOpTimeout = 3 * time.Second
)
// ConfigToClient transforms yaml configuration <yc> modelled by Config
// into ClientConfig, which is ready for use with the underlying coreos/etcd
// package.
// If the etcd endpoint addresses are not specified in the configuration,
// the function will query the ETCDV3_ENDPOINTS environment variable
// for a non-empty value. If neither the config nor the environment specify the
// endpoint location, a default address "127.0.0.1:2379" is assumed.
// The function may return error only if TLS connection is selected and the
// CA or client certificate is not accessible/valid.
func ConfigToClient(yc *Config) (*ClientConfig, error) {
dialTimeout := defaultDialTimeout
if yc.DialTimeout != 0 {
dialTimeout = yc.DialTimeout
}
opTimeout := defaultOpTimeout
if yc.OpTimeout != 0 {
opTimeout = yc.OpTimeout
}
clientv3Cfg := &clientv3.Config{
Endpoints: yc.Endpoints,
DialTimeout: dialTimeout,
}
cfg := &ClientConfig{Config: clientv3Cfg, OpTimeout: opTimeout}
if len(cfg.Endpoints) == 0 {
if ep := os.Getenv("ETCDV3_ENDPOINTS"); ep != "" {
cfg.Endpoints = strings.Split(ep, ",")
} else {
cfg.Endpoints = []string{"127.0.0.1:2379"}
}
}
if yc.InsecureTransport {
cfg.TLS = nil
return cfg, nil
}
var (
cert *tls.Certificate
cp *x509.CertPool
err error
)
if yc.Certfile != "" && yc.Keyfile != "" {
cert, err = tlsutil.NewCert(yc.Certfile, yc.Keyfile, nil)
if err != nil {
return nil, err
}
}
if yc.CAfile != "" {
cp, err = tlsutil.NewCertPool([]string{yc.CAfile})
if err != nil {
return nil, err
}
}
tlscfg := &tls.Config{
MinVersion: tls.VersionTLS10,
InsecureSkipVerify: yc.InsecureSkipTLSVerify,
RootCAs: cp,
}
if cert != nil {
tlscfg.Certificates = []tls.Certificate{*cert}
}
if yc.Certfile != "" || yc.Keyfile != "" || yc.CAfile != "" {
cfg.TLS = tlscfg
}
return cfg, nil
}