rulevalidation_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 validation
  14. import (
  15. "hash/fnv"
  16. "io"
  17. "reflect"
  18. "sort"
  19. "testing"
  20. "k8s.io/kubernetes/pkg/api"
  21. "k8s.io/kubernetes/pkg/apis/rbac"
  22. "k8s.io/kubernetes/pkg/auth/user"
  23. "k8s.io/kubernetes/pkg/util/diff"
  24. )
  25. // compute a hash of a policy rule so we can sort in a deterministic order
  26. func hashOf(p rbac.PolicyRule) string {
  27. hash := fnv.New32()
  28. writeStrings := func(slis ...[]string) {
  29. for _, sli := range slis {
  30. for _, s := range sli {
  31. io.WriteString(hash, s)
  32. }
  33. }
  34. }
  35. writeStrings(p.Verbs, p.APIGroups, p.Resources, p.ResourceNames, p.NonResourceURLs)
  36. return string(hash.Sum(nil))
  37. }
  38. // byHash sorts a set of policy rules by a hash of its fields
  39. type byHash []rbac.PolicyRule
  40. func (b byHash) Len() int { return len(b) }
  41. func (b byHash) Less(i, j int) bool { return hashOf(b[i]) < hashOf(b[j]) }
  42. func (b byHash) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  43. func TestDefaultRuleResolver(t *testing.T) {
  44. ruleReadPods := rbac.PolicyRule{
  45. Verbs: []string{"GET", "WATCH"},
  46. APIGroups: []string{"v1"},
  47. Resources: []string{"pods"},
  48. }
  49. ruleReadServices := rbac.PolicyRule{
  50. Verbs: []string{"GET", "WATCH"},
  51. APIGroups: []string{"v1"},
  52. Resources: []string{"services"},
  53. }
  54. ruleWriteNodes := rbac.PolicyRule{
  55. Verbs: []string{"PUT", "CREATE", "UPDATE"},
  56. APIGroups: []string{"v1"},
  57. Resources: []string{"nodes"},
  58. }
  59. ruleAdmin := rbac.PolicyRule{
  60. Verbs: []string{"*"},
  61. APIGroups: []string{"*"},
  62. Resources: []string{"*"},
  63. }
  64. staticRoles1 := staticRoles{
  65. roles: []rbac.Role{
  66. {
  67. ObjectMeta: api.ObjectMeta{Namespace: "namespace1", Name: "readthings"},
  68. Rules: []rbac.PolicyRule{ruleReadPods, ruleReadServices},
  69. },
  70. },
  71. clusterRoles: []rbac.ClusterRole{
  72. {
  73. ObjectMeta: api.ObjectMeta{Name: "cluster-admin"},
  74. Rules: []rbac.PolicyRule{ruleAdmin},
  75. },
  76. {
  77. ObjectMeta: api.ObjectMeta{Name: "write-nodes"},
  78. Rules: []rbac.PolicyRule{ruleWriteNodes},
  79. },
  80. },
  81. roleBindings: []rbac.RoleBinding{
  82. {
  83. ObjectMeta: api.ObjectMeta{Namespace: "namespace1"},
  84. Subjects: []rbac.Subject{
  85. {Kind: rbac.UserKind, Name: "foobar"},
  86. {Kind: rbac.GroupKind, Name: "group1"},
  87. },
  88. RoleRef: api.ObjectReference{Kind: "Role", Namespace: "namespace1", Name: "readthings"},
  89. },
  90. },
  91. clusterRoleBindings: []rbac.ClusterRoleBinding{
  92. {
  93. Subjects: []rbac.Subject{
  94. {Kind: rbac.UserKind, Name: "admin"},
  95. {Kind: rbac.GroupKind, Name: "admin"},
  96. },
  97. RoleRef: api.ObjectReference{Kind: "ClusterRole", Name: "cluster-admin"},
  98. },
  99. },
  100. }
  101. tests := []struct {
  102. staticRoles
  103. // For a given context, what are the rules that apply?
  104. ctx api.Context
  105. effectiveRules []rbac.PolicyRule
  106. }{
  107. {
  108. staticRoles: staticRoles1,
  109. ctx: api.WithNamespace(
  110. api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "foobar"}), "namespace1",
  111. ),
  112. effectiveRules: []rbac.PolicyRule{ruleReadPods, ruleReadServices},
  113. },
  114. {
  115. staticRoles: staticRoles1,
  116. ctx: api.WithNamespace(
  117. // Same as above but diffrerent namespace. Should return no rules.
  118. api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "foobar"}), "namespace2",
  119. ),
  120. effectiveRules: []rbac.PolicyRule{},
  121. },
  122. {
  123. staticRoles: staticRoles1,
  124. // GetEffectivePolicyRules only returns the policies for the namespace, not the master namespace.
  125. ctx: api.WithNamespace(
  126. api.WithUser(api.NewContext(), &user.DefaultInfo{
  127. Name: "foobar", Groups: []string{"admin"},
  128. }), "namespace1",
  129. ),
  130. effectiveRules: []rbac.PolicyRule{ruleReadPods, ruleReadServices},
  131. },
  132. {
  133. staticRoles: staticRoles1,
  134. // Same as above but without a namespace. Only cluster rules should apply.
  135. ctx: api.WithUser(api.NewContext(), &user.DefaultInfo{
  136. Name: "foobar", Groups: []string{"admin"},
  137. }),
  138. effectiveRules: []rbac.PolicyRule{ruleAdmin},
  139. },
  140. {
  141. staticRoles: staticRoles1,
  142. ctx: api.WithUser(api.NewContext(), &user.DefaultInfo{}),
  143. effectiveRules: []rbac.PolicyRule{},
  144. },
  145. }
  146. for i, tc := range tests {
  147. ruleResolver := newMockRuleResolver(&tc.staticRoles)
  148. rules, err := ruleResolver.GetEffectivePolicyRules(tc.ctx)
  149. if err != nil {
  150. t.Errorf("case %d: GetEffectivePolicyRules(context)=%v", i, err)
  151. continue
  152. }
  153. // Sort for deep equals
  154. sort.Sort(byHash(rules))
  155. sort.Sort(byHash(tc.effectiveRules))
  156. if !reflect.DeepEqual(rules, tc.effectiveRules) {
  157. ruleDiff := diff.ObjectDiff(rules, tc.effectiveRules)
  158. t.Errorf("case %d: %s", i, ruleDiff)
  159. }
  160. }
  161. }
  162. func TestAppliesTo(t *testing.T) {
  163. tests := []struct {
  164. subjects []rbac.Subject
  165. ctx api.Context
  166. appliesTo bool
  167. testCase string
  168. }{
  169. {
  170. subjects: []rbac.Subject{
  171. {Kind: rbac.UserKind, Name: "foobar"},
  172. },
  173. ctx: api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "foobar"}),
  174. appliesTo: true,
  175. testCase: "single subject that matches username",
  176. },
  177. {
  178. subjects: []rbac.Subject{
  179. {Kind: rbac.UserKind, Name: "barfoo"},
  180. {Kind: rbac.UserKind, Name: "foobar"},
  181. },
  182. ctx: api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "foobar"}),
  183. appliesTo: true,
  184. testCase: "multiple subjects, one that matches username",
  185. },
  186. {
  187. subjects: []rbac.Subject{
  188. {Kind: rbac.UserKind, Name: "barfoo"},
  189. {Kind: rbac.UserKind, Name: "foobar"},
  190. },
  191. ctx: api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "zimzam"}),
  192. appliesTo: false,
  193. testCase: "multiple subjects, none that match username",
  194. },
  195. {
  196. subjects: []rbac.Subject{
  197. {Kind: rbac.UserKind, Name: "barfoo"},
  198. {Kind: rbac.GroupKind, Name: "foobar"},
  199. },
  200. ctx: api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "zimzam", Groups: []string{"foobar"}}),
  201. appliesTo: true,
  202. testCase: "multiple subjects, one that match group",
  203. },
  204. {
  205. subjects: []rbac.Subject{
  206. {Kind: rbac.UserKind, Name: "barfoo"},
  207. {Kind: rbac.GroupKind, Name: "foobar"},
  208. },
  209. ctx: api.WithNamespace(
  210. api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "zimzam", Groups: []string{"foobar"}}),
  211. "namespace1",
  212. ),
  213. appliesTo: true,
  214. testCase: "multiple subjects, one that match group, should ignore namespace",
  215. },
  216. {
  217. subjects: []rbac.Subject{
  218. {Kind: rbac.UserKind, Name: "barfoo"},
  219. {Kind: rbac.GroupKind, Name: "foobar"},
  220. {Kind: rbac.ServiceAccountKind, Namespace: "kube-system", Name: "default"},
  221. },
  222. ctx: api.WithNamespace(
  223. api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "system:serviceaccount:kube-system:default"}),
  224. "default",
  225. ),
  226. appliesTo: true,
  227. testCase: "multiple subjects with a service account that matches",
  228. },
  229. {
  230. subjects: []rbac.Subject{
  231. {Kind: rbac.UserKind, Name: "*"},
  232. },
  233. ctx: api.WithNamespace(
  234. api.WithUser(api.NewContext(), &user.DefaultInfo{Name: "foobar"}),
  235. "default",
  236. ),
  237. appliesTo: true,
  238. testCase: "multiple subjects with a service account that matches",
  239. },
  240. }
  241. for _, tc := range tests {
  242. got, err := appliesTo(tc.ctx, tc.subjects)
  243. if err != nil {
  244. t.Errorf("case %q %v", tc.testCase, err)
  245. continue
  246. }
  247. if got != tc.appliesTo {
  248. t.Errorf("case %q want appliesTo=%t, got appliesTo=%t", tc.testCase, tc.appliesTo, got)
  249. }
  250. }
  251. }