@@ -28,35 +28,49 @@ func BuildTransportTLS(tlsConfig TlsConfig) (*http.Transport, error) {
2828 }
2929
3030 klog .InfoS ("insecure connection made." , "insecure" , tlsConfig .Insecure , "endpoint" , tlsConfig .Endpoint )
31- } else {
32- var rootCAs []byte
33- if strings .Contains (tlsConfig .CACert , "-----BEGIN CERTIFICATE-----" ) && strings .Contains (tlsConfig .CACert , "-----END CERTIFICATE-----" ) {
34- rootCAs = []byte (tlsConfig .CACert )
35- } else {
36- // Decode base64 CA cert
37- _rootCAs , err := base64 .StdEncoding .DecodeString (tlsConfig .CACert )
38- if err != nil {
39- return nil , fmt .Errorf ("failed to decode CA cert: %v" , err )
40- }
41-
42- rootCAs = _rootCAs
43- }
4431
45- // Create cert pool and add our CA
46- caCertPool := x509 .NewCertPool ()
47- if ! caCertPool .AppendCertsFromPEM (rootCAs ) {
48- return nil , fmt .Errorf ("failed to append CA cert: %s" , tlsConfig .CACert )
49- }
32+ return transport , nil
33+ }
5034
35+ if tlsConfig .CACert == "" {
5136 transport = & http.Transport {
5237 TLSClientConfig : & tls.Config {
53- RootCAs : caCertPool ,
5438 InsecureSkipVerify : false ,
5539 },
5640 }
5741
58- klog .InfoS ("secure connection made." , "insecure" , tlsConfig .Insecure , "endpoint" , tlsConfig .Endpoint )
42+ klog .InfoS ("secure connection made without CA certs." , "insecure" , tlsConfig .Insecure , "endpoint" , tlsConfig .Endpoint )
43+
44+ return transport , nil
5945 }
6046
47+ var rootCAs []byte
48+ if strings .Contains (tlsConfig .CACert , "-----BEGIN CERTIFICATE-----" ) && strings .Contains (tlsConfig .CACert , "-----END CERTIFICATE-----" ) {
49+ rootCAs = []byte (tlsConfig .CACert )
50+ } else {
51+ // Decode base64 CA cert
52+ _rootCAs , err := base64 .StdEncoding .DecodeString (tlsConfig .CACert )
53+ if err != nil {
54+ return nil , fmt .Errorf ("failed to decode CA cert: %v" , err )
55+ }
56+
57+ rootCAs = _rootCAs
58+ }
59+
60+ // Create cert pool and add our CA
61+ caCertPool := x509 .NewCertPool ()
62+ if ! caCertPool .AppendCertsFromPEM (rootCAs ) {
63+ return nil , fmt .Errorf ("failed to append CA cert: %s" , tlsConfig .CACert )
64+ }
65+
66+ transport = & http.Transport {
67+ TLSClientConfig : & tls.Config {
68+ RootCAs : caCertPool ,
69+ InsecureSkipVerify : false ,
70+ },
71+ }
72+
73+ klog .InfoS ("secure connection made with CA certs." , "insecure" , tlsConfig .Insecure , "endpoint" , tlsConfig .Endpoint )
74+
6175 return transport , nil
6276}
0 commit comments