forked from oliver006/redis_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.go
106 lines (91 loc) · 2.99 KB
/
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
package exporter
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
log "github.com/sirupsen/logrus"
)
// CreateClientTLSConfig verifies configured files and return a prepared tls.Config
func (e *Exporter) CreateClientTLSConfig() (*tls.Config, error) {
tlsConfig := tls.Config{
InsecureSkipVerify: e.options.SkipTLSVerification,
}
if e.options.ClientCertFile != "" && e.options.ClientKeyFile != "" {
cert, err := LoadKeyPair(e.options.ClientCertFile, e.options.ClientKeyFile)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{*cert}
}
if e.options.CaCertFile != "" {
certificates, err := LoadCAFile(e.options.CaCertFile)
if err != nil {
return nil, err
}
tlsConfig.RootCAs = certificates
}
return &tlsConfig, nil
}
// CreateServerTLSConfig verifies configured files and return a prepared tls.Config
func (e *Exporter) CreateServerTLSConfig(certFile, keyFile, caCertFile string) (*tls.Config, error) {
// Verify that the initial key pair is accepted
_, err := LoadKeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
tlsConfig := tls.Config{
GetCertificate: GetServerCertificateFunc(certFile, keyFile),
}
if caCertFile != "" {
// Verify that the initial CA file is accepted when configured
_, err := LoadCAFile(caCertFile)
if err != nil {
return nil, err
}
tlsConfig.GetConfigForClient = GetConfigForClientFunc(certFile, keyFile, caCertFile)
}
return &tlsConfig, nil
}
// GetServerCertificateFunc returns a function for tls.Config.GetCertificate
func GetServerCertificateFunc(certFile, keyFile string) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return LoadKeyPair(certFile, keyFile)
}
}
// GetConfigForClientFunc returns a function for tls.Config.GetConfigForClient
func GetConfigForClientFunc(certFile, keyFile, caCertFile string) func(*tls.ClientHelloInfo) (*tls.Config, error) {
return func(*tls.ClientHelloInfo) (*tls.Config, error) {
certificates, err := LoadCAFile(caCertFile)
if err != nil {
return nil, err
}
tlsConfig := tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: certificates,
GetCertificate: GetServerCertificateFunc(certFile, keyFile),
}
return &tlsConfig, nil
}
}
// LoadKeyPair reads and parses a public/private key pair from a pair of files.
// The files must contain PEM encoded data.
func LoadKeyPair(certFile, keyFile string) (*tls.Certificate, error) {
log.Debugf("Load key pair: %s %s", certFile, keyFile)
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
return &cert, nil
}
// LoadCAFile reads and parses CA certificates from a file into a pool.
// The file must contain PEM encoded data.
func LoadCAFile(caFile string) (*x509.CertPool, error) {
log.Debugf("Load CA cert file: %s", caFile)
pemCerts, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(pemCerts)
return pool, nil
}