@@ -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,68 @@ 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" },
145+ Verbs : []string {"create" , "update" , "delete" },
146+ },
147+ {
148+ APIGroups : []string {"" }, // Core API group
149+ Resources : []string {"namespaces" },
150+ Verbs : []string {"create" , "update" , "delete" },
151+ },
152+ {
153+ APIGroups : []string {"networking.k8s.io" },
154+ Resources : []string {"networkpolicies" },
155+ Verbs : []string {"create" , "update" , "delete" },
156+ },
157+ },
158+ }
159+ Expect (hubClient .Create (ctx , clusterRole )).To (Succeed ())
160+
161+ // --- Create ClusterRoleBinding ---
162+ clusterRoleBinding = & rbacv1.ClusterRoleBinding {
163+ ObjectMeta : metav1.ObjectMeta {
164+ Name : "service-accounts-binding" ,
165+ },
166+ Subjects : []rbacv1.Subject {
167+ {
168+ Kind : rbacv1 .ServiceAccountKind ,
169+ Name : "service-account-controller" , // The service account's name
170+ Namespace : "kube-system" , // The service account's namespace
171+ },
172+ {
173+ Kind : rbacv1 .ServiceAccountKind ,
174+ Name : "service-account-controller" , // The service account's name
175+ Namespace : "fleet-system" , // The service account's namespace
176+ },
177+ },
178+ RoleRef : rbacv1.RoleRef {
179+ APIGroup : rbacv1 .GroupName ,
180+ Kind : "ClusterRole" ,
181+ Name : clusterRole .Name ,
182+ },
183+ }
184+ Expect (hubClient .Create (ctx , clusterRoleBinding )).To (Succeed ())
185+ })
186+
187+ AfterAll (func () {
188+ By ("Cleaning up service account permissions" )
189+ Expect (hubClient .Delete (ctx , clusterRoleBinding )).To (Succeed ())
190+ Expect (hubClient .Delete (ctx , clusterRole )).To (Succeed ())
191+ })
192+
130193 It ("The VAP and its binding should exist" , func () {
131194 var vap admissionregistrationv1.ValidatingAdmissionPolicy
132195 Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapName }, & vap )).Should (Succeed (), "ValidatingAdmissionPolicy should be installed" )
@@ -169,6 +232,24 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
169232
170233 Expect (sysMastersClient .Delete (ctx , managedNS )).To (Succeed ())
171234 })
235+
236+ It ("should allow CREATE operation on managed namespace for system:serviceaccount:kube-system:default user" , func () {
237+ managedNS := createManagedNamespace ("test-managed-ns-kubesystem-sa" )
238+ By ("expecting successful CREATE operation with system:serviceaccount:kube-system:default user" )
239+ Expect (kubeSystemClient .Create (ctx , managedNS )).To (Succeed ())
240+
241+ By ("expecting successful DELETE operation on managed namespace" )
242+ Expect (sysMastersClient .Delete (ctx , managedNS )).To (Succeed ())
243+ })
244+
245+ It ("should allow CREATE operation on managed namespace for system:serviceaccounts:fleet-system user" , func () {
246+ managedNS := createManagedNamespace ("test-managed-ns-fleet-system" )
247+ By ("expecting successful CREATE operation with system:serviceaccounts:fleet-system user" )
248+ Expect (fleetSystemClient .Create (ctx , managedNS )).To (Succeed ())
249+
250+ By ("expecting successful DELETE operation on managed namespace" )
251+ Expect (fleetSystemClient .Delete (ctx , managedNS )).To (Succeed ())
252+ })
172253 })
173254
174255 Context ("When the namespace exists" , Ordered , func () {
@@ -227,6 +308,40 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
227308 }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
228309 })
229310
311+ It ("should allow UPDATE operation on managed namespace for system:serviceaccounts:kube-system user" , func () {
312+ var updateErr error
313+ Eventually (func () error {
314+ var ns corev1.Namespace
315+ if err := sysMastersClient .Get (ctx , types.NamespacedName {Name : managedNS .Name }, & ns ); err != nil {
316+ return err
317+ }
318+ ns .Annotations = map [string ]string {"test" : "annotation" }
319+ By ("expecting denial of UPDATE operation on managed namespace" )
320+ updateErr = kubeSystemClient .Update (ctx , & ns )
321+ if k8sErrors .IsConflict (updateErr ) {
322+ return updateErr
323+ }
324+ return nil
325+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
326+ })
327+
328+ It ("should allow UPDATE operation on managed namespace for system:serviceaccounts:fleet-system user" , func () {
329+ var updateErr error
330+ Eventually (func () error {
331+ var ns corev1.Namespace
332+ if err := sysMastersClient .Get (ctx , types.NamespacedName {Name : managedNS .Name }, & ns ); err != nil {
333+ return err
334+ }
335+ ns .Annotations = map [string ]string {"test" : "annotation" }
336+ By ("expecting denial of UPDATE operation on managed namespace" )
337+ updateErr = fleetSystemClient .Update (ctx , & ns )
338+ if k8sErrors .IsConflict (updateErr ) {
339+ return updateErr
340+ }
341+ return nil
342+ }, eventuallyDuration , eventuallyInterval ).Should (Succeed ())
343+ })
344+
230345 Context ("For other resources in scope" , func () {
231346 It ("should deny creating managed resource quotas" , func () {
232347 rq := createManagedResourceQuota ("default" , "default" )
@@ -289,6 +404,42 @@ var _ = Describe("ValidatingAdmissionPolicy for Managed Resources", Label("manag
289404 err = sysMastersClient .Delete (ctx , crp )
290405 Expect (err ).To (BeNil (), "system:masters user should delete managed CRP" )
291406 })
407+
408+ It ("should allow CREATE operation on managed ResourceQuota for kube-system service account" , func () {
409+ rq := createManagedResourceQuota (managedNS .Name , "test-rq-kubesystem-sa" )
410+ By ("expecting successful CREATE operation with kube-system service account" )
411+ Expect (kubeSystemClient .Create (ctx , rq )).To (Succeed ())
412+
413+ By ("expecting successful DELETE operation with kube-system service account" )
414+ Expect (kubeSystemClient .Delete (ctx , rq )).To (Succeed ())
415+ })
416+
417+ It ("should allow CREATE operation on managed ResourceQuota for fleet-system service account" , func () {
418+ rq := createManagedResourceQuota (managedNS .Name , "test-rq-fleetsystem-sa" )
419+ By ("expecting successful CREATE operation with fleet-system service account" )
420+ Expect (fleetSystemClient .Create (ctx , rq )).To (Succeed ())
421+
422+ By ("expecting successful DELETE operation with fleet-system service account" )
423+ Expect (fleetSystemClient .Delete (ctx , rq )).To (Succeed ())
424+ })
425+
426+ It ("should allow CREATE operation on managed NetworkPolicy for kube-system service account" , func () {
427+ netpol := createManagedNetworkPolicy (managedNS .Name , "test-netpol-kubesystem-sa" )
428+ By ("expecting successful CREATE operation with kube-system service account" )
429+ Expect (kubeSystemClient .Create (ctx , netpol )).To (Succeed ())
430+
431+ By ("expecting successful DELETE operation with kube-system service account" )
432+ Expect (kubeSystemClient .Delete (ctx , netpol )).To (Succeed ())
433+ })
434+
435+ It ("should allow CREATE operation on managed NetworkPolicy for fleet-system service account" , func () {
436+ netpol := createManagedNetworkPolicy (managedNS .Name , "test-netpol-fleetsystem-sa" )
437+ By ("expecting successful CREATE operation with fleet-system service account" )
438+ Expect (fleetSystemClient .Create (ctx , netpol )).To (Succeed ())
439+
440+ By ("expecting successful DELETE operation with fleet-system service account" )
441+ Expect (fleetSystemClient .Delete (ctx , netpol )).To (Succeed ())
442+ })
292443 })
293444
294445 AfterAll (func () {
0 commit comments