-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathtls.go
129 lines (111 loc) · 3.55 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package exporter
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
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
} else {
// Load the system certificate pool
rootCAs, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
tlsConfig.RootCAs = rootCAs
}
return &tlsConfig, nil
}
var tlsVersions = map[string]uint16{
"TLS1.3": tls.VersionTLS13,
"TLS1.2": tls.VersionTLS12,
"TLS1.1": tls.VersionTLS11,
"TLS1.0": tls.VersionTLS10,
}
// CreateServerTLSConfig verifies configuration and return a prepared tls.Config
func (e *Exporter) CreateServerTLSConfig(certFile, keyFile, caCertFile, minVersionString string) (*tls.Config, error) {
// Verify that the initial key pair is accepted
_, err := LoadKeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
// Get minimum acceptable TLS version from the config string
minVersion, ok := tlsVersions[minVersionString]
if !ok {
return nil, fmt.Errorf("configured minimum TLS version unknown: '%s'", minVersionString)
}
tlsConfig := tls.Config{
MinVersion: minVersion,
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 := os.ReadFile(caFile)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(pemCerts)
return pool, nil
}