discovery.go 3.9 KB

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