Skip to content

Commit 5089172

Browse files
authored
feat: Add permission filter into nss controller (#145)
1. Remove escalate and bind permission 2. Add permission filter to make sure namespace scope operator has enough permission to create roles
1 parent f75ed1f commit 5089172

File tree

7 files changed

+25
-69
lines changed

7 files changed

+25
-69
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ When the `NamespaceScope` CR is created/updated, it will:
108108
- update
109109
- watch
110110
- deletecollection
111-
- apiGroups:
112-
- rbac.authorization.k8s.io
113-
resources:
114-
- roles
115-
verbs:
116-
- escalate
117-
- bind
118111
---
119112
kind: RoleBinding
120113
apiVersion: rbac.authorization.k8s.io/v1

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ spec:
129129
- update
130130
- watch
131131
- deletecollection
132-
- apiGroups:
133-
- rbac.authorization.k8s.io
134-
resources:
135-
- roles
136-
verbs:
137-
- escalate
138-
- bind
139132
serviceAccountName: ibm-namespace-scope-operator
140133
strategy: deployment
141134
installModes:

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ spec:
6363
- update
6464
- watch
6565
- deletecollection
66-
- apiGroups:
67-
- rbac.authorization.k8s.io
68-
resources:
69-
- roles
70-
verbs:
71-
- escalate
72-
- bind
7366
serviceAccountName: ibm-namespace-scope-operator
7467
deployments:
7568
- label:

config/rbac/role.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,3 @@ rules:
1616
- update
1717
- watch
1818
- deletecollection
19-
- apiGroups:
20-
- rbac.authorization.k8s.io
21-
resources:
22-
- roles
23-
verbs:
24-
- escalate
25-
- bind

controllers/namespacescope_controller.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,6 @@ func (r *NamespaceScopeReconciler) createRoleForNSS(labels map[string]string, fr
479479
APIGroups: []string{"*"},
480480
Resources: []string{"*"},
481481
},
482-
{
483-
Verbs: []string{"escalate", "bind"},
484-
APIGroups: []string{"rbac.authorization.k8s.io"},
485-
Resources: []string{"roles"},
486-
},
487482
},
488483
}
489484
if err := r.Create(ctx, role); err != nil {
@@ -660,13 +655,14 @@ func (r *NamespaceScopeReconciler) CreateRole(roleNames []string, labels map[str
660655
hashedServiceAccount := sha256.Sum256([]byte(roleName + saName + fromNs))
661656
name := strings.Split(roleName, ".")[0] + "-" + hex.EncodeToString(hashedServiceAccount[:7])
662657
namespace := toNs
658+
rules := rulesFilter(originalRole.Rules)
663659
role := &rbacv1.Role{
664660
ObjectMeta: metav1.ObjectMeta{
665661
Name: name,
666662
Namespace: namespace,
667663
Labels: labels,
668664
},
669-
Rules: originalRole.Rules,
665+
Rules: rules,
670666
}
671667
if err := r.Create(ctx, role); err != nil {
672668
if errors.IsAlreadyExists(err) {
@@ -952,30 +948,32 @@ func (r *NamespaceScopeReconciler) checkNamespaceAdminAuth(namespace string) boo
952948
return false
953949
}
954950
}
955-
roleVerbs := []string{"escalate", "bind"}
956-
for _, verb := range roleVerbs {
957-
sar := &authorizationv1.SelfSubjectAccessReview{
958-
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
959-
ResourceAttributes: &authorizationv1.ResourceAttributes{
960-
Namespace: namespace,
961-
Verb: verb,
962-
Group: "rbac.authorization.k8s.io",
963-
Resource: "roles",
964-
},
965-
},
966-
}
967-
if err := r.Create(ctx, sar); err != nil {
968-
klog.Errorf("Failed to check operator namespace permission: %v", err)
969-
return false
970-
}
951+
return true
952+
}
971953

972-
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)
954+
func rulesFilter(orgRule []rbacv1.PolicyRule) []rbacv1.PolicyRule {
955+
verbMap := make(map[string]struct{})
956+
verbs := []string{"create", "delete", "get", "list", "patch", "update", "watch", "deletecollection"}
957+
for _, v := range verbs {
958+
verbMap[v] = struct{}{}
959+
}
973960

974-
if !sar.Status.Allowed {
975-
return false
961+
for i := 0; i < len(orgRule); i++ {
962+
for j := 0; j < len(orgRule[i].Verbs); j++ {
963+
if orgRule[i].Verbs[j] == "*" {
964+
orgRule[i].Verbs = append(orgRule[i].Verbs[:j], orgRule[i].Verbs[j+1:]...)
965+
orgRule[i].Verbs = append(orgRule[i].Verbs, verbs...)
966+
continue
967+
}
968+
if _, ok := verbMap[orgRule[i].Verbs[j]]; !ok {
969+
orgRule[i].Verbs = append(orgRule[i].Verbs[:j], orgRule[i].Verbs[j+1:]...)
970+
}
971+
}
972+
if len(orgRule[i].Verbs) == 0 {
973+
orgRule = append(orgRule[:i], orgRule[i+1:]...)
976974
}
977975
}
978-
return true
976+
return orgRule
979977
}
980978

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

deploy/role.yaml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,4 @@ rules:
1616
- list
1717
- patch
1818
- update
19-
- watch
20-
- apiGroups:
21-
- rbac.authorization.k8s.io
22-
resources:
23-
- roles
24-
verbs:
25-
- escalate
26-
- bind
19+
- watch

scripts/authorize-namespace.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,6 @@ rules:
143143
- update
144144
- watch
145145
- deletecollection
146-
- apiGroups:
147-
- rbac.authorization.k8s.io
148-
resources:
149-
- roles
150-
verbs:
151-
- escalate
152-
- bind
153146
EOF
154147

155148
#

0 commit comments

Comments
 (0)