transport.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2014 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 rest
  14. import (
  15. "crypto/tls"
  16. "errors"
  17. "net/http"
  18. "k8s.io/client-go/pkg/apis/clientauthentication"
  19. "k8s.io/client-go/plugin/pkg/client/auth/exec"
  20. "k8s.io/client-go/transport"
  21. )
  22. // HTTPClientFor returns an http.Client that will provide the authentication
  23. // or transport level security defined by the provided Config. Will return the
  24. // default http.DefaultClient if no special case behavior is needed.
  25. func HTTPClientFor(config *Config) (*http.Client, error) {
  26. transport, err := TransportFor(config)
  27. if err != nil {
  28. return nil, err
  29. }
  30. var httpClient *http.Client
  31. if transport != http.DefaultTransport || config.Timeout > 0 {
  32. httpClient = &http.Client{
  33. Transport: transport,
  34. Timeout: config.Timeout,
  35. }
  36. } else {
  37. httpClient = http.DefaultClient
  38. }
  39. return httpClient, nil
  40. }
  41. // TLSConfigFor returns a tls.Config that will provide the transport level security defined
  42. // by the provided Config. Will return nil if no transport level security is requested.
  43. func TLSConfigFor(config *Config) (*tls.Config, error) {
  44. cfg, err := config.TransportConfig()
  45. if err != nil {
  46. return nil, err
  47. }
  48. return transport.TLSConfigFor(cfg)
  49. }
  50. // TransportFor returns an http.RoundTripper that will provide the authentication
  51. // or transport level security defined by the provided Config. Will return the
  52. // default http.DefaultTransport if no special case behavior is needed.
  53. func TransportFor(config *Config) (http.RoundTripper, error) {
  54. cfg, err := config.TransportConfig()
  55. if err != nil {
  56. return nil, err
  57. }
  58. return transport.New(cfg)
  59. }
  60. // HTTPWrappersForConfig wraps a round tripper with any relevant layered behavior from the
  61. // config. Exposed to allow more clients that need HTTP-like behavior but then must hijack
  62. // the underlying connection (like WebSocket or HTTP2 clients). Pure HTTP clients should use
  63. // the higher level TransportFor or RESTClientFor methods.
  64. func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTripper, error) {
  65. cfg, err := config.TransportConfig()
  66. if err != nil {
  67. return nil, err
  68. }
  69. return transport.HTTPWrappersForConfig(cfg, rt)
  70. }
  71. // TransportConfig converts a client config to an appropriate transport config.
  72. func (c *Config) TransportConfig() (*transport.Config, error) {
  73. conf := &transport.Config{
  74. UserAgent: c.UserAgent,
  75. Transport: c.Transport,
  76. WrapTransport: c.WrapTransport,
  77. DisableCompression: c.DisableCompression,
  78. TLS: transport.TLSConfig{
  79. Insecure: c.Insecure,
  80. ServerName: c.ServerName,
  81. CAFile: c.CAFile,
  82. CAData: c.CAData,
  83. CertFile: c.CertFile,
  84. CertData: c.CertData,
  85. KeyFile: c.KeyFile,
  86. KeyData: c.KeyData,
  87. NextProtos: c.NextProtos,
  88. },
  89. Username: c.Username,
  90. Password: c.Password,
  91. BearerToken: c.BearerToken,
  92. BearerTokenFile: c.BearerTokenFile,
  93. Impersonate: transport.ImpersonationConfig{
  94. UserName: c.Impersonate.UserName,
  95. UID: c.Impersonate.UID,
  96. Groups: c.Impersonate.Groups,
  97. Extra: c.Impersonate.Extra,
  98. },
  99. Proxy: c.Proxy,
  100. }
  101. if c.Dial != nil {
  102. conf.DialHolder = &transport.DialHolder{Dial: c.Dial}
  103. }
  104. if c.ExecProvider != nil && c.AuthProvider != nil {
  105. return nil, errors.New("execProvider and authProvider cannot be used in combination")
  106. }
  107. if c.ExecProvider != nil {
  108. var cluster *clientauthentication.Cluster
  109. if c.ExecProvider.ProvideClusterInfo {
  110. var err error
  111. cluster, err = ConfigToExecCluster(c)
  112. if err != nil {
  113. return nil, err
  114. }
  115. }
  116. provider, err := exec.GetAuthenticator(c.ExecProvider, cluster)
  117. if err != nil {
  118. return nil, err
  119. }
  120. if err := provider.UpdateTransportConfig(conf); err != nil {
  121. return nil, err
  122. }
  123. }
  124. if c.AuthProvider != nil {
  125. provider, err := GetAuthProvider(c.Host, c.AuthProvider, c.AuthConfigPersister)
  126. if err != nil {
  127. return nil, err
  128. }
  129. conf.Wrap(provider.WrapTransport)
  130. }
  131. return conf, nil
  132. }
  133. // Wrap adds a transport middleware function that will give the caller
  134. // an opportunity to wrap the underlying http.RoundTripper prior to the
  135. // first API call being made. The provided function is invoked after any
  136. // existing transport wrappers are invoked.
  137. func (c *Config) Wrap(fn transport.WrapperFunc) {
  138. c.WrapTransport = transport.Wrappers(c.WrapTransport, fn)
  139. }