2 Copyright 2014 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
24 "k8s.io/client-go/plugin/pkg/client/auth/exec"
25 "k8s.io/client-go/transport"
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()
35 return transport.TLSConfigFor(cfg)
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()
46 return transport.New(cfg)
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()
58 return transport.HTTPWrappersForConfig(cfg, rt)
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{
69 ServerName: c.ServerName,
79 BearerToken: c.BearerToken,
80 Impersonate: transport.ImpersonationConfig{
81 UserName: c.Impersonate.UserName,
82 Groups: c.Impersonate.Groups,
83 Extra: c.Impersonate.Extra,
88 if c.ExecProvider != nil && c.AuthProvider != nil {
89 return nil, errors.New("execProvider and authProvider cannot be used in combination")
92 if c.ExecProvider != nil {
93 provider, err := exec.GetAuthenticator(c.ExecProvider)
97 if err := provider.UpdateTransportConfig(conf); err != nil {
101 if c.AuthProvider != nil {
102 provider, err := GetAuthProvider(c.Host, c.AuthProvider, c.AuthConfigPersister)
106 wt := conf.WrapTransport
108 conf.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
109 return provider.WrapTransport(wt(rt))
112 conf.WrapTransport = provider.WrapTransport