Skip to content

Commit 702f441

Browse files
committed
✨ Add new resourceNames field in RBAC marker
This makes it possible to generate policy rules restricted to specific instances.
1 parent 92e95c1 commit 702f441

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

pkg/rbac/parser.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
//
2020
// The markers take the form:
2121
//
22-
// +kubebuilder:rbac:groups=<groups>,resources=<resources>,verbs=<verbs>,urls=<non resource urls>
22+
// +kubebuilder:rbac:groups=<groups>,resources=<resources>,resourceNames=<resource names>,verbs=<verbs>,urls=<non resource urls>
2323
package rbac
2424

2525
import (
@@ -48,6 +48,11 @@ type Rule struct {
4848
Groups []string `marker:",optional"`
4949
// Resources specifies the API resources that this rule encompasses.
5050
Resources []string `marker:",optional"`
51+
// ResourceNames specifies the names of the API resources that this rule encompasses.
52+
//
53+
// Create requests cannot be restricted by resourcename, as the object's name
54+
// is not known at authorization time.
55+
ResourceNames []string `marker:",optional"`
5156
// Verbs specifies the (lowercase) kubernetes API verbs that this rule encompasses.
5257
Verbs []string
5358
// URL specifies the non-resource URLs that this rule encompasses.
@@ -60,13 +65,14 @@ type Rule struct {
6065

6166
// ruleKey represents the resources and non-resources a Rule applies.
6267
type ruleKey struct {
63-
Groups string
64-
Resources string
65-
URLs string
68+
Groups string
69+
Resources string
70+
ResourceNames string
71+
URLs string
6672
}
6773

6874
func (key ruleKey) String() string {
69-
return fmt.Sprintf("%s + %s + %s", key.Groups, key.Resources, key.URLs)
75+
return fmt.Sprintf("%s + %s + %s + %s", key.Groups, key.Resources, key.ResourceNames, key.URLs)
7076
}
7177

7278
// ruleKeys implements sort.Interface
@@ -80,9 +86,10 @@ func (keys ruleKeys) Less(i, j int) bool { return keys[i].String() < keys[j].Str
8086
func (r *Rule) key() ruleKey {
8187
r.normalize()
8288
return ruleKey{
83-
Groups: strings.Join(r.Groups, "&"),
84-
Resources: strings.Join(r.Resources, "&"),
85-
URLs: strings.Join(r.URLs, "&"),
89+
Groups: strings.Join(r.Groups, "&"),
90+
Resources: strings.Join(r.Resources, "&"),
91+
ResourceNames: strings.Join(r.ResourceNames, "&"),
92+
URLs: strings.Join(r.URLs, "&"),
8693
}
8794
}
8895

@@ -96,6 +103,7 @@ func (r *Rule) addVerbs(verbs []string) {
96103
func (r *Rule) normalize() {
97104
r.Groups = removeDupAndSort(r.Groups)
98105
r.Resources = removeDupAndSort(r.Resources)
106+
r.ResourceNames = removeDupAndSort(r.ResourceNames)
99107
r.Verbs = removeDupAndSort(r.Verbs)
100108
r.URLs = removeDupAndSort(r.URLs)
101109
}
@@ -130,6 +138,7 @@ func (r *Rule) ToRule() rbacv1.PolicyRule {
130138
APIGroups: r.Groups,
131139
Verbs: r.Verbs,
132140
Resources: r.Resources,
141+
ResourceNames: r.ResourceNames,
133142
NonResourceURLs: r.URLs,
134143
}
135144
}

pkg/rbac/testdata/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ package controller
1010
// +kubebuilder:rbac:groups=cron;batch,resources=jobs/status,verbs=get;create
1111
// +kubebuilder:rbac:groups=batch,resources=jobs/status,verbs=watch;watch
1212
// +kubebuilder:rbac:groups=art,resources=jobs,verbs=get,namespace=park
13+
// +kubebuilder:rbac:groups=batch.io,resources=cronjobs,resourceNames=foo;bar;baz,verbs=get;watch

pkg/rbac/testdata/role.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ rules:
3434
- create
3535
- get
3636
- watch
37+
- apiGroups:
38+
- batch.io
39+
resourceNames:
40+
- bar
41+
- baz
42+
- foo
43+
resources:
44+
- cronjobs
45+
verbs:
46+
- get
47+
- watch
3748
- apiGroups:
3849
- batch.io
3950
resources:

pkg/rbac/zz_generated.markerhelp.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)