bootstrap_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. "context"
  16. "reflect"
  17. "testing"
  18. corev1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/labels"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. "k8s.io/apiserver/pkg/authentication/user"
  24. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  25. )
  26. type lister struct {
  27. secrets []*corev1.Secret
  28. }
  29. func (l *lister) List(selector labels.Selector) (ret []*corev1.Secret, err error) {
  30. return l.secrets, nil
  31. }
  32. func (l *lister) Get(name string) (*corev1.Secret, error) {
  33. for _, s := range l.secrets {
  34. if s.Name == name {
  35. return s, nil
  36. }
  37. }
  38. return nil, errors.NewNotFound(schema.GroupResource{}, name)
  39. }
  40. const (
  41. // Fake values for testing.
  42. tokenID = "foobar" // 6 letters
  43. tokenSecret = "circumnavigation" // 16 letters
  44. )
  45. func TestTokenAuthenticator(t *testing.T) {
  46. now := metav1.Now()
  47. tests := []struct {
  48. name string
  49. secrets []*corev1.Secret
  50. token string
  51. wantNotFound bool
  52. wantUser *user.DefaultInfo
  53. }{
  54. {
  55. name: "valid token",
  56. secrets: []*corev1.Secret{
  57. {
  58. ObjectMeta: metav1.ObjectMeta{
  59. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  60. },
  61. Data: map[string][]byte{
  62. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  63. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  64. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  65. },
  66. Type: "bootstrap.kubernetes.io/token",
  67. },
  68. },
  69. token: tokenID + "." + tokenSecret,
  70. wantUser: &user.DefaultInfo{
  71. Name: "system:bootstrap:" + tokenID,
  72. Groups: []string{"system:bootstrappers"},
  73. },
  74. },
  75. {
  76. name: "valid token with extra group",
  77. secrets: []*corev1.Secret{
  78. {
  79. ObjectMeta: metav1.ObjectMeta{
  80. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  81. },
  82. Data: map[string][]byte{
  83. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  84. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  85. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  86. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:foo"),
  87. },
  88. Type: "bootstrap.kubernetes.io/token",
  89. },
  90. },
  91. token: tokenID + "." + tokenSecret,
  92. wantUser: &user.DefaultInfo{
  93. Name: "system:bootstrap:" + tokenID,
  94. Groups: []string{"system:bootstrappers", "system:bootstrappers:foo"},
  95. },
  96. },
  97. {
  98. name: "invalid group",
  99. secrets: []*corev1.Secret{
  100. {
  101. ObjectMeta: metav1.ObjectMeta{
  102. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  103. },
  104. Data: map[string][]byte{
  105. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  106. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  107. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  108. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("foo"),
  109. },
  110. Type: "bootstrap.kubernetes.io/token",
  111. },
  112. },
  113. token: tokenID + "." + tokenSecret,
  114. wantNotFound: true,
  115. },
  116. {
  117. name: "invalid secret name",
  118. secrets: []*corev1.Secret{
  119. {
  120. ObjectMeta: metav1.ObjectMeta{
  121. Name: "bad-name",
  122. },
  123. Data: map[string][]byte{
  124. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  125. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  126. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  127. },
  128. Type: "bootstrap.kubernetes.io/token",
  129. },
  130. },
  131. token: tokenID + "." + tokenSecret,
  132. wantNotFound: true,
  133. },
  134. {
  135. name: "no usage",
  136. secrets: []*corev1.Secret{
  137. {
  138. ObjectMeta: metav1.ObjectMeta{
  139. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  140. },
  141. Data: map[string][]byte{
  142. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  143. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  144. },
  145. Type: "bootstrap.kubernetes.io/token",
  146. },
  147. },
  148. token: tokenID + "." + tokenSecret,
  149. wantNotFound: true,
  150. },
  151. {
  152. name: "wrong token",
  153. secrets: []*corev1.Secret{
  154. {
  155. ObjectMeta: metav1.ObjectMeta{
  156. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  157. },
  158. Data: map[string][]byte{
  159. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  160. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  161. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  162. },
  163. Type: "bootstrap.kubernetes.io/token",
  164. },
  165. },
  166. token: "barfoo" + "." + tokenSecret,
  167. wantNotFound: true,
  168. },
  169. {
  170. name: "deleted token",
  171. secrets: []*corev1.Secret{
  172. {
  173. ObjectMeta: metav1.ObjectMeta{
  174. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  175. DeletionTimestamp: &now,
  176. },
  177. Data: map[string][]byte{
  178. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  179. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  180. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  181. },
  182. Type: "bootstrap.kubernetes.io/token",
  183. },
  184. },
  185. token: tokenID + "." + tokenSecret,
  186. wantNotFound: true,
  187. },
  188. {
  189. name: "expired token",
  190. secrets: []*corev1.Secret{
  191. {
  192. ObjectMeta: metav1.ObjectMeta{
  193. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  194. },
  195. Data: map[string][]byte{
  196. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  197. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  198. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  199. bootstrapapi.BootstrapTokenExpirationKey: []byte("2009-11-10T23:00:00Z"),
  200. },
  201. Type: "bootstrap.kubernetes.io/token",
  202. },
  203. },
  204. token: tokenID + "." + tokenSecret,
  205. wantNotFound: true,
  206. },
  207. {
  208. name: "not expired token",
  209. secrets: []*corev1.Secret{
  210. {
  211. ObjectMeta: metav1.ObjectMeta{
  212. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  213. },
  214. Data: map[string][]byte{
  215. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  216. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  217. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  218. bootstrapapi.BootstrapTokenExpirationKey: []byte("2109-11-10T23:00:00Z"),
  219. },
  220. Type: "bootstrap.kubernetes.io/token",
  221. },
  222. },
  223. token: tokenID + "." + tokenSecret,
  224. wantUser: &user.DefaultInfo{
  225. Name: "system:bootstrap:" + tokenID,
  226. Groups: []string{"system:bootstrappers"},
  227. },
  228. },
  229. {
  230. name: "token id wrong length",
  231. secrets: []*corev1.Secret{
  232. {
  233. ObjectMeta: metav1.ObjectMeta{
  234. Name: bootstrapapi.BootstrapTokenSecretPrefix + "foo",
  235. },
  236. Data: map[string][]byte{
  237. bootstrapapi.BootstrapTokenIDKey: []byte("foo"),
  238. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  239. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  240. },
  241. Type: "bootstrap.kubernetes.io/token",
  242. },
  243. },
  244. // Token ID must be 6 characters.
  245. token: "foo" + "." + tokenSecret,
  246. wantNotFound: true,
  247. },
  248. }
  249. for _, test := range tests {
  250. func() {
  251. a := NewTokenAuthenticator(&lister{test.secrets})
  252. resp, found, err := a.AuthenticateToken(context.Background(), test.token)
  253. if err != nil {
  254. t.Errorf("test %q returned an error: %v", test.name, err)
  255. return
  256. }
  257. if !found {
  258. if !test.wantNotFound {
  259. t.Errorf("test %q expected to get user", test.name)
  260. }
  261. return
  262. }
  263. if test.wantNotFound {
  264. t.Errorf("test %q expected to not get a user", test.name)
  265. return
  266. }
  267. gotUser := resp.User.(*user.DefaultInfo)
  268. if !reflect.DeepEqual(gotUser, test.wantUser) {
  269. t.Errorf("test %q want user=%#v, got=%#v", test.name, test.wantUser, gotUser)
  270. }
  271. }()
  272. }
  273. }