@@ -26,6 +26,7 @@ import (
2626 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2727 corev1 "k8s.io/api/core/v1"
2828 networkingv1 "k8s.io/api/networking/v1"
29+ rbacv1 "k8s.io/api/rbac/v1"
2930 k8sErrors "k8s.io/apimachinery/pkg/api/errors"
3031 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132 "k8s.io/apimachinery/pkg/types"
@@ -127,6 +128,63 @@ func expectDeniedByVAP(err error) {
127128}
128129
129130var _ = Describe ("ValidatingAdmissionPolicy for Managed Resources" , Label ("managedresource" ), Ordered , func () {
131+ var clusterRole * rbacv1.ClusterRole
132+ var clusterRoleBinding * rbacv1.ClusterRoleBinding
133+
134+ BeforeAll (func () {
135+ By ("Give permissions to service accounts" )
136+ // --- Create ClusterRole ---
137+ clusterRole = & rbacv1.ClusterRole {
138+ ObjectMeta : metav1.ObjectMeta {
139+ Name : "allow-certain-managed-resources" ,
140+ },
141+ Rules : []rbacv1.PolicyRule {
142+ {
143+ APIGroups : []string {"" }, // Core API group
144+ Resources : []string {"resourcequotas" , "namespaces" },
145+ Verbs : []string {"create" , "update" , "delete" },
146+ },
147+ {
148+ APIGroups : []string {"networking.k8s.io" },
149+ Resources : []string {"networkpolicies" },
150+ Verbs : []string {"create" , "update" , "delete" },
151+ },
152+ },
153+ }
154+ Expect (hubClient .Create (ctx , clusterRole )).To (Succeed ())
155+
156+ // --- Create ClusterRoleBinding ---
157+ clusterRoleBinding = & rbacv1.ClusterRoleBinding {
158+ ObjectMeta : metav1.ObjectMeta {
159+ Name : "service-accounts-binding-for-managed-resources" ,
160+ },
161+ Subjects : []rbacv1.Subject {
162+ {
163+ Kind : rbacv1 .ServiceAccountKind ,
164+ Name : "service-account-controller" , // The service account's name
165+ Namespace : "kube-system" , // The service account's namespace
166+ },
167+ {
168+ Kind : rbacv1 .ServiceAccountKind ,
169+ Name : "service-account-controller" , // The service account's name
170+ Namespace : "fleet-system" , // The service account's namespace
171+ },
172+ },
173+ RoleRef : rbacv1.RoleRef {
174+ APIGroup : rbacv1 .GroupName ,
175+ Kind : "ClusterRole" ,
176+ Name : clusterRole .Name ,
177+ },
178+ }
179+ Expect (hubClient .Create (ctx , clusterRoleBinding )).To (Succeed ())
180+ })
181+
182+ AfterAll (func () {
183+ By ("Cleaning up service account permissions" )
184+ Expect (hubClient .Delete (ctx , clusterRoleBinding )).To (Succeed ())
185+ Expect (hubClient .Delete (ctx , clusterRole )).To (Succeed ())
186+ })
187+
130188 It ("The VAP and its binding should exist" , func () {
131189 var vap admissionregistrationv1.ValidatingAdmissionPolicy
132190 Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapName }, & vap )).Should (Succeed (), "ValidatingAdmissionPolicy should be installed" )
@@ -169,6 +227,24 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
169227
170228 Expect (sysMastersClient .Delete (ctx , managedNS )).To (Succeed ())
171229 })
230+
231+ It ("should allow CREATE operation on managed namespace for system:serviceaccount:kube-system user" , func () {
232+ managedNS := createManagedNamespace ("test-managed-ns-kubesystem-sa" )
233+ By ("expecting successful CREATE operation with system:serviceaccount:kube-system user" )
234+ Expect (kubeSystemClient .Create (ctx , managedNS )).To (Succeed ())
235+
236+ By ("expecting successful DELETE operation on managed namespace" )
237+ Expect (sysMastersClient .Delete (ctx , managedNS )).To (Succeed ())
238+ })
239+
240+ It ("should allow CREATE operation on managed namespace for system:serviceaccounts:fleet-system user" , func () {
241+ managedNS := createManagedNamespace ("test-managed-ns-fleet-system" )
242+ By ("expecting successful CREATE operation with system:serviceaccounts:fleet-system user" )
243+ Expect (fleetSystemClient .Create (ctx , managedNS )).To (Succeed ())
244+
245+ By ("expecting successful DELETE operation on managed namespace" )
246+ Expect (fleetSystemClient .Delete (ctx , managedNS )).To (Succeed ())
247+ })
172248 })
173249
174250 Context ("When the namespace exists" , Ordered , func () {
@@ -227,6 +303,40 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
227303 }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
228304 })
229305
306+ It ("should allow UPDATE operation on managed namespace for system:serviceaccounts:kube-system user" , func () {
307+ var updateErr error
308+ Eventually (func () error {
309+ var ns corev1.Namespace
310+ if err := sysMastersClient .Get (ctx , types.NamespacedName {Name : managedNS .Name }, & ns ); err != nil {
311+ return err
312+ }
313+ ns .Annotations = map [string ]string {"test" : "annotation" }
314+ By ("expecting denial of UPDATE operation on managed namespace" )
315+ updateErr = kubeSystemClient .Update (ctx , & ns )
316+ if k8sErrors .IsConflict (updateErr ) {
317+ return updateErr
318+ }
319+ return nil
320+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
321+ })
322+
323+ It ("should allow UPDATE operation on managed namespace for system:serviceaccounts:fleet-system user" , func () {
324+ var updateErr error
325+ Eventually (func () error {
326+ var ns corev1.Namespace
327+ if err := sysMastersClient .Get (ctx , types.NamespacedName {Name : managedNS .Name }, & ns ); err != nil {
328+ return err
329+ }
330+ ns .Annotations = map [string ]string {"test" : "annotation" }
331+ By ("expecting denial of UPDATE operation on managed namespace" )
332+ updateErr = fleetSystemClient .Update (ctx , & ns )
333+ if k8sErrors .IsConflict (updateErr ) {
334+ return updateErr
335+ }
336+ return nil
337+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
338+ })
339+
230340 Context ("For other resources in scope" , func () {
231341 It ("should deny creating managed resource quotas" , func () {
232342 rq := createManagedResourceQuota ("default" , "default" )
@@ -289,6 +399,42 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
289399 err = sysMastersClient .Delete (ctx , crp )
290400 Expect (err ).To (BeNil (), "system:masters user should delete managed CRP" )
291401 })
402+
403+ It ("should allow CREATE operation on managed ResourceQuota for kube-system service account" , func () {
404+ rq := createManagedResourceQuota (managedNS .Name , "default" )
405+ By ("expecting successful CREATE operation with kube-system service account" )
406+ Expect (kubeSystemClient .Create (ctx , rq )).To (Succeed ())
407+
408+ By ("expecting successful DELETE operation with kube-system service account" )
409+ Expect (kubeSystemClient .Delete (ctx , rq )).To (Succeed ())
410+ })
411+
412+ It ("should allow CREATE operation on managed ResourceQuota for fleet-system service account" , func () {
413+ rq := createManagedResourceQuota (managedNS .Name , "default" )
414+ By ("expecting successful CREATE operation with fleet-system service account" )
415+ Expect (fleetSystemClient .Create (ctx , rq )).To (Succeed ())
416+
417+ By ("expecting successful DELETE operation with fleet-system service account" )
418+ Expect (fleetSystemClient .Delete (ctx , rq )).To (Succeed ())
419+ })
420+
421+ It ("should allow CREATE operation on managed NetworkPolicy for kube-system service account" , func () {
422+ netpol := createManagedNetworkPolicy (managedNS .Name , "default" )
423+ By ("expecting successful CREATE operation with kube-system service account" )
424+ Expect (kubeSystemClient .Create (ctx , netpol )).To (Succeed ())
425+
426+ By ("expecting successful DELETE operation with kube-system service account" )
427+ Expect (kubeSystemClient .Delete (ctx , netpol )).To (Succeed ())
428+ })
429+
430+ It ("should allow CREATE operation on managed NetworkPolicy for fleet-system service account" , func () {
431+ netpol := createManagedNetworkPolicy (managedNS .Name , "default" )
432+ By ("expecting successful CREATE operation with fleet-system service account" )
433+ Expect (fleetSystemClient .Create (ctx , netpol )).To (Succeed ())
434+
435+ By ("expecting successful DELETE operation with fleet-system service account" )
436+ Expect (fleetSystemClient .Delete (ctx , netpol )).To (Succeed ())
437+ })
292438 })
293439
294440 AfterAll (func () {
0 commit comments