@@ -24,6 +24,7 @@ import (
2424 "k8s.io/apimachinery/pkg/watch"
2525 "k8s.io/apiserver/pkg/endpoints/request"
2626 "k8s.io/apiserver/pkg/registry/rest"
27+ "k8s.io/klog/v2"
2728 "sigs.k8s.io/controller-runtime/pkg/client"
2829
2930 corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1"
@@ -123,8 +124,18 @@ func (r *REST) Get(
123124 return nil , apierrors .NewNotFound (r .gvr .GroupResource (), name )
124125 }
125126
127+ // Check if user has access to this namespace
128+ hasAccess , err := r .hasAccessToNamespace (ctx , name )
129+ if err != nil {
130+ return nil , err
131+ }
132+ if ! hasAccess {
133+ // Return NotFound instead of Forbidden to prevent enumeration
134+ return nil , apierrors .NewNotFound (r .gvr .GroupResource (), name )
135+ }
136+
126137 ns := & corev1.Namespace {}
127- err : = r .c .Get (ctx , types.NamespacedName {Namespace : "" , Name : name }, ns , & client.GetOptions {Raw : opts })
138+ err = r .c .Get (ctx , types.NamespacedName {Namespace : "" , Name : name }, ns , & client.GetOptions {Raw : opts })
128139 if err != nil {
129140 return nil , err
130141 }
@@ -189,6 +200,17 @@ func (r *REST) Watch(ctx context.Context, opts *metainternal.ListOptions) (watch
189200 continue
190201 }
191202
203+ // Check if user has access to this namespace
204+ hasAccess , err := r .hasAccessToNamespace (ctx , ns .Name )
205+ if err != nil {
206+ klog .Errorf ("Failed to check access for namespace %s in watch: %v" , ns .Name , err )
207+ continue
208+ }
209+ if ! hasAccess {
210+ // User doesn't have access, skip this event
211+ continue
212+ }
213+
192214 out := & corev1alpha1.TenantNamespace {
193215 TypeMeta : metav1.TypeMeta {
194216 APIVersion : corev1alpha1 .SchemeGroupVersion .String (),
@@ -359,7 +381,11 @@ func (r *REST) filterAccessible(
359381 break subjectLoop
360382 }
361383 case "ServiceAccount" :
362- if u .GetName () == fmt .Sprintf ("system:serviceaccount:%s:%s" , subj .Namespace , subj .Name ) {
384+ saNamespace := subj .Namespace
385+ if saNamespace == "" {
386+ saNamespace = rbs .Items [i ].Namespace
387+ }
388+ if u .GetName () == fmt .Sprintf ("system:serviceaccount:%s:%s" , saNamespace , subj .Name ) {
363389 allowedNameSet [rbs .Items [i ].Namespace ] = struct {}{}
364390 break subjectLoop
365391 }
@@ -373,6 +399,65 @@ func (r *REST) filterAccessible(
373399 return allowed , nil
374400}
375401
402+ // hasAccessToNamespace checks if the user has access to a single namespace.
403+ // This is optimized for Get/Watch operations where we check one namespace at a time.
404+ // It lists RoleBindings only in the target namespace instead of all cluster RoleBindings.
405+ func (r * REST ) hasAccessToNamespace (
406+ ctx context.Context ,
407+ namespace string ,
408+ ) (bool , error ) {
409+ u , ok := request .UserFrom (ctx )
410+ if ! ok {
411+ return false , fmt .Errorf ("user missing in context" )
412+ }
413+
414+ // Check privileged groups
415+ groups := make (map [string ]struct {})
416+ for _ , group := range u .GetGroups () {
417+ groups [group ] = struct {}{}
418+ }
419+ if _ , ok := groups ["system:masters" ]; ok {
420+ return true , nil
421+ }
422+ if _ , ok := groups ["cozystack-cluster-admin" ]; ok {
423+ return true , nil
424+ }
425+
426+ // List RoleBindings only in the target namespace
427+ rbs := & rbacv1.RoleBindingList {}
428+ err := r .c .List (ctx , rbs , client .InNamespace (namespace ))
429+ if err != nil {
430+ return false , fmt .Errorf ("failed to list rolebindings in %s: %w" , namespace , err )
431+ }
432+
433+ // Check if user is in any RoleBinding subjects
434+ for i := range rbs .Items {
435+ for j := range rbs .Items [i ].Subjects {
436+ subj := rbs .Items [i ].Subjects [j ]
437+ switch subj .Kind {
438+ case "Group" :
439+ if _ , ok := groups [subj .Name ]; ok {
440+ return true , nil
441+ }
442+ case "User" :
443+ if subj .Name == u .GetName () {
444+ return true , nil
445+ }
446+ case "ServiceAccount" :
447+ saNamespace := subj .Namespace
448+ if saNamespace == "" {
449+ saNamespace = rbs .Items [i ].Namespace
450+ }
451+ if u .GetName () == fmt .Sprintf ("system:serviceaccount:%s:%s" , saNamespace , subj .Name ) {
452+ return true , nil
453+ }
454+ }
455+ }
456+ }
457+
458+ return false , nil
459+ }
460+
376461// -----------------------------------------------------------------------------
377462// Boiler-plate
378463// -----------------------------------------------------------------------------
0 commit comments