Skip to content

Commit

Permalink
rbac: fix deduplication of core group and add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 29, 2024
1 parent b00261b commit 5f660af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pkg/rbac/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ func removeDupAndSort(strs []string) []string {

// ToRule converts this rule to its Kubernetes API form.
func (r *Rule) ToRule() rbacv1.PolicyRule {
// fix the group names first, since letting people type "core" is nice
for i, group := range r.Groups {
if group == "core" {
r.Groups[i] = ""
}
}
return rbacv1.PolicyRule{
APIGroups: r.Groups,
Verbs: r.Verbs,
Expand Down Expand Up @@ -230,6 +224,11 @@ func GenerateRoles(ctx *genall.GenerationContext, roleName string) ([]interface{
ruleMap := make(map[ruleKey]*Rule)
// all the Rules having the same ruleKey will be merged into the first Rule
for _, rule := range rules {
// fix the group name first, since letting people type "core" is nice
if len(rule.Groups) == 1 && rule.Groups[0] == "core" {
rule.Groups = []string{""}
}

key := rule.key()
if _, ok := ruleMap[key]; !ok {
ruleMap[key] = rule
Expand Down
2 changes: 2 additions & 0 deletions pkg/rbac/testdata/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ package controller
// +kubebuilder:rbac:groups=not-deduplicate-groups2,resources=some,verbs=list
// +kubebuilder:rbac:urls=/url-to-duplicate,verbs=get
// +kubebuilder:rbac:urls=/another/url-to-duplicate,verbs=get
// +kubebuilder:rbac:groups=core,resources=deduplicate,verbs=list
// +kubebuilder:rbac:groups="",resources=me,verbs=list
7 changes: 7 additions & 0 deletions pkg/rbac/testdata/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ rules:
- /url-to-duplicate
verbs:
- get
- apiGroups:
- ""
resources:
- deduplicate
- me
verbs:
- list
- apiGroups:
- art
resources:
Expand Down

0 comments on commit 5f660af

Please sign in to comment.