rbac.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2018 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 rbac
  14. import (
  15. rbacv1 "k8s.io/api/rbac/v1"
  16. "reflect"
  17. "strings"
  18. )
  19. type simpleResource struct {
  20. Group string
  21. Resource string
  22. ResourceNameExist bool
  23. ResourceName string
  24. }
  25. // CompactRules combines rules that contain a single APIGroup/Resource, differ only by verb, and contain no other attributes.
  26. // this is a fast check, and works well with the decomposed "missing rules" list from a Covers check.
  27. func CompactRules(rules []rbacv1.PolicyRule) ([]rbacv1.PolicyRule, error) {
  28. compacted := make([]rbacv1.PolicyRule, 0, len(rules))
  29. simpleRules := map[simpleResource]*rbacv1.PolicyRule{}
  30. for _, rule := range rules {
  31. if resource, isSimple := isSimpleResourceRule(&rule); isSimple {
  32. if existingRule, ok := simpleRules[resource]; ok {
  33. // Add the new verbs to the existing simple resource rule
  34. if existingRule.Verbs == nil {
  35. existingRule.Verbs = []string{}
  36. }
  37. existingRule.Verbs = append(existingRule.Verbs, rule.Verbs...)
  38. } else {
  39. // Copy the rule to accumulate matching simple resource rules into
  40. simpleRules[resource] = rule.DeepCopy()
  41. }
  42. } else {
  43. compacted = append(compacted, rule)
  44. }
  45. }
  46. // Once we've consolidated the simple resource rules, add them to the compacted list
  47. for _, simpleRule := range simpleRules {
  48. compacted = append(compacted, *simpleRule)
  49. }
  50. return compacted, nil
  51. }
  52. // isSimpleResourceRule returns true if the given rule contains verbs, a single resource, a single API group, at most one Resource Name, and no other values
  53. func isSimpleResourceRule(rule *rbacv1.PolicyRule) (simpleResource, bool) {
  54. resource := simpleResource{}
  55. // If we have "complex" rule attributes, return early without allocations or expensive comparisons
  56. if len(rule.ResourceNames) > 1 || len(rule.NonResourceURLs) > 0 {
  57. return resource, false
  58. }
  59. // If we have multiple api groups or resources, return early
  60. if len(rule.APIGroups) != 1 || len(rule.Resources) != 1 {
  61. return resource, false
  62. }
  63. // Test if this rule only contains APIGroups/Resources/Verbs/ResourceNames
  64. simpleRule := &rbacv1.PolicyRule{APIGroups: rule.APIGroups, Resources: rule.Resources, Verbs: rule.Verbs, ResourceNames: rule.ResourceNames}
  65. if !reflect.DeepEqual(simpleRule, rule) {
  66. return resource, false
  67. }
  68. if len(rule.ResourceNames) == 0 {
  69. resource = simpleResource{Group: rule.APIGroups[0], Resource: rule.Resources[0], ResourceNameExist: false}
  70. } else {
  71. resource = simpleResource{Group: rule.APIGroups[0], Resource: rule.Resources[0], ResourceNameExist: true, ResourceName: rule.ResourceNames[0]}
  72. }
  73. return resource, true
  74. }
  75. // BreakdownRule takes a rule and builds an equivalent list of rules that each have at most one verb, one
  76. // resource, and one resource name
  77. func BreakdownRule(rule rbacv1.PolicyRule) []rbacv1.PolicyRule {
  78. subrules := []rbacv1.PolicyRule{}
  79. for _, group := range rule.APIGroups {
  80. for _, resource := range rule.Resources {
  81. for _, verb := range rule.Verbs {
  82. if len(rule.ResourceNames) > 0 {
  83. for _, resourceName := range rule.ResourceNames {
  84. subrules = append(subrules, rbacv1.PolicyRule{APIGroups: []string{group}, Resources: []string{resource}, Verbs: []string{verb}, ResourceNames: []string{resourceName}})
  85. }
  86. } else {
  87. subrules = append(subrules, rbacv1.PolicyRule{APIGroups: []string{group}, Resources: []string{resource}, Verbs: []string{verb}})
  88. }
  89. }
  90. }
  91. }
  92. // Non-resource URLs are unique because they only combine with verbs.
  93. for _, nonResourceURL := range rule.NonResourceURLs {
  94. for _, verb := range rule.Verbs {
  95. subrules = append(subrules, rbacv1.PolicyRule{NonResourceURLs: []string{nonResourceURL}, Verbs: []string{verb}})
  96. }
  97. }
  98. return subrules
  99. }
  100. // SortableRuleSlice is used to sort rule slice
  101. type SortableRuleSlice []rbacv1.PolicyRule
  102. func (s SortableRuleSlice) Len() int { return len(s) }
  103. func (s SortableRuleSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  104. func (s SortableRuleSlice) Less(i, j int) bool {
  105. return strings.Compare(s[i].String(), s[j].String()) < 0
  106. }