bootstrap_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. tokenID = "foobar" // 6 letters
  42. tokenSecret = "circumnavigation" // 16 letters
  43. )
  44. func TestTokenAuthenticator(t *testing.T) {
  45. now := metav1.Now()
  46. tests := []struct {
  47. name string
  48. secrets []*corev1.Secret
  49. token string
  50. wantNotFound bool
  51. wantUser *user.DefaultInfo
  52. }{
  53. {
  54. name: "valid token",
  55. secrets: []*corev1.Secret{
  56. {
  57. ObjectMeta: metav1.ObjectMeta{
  58. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  59. },
  60. Data: map[string][]byte{
  61. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  62. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  63. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  64. },
  65. Type: "bootstrap.kubernetes.io/token",
  66. },
  67. },
  68. token: tokenID + "." + tokenSecret,
  69. wantUser: &user.DefaultInfo{
  70. Name: "system:bootstrap:" + tokenID,
  71. Groups: []string{"system:bootstrappers"},
  72. },
  73. },
  74. {
  75. name: "valid token with extra group",
  76. secrets: []*corev1.Secret{
  77. {
  78. ObjectMeta: metav1.ObjectMeta{
  79. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  80. },
  81. Data: map[string][]byte{
  82. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  83. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  84. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  85. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:foo"),
  86. },
  87. Type: "bootstrap.kubernetes.io/token",
  88. },
  89. },
  90. token: tokenID + "." + tokenSecret,
  91. wantUser: &user.DefaultInfo{
  92. Name: "system:bootstrap:" + tokenID,
  93. Groups: []string{"system:bootstrappers", "system:bootstrappers:foo"},
  94. },
  95. },
  96. {
  97. name: "invalid group",
  98. secrets: []*corev1.Secret{
  99. {
  100. ObjectMeta: metav1.ObjectMeta{
  101. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  102. },
  103. Data: map[string][]byte{
  104. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  105. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  106. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  107. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("foo"),
  108. },
  109. Type: "bootstrap.kubernetes.io/token",
  110. },
  111. },
  112. token: tokenID + "." + tokenSecret,
  113. wantNotFound: true,
  114. },
  115. {
  116. name: "invalid secret name",
  117. secrets: []*corev1.Secret{
  118. {
  119. ObjectMeta: metav1.ObjectMeta{
  120. Name: "bad-name",
  121. },
  122. Data: map[string][]byte{
  123. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  124. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  125. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  126. },
  127. Type: "bootstrap.kubernetes.io/token",
  128. },
  129. },
  130. token: tokenID + "." + tokenSecret,
  131. wantNotFound: true,
  132. },
  133. {
  134. name: "no usage",
  135. secrets: []*corev1.Secret{
  136. {
  137. ObjectMeta: metav1.ObjectMeta{
  138. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  139. },
  140. Data: map[string][]byte{
  141. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  142. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  143. },
  144. Type: "bootstrap.kubernetes.io/token",
  145. },
  146. },
  147. token: tokenID + "." + tokenSecret,
  148. wantNotFound: true,
  149. },
  150. {
  151. name: "wrong token",
  152. secrets: []*corev1.Secret{
  153. {
  154. ObjectMeta: metav1.ObjectMeta{
  155. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  156. },
  157. Data: map[string][]byte{
  158. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  159. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  160. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  161. },
  162. Type: "bootstrap.kubernetes.io/token",
  163. },
  164. },
  165. token: "barfoo" + "." + tokenSecret,
  166. wantNotFound: true,
  167. },
  168. {
  169. name: "deleted token",
  170. secrets: []*corev1.Secret{
  171. {
  172. ObjectMeta: metav1.ObjectMeta{
  173. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  174. DeletionTimestamp: &now,
  175. },
  176. Data: map[string][]byte{
  177. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  178. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  179. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  180. },
  181. Type: "bootstrap.kubernetes.io/token",
  182. },
  183. },
  184. token: tokenID + "." + tokenSecret,
  185. wantNotFound: true,
  186. },
  187. {
  188. name: "expired token",
  189. secrets: []*corev1.Secret{
  190. {
  191. ObjectMeta: metav1.ObjectMeta{
  192. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  193. },
  194. Data: map[string][]byte{
  195. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  196. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  197. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  198. bootstrapapi.BootstrapTokenExpirationKey: []byte("2009-11-10T23:00:00Z"),
  199. },
  200. Type: "bootstrap.kubernetes.io/token",
  201. },
  202. },
  203. token: tokenID + "." + tokenSecret,
  204. wantNotFound: true,
  205. },
  206. {
  207. name: "not expired token",
  208. secrets: []*corev1.Secret{
  209. {
  210. ObjectMeta: metav1.ObjectMeta{
  211. Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
  212. },
  213. Data: map[string][]byte{
  214. bootstrapapi.BootstrapTokenIDKey: []byte(tokenID),
  215. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  216. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  217. bootstrapapi.BootstrapTokenExpirationKey: []byte("2109-11-10T23:00:00Z"),
  218. },
  219. Type: "bootstrap.kubernetes.io/token",
  220. },
  221. },
  222. token: tokenID + "." + tokenSecret,
  223. wantUser: &user.DefaultInfo{
  224. Name: "system:bootstrap:" + tokenID,
  225. Groups: []string{"system:bootstrappers"},
  226. },
  227. },
  228. {
  229. name: "token id wrong length",
  230. secrets: []*corev1.Secret{
  231. {
  232. ObjectMeta: metav1.ObjectMeta{
  233. Name: bootstrapapi.BootstrapTokenSecretPrefix + "foo",
  234. },
  235. Data: map[string][]byte{
  236. bootstrapapi.BootstrapTokenIDKey: []byte("foo"),
  237. bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret),
  238. bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"),
  239. },
  240. Type: "bootstrap.kubernetes.io/token",
  241. },
  242. },
  243. // Token ID must be 6 characters.
  244. token: "foo" + "." + tokenSecret,
  245. wantNotFound: true,
  246. },
  247. }
  248. for _, test := range tests {
  249. func() {
  250. a := NewTokenAuthenticator(&lister{test.secrets})
  251. resp, found, err := a.AuthenticateToken(context.Background(), test.token)
  252. if err != nil {
  253. t.Errorf("test %q returned an error: %v", test.name, err)
  254. return
  255. }
  256. if !found {
  257. if !test.wantNotFound {
  258. t.Errorf("test %q expected to get user", test.name)
  259. }
  260. return
  261. }
  262. if test.wantNotFound {
  263. t.Errorf("test %q expected to not get a user", test.name)
  264. return
  265. }
  266. gotUser := resp.User.(*user.DefaultInfo)
  267. if !reflect.DeepEqual(gotUser, test.wantUser) {
  268. t.Errorf("test %q want user=%#v, got=%#v", test.name, test.wantUser, gotUser)
  269. }
  270. }()
  271. }
  272. }
  273. func TestGetGroups(t *testing.T) {
  274. tests := []struct {
  275. name string
  276. secret *corev1.Secret
  277. expectResult []string
  278. expectError bool
  279. }{
  280. {
  281. name: "not set",
  282. secret: &corev1.Secret{
  283. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  284. Data: map[string][]byte{},
  285. },
  286. expectResult: []string{"system:bootstrappers"},
  287. },
  288. {
  289. name: "set to empty value",
  290. secret: &corev1.Secret{
  291. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  292. Data: map[string][]byte{
  293. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte(""),
  294. },
  295. },
  296. expectResult: []string{"system:bootstrappers"},
  297. },
  298. {
  299. name: "invalid prefix",
  300. secret: &corev1.Secret{
  301. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  302. Data: map[string][]byte{
  303. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("foo"),
  304. },
  305. },
  306. expectError: true,
  307. },
  308. {
  309. name: "valid",
  310. secret: &corev1.Secret{
  311. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  312. Data: map[string][]byte{
  313. bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:foo,system:bootstrappers:bar,system:bootstrappers:bar"),
  314. },
  315. },
  316. // expect the results in deduplicated, sorted order
  317. expectResult: []string{
  318. "system:bootstrappers",
  319. "system:bootstrappers:bar",
  320. "system:bootstrappers:foo",
  321. },
  322. },
  323. }
  324. for _, test := range tests {
  325. result, err := getGroups(test.secret)
  326. if test.expectError {
  327. if err == nil {
  328. t.Errorf("test %q expected an error, but didn't get one (result: %#v)", test.name, result)
  329. }
  330. continue
  331. }
  332. if err != nil {
  333. t.Errorf("test %q return an unexpected error: %v", test.name, err)
  334. continue
  335. }
  336. if !reflect.DeepEqual(result, test.expectResult) {
  337. t.Errorf("test %q expected %#v, got %#v", test.name, test.expectResult, result)
  338. }
  339. }
  340. }