-
-
Notifications
You must be signed in to change notification settings - Fork 578
/
tls.go
253 lines (202 loc) · 7.95 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
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
package tools
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
)
type ConfigGetter interface {
GetBool(name string) bool
GetString(name string) string
}
// ReadFileFunc is an abstraction for os.ReadFile but also io/fs.ReadFile
// wrapped with an io/fs.FS instance.
//
// Note that os.DirFS has slightly different semantics compared to the native
// filesystem APIs, see https://go.dev/issue/44279
type ReadFileFunc func(name string) ([]byte, error)
// MakeTLSConfig constructs a tls.Config instance using the given configuration
// scoped under key prefix.
func MakeTLSConfig(v ConfigGetter, keyPrefix string, readFile ReadFileFunc) (*tls.Config, error) {
tlsConfig := &tls.Config{}
loaders := []tlsConfigLoader{
chainTLSConfigLoaders(loadCertFromFile, loadCertFromPEM),
chainTLSConfigLoaders(loadRootCAFromFile, loadRootCAFromPEM),
chainTLSConfigLoaders(loadMutualTLSFromFile, loadMutualTLSFromPEM),
}
for _, loadConfig := range loaders {
if _, err := loadConfig(tlsConfig, v, keyPrefix, readFile); err != nil {
return nil, err
}
}
tlsConfig.ServerName = v.GetString(keyPrefix + "tls_server_name")
tlsConfig.InsecureSkipVerify = v.GetBool(keyPrefix + "tls_insecure_skip_verify")
return tlsConfig, nil
}
// tlsConfigLoader is a function that loads TLS from the given ConfigGetter.
// It returns false, nil if configuration does not exist, true, nil on success,
// or true, err ≠ nil if there was an error loading the configuration.
type tlsConfigLoader func(c *tls.Config, v ConfigGetter, keyPrefix string, readFile ReadFileFunc) (bool, error)
// chainTLSConfigLoaders returns tlsConfigLoader function that attempts to load
// TLS configuration until either a configuration is found or an error occurs.
func chainTLSConfigLoaders(loaders ...tlsConfigLoader) tlsConfigLoader {
return func(c *tls.Config, v ConfigGetter, keyPrefix string, readFile ReadFileFunc) (bool, error) {
for _, f := range loaders {
found, err := f(c, v, keyPrefix, readFile)
if found || err != nil {
return found, err
}
}
return false, nil
}
}
// loadCertFromFile loads the TLS configuration with certificate from key pair
// files containing PEM-encoded TLS key and certificate.
func loadCertFromFile(tlsConfig *tls.Config, v ConfigGetter, keyPrefix string, readFile ReadFileFunc) (bool, error) {
certFileKeyName, keyFileKeyName := keyPrefix+"tls_cert", keyPrefix+"tls_key"
certFile, keyFile := v.GetString(certFileKeyName), v.GetString(keyFileKeyName)
if certFile == "" || keyFile == "" {
return false, nil
}
certPEMBlock, err := readFile(certFile)
if err != nil {
return true, fmt.Errorf("read TLS certificate for %s: %w", certFileKeyName, err)
}
keyPEMBlock, err := readFile(keyFile)
if err != nil {
return true, fmt.Errorf("read TLS key for %s: %w", keyFileKeyName, err)
}
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return true, fmt.Errorf("parse certificate/key pair for %s/%s: %w", certFileKeyName, keyFileKeyName, err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
return true, nil
}
// loadCertFromPEM loads the TLS configuration with certificate from key pair
// strings containing PEM-encoded TLS key and certificate.
func loadCertFromPEM(tlsConfig *tls.Config, v ConfigGetter, keyPrefix string, _ ReadFileFunc) (bool, error) {
certPEMKeyName, keyPEMKeyName := keyPrefix+"tls_cert_pem", keyPrefix+"tls_key_pem"
certPEM, keyPEM := v.GetString(certPEMKeyName), v.GetString(keyPEMKeyName)
if certPEM == "" || keyPEM == "" {
return false, nil
}
cert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
if err != nil {
return true, fmt.Errorf("parse certificate/key pair for %s/%s: %w", certPEMKeyName, keyPEMKeyName, err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
return true, nil
}
// loadRootCAFromFile loads the TLS configuration with root CA bundle from file
// containing PEM-encoded certificates.
func loadRootCAFromFile(tlsConfig *tls.Config, v ConfigGetter, keyPrefix string, readFile ReadFileFunc) (bool, error) {
keyName := keyPrefix + "tls_root_ca"
rootCAFile := v.GetString(keyName)
if rootCAFile == "" {
return false, nil
}
caCert, err := readFile(rootCAFile)
if err != nil {
return true, fmt.Errorf("read the root CA certificate for %s: %w", keyName, err)
}
caCertPool, err := newCertPoolFromPEM(caCert)
if err != nil {
return true, fmt.Errorf("parse root CA certificate for %s: %w", keyName, err)
}
tlsConfig.RootCAs = caCertPool
return true, nil
}
// loadRootCAFromPEM loads the TLS configuration with root CA bundle from
// string containing PEM-encoded certificates.
func loadRootCAFromPEM(tlsConfig *tls.Config, v ConfigGetter, keyPrefix string, _ ReadFileFunc) (bool, error) {
keyName := keyPrefix + "tls_root_ca_pem"
rootCAPEM := v.GetString(keyName)
if rootCAPEM == "" {
return false, nil
}
caCertPool, err := newCertPoolFromPEM([]byte(rootCAPEM))
if err != nil {
return true, fmt.Errorf("parse root CA certificate for %s: %w", keyName, err)
}
tlsConfig.RootCAs = caCertPool
return true, nil
}
// loadMutualTLSFromFile loads the TLS configuration for server-side mutual TLS
// authentication from file containing PEM-encoded certificates.
func loadMutualTLSFromFile(tlsConfig *tls.Config, v ConfigGetter, keyPrefix string, readFile ReadFileFunc) (bool, error) {
keyName := keyPrefix + "tls_client_ca"
clientCAFile := v.GetString(keyName)
if clientCAFile == "" {
return false, nil
}
caCert, err := readFile(clientCAFile)
if err != nil {
return true, fmt.Errorf("read the client CA certificate for %s: %w", keyName, err)
}
caCertPool, err := newCertPoolFromPEM(caCert)
if err != nil {
return true, fmt.Errorf("parse client CA certificate for %s: %w", keyName, err)
}
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
return true, nil
}
// loadMutualTLSFromPEM loads the TLS configuration for server-side mutual TLS
// authentication from string containing PEM-encoded certificates.
func loadMutualTLSFromPEM(tlsConfig *tls.Config, v ConfigGetter, keyPrefix string, _ ReadFileFunc) (bool, error) {
keyName := keyPrefix + "tls_client_ca_pem"
clientCAPEM := v.GetString(keyName)
if clientCAPEM == "" {
return false, nil
}
caCertPool, err := newCertPoolFromPEM([]byte(clientCAPEM))
if err != nil {
return true, fmt.Errorf("parse client CA certificate for %s: %w", keyName, err)
}
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
return true, nil
}
// newCertPoolFromPEM returns certificate pool for the given PEM-encoded
// certificate bundle. Note that it currently ignores invalid blocks.
func newCertPoolFromPEM(pem []byte) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM(pem)
if !ok {
return nil, errors.New("no valid certificates found")
}
return certPool, nil
}
type TLSOptions struct {
Cert string `mapstructure:"tls_cert" json:"tls_cert"`
Key string `mapstructure:"tls_key" json:"tls_key"`
CertPem string `mapstructure:"tls_cert_pem" json:"tls_cert_pem"`
KeyPem string `mapstructure:"tls_key_pem" json:"tls_key_pem"`
RootCA string `mapstructure:"tls_root_ca" json:"tls_root_ca"`
RootCAPem string `mapstructure:"tls_root_ca_pem" json:"tls_root_ca_pem"`
ClientCA string `mapstructure:"tls_client_ca" json:"tls_client_ca"`
ClientCAPem string `mapstructure:"tls_client_ca_pem" json:"tls_client_ca_pem"`
InsecureSkipVerify bool `mapstructure:"tls_insecure_skip_verify" json:"tls_insecure_skip_verify"`
ServerName string `mapstructure:"server_name" json:"server_name"`
}
func (t TLSOptions) ToMap() (TLSOptionsMap, error) {
var m TLSOptionsMap
jsonData, _ := json.Marshal(m)
err := json.Unmarshal(jsonData, &m)
return m, err
}
type TLSOptionsMap map[string]any
func (t TLSOptionsMap) GetBool(name string) bool {
if _, ok := t[name]; !ok {
return false
}
return t[name].(bool)
}
func (t TLSOptionsMap) GetString(name string) string {
if _, ok := t[name]; !ok {
return ""
}
return t[name].(string)
}