Skip to content

Commit

Permalink
Compact resource rules in the same API group with identical verbs
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Oct 26, 2017
1 parent b3f140b commit f52924c
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkg
import (
"fmt"
"io"
"reflect"
"sort"
"strings"

Expand Down Expand Up @@ -53,8 +54,41 @@ func compactRules(rules []rbac.PolicyRule) []rbac.PolicyRule {
for i := range compactRules {
compactRules[i].Verbs = sets.NewString(compactRules[i].Verbs...).List()
}
sort.Stable(rbac.SortableRuleSlice(compactRules))
return compactRules

accumulatingRules := []rbac.PolicyRule{}
for _, rule := range compactRules {
// Non-resource rules just accumulate
if len(rule.Resources) == 0 {
accumulatingRules = append(accumulatingRules, rule)
continue
}

accumulated := false
// strip resource
resourcelessRule := rule
resourcelessRule.Resources = nil
for j, accumulatingRule := range accumulatingRules {
// strip resource
resourcelessAccumulatingRule := accumulatingRule
resourcelessAccumulatingRule.Resources = nil

// if all other fields are identical (api group, verbs, names, etc, accumulate resources)
if reflect.DeepEqual(resourcelessRule, resourcelessAccumulatingRule) {
combinedResources := sets.NewString(accumulatingRule.Resources...)
combinedResources.Insert(rule.Resources...)
accumulatingRule.Resources = combinedResources.List()
accumulatingRules[j] = accumulatingRule
accumulated = true
break
}
}
if !accumulated {
accumulatingRules = append(accumulatingRules, rule)
}
}

sort.Stable(rbac.SortableRuleSlice(accumulatingRules))
return accumulatingRules
}

func sortRequests(requests []authorizer.AttributesRecord) {
Expand Down

0 comments on commit f52924c

Please sign in to comment.