-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_utils.go
49 lines (40 loc) · 1.15 KB
/
tls_utils.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
package aviation
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"github.com/pkg/errors"
)
// GetClientTLSConfig creates a creates a client-side TLS configuration based
// on the given ca, cert, and key.
func GetClientTLSConfig(ca, crt, key []byte) (*tls.Config, error) {
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(ca) {
return nil, errors.New("credentials: failed to append certificates")
}
keyPair, err := tls.X509KeyPair(crt, key)
if err != nil {
return nil, errors.Wrap(err, "problem reading client cert")
}
return &tls.Config{
Certificates: []tls.Certificate{keyPair},
RootCAs: cp,
}, nil
}
// GetClientTLSConfigFromFiles creates a creates a client-side TLS
// configuration based on the given ca, cert, and key files.
func GetClientTLSConfigFromFiles(caFile, crtFile, keyFile string) (*tls.Config, error) {
ca, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, errors.WithStack(err)
}
crt, err := ioutil.ReadFile(crtFile)
if err != nil {
return nil, errors.WithStack(err)
}
key, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, errors.WithStack(err)
}
return GetClientTLSConfig(ca, crt, key)
}