k8s.io/kubernetes@v1.29.3/test/e2e_kubeadm/bootstrap_token_test.go (about)

     1  /*
     2  Copyright 2019 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 kubeadm
    18  
    19  import (
    20  	"context"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	rbacv1 "k8s.io/api/rbac/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	bootstrapapi "k8s.io/cluster-bootstrap/token/api"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	admissionapi "k8s.io/pod-security-admission/api"
    28  
    29  	"github.com/onsi/ginkgo/v2"
    30  	"github.com/onsi/gomega"
    31  )
    32  
    33  const (
    34  	bootstrapTokensGroup                             = "system:bootstrappers:kubeadm:default-node-token"
    35  	bootstrapTokensAllowPostCSRClusterRoleBinding    = "kubeadm:kubelet-bootstrap"
    36  	bootstrapTokensAllowPostCSRClusterRoleName       = "system:node-bootstrapper"
    37  	bootstrapTokensCSRAutoApprovalClusterRoleBinding = "kubeadm:node-autoapprove-bootstrap"
    38  	bootstrapTokensCSRAutoApprovalClusterRoleName    = "system:certificates.k8s.io:certificatesigningrequests:nodeclient"
    39  )
    40  
    41  // Define container for all the test specification aimed at verifying
    42  // that kubeadm creates the bootstrap token, the system:bootstrappers:kubeadm:default-node-token group
    43  // and that all the related RBAC rules are in place
    44  var _ = Describe("bootstrap token", func() {
    45  
    46  	// Get an instance of the k8s test framework
    47  	f := framework.NewDefaultFramework("bootstrap token")
    48  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    49  
    50  	// Tests in this container are not expected to create new objects in the cluster
    51  	// so we are disabling the creation of a namespace in order to get a faster execution
    52  	f.SkipNamespaceCreation = true
    53  
    54  	ginkgo.It("should exist and be properly configured", func(ctx context.Context) {
    55  		secrets, err := f.ClientSet.CoreV1().
    56  			Secrets(kubeSystemNamespace).
    57  			List(ctx, metav1.ListOptions{})
    58  		framework.ExpectNoError(err, "error reading Secrets")
    59  
    60  		tokenNum := 0
    61  		for _, s := range secrets.Items {
    62  			// check extra group and usage of token, make sure at least one token exist
    63  			if s.Type == corev1.SecretTypeBootstrapToken && string(s.Data[bootstrapapi.BootstrapTokenExtraGroupsKey]) == bootstrapTokensGroup {
    64  				usageBootstrapAuthentication := string(s.Data[bootstrapapi.BootstrapTokenUsageAuthentication])
    65  				usageBootstrapSigning := string(s.Data[bootstrapapi.BootstrapTokenUsageSigningKey])
    66  				gomega.Expect(usageBootstrapAuthentication).Should(gomega.Equal("true"), "the bootstrap token should be able to be used for authentication")
    67  				gomega.Expect(usageBootstrapSigning).Should(gomega.Equal("true"), "the bootstrap token should be able to be used for signing")
    68  				tokenNum++
    69  			}
    70  		}
    71  		gomega.Expect(tokenNum).Should(gomega.BeNumerically(">", 0), "At least one bootstrap token should exist")
    72  	})
    73  
    74  	ginkgo.It("should be allowed to post CSR for kubelet certificates on joining nodes", func(ctx context.Context) {
    75  		ExpectClusterRoleBindingWithSubjectAndRole(f.ClientSet,
    76  			bootstrapTokensAllowPostCSRClusterRoleBinding,
    77  			rbacv1.GroupKind, bootstrapTokensGroup,
    78  			bootstrapTokensAllowPostCSRClusterRoleName,
    79  		)
    80  	})
    81  
    82  	ginkgo.It("should be allowed to auto approve CSR for kubelet certificates on joining nodes", func(ctx context.Context) {
    83  		ExpectClusterRoleBindingWithSubjectAndRole(f.ClientSet,
    84  			bootstrapTokensCSRAutoApprovalClusterRoleBinding,
    85  			rbacv1.GroupKind, bootstrapTokensGroup,
    86  			bootstrapTokensCSRAutoApprovalClusterRoleName,
    87  		)
    88  	})
    89  })