exec.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2020 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rest
  14. import (
  15. "fmt"
  16. "net/http"
  17. "net/url"
  18. clientauthenticationapi "k8s.io/client-go/pkg/apis/clientauthentication"
  19. )
  20. // This file contains Config logic related to exec credential plugins.
  21. // ConfigToExecCluster creates a clientauthenticationapi.Cluster with the corresponding fields from
  22. // the provided Config.
  23. func ConfigToExecCluster(config *Config) (*clientauthenticationapi.Cluster, error) {
  24. caData, err := dataFromSliceOrFile(config.CAData, config.CAFile)
  25. if err != nil {
  26. return nil, fmt.Errorf("failed to load CA bundle for execProvider: %v", err)
  27. }
  28. var proxyURL string
  29. if config.Proxy != nil {
  30. req, err := http.NewRequest("", config.Host, nil)
  31. if err != nil {
  32. return nil, fmt.Errorf("failed to create proxy URL request for execProvider: %w", err)
  33. }
  34. url, err := config.Proxy(req)
  35. if err != nil {
  36. return nil, fmt.Errorf("failed to get proxy URL for execProvider: %w", err)
  37. }
  38. if url != nil {
  39. proxyURL = url.String()
  40. }
  41. }
  42. return &clientauthenticationapi.Cluster{
  43. Server: config.Host,
  44. TLSServerName: config.ServerName,
  45. InsecureSkipTLSVerify: config.Insecure,
  46. CertificateAuthorityData: caData,
  47. ProxyURL: proxyURL,
  48. DisableCompression: config.DisableCompression,
  49. Config: config.ExecProvider.Config,
  50. }, nil
  51. }
  52. // ExecClusterToConfig creates a Config with the corresponding fields from the provided
  53. // clientauthenticationapi.Cluster. The returned Config will be anonymous (i.e., it will not have
  54. // any authentication-related fields set).
  55. func ExecClusterToConfig(cluster *clientauthenticationapi.Cluster) (*Config, error) {
  56. var proxy func(*http.Request) (*url.URL, error)
  57. if cluster.ProxyURL != "" {
  58. proxyURL, err := url.Parse(cluster.ProxyURL)
  59. if err != nil {
  60. return nil, fmt.Errorf("cannot parse proxy URL: %w", err)
  61. }
  62. proxy = http.ProxyURL(proxyURL)
  63. }
  64. return &Config{
  65. Host: cluster.Server,
  66. TLSClientConfig: TLSClientConfig{
  67. Insecure: cluster.InsecureSkipTLSVerify,
  68. ServerName: cluster.TLSServerName,
  69. CAData: cluster.CertificateAuthorityData,
  70. },
  71. Proxy: proxy,
  72. DisableCompression: cluster.DisableCompression,
  73. }, nil
  74. }