policy_compact.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 validation
  14. import (
  15. "reflect"
  16. rbacv1 "k8s.io/api/rbac/v1"
  17. )
  18. type simpleResource struct {
  19. Group string
  20. Resource string
  21. ResourceNameExist bool
  22. ResourceName string
  23. }
  24. // CompactRules combines rules that contain a single APIGroup/Resource, differ only by verb, and contain no other attributes.
  25. // this is a fast check, and works well with the decomposed "missing rules" list from a Covers check.
  26. func CompactRules(rules []rbacv1.PolicyRule) ([]rbacv1.PolicyRule, error) {
  27. compacted := make([]rbacv1.PolicyRule, 0, len(rules))
  28. simpleRules := map[simpleResource]*rbacv1.PolicyRule{}
  29. for _, rule := range rules {
  30. if resource, isSimple := isSimpleResourceRule(&rule); isSimple {
  31. if existingRule, ok := simpleRules[resource]; ok {
  32. // Add the new verbs to the existing simple resource rule
  33. if existingRule.Verbs == nil {
  34. existingRule.Verbs = []string{}
  35. }
  36. existingRule.Verbs = append(existingRule.Verbs, rule.Verbs...)
  37. } else {
  38. // Copy the rule to accumulate matching simple resource rules into
  39. simpleRules[resource] = rule.DeepCopy()
  40. }
  41. } else {
  42. compacted = append(compacted, rule)
  43. }
  44. }
  45. // Once we've consolidated the simple resource rules, add them to the compacted list
  46. for _, simpleRule := range simpleRules {
  47. compacted = append(compacted, *simpleRule)
  48. }
  49. return compacted, nil
  50. }
  51. // 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
  52. func isSimpleResourceRule(rule *rbacv1.PolicyRule) (simpleResource, bool) {
  53. resource := simpleResource{}
  54. // If we have "complex" rule attributes, return early without allocations or expensive comparisons
  55. if len(rule.ResourceNames) > 1 || len(rule.NonResourceURLs) > 0 {
  56. return resource, false
  57. }
  58. // If we have multiple api groups or resources, return early
  59. if len(rule.APIGroups) != 1 || len(rule.Resources) != 1 {
  60. return resource, false
  61. }
  62. // Test if this rule only contains APIGroups/Resources/Verbs/ResourceNames
  63. simpleRule := &rbacv1.PolicyRule{APIGroups: rule.APIGroups, Resources: rule.Resources, Verbs: rule.Verbs, ResourceNames: rule.ResourceNames}
  64. if !reflect.DeepEqual(simpleRule, rule) {
  65. return resource, false
  66. }
  67. if len(rule.ResourceNames) == 0 {
  68. resource = simpleResource{Group: rule.APIGroups[0], Resource: rule.Resources[0], ResourceNameExist: false}
  69. } else {
  70. resource = simpleResource{Group: rule.APIGroups[0], Resource: rule.Resources[0], ResourceNameExist: true, ResourceName: rule.ResourceNames[0]}
  71. }
  72. return resource, true
  73. }