util_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 bootstrap
  14. import (
  15. "testing"
  16. "time"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  21. )
  22. const (
  23. givenTokenID = "abc123"
  24. givenTokenID2 = "def456"
  25. givenTokenSecret = "tokenSecret"
  26. )
  27. func timeString(delta time.Duration) string {
  28. return time.Now().Add(delta).Format(time.RFC3339)
  29. }
  30. func TestValidateSecretForSigning(t *testing.T) {
  31. cases := []struct {
  32. description string
  33. tokenID string
  34. tokenSecret string
  35. okToSign string
  36. expiration string
  37. valid bool
  38. }{
  39. {
  40. "Signing token with no exp",
  41. givenTokenID, givenTokenSecret, "true", "", true,
  42. },
  43. {
  44. "Signing token with valid exp",
  45. givenTokenID, givenTokenSecret, "true", timeString(time.Hour), true,
  46. },
  47. {
  48. "Expired signing token",
  49. givenTokenID, givenTokenSecret, "true", timeString(-time.Hour), false,
  50. },
  51. {
  52. "Signing token with bad exp",
  53. givenTokenID, givenTokenSecret, "true", "garbage", false,
  54. },
  55. {
  56. "Signing token without signing bit",
  57. givenTokenID, givenTokenSecret, "", "garbage", false,
  58. },
  59. {
  60. "Signing token with bad signing bit",
  61. givenTokenID, givenTokenSecret, "", "", false,
  62. },
  63. {
  64. "Signing token with no ID",
  65. "", givenTokenSecret, "true", "", false,
  66. },
  67. {
  68. "Signing token with no secret",
  69. givenTokenID, "", "true", "", false,
  70. },
  71. }
  72. for _, tc := range cases {
  73. secret := &v1.Secret{
  74. ObjectMeta: metav1.ObjectMeta{
  75. Namespace: metav1.NamespaceSystem,
  76. Name: bootstrapapi.BootstrapTokenSecretPrefix + givenTokenID,
  77. ResourceVersion: "1",
  78. },
  79. Type: bootstrapapi.SecretTypeBootstrapToken,
  80. Data: map[string][]byte{
  81. bootstrapapi.BootstrapTokenIDKey: []byte(tc.tokenID),
  82. bootstrapapi.BootstrapTokenSecretKey: []byte(tc.tokenSecret),
  83. bootstrapapi.BootstrapTokenUsageSigningKey: []byte(tc.okToSign),
  84. bootstrapapi.BootstrapTokenExpirationKey: []byte(tc.expiration),
  85. },
  86. }
  87. tokenID, tokenSecret, ok := validateSecretForSigning(secret)
  88. if ok != tc.valid {
  89. t.Errorf("%s: Unexpected validation failure. Expected %v, got %v", tc.description, tc.valid, ok)
  90. }
  91. if ok {
  92. if tokenID != tc.tokenID {
  93. t.Errorf("%s: Unexpected Token ID. Expected %q, got %q", tc.description, givenTokenID, tokenID)
  94. }
  95. if tokenSecret != tc.tokenSecret {
  96. t.Errorf("%s: Unexpected Token Secret. Expected %q, got %q", tc.description, givenTokenSecret, tokenSecret)
  97. }
  98. }
  99. }
  100. }
  101. func TestValidateSecret(t *testing.T) {
  102. secret := &v1.Secret{
  103. ObjectMeta: metav1.ObjectMeta{
  104. Namespace: metav1.NamespaceSystem,
  105. Name: bootstrapapi.BootstrapTokenSecretPrefix + givenTokenID,
  106. ResourceVersion: "1",
  107. },
  108. Type: bootstrapapi.SecretTypeBootstrapToken,
  109. Data: map[string][]byte{
  110. bootstrapapi.BootstrapTokenIDKey: []byte(givenTokenID),
  111. bootstrapapi.BootstrapTokenSecretKey: []byte(givenTokenSecret),
  112. bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"),
  113. },
  114. }
  115. tokenID, tokenSecret, ok := validateSecretForSigning(secret)
  116. if !ok {
  117. t.Errorf("Unexpected validation failure.")
  118. }
  119. if tokenID != givenTokenID {
  120. t.Errorf("Unexpected Token ID. Expected %q, got %q", givenTokenID, tokenID)
  121. }
  122. if tokenSecret != givenTokenSecret {
  123. t.Errorf("Unexpected Token Secret. Expected %q, got %q", givenTokenSecret, tokenSecret)
  124. }
  125. }
  126. func TestBadSecretName(t *testing.T) {
  127. secret := &v1.Secret{
  128. ObjectMeta: metav1.ObjectMeta{
  129. Namespace: metav1.NamespaceSystem,
  130. Name: givenTokenID,
  131. ResourceVersion: "1",
  132. },
  133. Type: bootstrapapi.SecretTypeBootstrapToken,
  134. Data: map[string][]byte{
  135. bootstrapapi.BootstrapTokenIDKey: []byte(givenTokenID),
  136. bootstrapapi.BootstrapTokenSecretKey: []byte(givenTokenSecret),
  137. bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"),
  138. },
  139. }
  140. _, _, ok := validateSecretForSigning(secret)
  141. if ok {
  142. t.Errorf("Token validation should fail with bad name")
  143. }
  144. }
  145. func TestMismatchSecretName(t *testing.T) {
  146. secret := &v1.Secret{
  147. ObjectMeta: metav1.ObjectMeta{
  148. Namespace: metav1.NamespaceSystem,
  149. Name: bootstrapapi.BootstrapTokenSecretPrefix + givenTokenID2,
  150. ResourceVersion: "1",
  151. },
  152. Type: bootstrapapi.SecretTypeBootstrapToken,
  153. Data: map[string][]byte{
  154. bootstrapapi.BootstrapTokenIDKey: []byte(givenTokenID),
  155. bootstrapapi.BootstrapTokenSecretKey: []byte(givenTokenSecret),
  156. bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"),
  157. },
  158. }
  159. _, _, ok := validateSecretForSigning(secret)
  160. if ok {
  161. t.Errorf("Token validation should fail with mismatched name")
  162. }
  163. }
  164. func TestParseSecretName(t *testing.T) {
  165. tokenID, ok := parseSecretName("bootstrap-token-abc123")
  166. assert.True(t, ok, "parseSecretName should accept valid name")
  167. assert.Equal(t, "abc123", tokenID, "parseSecretName should return token ID")
  168. _, ok = parseSecretName("")
  169. assert.False(t, ok, "parseSecretName should reject blank name")
  170. _, ok = parseSecretName("abc123")
  171. assert.False(t, ok, "parseSecretName should reject with no prefix")
  172. _, ok = parseSecretName("bootstrap-token-")
  173. assert.False(t, ok, "parseSecretName should reject no token ID")
  174. _, ok = parseSecretName("bootstrap-token-abc")
  175. assert.False(t, ok, "parseSecretName should reject short token ID")
  176. _, ok = parseSecretName("bootstrap-token-abc123ghi")
  177. assert.False(t, ok, "parseSecretName should reject long token ID")
  178. _, ok = parseSecretName("bootstrap-token-ABC123")
  179. assert.False(t, ok, "parseSecretName should reject invalid token ID")
  180. }