-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathconfigtls.go
More file actions
195 lines (170 loc) · 6.63 KB
/
configtls.go
File metadata and controls
195 lines (170 loc) · 6.63 KB
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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package configtls
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"path/filepath"
)
// TLSSetting exposes the common client and server TLS configurations.
// Note: Since there isn't anything specific to a server connection. Components
// with server connections should use TLSSetting.
type TLSSetting struct {
// Path to the CA cert. For a client this verifies the server certificate.
// For a server this verifies client certificates. If empty uses system root CA.
// (optional)
CAFile string `mapstructure:"ca_file"`
// Path to the TLS cert to use for TLS required connections. (optional)
CertFile string `mapstructure:"cert_file"`
// Path to the TLS key to use for TLS required connections. (optional)
KeyFile string `mapstructure:"key_file"`
// MinVersion sets the minimum TLS version that is acceptable.
// If not set, TLS 1.0 is used. (optional)
MinVersion string `mapstructure:"min_version"`
// MaxVersion sets the maximum TLS version that is acceptable.
// If not set, TLS 1.3 is used. (optional)
MaxVersion string `mapstructure:"max_version"`
}
// TLSClientSetting contains TLS configurations that are specific to client
// connections in addition to the common configurations. This should be used by
// components configuring TLS client connections.
type TLSClientSetting struct {
// squash ensures fields are correctly decoded in embedded struct.
TLSSetting `mapstructure:",squash"`
// These are config options specific to client connections.
// In gRPC when set to true, this is used to disable the client transport security.
// See https://godoc.org/google.golang.org/grpc#WithInsecure.
// In HTTP, this disables verifying the server's certificate chain and host name
// (InsecureSkipVerify in the tls Config). Please refer to
// https://godoc.org/crypto/tls#Config for more information.
// (optional, default false)
Insecure bool `mapstructure:"insecure"`
// InsecureSkipVerify will enable TLS but not verify the certificate.
InsecureSkipVerify bool `mapstructure:"insecure_skip_verify"`
// ServerName requested by client for virtual hosting.
// This sets the ServerName in the TLSConfig. Please refer to
// https://godoc.org/crypto/tls#Config for more information. (optional)
ServerName string `mapstructure:"server_name_override"`
}
// TLSServerSetting contains TLS configurations that are specific to server
// connections in addition to the common configurations. This should be used by
// components configuring TLS server connections.
type TLSServerSetting struct {
// squash ensures fields are correctly decoded in embedded struct.
TLSSetting `mapstructure:",squash"`
// These are config options specific to server connections.
// Path to the TLS cert to use by the server to verify a client certificate. (optional)
// This sets the ClientCAs and ClientAuth to RequireAndVerifyClientCert in the TLSConfig. Please refer to
// https://godoc.org/crypto/tls#Config for more information. (optional)
ClientCAFile string `mapstructure:"client_ca_file"`
}
// LoadTLSConfig loads TLS certificates and returns a tls.Config.
// This will set the RootCAs and Certificates of a tls.Config.
func (c TLSSetting) loadTLSConfig() (*tls.Config, error) {
// There is no need to load the System Certs for RootCAs because
// if the value is nil, it will default to checking against th System Certs.
var err error
var certPool *x509.CertPool
if len(c.CAFile) != 0 {
// Set up user specified truststore.
certPool, err = c.loadCert(c.CAFile)
if err != nil {
return nil, fmt.Errorf("failed to load CA CertPool: %w", err)
}
}
if (c.CertFile == "" && c.KeyFile != "") || (c.CertFile != "" && c.KeyFile == "") {
return nil, fmt.Errorf("for auth via TLS, either both certificate and key must be supplied, or neither")
}
var certificates []tls.Certificate
if c.CertFile != "" && c.KeyFile != "" {
var tlsCert tls.Certificate
tlsCert, err = tls.LoadX509KeyPair(filepath.Clean(c.CertFile), filepath.Clean(c.KeyFile))
if err != nil {
return nil, fmt.Errorf("failed to load TLS cert and key: %w", err)
}
certificates = append(certificates, tlsCert)
}
minTLS, err := convertVersion(c.MinVersion)
if err != nil {
return nil, fmt.Errorf("invalid TLS min_version: %w", err)
}
maxTLS, err := convertVersion(c.MaxVersion)
if err != nil {
return nil, fmt.Errorf("invalid TLS max_version: %w", err)
}
return &tls.Config{
RootCAs: certPool,
Certificates: certificates,
MinVersion: minTLS,
MaxVersion: maxTLS,
}, nil
}
func (c TLSSetting) loadCert(caPath string) (*x509.CertPool, error) {
caPEM, err := ioutil.ReadFile(filepath.Clean(caPath))
if err != nil {
return nil, fmt.Errorf("failed to load CA %s: %w", caPath, err)
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caPEM) {
return nil, fmt.Errorf("failed to parse CA %s", caPath)
}
return certPool, nil
}
// LoadTLSConfig loads the TLS configuration.
func (c TLSClientSetting) LoadTLSConfig() (*tls.Config, error) {
if c.Insecure && c.CAFile == "" {
return nil, nil
}
tlsCfg, err := c.TLSSetting.loadTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to load TLS config: %w", err)
}
tlsCfg.ServerName = c.ServerName
tlsCfg.InsecureSkipVerify = c.InsecureSkipVerify
return tlsCfg, nil
}
// LoadTLSConfig loads the TLS configuration.
func (c TLSServerSetting) LoadTLSConfig() (*tls.Config, error) {
tlsCfg, err := c.loadTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to load TLS config: %w", err)
}
if c.ClientCAFile != "" {
certPool, err := c.loadCert(c.ClientCAFile)
if err != nil {
return nil, fmt.Errorf("failed to load TLS config: failed to load client CA CertPool: %w", err)
}
tlsCfg.ClientCAs = certPool
tlsCfg.ClientAuth = tls.RequireAndVerifyClientCert
}
return tlsCfg, nil
}
func convertVersion(v string) (uint16, error) {
if v == "" {
return 0, nil // default
}
val, ok := tlsVersions[v]
if !ok {
return 0, fmt.Errorf("unsupported TLS version: %q", v)
}
return val, nil
}
var tlsVersions = map[string]uint16{
"1.0": tls.VersionTLS10,
"1.1": tls.VersionTLS11,
"1.2": tls.VersionTLS12,
"1.3": tls.VersionTLS13,
}