Skip to content

Commit 9f12176

Browse files
authored
fix: Grant escalate and bind permission to role (#78)
Grant escalate and bind permission to role to make sure NamespaceScope operator can create any roles and create rolebinding for it.
1 parent c77252b commit 9f12176

File tree

7 files changed

+126
-20
lines changed

7 files changed

+126
-20
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@ When the `NamespaceScope` CR is created/updated, it will:
8686
resources:
8787
- '*'
8888
verbs:
89-
- '*'
89+
- create
90+
- delete
91+
- get
92+
- list
93+
- patch
94+
- update
95+
- watch
96+
- apiGroups:
97+
- rbac.authorization.k8s.io
98+
resources:
99+
- roles
100+
verbs:
101+
- escalate
102+
- bind
90103
---
91104
kind: RoleBinding
92105
apiVersion: rbac.authorization.k8s.io/v1

bundle-restricted/manifests/ibm-namespace-scope-operator-restricted.clusterserviceversion.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ spec:
150150
- patch
151151
- update
152152
- watch
153+
- apiGroups:
154+
- rbac.authorization.k8s.io
155+
resources:
156+
- roles
157+
verbs:
158+
- escalate
159+
- bind
153160
serviceAccountName: ibm-namespace-scope-operator
154161
strategy: deployment
155162
installModes:

bundle/manifests/ibm-namespace-scope-operator.clusterserviceversion.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,20 @@ spec:
5454
resources:
5555
- '*'
5656
verbs:
57-
- '*'
57+
- create
58+
- delete
59+
- get
60+
- list
61+
- patch
62+
- update
63+
- watch
64+
- apiGroups:
65+
- rbac.authorization.k8s.io
66+
resources:
67+
- roles
68+
verbs:
69+
- escalate
70+
- bind
5871
serviceAccountName: ibm-namespace-scope-operator
5972
deployments:
6073
- name: ibm-namespace-scope-operator

config/rbac/role.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ rules:
88
resources:
99
- "*"
1010
verbs:
11-
- "*"
11+
- create
12+
- delete
13+
- get
14+
- list
15+
- patch
16+
- update
17+
- watch
18+
- apiGroups:
19+
- rbac.authorization.k8s.io
20+
resources:
21+
- roles
22+
verbs:
23+
- escalate
24+
- bind

controllers/namespacescope_controller.go

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,15 @@ func (r *NamespaceScopeReconciler) createRoleForNSS(labels map[string]string, fr
358358
},
359359
Rules: []rbacv1.PolicyRule{
360360
{
361-
Verbs: []string{"*"},
361+
Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"},
362362
APIGroups: []string{"*"},
363363
Resources: []string{"*"},
364364
},
365+
{
366+
Verbs: []string{"escalate", "bind"},
367+
APIGroups: []string{"rbac.authorization.k8s.io"},
368+
Resources: []string{"roles"},
369+
},
365370
},
366371
}
367372
if err := r.Create(ctx, role); err != nil {
@@ -668,24 +673,53 @@ func (r *NamespaceScopeReconciler) checkGetNSAuth() bool {
668673

669674
// Check if operator has namespace admin permission
670675
func (r *NamespaceScopeReconciler) checkNamespaceAdminAuth(namespace string) bool {
671-
sar := &authorizationv1.SelfSubjectAccessReview{
672-
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
673-
ResourceAttributes: &authorizationv1.ResourceAttributes{
674-
Namespace: namespace,
675-
Verb: "*",
676-
Group: "*",
677-
Resource: "*",
676+
verbs := []string{"create", "delete", "get", "list", "patch", "update", "watch"}
677+
for _, verb := range verbs {
678+
sar := &authorizationv1.SelfSubjectAccessReview{
679+
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
680+
ResourceAttributes: &authorizationv1.ResourceAttributes{
681+
Namespace: namespace,
682+
Verb: verb,
683+
Group: "*",
684+
Resource: "*",
685+
},
678686
},
679-
},
680-
}
687+
}
688+
if err := r.Create(ctx, sar); err != nil {
689+
klog.Errorf("Failed to check operator namespace permission: %v", err)
690+
return false
691+
}
681692

682-
if err := r.Create(ctx, sar); err != nil {
683-
klog.Errorf("Failed to check operator namespace admin permission: %v", err)
684-
return false
693+
klog.V(2).Infof("Namespace admin permission in namespace %s, Allowed: %t, Denied: %t, Reason: %s", namespace, sar.Status.Allowed, sar.Status.Denied, sar.Status.Reason)
694+
695+
if !sar.Status.Allowed {
696+
return false
697+
}
685698
}
699+
roleVerbs := []string{"escalate", "bind"}
700+
for _, verb := range roleVerbs {
701+
sar := &authorizationv1.SelfSubjectAccessReview{
702+
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
703+
ResourceAttributes: &authorizationv1.ResourceAttributes{
704+
Namespace: namespace,
705+
Verb: verb,
706+
Group: "rbac.authorization.k8s.io",
707+
Resource: "roles",
708+
},
709+
},
710+
}
711+
if err := r.Create(ctx, sar); err != nil {
712+
klog.Errorf("Failed to check operator namespace permission: %v", err)
713+
return false
714+
}
686715

687-
klog.V(2).Infof("Namespace admin permission in namesapce %s, Allowed: %t, Denied: %t, Reason: %s", namespace, sar.Status.Allowed, sar.Status.Denied, sar.Status.Reason)
688-
return sar.Status.Allowed
716+
klog.V(2).Infof("Namespace admin permission in namespace %s, Allowed: %t, Denied: %t, Reason: %s", namespace, sar.Status.Allowed, sar.Status.Denied, sar.Status.Reason)
717+
718+
if !sar.Status.Allowed {
719+
return false
720+
}
721+
}
722+
return true
689723
}
690724

691725
func (r *NamespaceScopeReconciler) getValidatedNamespaces(instance *operatorv1.NamespaceScope) ([]string, error) {

deploy/role.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@ rules:
1010
resources:
1111
- "*"
1212
verbs:
13-
- "*"
13+
- create
14+
- delete
15+
- get
16+
- list
17+
- patch
18+
- update
19+
- watch
20+
- apiGroups:
21+
- rbac.authorization.k8s.io
22+
resources:
23+
- roles
24+
verbs:
25+
- escalate
26+
- bind

scripts/authorize-namespace.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,20 @@ rules:
141141
resources:
142142
- "*"
143143
verbs:
144-
- "*"
144+
- create
145+
- delete
146+
- get
147+
- list
148+
- patch
149+
- update
150+
- watch
151+
- apiGroups:
152+
- rbac.authorization.k8s.io
153+
resources:
154+
- roles
155+
verbs:
156+
- escalate
157+
- bind
145158
EOF
146159

147160
#

0 commit comments

Comments
 (0)