@@ -855,8 +855,20 @@ func TestPodSecurityContextMask(t *testing.T) {
855855 },
856856 }
857857
858- want := & corev1.PodSecurityContext {}
859- ctx := context .Background ()
858+ want := & corev1.PodSecurityContext {
859+ SeccompProfile : & corev1.SeccompProfile {
860+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
861+ LocalhostProfile : nil ,
862+ },
863+ }
864+ ctx := config .ToContext (context .Background (),
865+ & config.Config {
866+ Features : & config.Features {
867+ SecurePodDefaults : config .Enabled ,
868+ PodSpecSecurityContext : config .Disabled ,
869+ },
870+ },
871+ )
860872
861873 got := PodSecurityContextMask (ctx , in )
862874
@@ -967,6 +979,111 @@ func TestPodSecurityContextMask_SecurePodDefaultsEnabled(t *testing.T) {
967979 }
968980}
969981
982+ func TestPodSecurityContextMask_SecurePodDefaults_AllowRootBounded (t * testing.T ) {
983+ // Test that AllowRootBounded works the same as Enabled for masking purposes
984+ want := & corev1.PodSecurityContext {
985+ SeccompProfile : & corev1.SeccompProfile {
986+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
987+ },
988+ }
989+
990+ in := & corev1.PodSecurityContext {
991+ SELinuxOptions : & corev1.SELinuxOptions {},
992+ WindowsOptions : & corev1.WindowsSecurityContextOptions {},
993+ SupplementalGroups : []int64 {1 },
994+ Sysctls : []corev1.Sysctl {},
995+ RunAsUser : ptr .Int64 (1 ),
996+ RunAsGroup : ptr .Int64 (1 ),
997+ RunAsNonRoot : ptr .Bool (true ),
998+ FSGroup : ptr .Int64 (1 ),
999+ SeccompProfile : & corev1.SeccompProfile {
1000+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
1001+ },
1002+ }
1003+
1004+ ctx := config .ToContext (context .Background (),
1005+ & config.Config {
1006+ Features : & config.Features {
1007+ SecurePodDefaults : config .AllowRootBounded ,
1008+ PodSpecSecurityContext : config .Disabled ,
1009+ },
1010+ },
1011+ )
1012+
1013+ got := PodSecurityContextMask (ctx , in )
1014+
1015+ if & want == & got {
1016+ t .Error ("Input and output share addresses. Want different addresses" )
1017+ }
1018+
1019+ if diff , err := kmp .SafeDiff (want , got ); err != nil {
1020+ t .Error ("Got error comparing output, err =" , err )
1021+ } else if diff != "" {
1022+ t .Error ("PodSecurityContextMask (-want, +got):" , diff )
1023+ }
1024+ }
1025+
1026+ func TestCapabilitiesMask_SecurePodDefaults_AllowRootBounded (t * testing.T ) {
1027+ // Ensures users can only add NET_BIND_SERVICE or nil capabilities
1028+ tests := []struct {
1029+ name string
1030+ in * corev1.Capabilities
1031+ want * corev1.Capabilities
1032+ }{{
1033+ name : "empty" ,
1034+ in : & corev1.Capabilities {
1035+ Add : nil ,
1036+ },
1037+ want : & corev1.Capabilities {
1038+ Add : nil ,
1039+ },
1040+ }, {
1041+ name : "allows NET_BIND_SERVICE capability" ,
1042+ in : & corev1.Capabilities {
1043+ Add : []corev1.Capability {"NET_BIND_SERVICE" },
1044+ },
1045+ want : & corev1.Capabilities {
1046+ Add : []corev1.Capability {"NET_BIND_SERVICE" },
1047+ },
1048+ }, {
1049+ name : "prevents restricted fields" ,
1050+ in : & corev1.Capabilities {
1051+ Add : []corev1.Capability {"CHOWN" },
1052+ },
1053+ want : & corev1.Capabilities {
1054+ Add : nil ,
1055+ },
1056+ }}
1057+
1058+ for _ , test := range tests {
1059+ ctx := config .ToContext (context .Background (),
1060+ & config.Config {
1061+ Features : & config.Features {
1062+ SecurePodDefaults : config .AllowRootBounded ,
1063+ },
1064+ },
1065+ )
1066+
1067+ t .Run (test .name , func (t * testing.T ) {
1068+ got := CapabilitiesMask (ctx , test .in )
1069+
1070+ if & test .want == & got {
1071+ t .Error ("Input and output share addresses. Want different addresses" )
1072+ }
1073+
1074+ if diff , err := kmp .SafeDiff (test .want , got ); err != nil {
1075+ t .Error ("Got error comparing output, err =" , err )
1076+ } else if diff != "" {
1077+ t .Error ("CapabilitiesMask (-want, +got):" , diff )
1078+ }
1079+
1080+ if got = CapabilitiesMask (ctx , nil ); got != nil {
1081+ t .Errorf ("CapabilitiesMask = %v, want: nil" , got )
1082+ }
1083+ })
1084+ }
1085+ }
1086+
9701087func TestSecurityContextMask (t * testing.T ) {
9711088 mtype := corev1 .UnmaskedProcMount
9721089 want := & corev1.SecurityContext {
0 commit comments