forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
44 lines (41 loc) · 1.05 KB
/
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
40
41
42
43
44
package tlsconfig
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
)
// Create creates a new tls.Config object from the given certs, key, and CA files.
func Create(
SSLCA, SSLCert, SSLKey string,
InsecureSkipVerify bool,
) (*tls.Config, error) {
t := &tls.Config{
InsecureSkipVerify: InsecureSkipVerify,
}
if SSLCert != "" && SSLKey != "" {
cert, err := tls.LoadX509KeyPair(SSLCert, SSLKey)
if err != nil {
return nil, fmt.Errorf(
"Could not load TLS client key/certificate: %s",
err)
}
t.Certificates = []tls.Certificate{cert}
} else if SSLCert != "" {
return nil, errors.New("Must provide both key and cert files: only cert file provided.")
} else if SSLKey != "" {
return nil, errors.New("Must provide both key and cert files: only key file provided.")
}
if SSLCA != "" {
caCert, err := ioutil.ReadFile(SSLCA)
if err != nil {
return nil, fmt.Errorf("Could not load TLS CA: %s",
err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
t.RootCAs = caCertPool
}
return t, nil
}