cache.go 5.4 KB

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