Skip to content

Commit f758d19

Browse files
committed
prepareSTSClientTransport tls function refactor
- Reading root ca certificates operation will run only once after Console starts, reduce the chance of panics happening during runtime - Fixed bug in which tls.config insecureSkipVerification configuration could get overrided after variable reasignation
1 parent e0ff662 commit f758d19

File tree

1 file changed

+23
-46
lines changed

1 file changed

+23
-46
lines changed

restapi/tls.go

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,41 @@ package restapi
1919
import (
2020
"crypto/tls"
2121
"crypto/x509"
22-
"fmt"
2322
"io/ioutil"
2423
"net"
2524
"net/http"
2625
"time"
2726
)
2827

29-
var (
30-
certDontExists = "File certificate doesn't exists: %s"
31-
)
28+
func mustGetCertPool() *x509.CertPool {
29+
caCertFileNames := getMinioServerTLSRootCAs()
30+
// If CAs certificates are configured we save them to the http.Client RootCAs store
31+
certs := x509.NewCertPool()
32+
for _, caCert := range caCertFileNames {
33+
pemData, err := ioutil.ReadFile(caCert)
34+
if err != nil {
35+
// if there was an error reading pem file stop console
36+
panic(err)
37+
}
38+
certs.AppendCertsFromPEM(pemData)
39+
}
40+
return certs
41+
}
3242

33-
func prepareSTSClientTransport(insecure bool) *http.Transport {
34-
// This takes github.com/minio/minio/pkg/madmin/transport.go as an example
35-
//
36-
// DefaultTransport - this default transport is similar to
37-
// http.DefaultTransport but with additional param DisableCompression
38-
// is set to true to avoid decompressing content with 'gzip' encoding.
43+
var certPool = mustGetCertPool()
3944

40-
// Keep TLS config.
45+
func prepareSTSClientTransport(insecure bool) *http.Transport {
4146
tlsConfig := &tls.Config{
4247
// Can't use SSLv3 because of POODLE and BEAST
4348
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
4449
// Can't use TLSv1.1 because of RC4 cipher usage
4550
MinVersion: tls.VersionTLS12,
4651
}
47-
if insecure {
48-
tlsConfig.InsecureSkipVerify = true
49-
}
50-
52+
// This takes github.com/minio/minio/pkg/madmin/transport.go as an example
53+
//
54+
// DefaultTransport - this default transport is similar to
55+
// http.DefaultTransport but with additional param DisableCompression
56+
// is set to true to avoid decompressing content with 'gzip' encoding.
5157
DefaultTransport := &http.Transport{
5258
Proxy: http.ProxyFromEnvironment,
5359
DialContext: (&net.Dialer{
@@ -63,37 +69,8 @@ func prepareSTSClientTransport(insecure bool) *http.Transport {
6369
DisableCompression: true,
6470
TLSClientConfig: tlsConfig,
6571
}
66-
// If Minio instance is running with TLS enabled and it's using a self-signed certificate
67-
// or a certificate issued by a custom certificate authority we prepare a new custom *http.Transport
68-
if getMinIOEndpointIsSecure() {
69-
caCertFileNames := getMinioServerTLSRootCAs()
70-
tlsConfig := &tls.Config{
71-
// Can't use SSLv3 because of POODLE and BEAST
72-
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
73-
// Can't use TLSv1.1 because of RC4 cipher usage
74-
MinVersion: tls.VersionTLS12,
75-
}
76-
// If CAs certificates are configured we save them to the http.Client RootCAs store
77-
if len(caCertFileNames) > 0 {
78-
certs := x509.NewCertPool()
79-
for _, caCert := range caCertFileNames {
80-
// Validate certificate exists
81-
if FileExists(caCert) {
82-
pemData, err := ioutil.ReadFile(caCert)
83-
if err != nil {
84-
// if there was an error reading pem file stop console
85-
panic(err)
86-
}
87-
certs.AppendCertsFromPEM(pemData)
88-
} else {
89-
// if provided cert filename doesn't exists stop console
90-
panic(fmt.Sprintf(certDontExists, caCert))
91-
}
92-
}
93-
tlsConfig.RootCAs = certs
94-
}
95-
DefaultTransport.TLSClientConfig = tlsConfig
96-
}
72+
tlsConfig.InsecureSkipVerify = insecure
73+
tlsConfig.RootCAs = certPool
9774
return DefaultTransport
9875
}
9976

0 commit comments

Comments
 (0)