util_test.go 5.1 KB

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