-
Notifications
You must be signed in to change notification settings - Fork 53
/
mtls.go
49 lines (43 loc) · 1.02 KB
/
mtls.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 util
import (
"crypto/tls"
"crypto/x509"
"os"
"github.com/rancher/opni/pkg/config/v1beta1"
)
func LoadClientMTLSConfig(certs *v1beta1.MTLSSpec) (*tls.Config, error) {
clientCert, err := tls.LoadX509KeyPair(certs.ClientCert, certs.ClientKey)
if err != nil {
return nil, err
}
clientCAPool := x509.NewCertPool()
if certs.ClientCA != "" {
clientCAData, err := os.ReadFile(certs.ClientCA)
if err != nil {
return nil, err
}
clientCA, err := ParsePEMEncodedCert(clientCAData)
if err != nil {
return nil, err
}
clientCAPool.AddCert(clientCA)
}
serverCAPool := x509.NewCertPool()
if certs.ServerCA != "" {
serverCAData, err := os.ReadFile(certs.ServerCA)
if err != nil {
return nil, err
}
serverCA, err := ParsePEMEncodedCert(serverCAData)
if err != nil {
return nil, err
}
serverCAPool.AddCert(serverCA)
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{clientCert},
ClientCAs: clientCAPool,
RootCAs: serverCAPool,
}, nil
}