Skip to content

Commit 9889c6c

Browse files
authored
Merge pull request #13 from sbunce/sbunce/rbacsyncconf_clusterrole
controller: allow RBACSyncConfig to bind to ClusterRole
2 parents 9190c6c + 8c3daca commit 9889c6c

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ For namespace-scoped `RBACSyncConfig`, the behavior is nearly identical except
123123
for the following:
124124

125125
1. `RBACSyncConfig` must be defined with a namespace.
126-
2. `RBACSyncConfig` can only reference `Roles`.
127-
3. All `RoleBindings` created as a result of the `RBACSyncConfig` will be in
126+
2. All `RoleBindings` created as a result of the `RBACSyncConfig` will be in
128127
the same namespace.
129128

129+
`RBACSyncConfig` may reference a `ClusterRole` to grant permissions to
130+
namespaced resources defined in the `ClusterRole` within the `RoleBinding`’s
131+
namespace. This allows administrators to define a set of common roles for the
132+
entire cluster, then reuse them within multiple namespaces.
133+
130134
When deciding between using the two, you should mostly only need to look at
131135
whether your assigning `ClusterRoles` or `Roles` and then use the equivalent
132136
configuration. Refer to the [Kubernetes RBAC documentation](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)

pkg/controller/controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,12 @@ func (c *Controller) handleConfig(config *rbacsyncv1alpha.RBACSyncConfig) error
337337
active := map[string]struct{}{}
338338

339339
for _, binding := range config.Spec.Bindings {
340-
// need to valdiate that we only create rolebindings with this configuration.
341-
if binding.RoleRef.Kind != "Role" {
340+
switch binding.RoleRef.Kind {
341+
case "Role", "ClusterRole":
342+
// valid role reference kind for the role binding.
343+
default:
342344
c.recorder.Eventf(config, corev1.EventTypeWarning,
343-
EventReasonBindingError, "RoleRef kind %q invalid for RBACSyncConfig on group %q, use only Role",
345+
EventReasonBindingError, "RoleRef kind %q invalid for RBACSyncConfig on group %q, use only Role or ClusterRole",
344346
binding.RoleRef.Kind, binding.Group)
345347
continue
346348
}

pkg/controller/controller_test.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ func TestControllerRBACSyncConfig(t *testing.T) {
6464
},
6565
},
6666

67+
// Ensure that we can bind cluster roles.
68+
{
69+
input: newRBACSyncConfig("testing", "clusterrole",
70+
[]rbacsyncv1alpha.Membership{
71+
newMembership("group0",
72+
[]rbacv1.Subject{
73+
newUserSubject("user0"),
74+
newUserSubject("user1"),
75+
}),
76+
},
77+
[]rbacsyncv1alpha.Binding{
78+
newBinding("group0", "role0", "Role"),
79+
newBinding("group0", "role1", "ClusterRole"),
80+
newBinding("upstream", "role0", "Role"),
81+
}),
82+
events: []string{
83+
"Normal ConfigEnqueued RBACSyncConfig testing/clusterrole enqueued",
84+
"Normal BindingConfigured RoleBinding testing/clusterrole-group0-role0 configured",
85+
"Normal BindingConfigured RoleBinding testing/clusterrole-group0-role1 configured",
86+
"Normal BindingConfigured RoleBinding testing/clusterrole-upstream-role0 configured",
87+
},
88+
},
89+
6790
// Ensure that we can reference a role in two separate bindings.
6891
{
6992
input: newRBACSyncConfig("testing", "duplicates",
@@ -106,16 +129,13 @@ func TestControllerRBACSyncConfig(t *testing.T) {
106129
},
107130
[]rbacsyncv1alpha.Binding{
108131
newBinding("group0", "role0", "Role"),
109-
110-
// This will cause a failure event because we cannot
111-
// create clusterroles from the non-cluster version.
112-
newBinding("group0", "role1", "ClusterRole"),
132+
newBinding("group0", "role1", "ThisRoleTypeDoesNotExist"),
113133
newBinding("upstream", "role0", "Role"),
114134
}),
115135
events: []string{
116136
"Normal ConfigEnqueued RBACSyncConfig testing/invalidrole enqueued",
117137
"Normal BindingConfigured RoleBinding testing/invalidrole-group0-role0 configured",
118-
"Warning BindingError RoleRef kind \"ClusterRole\" invalid for RBACSyncConfig on group \"group0\", use only Role",
138+
"Warning BindingError RoleRef kind \"ThisRoleTypeDoesNotExist\" invalid for RBACSyncConfig on group \"group0\", use only Role or ClusterRole",
119139
"Normal BindingConfigured RoleBinding testing/invalidrole-upstream-role0 configured",
120140
},
121141
},
@@ -716,7 +736,9 @@ func makeExpectedRoleBindings(t *testing.T, config *rbacsyncv1alpha.RBACSyncConf
716736

717737
seen := map[string]struct{}{}
718738
for _, binding := range config.Spec.Bindings {
719-
if binding.RoleRef.Kind != "Role" {
739+
switch binding.RoleRef.Kind {
740+
case "Role", "ClusterRole":
741+
default:
720742
// these are skipped
721743
continue
722744
}

0 commit comments

Comments
 (0)