cache.go 4.3 KB

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