Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

🐛 rbac: fix deduplication of core group and add test coverage #1045

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 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,13 @@ 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
for i, name := range rule.Groups {
if name == "core" {
rule.Groups[i] = ""
}
}

key := rule.key()
if _, ok := ruleMap[key]; !ok {
ruleMap[key] = rule
Expand Down
3 changes: 3 additions & 0 deletions pkg/rbac/testdata/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ 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
// +kubebuilder:rbac:groups=core;"";some-other-to-deduplicate-with-core,resources=me,verbs=list;get
15 changes: 15 additions & 0 deletions pkg/rbac/testdata/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ rules:
- /url-to-duplicate
verbs:
- get
- apiGroups:
- ""
resources:
- deduplicate
- me
verbs:
- list
- apiGroups:
- ""
- some-other-to-deduplicate-with-core
resources:
- me
verbs:
- get
- list
- apiGroups:
- art
resources:
Expand Down
Loading