-
Notifications
You must be signed in to change notification settings - Fork 53
/
tls.go
57 lines (52 loc) · 1.19 KB
/
tls.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
53
54
55
56
57
package apiextensions
import (
"crypto/tls"
"github.com/rancher/opni/pkg/config/v1beta1"
"github.com/rancher/opni/pkg/util"
)
func (tc *CertConfig) TLSConfig() (*tls.Config, error) {
certCfg := v1beta1.CertsSpec{}
if tc.Ca != "" {
certCfg.CACert = &tc.Ca
}
if tc.CaData != "" {
certCfg.CACertData = &tc.CaData
}
if tc.Cert != "" {
certCfg.ServingCert = &tc.Cert
}
if tc.CertData != "" {
certCfg.ServingCertData = &tc.CertData
}
if tc.Key != "" {
certCfg.ServingKey = &tc.Key
}
if tc.KeyData != "" {
certCfg.ServingKeyData = &tc.KeyData
}
bundle, caPool, err := util.LoadServingCertBundle(certCfg)
if err != nil {
return nil, err
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: caPool,
Certificates: []tls.Certificate{*bundle},
}, nil
}
func strOrEmpty(s *string) string {
if s == nil {
return ""
}
return *s
}
func NewCertConfig(certs v1beta1.CertsSpec) *CertConfig {
return &CertConfig{
Ca: strOrEmpty(certs.CACert),
CaData: strOrEmpty(certs.CACertData),
Cert: strOrEmpty(certs.ServingCert),
CertData: strOrEmpty(certs.ServingCertData),
Key: strOrEmpty(certs.ServingKey),
KeyData: strOrEmpty(certs.ServingKeyData),
}
}