k8s.io/client-go@v0.22.2/rest/transport.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package rest 18 19 import ( 20 "crypto/tls" 21 "errors" 22 "net/http" 23 24 "k8s.io/client-go/pkg/apis/clientauthentication" 25 "k8s.io/client-go/plugin/pkg/client/auth/exec" 26 "k8s.io/client-go/transport" 27 ) 28 29 // TLSConfigFor returns a tls.Config that will provide the transport level security defined 30 // by the provided Config. Will return nil if no transport level security is requested. 31 func TLSConfigFor(config *Config) (*tls.Config, error) { 32 cfg, err := config.TransportConfig() 33 if err != nil { 34 return nil, err 35 } 36 return transport.TLSConfigFor(cfg) 37 } 38 39 // TransportFor returns an http.RoundTripper that will provide the authentication 40 // or transport level security defined by the provided Config. Will return the 41 // default http.DefaultTransport if no special case behavior is needed. 42 func TransportFor(config *Config) (http.RoundTripper, error) { 43 cfg, err := config.TransportConfig() 44 if err != nil { 45 return nil, err 46 } 47 return transport.New(cfg) 48 } 49 50 // HTTPWrappersForConfig wraps a round tripper with any relevant layered behavior from the 51 // config. Exposed to allow more clients that need HTTP-like behavior but then must hijack 52 // the underlying connection (like WebSocket or HTTP2 clients). Pure HTTP clients should use 53 // the higher level TransportFor or RESTClientFor methods. 54 func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTripper, error) { 55 cfg, err := config.TransportConfig() 56 if err != nil { 57 return nil, err 58 } 59 return transport.HTTPWrappersForConfig(cfg, rt) 60 } 61 62 // TransportConfig converts a client config to an appropriate transport config. 63 func (c *Config) TransportConfig() (*transport.Config, error) { 64 conf := &transport.Config{ 65 UserAgent: c.UserAgent, 66 Transport: c.Transport, 67 WrapTransport: c.WrapTransport, 68 DisableCompression: c.DisableCompression, 69 TLS: transport.TLSConfig{ 70 Insecure: c.Insecure, 71 ServerName: c.ServerName, 72 CAFile: c.CAFile, 73 CAData: c.CAData, 74 CertFile: c.CertFile, 75 CertData: c.CertData, 76 KeyFile: c.KeyFile, 77 KeyData: c.KeyData, 78 NextProtos: c.NextProtos, 79 }, 80 Username: c.Username, 81 Password: c.Password, 82 BearerToken: c.BearerToken, 83 BearerTokenFile: c.BearerTokenFile, 84 Impersonate: transport.ImpersonationConfig{ 85 UserName: c.Impersonate.UserName, 86 Groups: c.Impersonate.Groups, 87 Extra: c.Impersonate.Extra, 88 }, 89 Dial: c.Dial, 90 Proxy: c.Proxy, 91 } 92 93 if c.ExecProvider != nil && c.AuthProvider != nil { 94 return nil, errors.New("execProvider and authProvider cannot be used in combination") 95 } 96 97 if c.ExecProvider != nil { 98 var cluster *clientauthentication.Cluster 99 if c.ExecProvider.ProvideClusterInfo { 100 var err error 101 cluster, err = ConfigToExecCluster(c) 102 if err != nil { 103 return nil, err 104 } 105 } 106 provider, err := exec.GetAuthenticator(c.ExecProvider, cluster) 107 if err != nil { 108 return nil, err 109 } 110 if err := provider.UpdateTransportConfig(conf); err != nil { 111 return nil, err 112 } 113 } 114 if c.AuthProvider != nil { 115 provider, err := GetAuthProvider(c.Host, c.AuthProvider, c.AuthConfigPersister) 116 if err != nil { 117 return nil, err 118 } 119 conf.Wrap(provider.WrapTransport) 120 } 121 return conf, nil 122 } 123 124 // Wrap adds a transport middleware function that will give the caller 125 // an opportunity to wrap the underlying http.RoundTripper prior to the 126 // first API call being made. The provided function is invoked after any 127 // existing transport wrappers are invoked. 128 func (c *Config) Wrap(fn transport.WrapperFunc) { 129 c.WrapTransport = transport.Wrappers(c.WrapTransport, fn) 130 }