2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
27 // New returns an http.RoundTripper that will provide the authentication
28 // or transport level security defined by the provided Config.
29 func New(config *Config) (http.RoundTripper, error) {
30 // Set transport level security
31 if config.Transport != nil && (config.HasCA() || config.HasCertAuth() || config.HasCertCallback() || config.TLS.Insecure) {
32 return nil, fmt.Errorf("using a custom transport with TLS certificate options or the insecure flag is not allowed")
40 if config.Transport != nil {
43 rt, err = tlsCache.get(config)
49 return HTTPWrappersForConfig(config, rt)
52 // TLSConfigFor returns a tls.Config that will provide the transport level security defined
53 // by the provided Config. Will return nil if no transport level security is requested.
54 func TLSConfigFor(c *Config) (*tls.Config, error) {
55 if !(c.HasCA() || c.HasCertAuth() || c.HasCertCallback() || c.TLS.Insecure || len(c.TLS.ServerName) > 0) {
58 if c.HasCA() && c.TLS.Insecure {
59 return nil, fmt.Errorf("specifying a root certificates file with the insecure flag is not allowed")
61 if err := loadTLSFiles(c); err != nil {
65 tlsConfig := &tls.Config{
66 // Can't use SSLv3 because of POODLE and BEAST
67 // Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
68 // Can't use TLSv1.1 because of RC4 cipher usage
69 MinVersion: tls.VersionTLS12,
70 InsecureSkipVerify: c.TLS.Insecure,
71 ServerName: c.TLS.ServerName,
75 tlsConfig.RootCAs = rootCertPool(c.TLS.CAData)
78 var staticCert *tls.Certificate
80 // If key/cert were provided, verify them before setting up
81 // tlsConfig.GetClientCertificate.
82 cert, err := tls.X509KeyPair(c.TLS.CertData, c.TLS.KeyData)
89 if c.HasCertAuth() || c.HasCertCallback() {
90 tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
91 // Note: static key/cert data always take precedence over cert
93 if staticCert != nil {
94 return staticCert, nil
96 if c.HasCertCallback() {
97 cert, err := c.TLS.GetCert()
101 // GetCert may return empty value, meaning no cert.
107 // Both c.TLS.CertData/KeyData were unset and GetCert didn't return
108 // anything. Return an empty tls.Certificate, no client cert will
109 // be sent to the server.
110 return &tls.Certificate{}, nil
114 return tlsConfig, nil
117 // loadTLSFiles copies the data from the CertFile, KeyFile, and CAFile fields into the CertData,
118 // KeyData, and CAFile fields, or returns an error. If no error is returned, all three fields are
119 // either populated or were empty to start.
120 func loadTLSFiles(c *Config) error {
122 c.TLS.CAData, err = dataFromSliceOrFile(c.TLS.CAData, c.TLS.CAFile)
127 c.TLS.CertData, err = dataFromSliceOrFile(c.TLS.CertData, c.TLS.CertFile)
132 c.TLS.KeyData, err = dataFromSliceOrFile(c.TLS.KeyData, c.TLS.KeyFile)
139 // dataFromSliceOrFile returns data from the slice (if non-empty), or from the file,
140 // or an error if an error occurred reading the file
141 func dataFromSliceOrFile(data []byte, file string) ([]byte, error) {
146 fileData, err := ioutil.ReadFile(file)
155 // rootCertPool returns nil if caData is empty. When passed along, this will mean "use system CAs".
156 // When caData is not empty, it will be the ONLY information used in the CertPool.
157 func rootCertPool(caData []byte) *x509.CertPool {
158 // What we really want is a copy of x509.systemRootsPool, but that isn't exposed. It's difficult to build (see the go
159 // code for a look at the platform specific insanity), so we'll use the fact that RootCAs == nil gives us the system values
160 // It doesn't allow trusting either/or, but hopefully that won't be an issue
161 if len(caData) == 0 {
165 // if we have caData, use it
166 certPool := x509.NewCertPool()
167 certPool.AppendCertsFromPEM(caData)