-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
tls_helper.go
205 lines (177 loc) · 5.78 KB
/
tls_helper.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
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* 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 x
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type tlsConfigType int8
const (
TLSClientConfig tlsConfigType = iota
TLSServerConfig
)
const (
tlsRootCert = "ca.crt"
)
// TLSHelperConfig define params used to create a tls.Config
type TLSHelperConfig struct {
ConfigType tlsConfigType
CertDir string
CertRequired bool
Cert string
Key string
ServerName string
RootCACert string
ClientAuth string
UseSystemCACerts bool
}
func RegisterClientTLSFlags(flag *pflag.FlagSet) {
flag.String("tls_cacert", "",
"The CA Cert file used to verify server certificates. Required for enabling TLS.")
flag.Bool("tls_use_system_ca", true, "Include System CA into CA Certs.")
flag.String("tls_server_name", "", "Used to verify the server hostname.")
flag.String("tls_cert", "", "(optional) The Cert file provided by the client to the server.")
flag.String("tls_key", "", "(optional) The private key file "+
"provided by the client to the server.")
}
func LoadServerTLSConfig(v *viper.Viper, tlsCertFile string, tlsKeyFile string) (*tls.Config,
error) {
conf := TLSHelperConfig{}
conf.CertDir = v.GetString("tls_dir")
if conf.CertDir != "" {
conf.CertRequired = true
conf.RootCACert = path.Join(conf.CertDir, tlsRootCert)
conf.Cert = path.Join(conf.CertDir, tlsCertFile)
conf.Key = path.Join(conf.CertDir, tlsKeyFile)
conf.ClientAuth = v.GetString("tls_client_auth")
}
conf.UseSystemCACerts = v.GetBool("tls_use_system_ca")
return GenerateServerTLSConfig(&conf)
}
func LoadClientTLSConfig(v *viper.Viper) (*tls.Config, error) {
// When the --tls_cacert option is pecified, the connection will be set up using TLS instead of
// plaintext. However the client cert files are optional, depending on whether the server
// requires a client certificate.
caCert := v.GetString("tls_cacert")
if caCert != "" {
tlsCfg := tls.Config{}
// 1. set up the root CA
pool, err := generateCertPool(caCert, v.GetBool("tls_use_system_ca"))
if err != nil {
return nil, err
}
tlsCfg.RootCAs = pool
// 2. set up the server name for verification
tlsCfg.ServerName = v.GetString("tls_server_name")
// 3. optionally load the client cert files
certFile := v.GetString("tls_cert")
keyFile := v.GetString("tls_key")
if certFile != "" && keyFile != "" {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
tlsCfg.Certificates = []tls.Certificate{cert}
}
return &tlsCfg, nil
} else
// Attempt to determine if user specified *any* TLS option. Unfortunately and contrary to
// Viper's own documentation, there's no way to tell whether an option value came from a
// command-line option or a built-it default.
if v.GetString("tls_server_name") != "" ||
v.GetString("tls_cert") != "" ||
v.GetString("tls_key") != "" {
return nil, fmt.Errorf("--tls_cacert is required for enabling TLS")
}
return nil, nil
}
func generateCertPool(certPath string, useSystemCA bool) (*x509.CertPool, error) {
var pool *x509.CertPool
if useSystemCA {
var err error
if pool, err = x509.SystemCertPool(); err != nil {
return nil, err
}
} else {
pool = x509.NewCertPool()
}
if len(certPath) > 0 {
caFile, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
if !pool.AppendCertsFromPEM(caFile) {
return nil, fmt.Errorf("error reading CA file %q", certPath)
}
}
return pool, nil
}
func setupClientAuth(authType string) (tls.ClientAuthType, error) {
auth := map[string]tls.ClientAuthType{
"REQUEST": tls.RequestClientCert,
"REQUIREANY": tls.RequireAnyClientCert,
"VERIFYIFGIVEN": tls.VerifyClientCertIfGiven,
"REQUIREANDVERIFY": tls.RequireAndVerifyClientCert,
}
if len(authType) > 0 {
if v, has := auth[strings.ToUpper(authType)]; has {
return v, nil
}
return tls.NoClientCert, fmt.Errorf("Invalid client auth. Valid values [REQUEST, REQUIREANY, VERIFYIFGIVEN, REQUIREANDVERIFY]")
}
return tls.NoClientCert, nil
}
// GenerateServerTLSConfig creates and returns a new *tls.Config with the
// configuration provided.
func GenerateServerTLSConfig(config *TLSHelperConfig) (tlsCfg *tls.Config, err error) {
if config.CertRequired {
tlsCfg = new(tls.Config)
cert, err := tls.LoadX509KeyPair(config.Cert, config.Key)
if err != nil {
return nil, err
}
tlsCfg.Certificates = []tls.Certificate{cert}
pool, err := generateCertPool(config.RootCACert, config.UseSystemCACerts)
if err != nil {
return nil, err
}
tlsCfg.ClientCAs = pool
auth, err := setupClientAuth(config.ClientAuth)
if err != nil {
return nil, err
}
tlsCfg.ClientAuth = auth
tlsCfg.MinVersion = tls.VersionTLS11
tlsCfg.MaxVersion = tls.VersionTLS12
return tlsCfg, nil
}
return nil, nil
}
// GenerateClientTLSConfig creates and returns a new client side *tls.Config with the
// configuration provided.
func GenerateClientTLSConfig(config *TLSHelperConfig) (tlsCfg *tls.Config, err error) {
pool, err := generateCertPool(config.RootCACert, config.UseSystemCACerts)
if err != nil {
return nil, err
}
return &tls.Config{RootCAs: pool, ServerName: config.ServerName}, nil
}