rule_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. rbacv1 "k8s.io/api/rbac/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/util/diff"
  23. "k8s.io/apiserver/pkg/authentication/user"
  24. )
  25. // compute a hash of a policy rule so we can sort in a deterministic order
  26. func hashOf(p rbacv1.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 []rbacv1.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 := rbacv1.PolicyRule{
  45. Verbs: []string{"GET", "WATCH"},
  46. APIGroups: []string{"v1"},
  47. Resources: []string{"pods"},
  48. }
  49. ruleReadServices := rbacv1.PolicyRule{
  50. Verbs: []string{"GET", "WATCH"},
  51. APIGroups: []string{"v1"},
  52. Resources: []string{"services"},
  53. }
  54. ruleWriteNodes := rbacv1.PolicyRule{
  55. Verbs: []string{"PUT", "CREATE", "UPDATE"},
  56. APIGroups: []string{"v1"},
  57. Resources: []string{"nodes"},
  58. }
  59. ruleAdmin := rbacv1.PolicyRule{
  60. Verbs: []string{"*"},
  61. APIGroups: []string{"*"},
  62. Resources: []string{"*"},
  63. }
  64. staticRoles1 := StaticRoles{
  65. roles: []*rbacv1.Role{
  66. {
  67. ObjectMeta: metav1.ObjectMeta{Namespace: "namespace1", Name: "readthings"},
  68. Rules: []rbacv1.PolicyRule{ruleReadPods, ruleReadServices},
  69. },
  70. },
  71. clusterRoles: []*rbacv1.ClusterRole{
  72. {
  73. ObjectMeta: metav1.ObjectMeta{Name: "cluster-admin"},
  74. Rules: []rbacv1.PolicyRule{ruleAdmin},
  75. },
  76. {
  77. ObjectMeta: metav1.ObjectMeta{Name: "write-nodes"},
  78. Rules: []rbacv1.PolicyRule{ruleWriteNodes},
  79. },
  80. },
  81. roleBindings: []*rbacv1.RoleBinding{
  82. {
  83. ObjectMeta: metav1.ObjectMeta{Namespace: "namespace1"},
  84. Subjects: []rbacv1.Subject{
  85. {Kind: rbacv1.UserKind, Name: "foobar"},
  86. {Kind: rbacv1.GroupKind, Name: "group1"},
  87. },
  88. RoleRef: rbacv1.RoleRef{APIGroup: rbacv1.GroupName, Kind: "Role", Name: "readthings"},
  89. },
  90. },
  91. clusterRoleBindings: []*rbacv1.ClusterRoleBinding{
  92. {
  93. Subjects: []rbacv1.Subject{
  94. {Kind: rbacv1.UserKind, Name: "admin"},
  95. {Kind: rbacv1.GroupKind, Name: "admin"},
  96. },
  97. RoleRef: rbacv1.RoleRef{APIGroup: rbacv1.GroupName, 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. user user.Info
  105. namespace string
  106. effectiveRules []rbacv1.PolicyRule
  107. }{
  108. {
  109. StaticRoles: staticRoles1,
  110. user: &user.DefaultInfo{Name: "foobar"},
  111. namespace: "namespace1",
  112. effectiveRules: []rbacv1.PolicyRule{ruleReadPods, ruleReadServices},
  113. },
  114. {
  115. StaticRoles: staticRoles1,
  116. user: &user.DefaultInfo{Name: "foobar"},
  117. namespace: "namespace2",
  118. effectiveRules: nil,
  119. },
  120. {
  121. StaticRoles: staticRoles1,
  122. // Same as above but without a namespace. Only cluster rules should apply.
  123. user: &user.DefaultInfo{Name: "foobar", Groups: []string{"admin"}},
  124. effectiveRules: []rbacv1.PolicyRule{ruleAdmin},
  125. },
  126. {
  127. StaticRoles: staticRoles1,
  128. user: &user.DefaultInfo{},
  129. effectiveRules: nil,
  130. },
  131. }
  132. for i, tc := range tests {
  133. ruleResolver := newMockRuleResolver(&tc.StaticRoles)
  134. rules, err := ruleResolver.RulesFor(tc.user, tc.namespace)
  135. if err != nil {
  136. t.Errorf("case %d: GetEffectivePolicyRules(context)=%v", i, err)
  137. continue
  138. }
  139. // Sort for deep equals
  140. sort.Sort(byHash(rules))
  141. sort.Sort(byHash(tc.effectiveRules))
  142. if !reflect.DeepEqual(rules, tc.effectiveRules) {
  143. ruleDiff := diff.ObjectDiff(rules, tc.effectiveRules)
  144. t.Errorf("case %d: %s", i, ruleDiff)
  145. }
  146. }
  147. }
  148. func TestAppliesTo(t *testing.T) {
  149. tests := []struct {
  150. subjects []rbacv1.Subject
  151. user user.Info
  152. namespace string
  153. appliesTo bool
  154. index int
  155. testCase string
  156. }{
  157. {
  158. subjects: []rbacv1.Subject{
  159. {Kind: rbacv1.UserKind, Name: "foobar"},
  160. },
  161. user: &user.DefaultInfo{Name: "foobar"},
  162. appliesTo: true,
  163. index: 0,
  164. testCase: "single subject that matches username",
  165. },
  166. {
  167. subjects: []rbacv1.Subject{
  168. {Kind: rbacv1.UserKind, Name: "barfoo"},
  169. {Kind: rbacv1.UserKind, Name: "foobar"},
  170. },
  171. user: &user.DefaultInfo{Name: "foobar"},
  172. appliesTo: true,
  173. index: 1,
  174. testCase: "multiple subjects, one that matches username",
  175. },
  176. {
  177. subjects: []rbacv1.Subject{
  178. {Kind: rbacv1.UserKind, Name: "barfoo"},
  179. {Kind: rbacv1.UserKind, Name: "foobar"},
  180. },
  181. user: &user.DefaultInfo{Name: "zimzam"},
  182. appliesTo: false,
  183. testCase: "multiple subjects, none that match username",
  184. },
  185. {
  186. subjects: []rbacv1.Subject{
  187. {Kind: rbacv1.UserKind, Name: "barfoo"},
  188. {Kind: rbacv1.GroupKind, Name: "foobar"},
  189. },
  190. user: &user.DefaultInfo{Name: "zimzam", Groups: []string{"foobar"}},
  191. appliesTo: true,
  192. index: 1,
  193. testCase: "multiple subjects, one that match group",
  194. },
  195. {
  196. subjects: []rbacv1.Subject{
  197. {Kind: rbacv1.UserKind, Name: "barfoo"},
  198. {Kind: rbacv1.GroupKind, Name: "foobar"},
  199. },
  200. user: &user.DefaultInfo{Name: "zimzam", Groups: []string{"foobar"}},
  201. namespace: "namespace1",
  202. appliesTo: true,
  203. index: 1,
  204. testCase: "multiple subjects, one that match group, should ignore namespace",
  205. },
  206. {
  207. subjects: []rbacv1.Subject{
  208. {Kind: rbacv1.UserKind, Name: "barfoo"},
  209. {Kind: rbacv1.GroupKind, Name: "foobar"},
  210. {Kind: rbacv1.ServiceAccountKind, Namespace: "kube-system", Name: "default"},
  211. },
  212. user: &user.DefaultInfo{Name: "system:serviceaccount:kube-system:default"},
  213. namespace: "default",
  214. appliesTo: true,
  215. index: 2,
  216. testCase: "multiple subjects with a service account that matches",
  217. },
  218. {
  219. subjects: []rbacv1.Subject{
  220. {Kind: rbacv1.UserKind, Name: "*"},
  221. },
  222. user: &user.DefaultInfo{Name: "foobar"},
  223. namespace: "default",
  224. appliesTo: false,
  225. testCase: "* user subject name doesn't match all users",
  226. },
  227. {
  228. subjects: []rbacv1.Subject{
  229. {Kind: rbacv1.GroupKind, Name: user.AllAuthenticated},
  230. {Kind: rbacv1.GroupKind, Name: user.AllUnauthenticated},
  231. },
  232. user: &user.DefaultInfo{Name: "foobar", Groups: []string{user.AllAuthenticated}},
  233. namespace: "default",
  234. appliesTo: true,
  235. index: 0,
  236. testCase: "binding to all authenticated and unauthenticated subjects matches authenticated user",
  237. },
  238. {
  239. subjects: []rbacv1.Subject{
  240. {Kind: rbacv1.GroupKind, Name: user.AllAuthenticated},
  241. {Kind: rbacv1.GroupKind, Name: user.AllUnauthenticated},
  242. },
  243. user: &user.DefaultInfo{Name: "system:anonymous", Groups: []string{user.AllUnauthenticated}},
  244. namespace: "default",
  245. appliesTo: true,
  246. index: 1,
  247. testCase: "binding to all authenticated and unauthenticated subjects matches anonymous user",
  248. },
  249. }
  250. for _, tc := range tests {
  251. gotIndex, got := appliesTo(tc.user, tc.subjects, tc.namespace)
  252. if got != tc.appliesTo {
  253. t.Errorf("case %q want appliesTo=%t, got appliesTo=%t", tc.testCase, tc.appliesTo, got)
  254. }
  255. if gotIndex != tc.index {
  256. t.Errorf("case %q want index %d, got %d", tc.testCase, tc.index, gotIndex)
  257. }
  258. }
  259. }