bootstrap_signer.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2017 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 bootstrap
  14. import (
  15. . "github.com/onsi/ginkgo"
  16. . "github.com/onsi/gomega"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. "k8s.io/kubernetes/test/e2e/lifecycle"
  22. )
  23. const (
  24. TokenIDBytes = 3
  25. TokenSecretBytes = 8
  26. )
  27. var _ = lifecycle.SIGDescribe("[Feature:BootstrapTokens]", func() {
  28. var c clientset.Interface
  29. f := framework.NewDefaultFramework("bootstrap-signer")
  30. AfterEach(func() {
  31. if len(secretNeedClean) > 0 {
  32. By("delete the bootstrap token secret")
  33. err := c.CoreV1().Secrets(metav1.NamespaceSystem).Delete(secretNeedClean, &metav1.DeleteOptions{})
  34. framework.ExpectNoError(err)
  35. secretNeedClean = ""
  36. }
  37. })
  38. BeforeEach(func() {
  39. c = f.ClientSet
  40. })
  41. It("should sign the new added bootstrap tokens", func() {
  42. By("create a new bootstrap token secret")
  43. tokenId, err := GenerateTokenId()
  44. framework.ExpectNoError(err)
  45. secret := newTokenSecret(tokenId, "tokenSecret")
  46. _, err = c.CoreV1().Secrets(metav1.NamespaceSystem).Create(secret)
  47. secretNeedClean = bootstrapapi.BootstrapTokenSecretPrefix + tokenId
  48. framework.ExpectNoError(err)
  49. By("wait for the bootstrap token secret be signed")
  50. err = WaitforSignedClusterInfoByBootStrapToken(c, tokenId)
  51. framework.ExpectNoError(err)
  52. })
  53. It("should resign the bootstrap tokens when the clusterInfo ConfigMap updated [Serial][Disruptive]", func() {
  54. By("create a new bootstrap token secret")
  55. tokenId, err := GenerateTokenId()
  56. framework.ExpectNoError(err)
  57. secret := newTokenSecret(tokenId, "tokenSecret")
  58. secret, err = c.CoreV1().Secrets(metav1.NamespaceSystem).Create(secret)
  59. secretNeedClean = bootstrapapi.BootstrapTokenSecretPrefix + tokenId
  60. By("wait for the bootstrap token secret be signed")
  61. err = WaitforSignedClusterInfoByBootStrapToken(c, tokenId)
  62. cfgMap, err := f.ClientSet.CoreV1().ConfigMaps(metav1.NamespacePublic).Get(bootstrapapi.ConfigMapClusterInfo, metav1.GetOptions{})
  63. framework.ExpectNoError(err)
  64. signedToken, ok := cfgMap.Data[bootstrapapi.JWSSignatureKeyPrefix+tokenId]
  65. Expect(ok).Should(Equal(true))
  66. By("update the cluster-info ConfigMap")
  67. originalData := cfgMap.Data[bootstrapapi.KubeConfigKey]
  68. updatedKubeConfig, err := randBytes(20)
  69. framework.ExpectNoError(err)
  70. cfgMap.Data[bootstrapapi.KubeConfigKey] = updatedKubeConfig
  71. _, err = f.ClientSet.CoreV1().ConfigMaps(metav1.NamespacePublic).Update(cfgMap)
  72. framework.ExpectNoError(err)
  73. defer func() {
  74. By("update back the cluster-info ConfigMap")
  75. cfgMap, err = f.ClientSet.CoreV1().ConfigMaps(metav1.NamespacePublic).Get(bootstrapapi.ConfigMapClusterInfo, metav1.GetOptions{})
  76. framework.ExpectNoError(err)
  77. cfgMap.Data[bootstrapapi.KubeConfigKey] = originalData
  78. _, err = f.ClientSet.CoreV1().ConfigMaps(metav1.NamespacePublic).Update(cfgMap)
  79. framework.ExpectNoError(err)
  80. }()
  81. By("wait for signed bootstrap token updated")
  82. err = WaitForSignedClusterInfoGetUpdatedByBootstrapToken(c, tokenId, signedToken)
  83. framework.ExpectNoError(err)
  84. })
  85. It("should delete the signed bootstrap tokens from clusterInfo ConfigMap when bootstrap token is deleted", func() {
  86. By("create a new bootstrap token secret")
  87. tokenId, err := GenerateTokenId()
  88. framework.ExpectNoError(err)
  89. secret := newTokenSecret(tokenId, "tokenSecret")
  90. _, err = c.CoreV1().Secrets(metav1.NamespaceSystem).Create(secret)
  91. framework.ExpectNoError(err)
  92. By("wait for the bootstrap secret be signed")
  93. err = WaitforSignedClusterInfoByBootStrapToken(c, tokenId)
  94. framework.ExpectNoError(err)
  95. By("delete the bootstrap token secret")
  96. err = c.CoreV1().Secrets(metav1.NamespaceSystem).Delete(bootstrapapi.BootstrapTokenSecretPrefix+tokenId, &metav1.DeleteOptions{})
  97. framework.ExpectNoError(err)
  98. By("wait for the bootstrap token removed from cluster-info ConfigMap")
  99. err = WaitForSignedClusterInfoByBootstrapTokenToDisappear(c, tokenId)
  100. framework.ExpectNoError(err)
  101. })
  102. })