transport.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transport
  14. import (
  15. "crypto/tls"
  16. "crypto/x509"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. )
  21. // New returns an http.RoundTripper that will provide the authentication
  22. // or transport level security defined by the provided Config.
  23. func New(config *Config) (http.RoundTripper, error) {
  24. // Set transport level security
  25. if config.Transport != nil && (config.HasCA() || config.HasCertAuth() || config.HasCertCallback() || config.TLS.Insecure) {
  26. return nil, fmt.Errorf("using a custom transport with TLS certificate options or the insecure flag is not allowed")
  27. }
  28. var (
  29. rt http.RoundTripper
  30. err error
  31. )
  32. if config.Transport != nil {
  33. rt = config.Transport
  34. } else {
  35. rt, err = tlsCache.get(config)
  36. if err != nil {
  37. return nil, err
  38. }
  39. }
  40. return HTTPWrappersForConfig(config, rt)
  41. }
  42. // TLSConfigFor returns a tls.Config that will provide the transport level security defined
  43. // by the provided Config. Will return nil if no transport level security is requested.
  44. func TLSConfigFor(c *Config) (*tls.Config, error) {
  45. if !(c.HasCA() || c.HasCertAuth() || c.HasCertCallback() || c.TLS.Insecure || len(c.TLS.ServerName) > 0) {
  46. return nil, nil
  47. }
  48. if c.HasCA() && c.TLS.Insecure {
  49. return nil, fmt.Errorf("specifying a root certificates file with the insecure flag is not allowed")
  50. }
  51. if err := loadTLSFiles(c); err != nil {
  52. return nil, err
  53. }
  54. tlsConfig := &tls.Config{
  55. // Can't use SSLv3 because of POODLE and BEAST
  56. // Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
  57. // Can't use TLSv1.1 because of RC4 cipher usage
  58. MinVersion: tls.VersionTLS12,
  59. InsecureSkipVerify: c.TLS.Insecure,
  60. ServerName: c.TLS.ServerName,
  61. }
  62. if c.HasCA() {
  63. tlsConfig.RootCAs = rootCertPool(c.TLS.CAData)
  64. }
  65. var staticCert *tls.Certificate
  66. if c.HasCertAuth() {
  67. // If key/cert were provided, verify them before setting up
  68. // tlsConfig.GetClientCertificate.
  69. cert, err := tls.X509KeyPair(c.TLS.CertData, c.TLS.KeyData)
  70. if err != nil {
  71. return nil, err
  72. }
  73. staticCert = &cert
  74. }
  75. if c.HasCertAuth() || c.HasCertCallback() {
  76. tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
  77. // Note: static key/cert data always take precedence over cert
  78. // callback.
  79. if staticCert != nil {
  80. return staticCert, nil
  81. }
  82. if c.HasCertCallback() {
  83. cert, err := c.TLS.GetCert()
  84. if err != nil {
  85. return nil, err
  86. }
  87. // GetCert may return empty value, meaning no cert.
  88. if cert != nil {
  89. return cert, nil
  90. }
  91. }
  92. // Both c.TLS.CertData/KeyData were unset and GetCert didn't return
  93. // anything. Return an empty tls.Certificate, no client cert will
  94. // be sent to the server.
  95. return &tls.Certificate{}, nil
  96. }
  97. }
  98. return tlsConfig, nil
  99. }
  100. // loadTLSFiles copies the data from the CertFile, KeyFile, and CAFile fields into the CertData,
  101. // KeyData, and CAFile fields, or returns an error. If no error is returned, all three fields are
  102. // either populated or were empty to start.
  103. func loadTLSFiles(c *Config) error {
  104. var err error
  105. c.TLS.CAData, err = dataFromSliceOrFile(c.TLS.CAData, c.TLS.CAFile)
  106. if err != nil {
  107. return err
  108. }
  109. c.TLS.CertData, err = dataFromSliceOrFile(c.TLS.CertData, c.TLS.CertFile)
  110. if err != nil {
  111. return err
  112. }
  113. c.TLS.KeyData, err = dataFromSliceOrFile(c.TLS.KeyData, c.TLS.KeyFile)
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. }
  119. // dataFromSliceOrFile returns data from the slice (if non-empty), or from the file,
  120. // or an error if an error occurred reading the file
  121. func dataFromSliceOrFile(data []byte, file string) ([]byte, error) {
  122. if len(data) > 0 {
  123. return data, nil
  124. }
  125. if len(file) > 0 {
  126. fileData, err := ioutil.ReadFile(file)
  127. if err != nil {
  128. return []byte{}, err
  129. }
  130. return fileData, nil
  131. }
  132. return nil, nil
  133. }
  134. // rootCertPool returns nil if caData is empty. When passed along, this will mean "use system CAs".
  135. // When caData is not empty, it will be the ONLY information used in the CertPool.
  136. func rootCertPool(caData []byte) *x509.CertPool {
  137. // What we really want is a copy of x509.systemRootsPool, but that isn't exposed. It's difficult to build (see the go
  138. // code for a look at the platform specific insanity), so we'll use the fact that RootCAs == nil gives us the system values
  139. // It doesn't allow trusting either/or, but hopefully that won't be an issue
  140. if len(caData) == 0 {
  141. return nil
  142. }
  143. // if we have caData, use it
  144. certPool := x509.NewCertPool()
  145. certPool.AppendCertsFromPEM(caData)
  146. return certPool
  147. }