k8s.io/client-go@v0.31.1/rest/exec.go (about) 1 /* 2 Copyright 2020 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 "fmt" 21 "net/http" 22 "net/url" 23 24 clientauthenticationapi "k8s.io/client-go/pkg/apis/clientauthentication" 25 ) 26 27 // This file contains Config logic related to exec credential plugins. 28 29 // ConfigToExecCluster creates a clientauthenticationapi.Cluster with the corresponding fields from 30 // the provided Config. 31 func ConfigToExecCluster(config *Config) (*clientauthenticationapi.Cluster, error) { 32 caData, err := dataFromSliceOrFile(config.CAData, config.CAFile) 33 if err != nil { 34 return nil, fmt.Errorf("failed to load CA bundle for execProvider: %v", err) 35 } 36 37 var proxyURL string 38 if config.Proxy != nil { 39 req, err := http.NewRequest("", config.Host, nil) 40 if err != nil { 41 return nil, fmt.Errorf("failed to create proxy URL request for execProvider: %w", err) 42 } 43 url, err := config.Proxy(req) 44 if err != nil { 45 return nil, fmt.Errorf("failed to get proxy URL for execProvider: %w", err) 46 } 47 if url != nil { 48 proxyURL = url.String() 49 } 50 } 51 52 return &clientauthenticationapi.Cluster{ 53 Server: config.Host, 54 TLSServerName: config.ServerName, 55 InsecureSkipTLSVerify: config.Insecure, 56 CertificateAuthorityData: caData, 57 ProxyURL: proxyURL, 58 DisableCompression: config.DisableCompression, 59 Config: config.ExecProvider.Config, 60 }, nil 61 } 62 63 // ExecClusterToConfig creates a Config with the corresponding fields from the provided 64 // clientauthenticationapi.Cluster. The returned Config will be anonymous (i.e., it will not have 65 // any authentication-related fields set). 66 func ExecClusterToConfig(cluster *clientauthenticationapi.Cluster) (*Config, error) { 67 var proxy func(*http.Request) (*url.URL, error) 68 if cluster.ProxyURL != "" { 69 proxyURL, err := url.Parse(cluster.ProxyURL) 70 if err != nil { 71 return nil, fmt.Errorf("cannot parse proxy URL: %w", err) 72 } 73 proxy = http.ProxyURL(proxyURL) 74 } 75 76 return &Config{ 77 Host: cluster.Server, 78 TLSClientConfig: TLSClientConfig{ 79 Insecure: cluster.InsecureSkipTLSVerify, 80 ServerName: cluster.TLSServerName, 81 CAData: cluster.CertificateAuthorityData, 82 }, 83 Proxy: proxy, 84 DisableCompression: cluster.DisableCompression, 85 }, nil 86 }