k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/discovery/discovery.go (about) 1 /* 2 Copyright 2016 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 discovery 18 19 import ( 20 "net/url" 21 22 "github.com/pkg/errors" 23 24 clientcmdapi "k8s.io/client-go/tools/clientcmd/api" 25 "k8s.io/klog/v2" 26 27 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" 28 kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3" 29 "k8s.io/kubernetes/cmd/kubeadm/app/discovery/file" 30 "k8s.io/kubernetes/cmd/kubeadm/app/discovery/https" 31 "k8s.io/kubernetes/cmd/kubeadm/app/discovery/token" 32 kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig" 33 ) 34 35 // TokenUser defines token user 36 const TokenUser = "tls-bootstrap-token-user" 37 38 // For returns a kubeconfig object that can be used for doing the TLS Bootstrap with the right credentials 39 // Also, before returning anything, it makes sure it can trust the API Server 40 func For(cfg *kubeadmapi.JoinConfiguration) (*clientcmdapi.Config, error) { 41 // TODO: Print summary info about the CA certificate, along with the checksum signature 42 // we also need an ability for the user to configure the client to validate received CA cert against a checksum 43 config, err := DiscoverValidatedKubeConfig(cfg) 44 if err != nil { 45 return nil, errors.Wrap(err, "couldn't validate the identity of the API Server") 46 } 47 48 // If the users has provided a TLSBootstrapToken use it for the join process. 49 // This is usually the case of Token discovery, but it can also be used with a discovery file 50 // without embedded authentication credentials. 51 if len(cfg.Discovery.TLSBootstrapToken) != 0 { 52 klog.V(1).Info("[discovery] Using provided TLSBootstrapToken as authentication credentials for the join process") 53 54 _, clusterinfo := kubeconfigutil.GetClusterFromKubeConfig(config) 55 return kubeconfigutil.CreateWithToken( 56 clusterinfo.Server, 57 kubeadmapiv1.DefaultClusterName, 58 TokenUser, 59 clusterinfo.CertificateAuthorityData, 60 cfg.Discovery.TLSBootstrapToken, 61 ), nil 62 } 63 64 // if the config returned from discovery has authentication credentials, proceed with the TLS boostrap process 65 if kubeconfigutil.HasAuthenticationCredentials(config) { 66 return config, nil 67 } 68 69 // if there are no authentication credentials (nor in the config returned from discovery, nor in the TLSBootstrapToken), fail 70 return nil, errors.New("couldn't find authentication credentials for the TLS boostrap process. Please use Token discovery, a discovery file with embedded authentication credentials or a discovery file without authentication credentials but with the TLSBootstrapToken flag") 71 } 72 73 // DiscoverValidatedKubeConfig returns a validated Config object that specifies where the cluster is and the CA cert to trust 74 func DiscoverValidatedKubeConfig(cfg *kubeadmapi.JoinConfiguration) (*clientcmdapi.Config, error) { 75 timeout := cfg.Timeouts.Discovery.Duration 76 switch { 77 case cfg.Discovery.File != nil: 78 kubeConfigPath := cfg.Discovery.File.KubeConfigPath 79 if isHTTPSURL(kubeConfigPath) { 80 return https.RetrieveValidatedConfigInfo(kubeConfigPath, timeout) 81 } 82 return file.RetrieveValidatedConfigInfo(kubeConfigPath, timeout) 83 case cfg.Discovery.BootstrapToken != nil: 84 return token.RetrieveValidatedConfigInfo(&cfg.Discovery, timeout) 85 default: 86 return nil, errors.New("couldn't find a valid discovery configuration") 87 } 88 } 89 90 // isHTTPSURL checks whether the string is parsable as a URL and whether the Scheme is https 91 func isHTTPSURL(s string) bool { 92 u, err := url.Parse(s) 93 return err == nil && u.Scheme == "https" 94 }