cache.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "fmt"
  17. "net"
  18. "net/http"
  19. "strings"
  20. "sync"
  21. "time"
  22. utilnet "k8s.io/apimachinery/pkg/util/net"
  23. "k8s.io/apimachinery/pkg/util/wait"
  24. )
  25. // TlsTransportCache caches TLS http.RoundTrippers different configurations. The
  26. // same RoundTripper will be returned for configs with identical TLS options If
  27. // the config has no custom TLS options, http.DefaultTransport is returned.
  28. type tlsTransportCache struct {
  29. mu sync.Mutex
  30. transports map[tlsCacheKey]*http.Transport
  31. }
  32. // DialerStopCh is stop channel that is passed down to dynamic cert dialer.
  33. // It's exposed as variable for testing purposes to avoid testing for goroutine
  34. // leakages.
  35. var DialerStopCh = wait.NeverStop
  36. const idleConnsPerHost = 25
  37. var tlsCache = &tlsTransportCache{transports: make(map[tlsCacheKey]*http.Transport)}
  38. type tlsCacheKey struct {
  39. insecure bool
  40. caData string
  41. certData string
  42. keyData string `datapolicy:"security-key"`
  43. certFile string
  44. keyFile string
  45. serverName string
  46. nextProtos string
  47. disableCompression bool
  48. // these functions are wrapped to allow them to be used as map keys
  49. getCert *GetCertHolder
  50. dial *DialHolder
  51. }
  52. func (t tlsCacheKey) String() string {
  53. keyText := "<none>"
  54. if len(t.keyData) > 0 {
  55. keyText = "<redacted>"
  56. }
  57. return fmt.Sprintf("insecure:%v, caData:%#v, certData:%#v, keyData:%s, serverName:%s, disableCompression:%t, getCert:%p, dial:%p",
  58. t.insecure, t.caData, t.certData, keyText, t.serverName, t.disableCompression, t.getCert, t.dial)
  59. }
  60. func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
  61. key, canCache, err := tlsConfigKey(config)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if canCache {
  66. // Ensure we only create a single transport for the given TLS options
  67. c.mu.Lock()
  68. defer c.mu.Unlock()
  69. // See if we already have a custom transport for this config
  70. if t, ok := c.transports[key]; ok {
  71. return t, nil
  72. }
  73. }
  74. // Get the TLS options for this client config
  75. tlsConfig, err := TLSConfigFor(config)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // The options didn't require a custom TLS config
  80. if tlsConfig == nil && config.DialHolder == nil && config.Proxy == nil {
  81. return http.DefaultTransport, nil
  82. }
  83. var dial func(ctx context.Context, network, address string) (net.Conn, error)
  84. if config.DialHolder != nil {
  85. dial = config.DialHolder.Dial
  86. } else {
  87. dial = (&net.Dialer{
  88. Timeout: 30 * time.Second,
  89. KeepAlive: 30 * time.Second,
  90. }).DialContext
  91. }
  92. // If we use are reloading files, we need to handle certificate rotation properly
  93. // TODO(jackkleeman): We can also add rotation here when config.HasCertCallback() is true
  94. if config.TLS.ReloadTLSFiles && tlsConfig != nil && tlsConfig.GetClientCertificate != nil {
  95. dynamicCertDialer := certRotatingDialer(tlsConfig.GetClientCertificate, dial)
  96. tlsConfig.GetClientCertificate = dynamicCertDialer.GetClientCertificate
  97. dial = dynamicCertDialer.connDialer.DialContext
  98. go dynamicCertDialer.Run(DialerStopCh)
  99. }
  100. proxy := http.ProxyFromEnvironment
  101. if config.Proxy != nil {
  102. proxy = config.Proxy
  103. }
  104. transport := utilnet.SetTransportDefaults(&http.Transport{
  105. Proxy: proxy,
  106. TLSHandshakeTimeout: 10 * time.Second,
  107. TLSClientConfig: tlsConfig,
  108. MaxIdleConnsPerHost: idleConnsPerHost,
  109. DialContext: dial,
  110. DisableCompression: config.DisableCompression,
  111. })
  112. if canCache {
  113. // Cache a single transport for these options
  114. c.transports[key] = transport
  115. }
  116. return transport, nil
  117. }
  118. // tlsConfigKey returns a unique key for tls.Config objects returned from TLSConfigFor
  119. func tlsConfigKey(c *Config) (tlsCacheKey, bool, error) {
  120. // Make sure ca/key/cert content is loaded
  121. if err := loadTLSFiles(c); err != nil {
  122. return tlsCacheKey{}, false, err
  123. }
  124. if c.Proxy != nil {
  125. // cannot determine equality for functions
  126. return tlsCacheKey{}, false, nil
  127. }
  128. k := tlsCacheKey{
  129. insecure: c.TLS.Insecure,
  130. caData: string(c.TLS.CAData),
  131. serverName: c.TLS.ServerName,
  132. nextProtos: strings.Join(c.TLS.NextProtos, ","),
  133. disableCompression: c.DisableCompression,
  134. getCert: c.TLS.GetCertHolder,
  135. dial: c.DialHolder,
  136. }
  137. if c.TLS.ReloadTLSFiles {
  138. k.certFile = c.TLS.CertFile
  139. k.keyFile = c.TLS.KeyFile
  140. } else {
  141. k.certData = string(c.TLS.CertData)
  142. k.keyData = string(c.TLS.KeyData)
  143. }
  144. return k, true, nil
  145. }