/
utils_tls.go
318 lines (267 loc) · 8.09 KB
/
utils_tls.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
package utils
import (
"crypto/tls"
"crypto/x509"
"encoding/hex"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
"github.com/akutz/gotil"
"github.com/rexray/rexray/libstorage/api/context"
"github.com/rexray/rexray/libstorage/api/types"
)
var knownHostRX = regexp.MustCompile(`(?i)^([^\s]+?)\s([^\s]+?)\s(.+)$`)
// ParseKnownHost parses a known host line that's in the expected format:
// "host algorithm fingerprint".
func ParseKnownHost(
ctx types.Context,
text string) (*types.TLSKnownHost, error) {
m := knownHostRX.FindStringSubmatch(text)
if len(m) == 0 {
return nil, nil
}
ctx.WithFields(map[string]interface{}{
"khHost": m[1],
"khAlg": m[2],
"khSig": m[3],
}).Debug("parsing known_hosts file fields")
buf, err := hex.DecodeString(strings.Replace(m[3], ":", "", -1))
if err != nil {
return nil, goof.WithError(
"error decoding known host fingerprint", err)
}
return &types.TLSKnownHost{
Host: m[1],
Alg: m[2],
Fingerprint: buf,
}, nil
}
// ParseTLSConfig returns a new TLS configuration.
func ParseTLSConfig(
ctx types.Context,
config gofig.Config,
proto string,
fields log.Fields,
roots ...string) (tlsConfig *types.TLSConfig, tlsErr error) {
if strings.EqualFold(proto, "unix") {
enable, _ := strconv.ParseBool(
os.Getenv("LIBSTORAGE_TLS_SOCKITTOME"))
if !enable {
ctx.Debug("disabling tls for unix sockets")
return nil, nil
}
}
ctx.Debug("parsing tls config")
pathConfig := context.MustPathConfig(ctx)
f := func(k string, v interface{}) {
if fields == nil {
return
}
fields[k] = v
ctx.WithField(k, v).Debug("tls field set")
}
newTLS := func(k string, v interface{}) {
if tlsConfig != nil {
return
}
ctx.WithField(k, v).Info("tls enabled")
tlsConfig = &types.TLSConfig{Config: tls.Config{}}
}
// check to see if TLS is disabled
if ok := getBool(config, types.ConfigTLSDisabled, roots...); ok {
f(types.ConfigTLSDisabled, true)
ctx.WithField(types.ConfigTLSDisabled, false).Info("tls disabled")
return nil, nil
}
// check to see if TLS is enabled with a simple truthy value
if ok := getBool(config, types.ConfigTLS, roots...); ok {
f(types.ConfigTLS, true)
newTLS(types.ConfigTLS, true)
} else if v := getString(config, types.ConfigTLS, roots...); v != "" {
// check to see if TLS is disabled
if strings.EqualFold(v, "false") {
f(types.ConfigTLS, "false")
ctx.WithField(types.ConfigTLS, "false").Info("tls disabled")
return nil, nil
}
// check to see if TLS is enabled with insecure
if strings.EqualFold(v, "insecure") {
f(types.ConfigTLS, "insecure")
newTLS(types.ConfigTLS, "insecure")
tlsConfig.InsecureSkipVerify = true
// check to see if TLS is enabled with peers
} else if strings.EqualFold(v, "verifyPeers") {
f(types.ConfigTLS, "verifyPeers")
newTLS(types.ConfigTLS, "verifyPeers")
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyPeers = true
// check to see if TLS is enabled with an expected sha256
// fingerprint
} else if kh, err := ParseKnownHost(ctx, v); err != nil {
ctx.Error(err)
return nil, err
} else if kh != nil {
f(types.ConfigTLS, kh.String())
newTLS(types.ConfigTLS, kh.String())
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyPeers = true
tlsConfig.KnownHost = kh
}
}
// always check for the user's known_hosts file
if gotil.FileExists(pathConfig.UserDefaultTLSKnownHosts) {
newTLS("usrKnownHosts", pathConfig.UserDefaultTLSKnownHosts)
f("usrKnownHosts", pathConfig.UserDefaultTLSKnownHosts)
tlsConfig.UsrKnownHosts = pathConfig.UserDefaultTLSKnownHosts
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyPeers = true
}
// always check for the system's known_hosts file
if err := func() error {
if !isSet(config, types.ConfigTLSKnownHosts, roots...) {
return nil
}
khFile := getString(config, types.ConfigTLSKnownHosts, roots...)
// is the known_hosts file the same as the default known_hosts
// file? It's not possible to use os.SameFile as the files may not
// yet exist
isDefKH := strings.EqualFold(khFile, pathConfig.DefaultTLSKnownHosts)
if !gotil.FileExists(khFile) {
if !isDefKH {
return goof.WithFields(map[string]interface{}{
"path": khFile,
"defaultPath": pathConfig.DefaultTLSKnownHosts,
}, "invalid known_hosts file")
}
return nil
}
newTLS(types.ConfigTLSKnownHosts, khFile)
f(types.ConfigTLSKnownHosts, khFile)
tlsConfig.SysKnownHosts = khFile
tlsConfig.VerifyPeers = true
tlsConfig.InsecureSkipVerify = true
return nil
}(); err != nil {
return nil, err
}
// always check for the cacerts file
if err := func() error {
if !isSet(config, types.ConfigTLSTrustedCertsFile, roots...) {
return nil
}
caCerts := getString(config, types.ConfigTLSTrustedCertsFile, roots...)
// is the key file the same as the default cacerts file? It's not
// possible to use os.SameFile as the files may not yet exist
isDefCA := strings.EqualFold(
caCerts, pathConfig.DefaultTLSTrustedRootsFile)
if !gotil.FileExists(caCerts) {
if !isDefCA {
return goof.WithField("path", caCerts, "invalid cacerts file")
}
return nil
}
buf, err := func() ([]byte, error) {
f, err := os.Open(caCerts)
if err != nil {
return nil, goof.WithFieldE(
"path", caCerts, "error opening cacerts file", err)
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, goof.WithFieldE(
"path", caCerts, "error reading cacerts file", err)
}
return buf, nil
}()
if err != nil {
return err
}
newTLS(types.ConfigTLSTrustedCertsFile, caCerts)
f(types.ConfigTLSTrustedCertsFile, caCerts)
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(buf)
tlsConfig.RootCAs = certPool
tlsConfig.ClientCAs = certPool
return nil
}(); err != nil {
return nil, err
}
// always check for the cert and key files
if err := func() error {
if !isSet(config, types.ConfigTLSKeyFile, roots...) {
return nil
}
keyFile := getString(config, types.ConfigTLSKeyFile, roots...)
// is the key file the same as the default key file? It's not
// possible to use os.SameFile as the files may not yet exist
isDefKF := strings.EqualFold(keyFile, pathConfig.DefaultTLSKeyFile)
if !gotil.FileExists(keyFile) {
if !isDefKF {
return goof.WithField("path", keyFile, "invalid key file")
}
return nil
}
crtFile := getString(config, types.ConfigTLSCertFile, roots...)
// is the cert file the same as the default cert file? It's not
// possible to use os.SameFile as the files may not yet exist
isDefCF := strings.EqualFold(crtFile, pathConfig.DefaultTLSCertFile)
if !gotil.FileExists(crtFile) {
if !isDefCF {
return goof.WithField("path", crtFile, "invalid crt file")
}
return nil
}
newTLS(types.ConfigTLSKeyFile, keyFile)
f(types.ConfigTLSKeyFile, keyFile)
f(types.ConfigTLSCertFile, crtFile)
cer, err := tls.LoadX509KeyPair(crtFile, keyFile)
if err != nil {
return goof.WithError("error loading x509 pair", err)
}
tlsConfig.Certificates = []tls.Certificate{cer}
return nil
}(); err != nil {
return nil, err
}
if v := getString(
config,
types.ConfigTLSInsecure, roots...); v != "" {
bv, _ := strconv.ParseBool(v)
newTLS(types.ConfigTLSInsecure, bv)
f(types.ConfigTLSInsecure, bv)
tlsConfig.InsecureSkipVerify = bv
}
if v := getString(
config,
types.ConfigTLSVerifyPeers, roots...); v != "" {
bv, _ := strconv.ParseBool(v)
newTLS(types.ConfigTLSVerifyPeers, bv)
f(types.ConfigTLSVerifyPeers, bv)
tlsConfig.VerifyPeers = bv
tlsConfig.InsecureSkipVerify = bv
}
if v := getString(
config,
types.ConfigTLSClientCertRequired, roots...); v != "" {
bv, _ := strconv.ParseBool(v)
newTLS(types.ConfigTLSClientCertRequired, bv)
f(types.ConfigTLSClientCertRequired, bv)
if bv {
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
}
if v := getString(
config,
types.ConfigTLSServerName, roots...); v != "" {
newTLS(types.ConfigTLSServerName, v)
f(types.ConfigTLSServerName, v)
tlsConfig.ServerName = v
}
return tlsConfig, nil
}