bootstraptoken_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 auth
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "testing"
  20. "time"
  21. corev1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/labels"
  24. "k8s.io/apiserver/pkg/authentication/request/bearertoken"
  25. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  26. "k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/bootstrap"
  27. bootstraputil "k8s.io/kubernetes/test/e2e/lifecycle/bootstrap"
  28. "k8s.io/kubernetes/test/integration"
  29. "k8s.io/kubernetes/test/integration/framework"
  30. )
  31. type bootstrapSecrets []*corev1.Secret
  32. func (b bootstrapSecrets) List(selector labels.Selector) (ret []*corev1.Secret, err error) {
  33. return b, nil
  34. }
  35. func (b bootstrapSecrets) Get(name string) (*corev1.Secret, error) {
  36. return b[0], nil
  37. }
  38. // TestBootstrapTokenAuth tests the bootstrap token auth provider
  39. func TestBootstrapTokenAuth(t *testing.T) {
  40. tokenID, err := bootstraputil.GenerateTokenID()
  41. if err != nil {
  42. t.Fatalf("unexpected error: %v", err)
  43. }
  44. secret, err := bootstraputil.GenerateTokenSecret()
  45. if err != nil {
  46. t.Fatalf("unexpected error: %v", err)
  47. }
  48. var bootstrapSecretValid = &corev1.Secret{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Namespace: metav1.NamespaceSystem,
  51. Name: bootstrapapi.BootstrapTokenSecretPrefix,
  52. },
  53. Type: corev1.SecretTypeBootstrapToken,
  54. Data: map[string][]byte{
  55. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  56. bootstrapapi.BootstrapTokenSecretKey: []byte(secret),
  57. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  58. },
  59. }
  60. var bootstrapSecretInvalid = &corev1.Secret{
  61. ObjectMeta: metav1.ObjectMeta{
  62. Namespace: metav1.NamespaceSystem,
  63. Name: bootstrapapi.BootstrapTokenSecretPrefix,
  64. },
  65. Type: corev1.SecretTypeBootstrapToken,
  66. Data: map[string][]byte{
  67. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  68. bootstrapapi.BootstrapTokenSecretKey: []byte("invalid"),
  69. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  70. },
  71. }
  72. var expiredBootstrapToken = &corev1.Secret{
  73. ObjectMeta: metav1.ObjectMeta{
  74. Namespace: metav1.NamespaceSystem,
  75. Name: bootstrapapi.BootstrapTokenSecretPrefix,
  76. },
  77. Type: corev1.SecretTypeBootstrapToken,
  78. Data: map[string][]byte{
  79. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  80. bootstrapapi.BootstrapTokenSecretKey: []byte("invalid"),
  81. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  82. bootstrapapi.BootstrapTokenExpirationKey: []byte(bootstraputil.TimeStringFromNow(-time.Hour)),
  83. },
  84. }
  85. type request struct {
  86. verb string
  87. URL string
  88. body string
  89. statusCodes map[int]bool // Set of expected resp.StatusCode if all goes well.
  90. }
  91. tests := []struct {
  92. name string
  93. request request
  94. secret *corev1.Secret
  95. }{
  96. {
  97. name: "valid token",
  98. request: request{verb: "GET", URL: path("pods", "", ""), body: "", statusCodes: integration.Code200},
  99. secret: bootstrapSecretValid,
  100. },
  101. {
  102. name: "invalid token format",
  103. request: request{verb: "GET", URL: path("pods", "", ""), body: "", statusCodes: integration.Code401},
  104. secret: bootstrapSecretInvalid,
  105. },
  106. {
  107. name: "invalid token expired",
  108. request: request{verb: "GET", URL: path("pods", "", ""), body: "", statusCodes: integration.Code401},
  109. secret: expiredBootstrapToken,
  110. },
  111. }
  112. for _, test := range tests {
  113. authenticator := bearertoken.New(bootstrap.NewTokenAuthenticator(bootstrapSecrets{test.secret}))
  114. // Set up a master
  115. masterConfig := framework.NewIntegrationTestMasterConfig()
  116. masterConfig.GenericConfig.Authentication.Authenticator = authenticator
  117. _, s, closeFn := framework.RunAMaster(masterConfig)
  118. defer closeFn()
  119. ns := framework.CreateTestingNamespace("auth-bootstrap-token", s, t)
  120. defer framework.DeleteTestingNamespace(ns, s, t)
  121. previousResourceVersion := make(map[string]float64)
  122. transport := http.DefaultTransport
  123. token := tokenID + "." + secret
  124. var bodyStr string
  125. if test.request.body != "" {
  126. sub := ""
  127. if test.request.verb == "PUT" {
  128. // For update operations, insert previous resource version
  129. if resVersion := previousResourceVersion[getPreviousResourceVersionKey(test.request.URL, "")]; resVersion != 0 {
  130. sub += fmt.Sprintf(",\r\n\"resourceVersion\": \"%v\"", resVersion)
  131. }
  132. sub += fmt.Sprintf(",\r\n\"namespace\": %q", ns.Name)
  133. }
  134. bodyStr = fmt.Sprintf(test.request.body, sub)
  135. }
  136. test.request.body = bodyStr
  137. bodyBytes := bytes.NewReader([]byte(bodyStr))
  138. req, err := http.NewRequest(test.request.verb, s.URL+test.request.URL, bodyBytes)
  139. if err != nil {
  140. t.Fatalf("unexpected error: %v", err)
  141. }
  142. req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
  143. if test.request.verb == "PATCH" {
  144. req.Header.Set("Content-Type", "application/merge-patch+json")
  145. }
  146. func() {
  147. resp, err := transport.RoundTrip(req)
  148. if err != nil {
  149. t.Logf("case %v", test.name)
  150. t.Fatalf("unexpected error: %v", err)
  151. }
  152. defer resp.Body.Close()
  153. b, _ := ioutil.ReadAll(resp.Body)
  154. if _, ok := test.request.statusCodes[resp.StatusCode]; !ok {
  155. t.Logf("case %v", test.name)
  156. t.Errorf("Expected status one of %v, but got %v", test.request.statusCodes, resp.StatusCode)
  157. t.Errorf("Body: %v", string(b))
  158. } else {
  159. if test.request.verb == "POST" {
  160. // For successful create operations, extract resourceVersion
  161. id, currentResourceVersion, err := parseResourceVersion(b)
  162. if err == nil {
  163. key := getPreviousResourceVersionKey(test.request.URL, id)
  164. previousResourceVersion[key] = currentResourceVersion
  165. }
  166. }
  167. }
  168. }()
  169. }
  170. }