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