2017-05-23 18:56:10 +00:00
|
|
|
package crypto
|
2016-08-01 01:40:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// CreateTLSConfiguration initializes a tls.Config using a CA certificate, a certificate and a key
|
2017-08-10 08:35:23 +00:00
|
|
|
func CreateTLSConfiguration(caCertPath, certPath, keyPath string, skipTLSVerify bool) (*tls.Config, error) {
|
|
|
|
|
|
|
|
config := &tls.Config{}
|
|
|
|
|
|
|
|
if certPath != "" && keyPath != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Certificates = []tls.Certificate{cert}
|
2016-08-01 01:40:12 +00:00
|
|
|
}
|
2017-08-10 08:35:23 +00:00
|
|
|
|
|
|
|
if caCertPath != "" {
|
|
|
|
caCert, err := ioutil.ReadFile(caCertPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
config.RootCAs = caCertPool
|
2016-08-01 01:40:12 +00:00
|
|
|
}
|
2017-08-10 08:35:23 +00:00
|
|
|
|
|
|
|
config.InsecureSkipVerify = skipTLSVerify
|
2016-12-18 05:21:29 +00:00
|
|
|
return config, nil
|
2016-08-01 01:40:12 +00:00
|
|
|
}
|