policy_comparator.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "strings"
  16. rbacv1 "k8s.io/api/rbac/v1"
  17. )
  18. // Covers determines whether or not the ownerRules cover the servantRules in terms of allowed actions.
  19. // It returns whether or not the ownerRules cover and a list of the rules that the ownerRules do not cover.
  20. func Covers(ownerRules, servantRules []rbacv1.PolicyRule) (bool, []rbacv1.PolicyRule) {
  21. // 1. Break every servantRule into individual rule tuples: group, verb, resource, resourceName
  22. // 2. Compare the mini-rules against each owner rule. Because the breakdown is down to the most atomic level, we're guaranteed that each mini-servant rule will be either fully covered or not covered by a single owner rule
  23. // 3. Any left over mini-rules means that we are not covered and we have a nice list of them.
  24. // TODO: it might be nice to collapse the list down into something more human readable
  25. subrules := []rbacv1.PolicyRule{}
  26. for _, servantRule := range servantRules {
  27. subrules = append(subrules, BreakdownRule(servantRule)...)
  28. }
  29. uncoveredRules := []rbacv1.PolicyRule{}
  30. for _, subrule := range subrules {
  31. covered := false
  32. for _, ownerRule := range ownerRules {
  33. if ruleCovers(ownerRule, subrule) {
  34. covered = true
  35. break
  36. }
  37. }
  38. if !covered {
  39. uncoveredRules = append(uncoveredRules, subrule)
  40. }
  41. }
  42. return (len(uncoveredRules) == 0), uncoveredRules
  43. }
  44. // BreadownRule takes a rule and builds an equivalent list of rules that each have at most one verb, one
  45. // resource, and one resource name
  46. func BreakdownRule(rule rbacv1.PolicyRule) []rbacv1.PolicyRule {
  47. subrules := []rbacv1.PolicyRule{}
  48. for _, group := range rule.APIGroups {
  49. for _, resource := range rule.Resources {
  50. for _, verb := range rule.Verbs {
  51. if len(rule.ResourceNames) > 0 {
  52. for _, resourceName := range rule.ResourceNames {
  53. subrules = append(subrules, rbacv1.PolicyRule{APIGroups: []string{group}, Resources: []string{resource}, Verbs: []string{verb}, ResourceNames: []string{resourceName}})
  54. }
  55. } else {
  56. subrules = append(subrules, rbacv1.PolicyRule{APIGroups: []string{group}, Resources: []string{resource}, Verbs: []string{verb}})
  57. }
  58. }
  59. }
  60. }
  61. // Non-resource URLs are unique because they only combine with verbs.
  62. for _, nonResourceURL := range rule.NonResourceURLs {
  63. for _, verb := range rule.Verbs {
  64. subrules = append(subrules, rbacv1.PolicyRule{NonResourceURLs: []string{nonResourceURL}, Verbs: []string{verb}})
  65. }
  66. }
  67. return subrules
  68. }
  69. func has(set []string, ele string) bool {
  70. for _, s := range set {
  71. if s == ele {
  72. return true
  73. }
  74. }
  75. return false
  76. }
  77. func hasAll(set, contains []string) bool {
  78. owning := make(map[string]struct{}, len(set))
  79. for _, ele := range set {
  80. owning[ele] = struct{}{}
  81. }
  82. for _, ele := range contains {
  83. if _, ok := owning[ele]; !ok {
  84. return false
  85. }
  86. }
  87. return true
  88. }
  89. func resourceCoversAll(setResources, coversResources []string) bool {
  90. // if we have a star or an exact match on all resources, then we match
  91. if has(setResources, rbacv1.ResourceAll) || hasAll(setResources, coversResources) {
  92. return true
  93. }
  94. for _, path := range coversResources {
  95. // if we have an exact match, then we match.
  96. if has(setResources, path) {
  97. continue
  98. }
  99. // if we're not a subresource, then we definitely don't match. fail.
  100. if !strings.Contains(path, "/") {
  101. return false
  102. }
  103. tokens := strings.SplitN(path, "/", 2)
  104. resourceToCheck := "*/" + tokens[1]
  105. if !has(setResources, resourceToCheck) {
  106. return false
  107. }
  108. }
  109. return true
  110. }
  111. func nonResourceURLsCoversAll(set, covers []string) bool {
  112. for _, path := range covers {
  113. covered := false
  114. for _, owner := range set {
  115. if nonResourceURLCovers(owner, path) {
  116. covered = true
  117. break
  118. }
  119. }
  120. if !covered {
  121. return false
  122. }
  123. }
  124. return true
  125. }
  126. func nonResourceURLCovers(ownerPath, subPath string) bool {
  127. if ownerPath == subPath {
  128. return true
  129. }
  130. return strings.HasSuffix(ownerPath, "*") && strings.HasPrefix(subPath, strings.TrimRight(ownerPath, "*"))
  131. }
  132. // ruleCovers determines whether the ownerRule (which may have multiple verbs, resources, and resourceNames) covers
  133. // the subrule (which may only contain at most one verb, resource, and resourceName)
  134. func ruleCovers(ownerRule, subRule rbacv1.PolicyRule) bool {
  135. verbMatches := has(ownerRule.Verbs, rbacv1.VerbAll) || hasAll(ownerRule.Verbs, subRule.Verbs)
  136. groupMatches := has(ownerRule.APIGroups, rbacv1.APIGroupAll) || hasAll(ownerRule.APIGroups, subRule.APIGroups)
  137. resourceMatches := resourceCoversAll(ownerRule.Resources, subRule.Resources)
  138. nonResourceURLMatches := nonResourceURLsCoversAll(ownerRule.NonResourceURLs, subRule.NonResourceURLs)
  139. resourceNameMatches := false
  140. if len(subRule.ResourceNames) == 0 {
  141. resourceNameMatches = (len(ownerRule.ResourceNames) == 0)
  142. } else {
  143. resourceNameMatches = (len(ownerRule.ResourceNames) == 0) || hasAll(ownerRule.ResourceNames, subRule.ResourceNames)
  144. }
  145. return verbMatches && groupMatches && resourceMatches && resourceNameMatches && nonResourceURLMatches
  146. }