Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [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 | TLS: transport.TLSConfig{ |
| 68 | Insecure: c.Insecure, |
| 69 | ServerName: c.ServerName, |
| 70 | CAFile: c.CAFile, |
| 71 | CAData: c.CAData, |
| 72 | CertFile: c.CertFile, |
| 73 | CertData: c.CertData, |
| 74 | KeyFile: c.KeyFile, |
| 75 | KeyData: c.KeyData, |
| 76 | }, |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 77 | Username: c.Username, |
| 78 | Password: c.Password, |
| 79 | BearerToken: c.BearerToken, |
| 80 | BearerTokenFile: c.BearerTokenFile, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 81 | Impersonate: transport.ImpersonationConfig{ |
| 82 | UserName: c.Impersonate.UserName, |
| 83 | Groups: c.Impersonate.Groups, |
| 84 | Extra: c.Impersonate.Extra, |
| 85 | }, |
| 86 | Dial: c.Dial, |
| 87 | } |
| 88 | |
| 89 | if c.ExecProvider != nil && c.AuthProvider != nil { |
| 90 | return nil, errors.New("execProvider and authProvider cannot be used in combination") |
| 91 | } |
| 92 | |
| 93 | if c.ExecProvider != nil { |
| 94 | provider, err := exec.GetAuthenticator(c.ExecProvider) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | if err := provider.UpdateTransportConfig(conf); err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | } |
| 102 | if c.AuthProvider != nil { |
| 103 | provider, err := GetAuthProvider(c.Host, c.AuthProvider, c.AuthConfigPersister) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | conf.Wrap(provider.WrapTransport) |
| 108 | } |
| 109 | return conf, nil |
| 110 | } |
| 111 | |
| 112 | // Wrap adds a transport middleware function that will give the caller |
| 113 | // an opportunity to wrap the underlying http.RoundTripper prior to the |
| 114 | // first API call being made. The provided function is invoked after any |
| 115 | // existing transport wrappers are invoked. |
| 116 | func (c *Config) Wrap(fn transport.WrapperFunc) { |
| 117 | c.WrapTransport = transport.Wrappers(c.WrapTransport, fn) |
| 118 | } |