bootstrap_token_test.go 3.6 KB

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