@@ -26,6 +26,7 @@ import (
26
26
corev1 "k8s.io/api/core/v1"
27
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
28
"k8s.io/apimachinery/pkg/runtime"
29
+ "k8s.io/utils/ptr"
29
30
30
31
"github.com/project-codeflare/codeflare-operator/pkg/config"
31
32
)
@@ -277,6 +278,15 @@ func TestValidateCreate(t *testing.T) {
277
278
ReadOnly : true ,
278
279
},
279
280
},
281
+ SecurityContext : & corev1.SecurityContext {
282
+ AllowPrivilegeEscalation : ptr .To (false ),
283
+ Capabilities : & corev1.Capabilities {
284
+ Drop : []corev1.Capability {"ALL" },
285
+ },
286
+ SeccompProfile : & corev1.SeccompProfile {
287
+ Type : "RuntimeDefault" ,
288
+ },
289
+ },
280
290
},
281
291
},
282
292
Volumes : []corev1.Volume {
@@ -346,6 +356,14 @@ func TestValidateCreate(t *testing.T) {
346
356
test .Expect (err ).Should (HaveOccurred (), "Expected errors on call to ValidateCreate function due to manipulated head group service account name" )
347
357
})
348
358
359
+ t .Run ("Negative: Expected errors on call to ValidateCreate function due to manipulated head group container SecurityContext" , func (t * testing.T ) {
360
+ for i := range invalidRayCluster .Spec .HeadGroupSpec .Template .Spec .Containers {
361
+ invalidRayCluster .Spec .HeadGroupSpec .Template .Spec .Containers [i ].SecurityContext .AllowPrivilegeEscalation = ptr .To (true )
362
+ }
363
+ _ , err = rcWebhook .ValidateCreate (test .Ctx (), runtime .Object (invalidRayCluster ))
364
+ test .Expect (err ).Should (HaveOccurred (), "Expected errors on call to ValidateCreate function due to manipulated head group container SecurityContext" )
365
+ })
366
+
349
367
}
350
368
351
369
func TestValidateUpdate (t * testing.T ) {
@@ -409,6 +427,15 @@ func TestValidateUpdate(t *testing.T) {
409
427
ReadOnly : true ,
410
428
},
411
429
},
430
+ SecurityContext : & corev1.SecurityContext {
431
+ AllowPrivilegeEscalation : ptr .To (false ),
432
+ Capabilities : & corev1.Capabilities {
433
+ Drop : []corev1.Capability {"ALL" },
434
+ },
435
+ SeccompProfile : & corev1.SeccompProfile {
436
+ Type : "RuntimeDefault" ,
437
+ },
438
+ },
412
439
},
413
440
},
414
441
InitContainers : []corev1.Container {
@@ -485,6 +512,15 @@ func TestValidateUpdate(t *testing.T) {
485
512
{Name : "RAY_TLS_SERVER_KEY" , Value : "/home/ray/workspace/tls/server.key" },
486
513
{Name : "RAY_TLS_CA_CERT" , Value : "/home/ray/workspace/tls/ca.crt" },
487
514
},
515
+ SecurityContext : & corev1.SecurityContext {
516
+ AllowPrivilegeEscalation : ptr .To (false ),
517
+ Capabilities : & corev1.Capabilities {
518
+ Drop : []corev1.Capability {"ALL" },
519
+ },
520
+ SeccompProfile : & corev1.SeccompProfile {
521
+ Type : "RuntimeDefault" ,
522
+ },
523
+ },
488
524
},
489
525
},
490
526
InitContainers : []corev1.Container {
@@ -644,4 +680,22 @@ func TestValidateUpdate(t *testing.T) {
644
680
_ , err := rcWebhook .ValidateUpdate (test .Ctx (), runtime .Object (validRayCluster ), runtime .Object (invalidRayCluster ))
645
681
test .Expect (err ).Should (HaveOccurred (), "Expected errors on call to ValidateUpdate function due to manipulated env vars in the worker group" )
646
682
})
683
+
684
+ t .Run ("Negative: Expected errors on call to ValidateUpdate function due to manipulated SecurityContext in the head group container" , func (t * testing.T ) {
685
+ for i := range invalidRayCluster .Spec .HeadGroupSpec .Template .Spec .Containers {
686
+ invalidRayCluster .Spec .HeadGroupSpec .Template .Spec .Containers [i ].SecurityContext .AllowPrivilegeEscalation = ptr .To (true )
687
+ }
688
+ _ , err := rcWebhook .ValidateUpdate (test .Ctx (), runtime .Object (validRayCluster ), runtime .Object (invalidRayCluster ))
689
+ test .Expect (err ).Should (HaveOccurred (), "Expected errors on call to ValidateUpdate function due to manipulated SecurityContext in the head group container" )
690
+ })
691
+
692
+ t .Run ("Negative: Expected errors on call to ValidateUpdate function due to manipulated SecurityContext in the worker group container" , func (t * testing.T ) {
693
+ for i := range invalidRayCluster .Spec .WorkerGroupSpecs {
694
+ for j := range invalidRayCluster .Spec .WorkerGroupSpecs [i ].Template .Spec .Containers {
695
+ invalidRayCluster .Spec .WorkerGroupSpecs [i ].Template .Spec .Containers [j ].SecurityContext .AllowPrivilegeEscalation = ptr .To (true )
696
+ }
697
+ }
698
+ _ , err := rcWebhook .ValidateUpdate (test .Ctx (), runtime .Object (validRayCluster ), runtime .Object (invalidRayCluster ))
699
+ test .Expect (err ).Should (HaveOccurred (), "Expected errors on call to ValidateUpdate function due to manipulated SecurityContext in the worker group container" )
700
+ })
647
701
}
0 commit comments