Skip to content

Commit 51a4ebe

Browse files
authored
Restrict controller-manager Secrets access to Kueue namespace (#7188)
* Restrict controller-manager Secrets access to Kueue namespace * Add controller-manager secrets role to RBAC Kustomization * Add delegated namespaced secrets cache
1 parent 840dbfc commit 51a4ebe

File tree

10 files changed

+170
-46
lines changed

10 files changed

+170
-46
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{/* Code generated by yaml-processor. DO NOT EDIT. */}}
2+
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
kind: Role
5+
metadata:
6+
labels:
7+
{{- include "kueue.labels" . | nindent 4 }}
8+
name: '{{ include "kueue.fullname" . }}-manager-secrets-role'
9+
namespace: '{{ .Release.Namespace }}'
10+
rules:
11+
- apiGroups:
12+
- ""
13+
resources:
14+
- secrets
15+
verbs:
16+
- get
17+
- list
18+
- update
19+
- watch
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{/* Code generated by yaml-processor. DO NOT EDIT. */}}
2+
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
kind: RoleBinding
5+
metadata:
6+
labels:
7+
{{- include "kueue.labels" . | nindent 4 }}
8+
name: '{{ include "kueue.fullname" . }}-manager-secrets-rolebinding'
9+
namespace: '{{ .Release.Namespace }}'
10+
roleRef:
11+
apiGroup: rbac.authorization.k8s.io
12+
kind: Role
13+
name: '{{ include "kueue.fullname" . }}-manager-secrets-role'
14+
subjects:
15+
- kind: ServiceAccount
16+
name: '{{ include "kueue.fullname" . }}-controller-manager'
17+
namespace: '{{ .Release.Namespace }}'

charts/kueue/templates/rbac/role.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ rules:
6262
- list
6363
- update
6464
- watch
65-
- apiGroups:
66-
- ""
67-
resources:
68-
- secrets
69-
verbs:
70-
- get
71-
- list
72-
- update
73-
- watch
7465
- apiGroups:
7566
- admissionregistration.k8s.io
7667
resources:

config/components/rbac/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ resources:
99
- role_binding.yaml
1010
- leader_election_role.yaml
1111
- leader_election_role_binding.yaml
12+
- manager_secrets_role.yaml
13+
- manager_secrets_role_binding.yaml
1214
# The following RBAC configurations are used to protect
1315
# the metrics endpoint with authn/authz. These configurations
1416
# ensure that only authorized users and service accounts
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# permissions for the manager to access secrets.
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: Role
4+
metadata:
5+
name: manager-secrets-role
6+
namespace: system
7+
rules:
8+
- apiGroups:
9+
- ""
10+
resources:
11+
- secrets
12+
verbs:
13+
- get
14+
- list
15+
- update
16+
- watch
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: RoleBinding
3+
metadata:
4+
name: manager-secrets-rolebinding
5+
namespace: system
6+
roleRef:
7+
apiGroup: rbac.authorization.k8s.io
8+
kind: Role
9+
name: manager-secrets-role
10+
subjects:
11+
- kind: ServiceAccount
12+
name: controller-manager
13+
namespace: system

config/components/rbac/role.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ rules:
5959
- list
6060
- update
6161
- watch
62-
- apiGroups:
63-
- ""
64-
resources:
65-
- secrets
66-
verbs:
67-
- get
68-
- list
69-
- update
70-
- watch
7162
- apiGroups:
7263
- admissionregistration.k8s.io
7364
resources:

pkg/config/config.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@ import (
2121
"fmt"
2222
"os"
2323

24+
corev1 "k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/api/equality"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/apimachinery/pkg/runtime/serializer"
2829
ctrl "sigs.k8s.io/controller-runtime"
30+
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
31+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
2932
"sigs.k8s.io/controller-runtime/pkg/webhook"
3033

3134
configapi "sigs.k8s.io/kueue/apis/config/v1beta1"
3235
)
3336

37+
var (
38+
objectKeySecret = new(corev1.Secret)
39+
)
40+
3441
// fromFile provides an alternative to the deprecated ctrl.ConfigFile().AtPath(path).OfKind(&cfg)
3542
func fromFile(path string, scheme *runtime.Scheme, cfg *configapi.Configuration) error {
3643
content, err := os.ReadFile(path)
@@ -48,6 +55,8 @@ func fromFile(path string, scheme *runtime.Scheme, cfg *configapi.Configuration)
4855
// addTo provides an alternative to the deprecated o.AndFrom(&cfg)
4956
func addTo(o *ctrl.Options, cfg *configapi.Configuration) {
5057
addLeaderElectionTo(o, cfg)
58+
addCacheByObjectTo(o, cfg)
59+
5160
if o.Metrics.BindAddress == "" && cfg.Metrics.BindAddress != "" {
5261
o.Metrics.BindAddress = cfg.Metrics.BindAddress
5362
}
@@ -94,6 +103,24 @@ func addTo(o *ctrl.Options, cfg *configapi.Configuration) {
94103
}
95104
}
96105

106+
func addCacheByObjectTo(o *ctrl.Options, cfg *configapi.Configuration) {
107+
if cfg.Namespace == nil {
108+
// Invalid source; noop. This should not be reached
109+
// due to prior defaulting/validation.
110+
return
111+
}
112+
113+
if o.Cache.ByObject == nil {
114+
o.Cache.ByObject = make(map[ctrlclient.Object]ctrlcache.ByObject)
115+
}
116+
117+
o.Cache.ByObject[objectKeySecret] = ctrlcache.ByObject{
118+
Namespaces: map[string]ctrlcache.Config{
119+
*cfg.Namespace: {},
120+
},
121+
}
122+
}
123+
97124
func addLeaderElectionTo(o *ctrl.Options, cfg *configapi.Configuration) {
98125
if cfg.LeaderElection == nil {
99126
// The source does not have any configuration; noop

0 commit comments

Comments
 (0)