set_subject_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 set
  14. import (
  15. "reflect"
  16. "testing"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/cli-runtime/pkg/resource"
  21. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  22. )
  23. func TestValidate(t *testing.T) {
  24. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  25. defer tf.Cleanup()
  26. tests := map[string]struct {
  27. options *SubjectOptions
  28. expectErr bool
  29. }{
  30. "test-missing-subjects": {
  31. options: &SubjectOptions{
  32. Users: []string{},
  33. Groups: []string{},
  34. ServiceAccounts: []string{},
  35. },
  36. expectErr: true,
  37. },
  38. "test-invalid-serviceaccounts": {
  39. options: &SubjectOptions{
  40. Users: []string{},
  41. Groups: []string{},
  42. ServiceAccounts: []string{"foo"},
  43. },
  44. expectErr: true,
  45. },
  46. "test-missing-serviceaccounts-name": {
  47. options: &SubjectOptions{
  48. Users: []string{},
  49. Groups: []string{},
  50. ServiceAccounts: []string{"foo:"},
  51. },
  52. expectErr: true,
  53. },
  54. "test-missing-serviceaccounts-namespace": {
  55. options: &SubjectOptions{
  56. Infos: []*resource.Info{
  57. {
  58. Object: &rbacv1.ClusterRoleBinding{
  59. ObjectMeta: metav1.ObjectMeta{
  60. Name: "clusterrolebinding",
  61. },
  62. RoleRef: rbacv1.RoleRef{
  63. APIGroup: "rbac.authorization.k8s.io",
  64. Kind: "ClusterRole",
  65. Name: "role",
  66. },
  67. },
  68. },
  69. },
  70. Users: []string{},
  71. Groups: []string{},
  72. ServiceAccounts: []string{":foo"},
  73. },
  74. expectErr: true,
  75. },
  76. "test-valid-case": {
  77. options: &SubjectOptions{
  78. Infos: []*resource.Info{
  79. {
  80. Object: &rbacv1.RoleBinding{
  81. ObjectMeta: metav1.ObjectMeta{
  82. Name: "rolebinding",
  83. Namespace: "one",
  84. },
  85. RoleRef: rbacv1.RoleRef{
  86. APIGroup: "rbac.authorization.k8s.io",
  87. Kind: "ClusterRole",
  88. Name: "role",
  89. },
  90. },
  91. },
  92. },
  93. Users: []string{"foo"},
  94. Groups: []string{"foo"},
  95. ServiceAccounts: []string{"ns:foo"},
  96. },
  97. expectErr: false,
  98. },
  99. }
  100. for name, test := range tests {
  101. err := test.options.Validate()
  102. if test.expectErr && err != nil {
  103. continue
  104. }
  105. if !test.expectErr && err != nil {
  106. t.Errorf("%s: unexpected error: %v", name, err)
  107. }
  108. }
  109. }
  110. func TestUpdateSubjectForObject(t *testing.T) {
  111. tests := []struct {
  112. Name string
  113. obj runtime.Object
  114. subjects []rbacv1.Subject
  115. expected []rbacv1.Subject
  116. wantErr bool
  117. }{
  118. {
  119. Name: "invalid object type",
  120. obj: &rbacv1.Role{
  121. ObjectMeta: metav1.ObjectMeta{
  122. Name: "role",
  123. Namespace: "one",
  124. },
  125. },
  126. wantErr: true,
  127. },
  128. {
  129. Name: "add resource with users in rolebinding",
  130. obj: &rbacv1.RoleBinding{
  131. ObjectMeta: metav1.ObjectMeta{
  132. Name: "rolebinding",
  133. Namespace: "one",
  134. },
  135. Subjects: []rbacv1.Subject{
  136. {
  137. APIGroup: "rbac.authorization.k8s.io",
  138. Kind: "User",
  139. Name: "a",
  140. },
  141. },
  142. },
  143. subjects: []rbacv1.Subject{
  144. {
  145. APIGroup: "rbac.authorization.k8s.io",
  146. Kind: "User",
  147. Name: "a",
  148. },
  149. {
  150. APIGroup: "rbac.authorization.k8s.io",
  151. Kind: "User",
  152. Name: "b",
  153. },
  154. },
  155. expected: []rbacv1.Subject{
  156. {
  157. APIGroup: "rbac.authorization.k8s.io",
  158. Kind: "User",
  159. Name: "a",
  160. },
  161. {
  162. APIGroup: "rbac.authorization.k8s.io",
  163. Kind: "User",
  164. Name: "b",
  165. },
  166. },
  167. wantErr: false,
  168. },
  169. {
  170. Name: "add resource with groups in rolebinding",
  171. obj: &rbacv1.RoleBinding{
  172. ObjectMeta: metav1.ObjectMeta{
  173. Name: "rolebinding",
  174. Namespace: "one",
  175. },
  176. Subjects: []rbacv1.Subject{
  177. {
  178. APIGroup: "rbac.authorization.k8s.io",
  179. Kind: "Group",
  180. Name: "a",
  181. },
  182. },
  183. },
  184. subjects: []rbacv1.Subject{
  185. {
  186. APIGroup: "rbac.authorization.k8s.io",
  187. Kind: "Group",
  188. Name: "a",
  189. },
  190. {
  191. APIGroup: "rbac.authorization.k8s.io",
  192. Kind: "Group",
  193. Name: "b",
  194. },
  195. },
  196. expected: []rbacv1.Subject{
  197. {
  198. APIGroup: "rbac.authorization.k8s.io",
  199. Kind: "Group",
  200. Name: "a",
  201. },
  202. {
  203. APIGroup: "rbac.authorization.k8s.io",
  204. Kind: "Group",
  205. Name: "b",
  206. },
  207. },
  208. wantErr: false,
  209. },
  210. {
  211. Name: "add resource with serviceaccounts in rolebinding",
  212. obj: &rbacv1.RoleBinding{
  213. ObjectMeta: metav1.ObjectMeta{
  214. Name: "rolebinding",
  215. Namespace: "one",
  216. },
  217. Subjects: []rbacv1.Subject{
  218. {
  219. Kind: "ServiceAccount",
  220. Namespace: "one",
  221. Name: "a",
  222. },
  223. },
  224. },
  225. subjects: []rbacv1.Subject{
  226. {
  227. Kind: "ServiceAccount",
  228. Namespace: "one",
  229. Name: "a",
  230. },
  231. {
  232. Kind: "ServiceAccount",
  233. Namespace: "one",
  234. Name: "b",
  235. },
  236. },
  237. expected: []rbacv1.Subject{
  238. {
  239. Kind: "ServiceAccount",
  240. Namespace: "one",
  241. Name: "a",
  242. },
  243. {
  244. Kind: "ServiceAccount",
  245. Namespace: "one",
  246. Name: "b",
  247. },
  248. },
  249. wantErr: false,
  250. },
  251. {
  252. Name: "add resource with serviceaccounts in clusterrolebinding",
  253. obj: &rbacv1.ClusterRoleBinding{
  254. ObjectMeta: metav1.ObjectMeta{
  255. Name: "clusterrolebinding",
  256. },
  257. Subjects: []rbacv1.Subject{
  258. {
  259. APIGroup: "rbac.authorization.k8s.io",
  260. Kind: "User",
  261. Name: "a",
  262. },
  263. {
  264. APIGroup: "rbac.authorization.k8s.io",
  265. Kind: "Group",
  266. Name: "a",
  267. },
  268. },
  269. },
  270. subjects: []rbacv1.Subject{
  271. {
  272. Kind: "ServiceAccount",
  273. Namespace: "one",
  274. Name: "a",
  275. },
  276. },
  277. expected: []rbacv1.Subject{
  278. {
  279. APIGroup: "rbac.authorization.k8s.io",
  280. Kind: "User",
  281. Name: "a",
  282. },
  283. {
  284. APIGroup: "rbac.authorization.k8s.io",
  285. Kind: "Group",
  286. Name: "a",
  287. },
  288. {
  289. Kind: "ServiceAccount",
  290. Namespace: "one",
  291. Name: "a",
  292. },
  293. },
  294. wantErr: false,
  295. },
  296. }
  297. for _, tt := range tests {
  298. if _, err := updateSubjectForObject(tt.obj, tt.subjects, addSubjects); (err != nil) != tt.wantErr {
  299. t.Errorf("%q. updateSubjectForObject() error = %v, wantErr %v", tt.Name, err, tt.wantErr)
  300. }
  301. want := tt.expected
  302. var got []rbacv1.Subject
  303. switch t := tt.obj.(type) {
  304. case *rbacv1.RoleBinding:
  305. got = t.Subjects
  306. case *rbacv1.ClusterRoleBinding:
  307. got = t.Subjects
  308. }
  309. if !reflect.DeepEqual(got, want) {
  310. t.Errorf("%q. updateSubjectForObject() failed", tt.Name)
  311. t.Errorf("Got: %v", got)
  312. t.Errorf("Want: %v", want)
  313. }
  314. }
  315. }
  316. func TestAddSubject(t *testing.T) {
  317. tests := []struct {
  318. Name string
  319. existing []rbacv1.Subject
  320. subjects []rbacv1.Subject
  321. expected []rbacv1.Subject
  322. wantChange bool
  323. }{
  324. {
  325. Name: "add resource with users",
  326. existing: []rbacv1.Subject{
  327. {
  328. APIGroup: "rbac.authorization.k8s.io",
  329. Kind: "User",
  330. Name: "a",
  331. },
  332. {
  333. APIGroup: "rbac.authorization.k8s.io",
  334. Kind: "User",
  335. Name: "b",
  336. },
  337. },
  338. subjects: []rbacv1.Subject{
  339. {
  340. APIGroup: "rbac.authorization.k8s.io",
  341. Kind: "User",
  342. Name: "a",
  343. },
  344. },
  345. expected: []rbacv1.Subject{
  346. {
  347. APIGroup: "rbac.authorization.k8s.io",
  348. Kind: "User",
  349. Name: "a",
  350. },
  351. {
  352. APIGroup: "rbac.authorization.k8s.io",
  353. Kind: "User",
  354. Name: "b",
  355. },
  356. },
  357. wantChange: false,
  358. },
  359. {
  360. Name: "add resource with serviceaccounts",
  361. existing: []rbacv1.Subject{
  362. {
  363. Kind: "ServiceAccount",
  364. Namespace: "one",
  365. Name: "a",
  366. },
  367. {
  368. Kind: "ServiceAccount",
  369. Namespace: "one",
  370. Name: "b",
  371. },
  372. },
  373. subjects: []rbacv1.Subject{
  374. {
  375. Kind: "ServiceAccount",
  376. Namespace: "two",
  377. Name: "a",
  378. },
  379. },
  380. expected: []rbacv1.Subject{
  381. {
  382. Kind: "ServiceAccount",
  383. Namespace: "one",
  384. Name: "a",
  385. },
  386. {
  387. Kind: "ServiceAccount",
  388. Namespace: "one",
  389. Name: "b",
  390. },
  391. {
  392. Kind: "ServiceAccount",
  393. Namespace: "two",
  394. Name: "a",
  395. },
  396. },
  397. wantChange: true,
  398. },
  399. }
  400. for _, tt := range tests {
  401. changed := false
  402. got := []rbacv1.Subject{}
  403. if changed, got = addSubjects(tt.existing, tt.subjects); (changed != false) != tt.wantChange {
  404. t.Errorf("%q. addSubjects() changed = %v, wantChange = %v", tt.Name, changed, tt.wantChange)
  405. }
  406. want := tt.expected
  407. if !reflect.DeepEqual(got, want) {
  408. t.Errorf("%q. addSubjects() failed", tt.Name)
  409. t.Errorf("Got: %v", got)
  410. t.Errorf("Want: %v", want)
  411. }
  412. }
  413. }