-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
39 lines (31 loc) · 882 Bytes
/
config.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
package tls
import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"github.com/pkg/errors"
)
// NewConfig generate a *tls.Config, clientAuth should be false to generate client configuration
func NewConfig(caPath, certPath, keyPath string, clientAuth bool) (*tls.Config, error) {
caCertBytes, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, errors.Wrapf(err, "fail to read CA cert file %v", caPath)
}
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, errors.Wrapf(err, "fail to load certificate %v", certPath)
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(caCertBytes)
config := &tls.Config{
Certificates: []tls.Certificate{cert},
ClientCAs: pool,
RootCAs: pool,
}
if clientAuth {
config.ClientAuth = tls.RequireAndVerifyClientCert
}
config.Rand = rand.Reader
return config, nil
}