transport.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. "context"
  16. "crypto/tls"
  17. "crypto/x509"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "sync"
  22. "time"
  23. utilnet "k8s.io/apimachinery/pkg/util/net"
  24. "k8s.io/klog/v2"
  25. )
  26. // New returns an http.RoundTripper that will provide the authentication
  27. // or transport level security defined by the provided Config.
  28. func New(config *Config) (http.RoundTripper, error) {
  29. // Set transport level security
  30. if config.Transport != nil && (config.HasCA() || config.HasCertAuth() || config.HasCertCallback() || config.TLS.Insecure) {
  31. return nil, fmt.Errorf("using a custom transport with TLS certificate options or the insecure flag is not allowed")
  32. }
  33. var (
  34. rt http.RoundTripper
  35. err error
  36. )
  37. if config.Transport != nil {
  38. rt = config.Transport
  39. } else {
  40. rt, err = tlsCache.get(config)
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. return HTTPWrappersForConfig(config, rt)
  46. }
  47. // TLSConfigFor returns a tls.Config that will provide the transport level security defined
  48. // by the provided Config. Will return nil if no transport level security is requested.
  49. func TLSConfigFor(c *Config) (*tls.Config, error) {
  50. if !(c.HasCA() || c.HasCertAuth() || c.HasCertCallback() || c.TLS.Insecure || len(c.TLS.ServerName) > 0 || len(c.TLS.NextProtos) > 0) {
  51. return nil, nil
  52. }
  53. if c.HasCA() && c.TLS.Insecure {
  54. return nil, fmt.Errorf("specifying a root certificates file with the insecure flag is not allowed")
  55. }
  56. if err := loadTLSFiles(c); err != nil {
  57. return nil, err
  58. }
  59. tlsConfig := &tls.Config{
  60. // Can't use SSLv3 because of POODLE and BEAST
  61. // Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
  62. // Can't use TLSv1.1 because of RC4 cipher usage
  63. MinVersion: tls.VersionTLS12,
  64. InsecureSkipVerify: c.TLS.Insecure,
  65. ServerName: c.TLS.ServerName,
  66. NextProtos: c.TLS.NextProtos,
  67. }
  68. if c.HasCA() {
  69. tlsConfig.RootCAs = rootCertPool(c.TLS.CAData)
  70. }
  71. var staticCert *tls.Certificate
  72. // Treat cert as static if either key or cert was data, not a file
  73. if c.HasCertAuth() && !c.TLS.ReloadTLSFiles {
  74. // If key/cert were provided, verify them before setting up
  75. // tlsConfig.GetClientCertificate.
  76. cert, err := tls.X509KeyPair(c.TLS.CertData, c.TLS.KeyData)
  77. if err != nil {
  78. return nil, err
  79. }
  80. staticCert = &cert
  81. }
  82. var dynamicCertLoader func() (*tls.Certificate, error)
  83. if c.TLS.ReloadTLSFiles {
  84. dynamicCertLoader = cachingCertificateLoader(c.TLS.CertFile, c.TLS.KeyFile)
  85. }
  86. if c.HasCertAuth() || c.HasCertCallback() {
  87. tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
  88. // Note: static key/cert data always take precedence over cert
  89. // callback.
  90. if staticCert != nil {
  91. return staticCert, nil
  92. }
  93. // key/cert files lead to ReloadTLSFiles being set - takes precedence over cert callback
  94. if dynamicCertLoader != nil {
  95. return dynamicCertLoader()
  96. }
  97. if c.HasCertCallback() {
  98. cert, err := c.TLS.GetCert()
  99. if err != nil {
  100. return nil, err
  101. }
  102. // GetCert may return empty value, meaning no cert.
  103. if cert != nil {
  104. return cert, nil
  105. }
  106. }
  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
  111. }
  112. }
  113. return tlsConfig, nil
  114. }
  115. // loadTLSFiles copies the data from the CertFile, KeyFile, and CAFile fields into the CertData,
  116. // KeyData, and CAFile fields, or returns an error. If no error is returned, all three fields are
  117. // either populated or were empty to start.
  118. func loadTLSFiles(c *Config) error {
  119. var err error
  120. c.TLS.CAData, err = dataFromSliceOrFile(c.TLS.CAData, c.TLS.CAFile)
  121. if err != nil {
  122. return err
  123. }
  124. // Check that we are purely loading from files
  125. if len(c.TLS.CertFile) > 0 && len(c.TLS.CertData) == 0 && len(c.TLS.KeyFile) > 0 && len(c.TLS.KeyData) == 0 {
  126. c.TLS.ReloadTLSFiles = true
  127. }
  128. c.TLS.CertData, err = dataFromSliceOrFile(c.TLS.CertData, c.TLS.CertFile)
  129. if err != nil {
  130. return err
  131. }
  132. c.TLS.KeyData, err = dataFromSliceOrFile(c.TLS.KeyData, c.TLS.KeyFile)
  133. if err != nil {
  134. return err
  135. }
  136. return nil
  137. }
  138. // dataFromSliceOrFile returns data from the slice (if non-empty), or from the file,
  139. // or an error if an error occurred reading the file
  140. func dataFromSliceOrFile(data []byte, file string) ([]byte, error) {
  141. if len(data) > 0 {
  142. return data, nil
  143. }
  144. if len(file) > 0 {
  145. fileData, err := ioutil.ReadFile(file)
  146. if err != nil {
  147. return []byte{}, err
  148. }
  149. return fileData, nil
  150. }
  151. return nil, nil
  152. }
  153. // rootCertPool returns nil if caData is empty. When passed along, this will mean "use system CAs".
  154. // When caData is not empty, it will be the ONLY information used in the CertPool.
  155. func rootCertPool(caData []byte) *x509.CertPool {
  156. // What we really want is a copy of x509.systemRootsPool, but that isn't exposed. It's difficult to build (see the go
  157. // code for a look at the platform specific insanity), so we'll use the fact that RootCAs == nil gives us the system values
  158. // It doesn't allow trusting either/or, but hopefully that won't be an issue
  159. if len(caData) == 0 {
  160. return nil
  161. }
  162. // if we have caData, use it
  163. certPool := x509.NewCertPool()
  164. certPool.AppendCertsFromPEM(caData)
  165. return certPool
  166. }
  167. // WrapperFunc wraps an http.RoundTripper when a new transport
  168. // is created for a client, allowing per connection behavior
  169. // to be injected.
  170. type WrapperFunc func(rt http.RoundTripper) http.RoundTripper
  171. // Wrappers accepts any number of wrappers and returns a wrapper
  172. // function that is the equivalent of calling each of them in order. Nil
  173. // values are ignored, which makes this function convenient for incrementally
  174. // wrapping a function.
  175. func Wrappers(fns ...WrapperFunc) WrapperFunc {
  176. if len(fns) == 0 {
  177. return nil
  178. }
  179. // optimize the common case of wrapping a possibly nil transport wrapper
  180. // with an additional wrapper
  181. if len(fns) == 2 && fns[0] == nil {
  182. return fns[1]
  183. }
  184. return func(rt http.RoundTripper) http.RoundTripper {
  185. base := rt
  186. for _, fn := range fns {
  187. if fn != nil {
  188. base = fn(base)
  189. }
  190. }
  191. return base
  192. }
  193. }
  194. // ContextCanceller prevents new requests after the provided context is finished.
  195. // err is returned when the context is closed, allowing the caller to provide a context
  196. // appropriate error.
  197. func ContextCanceller(ctx context.Context, err error) WrapperFunc {
  198. return func(rt http.RoundTripper) http.RoundTripper {
  199. return &contextCanceller{
  200. ctx: ctx,
  201. rt: rt,
  202. err: err,
  203. }
  204. }
  205. }
  206. type contextCanceller struct {
  207. ctx context.Context
  208. rt http.RoundTripper
  209. err error
  210. }
  211. func (b *contextCanceller) RoundTrip(req *http.Request) (*http.Response, error) {
  212. select {
  213. case <-b.ctx.Done():
  214. return nil, b.err
  215. default:
  216. return b.rt.RoundTrip(req)
  217. }
  218. }
  219. func tryCancelRequest(rt http.RoundTripper, req *http.Request) {
  220. type canceler interface {
  221. CancelRequest(*http.Request)
  222. }
  223. switch rt := rt.(type) {
  224. case canceler:
  225. rt.CancelRequest(req)
  226. case utilnet.RoundTripperWrapper:
  227. tryCancelRequest(rt.WrappedRoundTripper(), req)
  228. default:
  229. klog.Warningf("Unable to cancel request for %T", rt)
  230. }
  231. }
  232. type certificateCacheEntry struct {
  233. cert *tls.Certificate
  234. err error
  235. birth time.Time
  236. }
  237. // isStale returns true when this cache entry is too old to be usable
  238. func (c *certificateCacheEntry) isStale() bool {
  239. return time.Now().Sub(c.birth) > time.Second
  240. }
  241. func newCertificateCacheEntry(certFile, keyFile string) certificateCacheEntry {
  242. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  243. return certificateCacheEntry{cert: &cert, err: err, birth: time.Now()}
  244. }
  245. // cachingCertificateLoader ensures that we don't hammer the filesystem when opening many connections
  246. // the underlying cert files are read at most once every second
  247. func cachingCertificateLoader(certFile, keyFile string) func() (*tls.Certificate, error) {
  248. current := newCertificateCacheEntry(certFile, keyFile)
  249. var currentMtx sync.RWMutex
  250. return func() (*tls.Certificate, error) {
  251. currentMtx.RLock()
  252. if current.isStale() {
  253. currentMtx.RUnlock()
  254. currentMtx.Lock()
  255. defer currentMtx.Unlock()
  256. if current.isStale() {
  257. current = newCertificateCacheEntry(certFile, keyFile)
  258. }
  259. } else {
  260. defer currentMtx.RUnlock()
  261. }
  262. return current.cert, current.err
  263. }
  264. }