-
Notifications
You must be signed in to change notification settings - Fork 11
/
init.go
161 lines (141 loc) · 3.83 KB
/
init.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
package service
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
etcd "github.com/coreos/etcd/client"
"github.com/coreos/etcd/pkg/transport"
)
var (
logger *log.Logger
clientSingleton etcd.Client
clientSingletonO = &sync.Once{}
hostname string
)
// KAPI provide a etcd KeysAPI for a client provided by the Client() method
func KAPI() etcd.KeysAPI {
return etcd.NewKeysAPI(Client())
}
// Client will generate a valid etcd client from the following environment variables:
// * ETCD_HOSTS: a list of etcd hosts comma separated
// * ETCD_HOST: a single etcd host
// * ETCD_CACERT: The ca certificate
// * ETCD_TLS_CERT: The client tls cert
// * ETCD_TLS_KEY: The client tls key
// * ETCD_TLS_INMEMORY: Is the tls configuration filename or raw certificates
func Client() etcd.Client {
clientSingletonO.Do(func() {
var err error
hosts := []string{"http://localhost:2379"}
if len(os.Getenv("ETCD_HOSTS")) != 0 {
hosts = strings.Split(os.Getenv("ETCD_HOSTS"), ",")
} else if len(os.Getenv("ETCD_HOST")) != 0 {
hosts = []string{os.Getenv("ETCD_HOST")}
} else if len(os.Getenv("ETCD_1_PORT_2379_TCP_ADDR")) != 0 {
hosts = []string{
"http://" +
os.Getenv("ETCD_1_PORT_2379_TCP_ADDR") +
":" + os.Getenv("ETCD_1_PORT_2379_TCP_PORT"),
}
}
cacert := os.Getenv("ETCD_CACERT")
tlskey := os.Getenv("ETCD_TLS_KEY")
tlscert := os.Getenv("ETCD_TLS_CERT")
if len(cacert) != 0 && len(tlskey) != 0 && len(tlscert) != 0 {
for i, host := range hosts {
if !strings.Contains(host, "https://") {
hosts[i] = strings.Replace(host, "http", "https", 1)
}
}
var tlsconfig *tls.Config
if os.Getenv("ETCD_TLS_INMEMORY") == "true" {
tlsconfig, err = tlsconfigFromMemory(tlscert, tlskey, cacert)
} else {
tlsconfig, err = tlsconfigFromFiles(tlscert, tlskey, cacert)
}
if err != nil {
panic(err)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsconfig,
}
c, err := etcd.New(etcd.Config{Endpoints: hosts, Transport: transport})
if err != nil {
panic(err)
}
clientSingleton = c
} else {
clientSingleton, err = etcd.New(etcd.Config{Endpoints: hosts, Transport: etcd.DefaultTransport})
if err != nil {
panic(err)
}
}
})
return clientSingleton
}
func init() {
if len(os.Getenv("HOSTNAME")) != 0 {
hostname = os.Getenv("HOSTNAME")
} else {
h, err := os.Hostname()
if err != nil {
panic(err)
}
hostname = h
}
logger = log.New(os.Stderr, "[etcd-discovery] ", log.LstdFlags)
}
func tlsconfigFromFiles(cert, key, ca string) (*tls.Config, error) {
return transport.TLSInfo{
CertFile: cert, KeyFile: key, TrustedCAFile: ca,
}.ClientConfig()
}
func tlsconfigFromMemory(certb64, keyb64, cab64 string) (*tls.Config, error) {
certpem, err := base64.StdEncoding.DecodeString(certb64)
if err != nil {
return nil, err
}
keypem, err := base64.StdEncoding.DecodeString(keyb64)
if err != nil {
return nil, err
}
capem, err := base64.StdEncoding.DecodeString(cab64)
if err != nil {
return nil, err
}
ca, _ := pem.Decode(capem)
if ca == nil {
return nil, errors.New("ca: invalid PEM")
}
certPool := x509.NewCertPool()
caCert, err := x509.ParseCertificate(ca.Bytes)
if err != nil {
return nil, fmt.Errorf("ca: not a valid certificate %v", err)
}
certPool.AddCert(caCert)
certkey, err := tls.X509KeyPair(certpem, keypem)
if err != nil {
return nil, fmt.Errorf("cert/key: invalid key pair/certificate %v", err)
}
return &tls.Config{
Certificates: []tls.Certificate{certkey},
RootCAs: certPool,
MinVersion: tls.VersionTLS12,
}, nil
}