file.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 file
  14. import (
  15. "github.com/pkg/errors"
  16. "k8s.io/api/core/v1"
  17. apierrors "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/wait"
  20. "k8s.io/client-go/tools/clientcmd"
  21. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  22. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  23. "k8s.io/klog"
  24. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  25. kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
  26. )
  27. // RetrieveValidatedConfigInfo connects to the API Server and makes sure it can talk
  28. // securely to the API Server using the provided CA cert and
  29. // optionally refreshes the cluster-info information from the cluster-info ConfigMap
  30. func RetrieveValidatedConfigInfo(filepath, clustername string) (*clientcmdapi.Config, error) {
  31. config, err := clientcmd.LoadFromFile(filepath)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return ValidateConfigInfo(config, clustername)
  36. }
  37. // ValidateConfigInfo connects to the API Server and makes sure it can talk
  38. // securely to the API Server using the provided CA cert/client certificates and
  39. // optionally refreshes the cluster-info information from the cluster-info ConfigMap
  40. func ValidateConfigInfo(config *clientcmdapi.Config, clustername string) (*clientcmdapi.Config, error) {
  41. err := validateKubeConfig(config)
  42. if err != nil {
  43. return nil, err
  44. }
  45. var kubeconfig *clientcmdapi.Config
  46. // If the discovery file config contains authentication credentials
  47. if kubeconfigutil.HasAuthenticationCredentials(config) {
  48. klog.V(1).Info("[discovery] Using authentication credentials from the discovery file for validating TLS connection")
  49. // Use the discovery file config for starting the join process
  50. kubeconfig = config
  51. // We should ensure that all the authentication info is embedded in config file, so everything will work also when
  52. // the kubeconfig file will be stored in /etc/kubernetes/boostrap-kubelet.conf
  53. if err := kubeconfigutil.EnsureAuthenticationInfoAreEmbedded(kubeconfig); err != nil {
  54. return nil, errors.Wrap(err, "error while reading client cert file or client key file")
  55. }
  56. } else {
  57. // If the discovery file config does not contains authentication credentials
  58. klog.V(1).Info("[discovery] Discovery file does not contains authentication credentials, using unauthenticated request for validating TLS connection")
  59. // Create a new kubeconfig object from the discovery file config, with only the server and the CA cert.
  60. // NB. We do this in order to not pick up other possible misconfigurations in the clusterinfo file
  61. var fileCluster = kubeconfigutil.GetClusterFromKubeConfig(config)
  62. kubeconfig = kubeconfigutil.CreateBasic(
  63. fileCluster.Server,
  64. clustername,
  65. "", // no user provided
  66. fileCluster.CertificateAuthorityData,
  67. )
  68. }
  69. // Try to read the cluster-info config map; this step was required by the original design in order
  70. // to validate the TLS connection to the server early in the process
  71. client, err := kubeconfigutil.ToClientSet(kubeconfig)
  72. if err != nil {
  73. return nil, err
  74. }
  75. currentCluster := kubeconfigutil.GetClusterFromKubeConfig(kubeconfig)
  76. klog.V(1).Infof("[discovery] Created cluster-info discovery client, requesting info from %q\n", currentCluster.Server)
  77. var clusterinfoCM *v1.ConfigMap
  78. wait.PollInfinite(constants.DiscoveryRetryInterval, func() (bool, error) {
  79. var err error
  80. clusterinfoCM, err = client.CoreV1().ConfigMaps(metav1.NamespacePublic).Get(bootstrapapi.ConfigMapClusterInfo, metav1.GetOptions{})
  81. if err != nil {
  82. if apierrors.IsForbidden(err) {
  83. // If the request is unauthorized, the cluster admin has not granted access to the cluster info configmap for unauthenticated users
  84. // In that case, trust the cluster admin and do not refresh the cluster-info data
  85. klog.Warningf("[discovery] Could not access the %s ConfigMap for refreshing the cluster-info information, but the TLS cert is valid so proceeding...\n", bootstrapapi.ConfigMapClusterInfo)
  86. return true, nil
  87. }
  88. klog.V(1).Infof("[discovery] Error reading the %s ConfigMap, will try again: %v\n", bootstrapapi.ConfigMapClusterInfo, err)
  89. return false, nil
  90. }
  91. return true, nil
  92. })
  93. // If we couldn't fetch the cluster-info ConfigMap, just return the cluster-info object the user provided
  94. if clusterinfoCM == nil {
  95. return kubeconfig, nil
  96. }
  97. // We somehow got hold of the ConfigMap, try to read some data from it. If we can't, fallback on the user-provided file
  98. refreshedBaseKubeConfig, err := tryParseClusterInfoFromConfigMap(clusterinfoCM)
  99. if err != nil {
  100. klog.V(1).Infof("[discovery] The %s ConfigMap isn't set up properly (%v), but the TLS cert is valid so proceeding...\n", bootstrapapi.ConfigMapClusterInfo, err)
  101. return kubeconfig, nil
  102. }
  103. refreshedCluster := kubeconfigutil.GetClusterFromKubeConfig(refreshedBaseKubeConfig)
  104. currentCluster.Server = refreshedCluster.Server
  105. currentCluster.CertificateAuthorityData = refreshedCluster.CertificateAuthorityData
  106. klog.V(1).Infof("[discovery] Synced Server and CertificateAuthorityData from the %s ConfigMap", bootstrapapi.ConfigMapClusterInfo)
  107. return kubeconfig, nil
  108. }
  109. // tryParseClusterInfoFromConfigMap tries to parse a kubeconfig file from a ConfigMap key
  110. func tryParseClusterInfoFromConfigMap(cm *v1.ConfigMap) (*clientcmdapi.Config, error) {
  111. kubeConfigString, ok := cm.Data[bootstrapapi.KubeConfigKey]
  112. if !ok || len(kubeConfigString) == 0 {
  113. return nil, errors.Errorf("no %s key in ConfigMap", bootstrapapi.KubeConfigKey)
  114. }
  115. parsedKubeConfig, err := clientcmd.Load([]byte(kubeConfigString))
  116. if err != nil {
  117. return nil, errors.Wrapf(err, "couldn't parse the kubeconfig file in the %s ConfigMap", bootstrapapi.ConfigMapClusterInfo)
  118. }
  119. return parsedKubeConfig, nil
  120. }
  121. // validateKubeConfig makes sure the user-provided kubeconfig file is valid
  122. func validateKubeConfig(config *clientcmdapi.Config) error {
  123. if len(config.Clusters) < 1 {
  124. return errors.New("the provided cluster-info kubeconfig file must have at least one Cluster defined")
  125. }
  126. defaultCluster := kubeconfigutil.GetClusterFromKubeConfig(config)
  127. if defaultCluster == nil {
  128. return errors.New("the provided cluster-info kubeconfig file must have an unnamed Cluster or a CurrentContext that specifies a non-nil Cluster")
  129. }
  130. return clientcmd.Validate(*config)
  131. }