transport.go 9.9 KB

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