transport.go 3.8 KB

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