transport.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "net/http"
  17. "k8s.io/client-go/transport"
  18. )
  19. // TLSConfigFor returns a tls.Config that will provide the transport level security defined
  20. // by the provided Config. Will return nil if no transport level security is requested.
  21. func TLSConfigFor(config *Config) (*tls.Config, error) {
  22. cfg, err := config.TransportConfig()
  23. if err != nil {
  24. return nil, err
  25. }
  26. return transport.TLSConfigFor(cfg)
  27. }
  28. // TransportFor returns an http.RoundTripper that will provide the authentication
  29. // or transport level security defined by the provided Config. Will return the
  30. // default http.DefaultTransport if no special case behavior is needed.
  31. func TransportFor(config *Config) (http.RoundTripper, error) {
  32. cfg, err := config.TransportConfig()
  33. if err != nil {
  34. return nil, err
  35. }
  36. return transport.New(cfg)
  37. }
  38. // HTTPWrappersForConfig wraps a round tripper with any relevant layered behavior from the
  39. // config. Exposed to allow more clients that need HTTP-like behavior but then must hijack
  40. // the underlying connection (like WebSocket or HTTP2 clients). Pure HTTP clients should use
  41. // the higher level TransportFor or RESTClientFor methods.
  42. func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTripper, error) {
  43. cfg, err := config.TransportConfig()
  44. if err != nil {
  45. return nil, err
  46. }
  47. return transport.HTTPWrappersForConfig(cfg, rt)
  48. }
  49. // TransportConfig converts a client config to an appropriate transport config.
  50. func (c *Config) TransportConfig() (*transport.Config, error) {
  51. wt := c.WrapTransport
  52. if c.AuthProvider != nil {
  53. provider, err := GetAuthProvider(c.Host, c.AuthProvider, c.AuthConfigPersister)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if wt != nil {
  58. previousWT := wt
  59. wt = func(rt http.RoundTripper) http.RoundTripper {
  60. return provider.WrapTransport(previousWT(rt))
  61. }
  62. } else {
  63. wt = provider.WrapTransport
  64. }
  65. }
  66. return &transport.Config{
  67. UserAgent: c.UserAgent,
  68. Transport: c.Transport,
  69. WrapTransport: wt,
  70. TLS: transport.TLSConfig{
  71. Insecure: c.Insecure,
  72. ServerName: c.ServerName,
  73. CAFile: c.CAFile,
  74. CAData: c.CAData,
  75. CertFile: c.CertFile,
  76. CertData: c.CertData,
  77. KeyFile: c.KeyFile,
  78. KeyData: c.KeyData,
  79. },
  80. Username: c.Username,
  81. Password: c.Password,
  82. BearerToken: c.BearerToken,
  83. Impersonate: transport.ImpersonationConfig{
  84. UserName: c.Impersonate.UserName,
  85. Groups: c.Impersonate.Groups,
  86. Extra: c.Impersonate.Extra,
  87. },
  88. }, nil
  89. }