-
Notifications
You must be signed in to change notification settings - Fork 47
/
client_config.go
52 lines (45 loc) · 1.24 KB
/
client_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
45
46
47
48
49
50
51
52
package certutil
import (
"crypto/tls"
"crypto/x509"
"github.com/blend/go-sdk/ex"
)
// NewClientTLSConfig returns a new client tls config.
// This is useful for making mutual tls calls to servers that require it.
func NewClientTLSConfig(clientCert KeyPair, certificateAuthorities []KeyPair) (*tls.Config, error) {
clientCertPEM, err := clientCert.CertBytes()
if err != nil {
return nil, ex.New(err)
}
clientKeyPEM, err := clientCert.KeyBytes()
if err != nil {
return nil, ex.New(err)
}
if len(clientCertPEM) == 0 {
return nil, ex.New("invalid key pair; empty cert pem data")
}
if len(clientKeyPEM) == 0 {
return nil, ex.New("invalid key pair; empty key pem data")
}
cert, err := tls.X509KeyPair(clientCertPEM, clientKeyPEM)
if err != nil {
return nil, ex.New(err)
}
config := new(tls.Config)
rootCAPool, err := x509.SystemCertPool()
if err != nil {
return nil, ex.New(err)
}
for _, caCert := range certificateAuthorities {
contents, err := caCert.CertBytes()
if err != nil {
return nil, ex.New(err)
}
if ok := rootCAPool.AppendCertsFromPEM(contents); !ok {
return nil, ex.New("failed to append ca cert file")
}
}
config.Certificates = []tls.Certificate{cert}
config.RootCAs = rootCAPool
return config, nil
}