mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-10-31 18:28:13 +00:00
Add SELinuxChangePolicy validation
This commit is contained in:
@@ -20557,6 +20557,7 @@ func TestValidateOSFields(t *testing.T) {
|
||||
"SecurityContext.RunAsGroup",
|
||||
"SecurityContext.RunAsUser",
|
||||
"SecurityContext.SELinuxOptions",
|
||||
"SecurityContext.SELinuxChangePolicy",
|
||||
"SecurityContext.SeccompProfile",
|
||||
"SecurityContext.ShareProcessNamespace",
|
||||
"SecurityContext.SupplementalGroups",
|
||||
@@ -25000,3 +25001,78 @@ func TestValidateContainerStatusAllocatedResourcesStatus(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateSELinuxChangePolicy(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pod *core.Pod
|
||||
allowOnlyRecursive bool
|
||||
wantErrs field.ErrorList
|
||||
}{
|
||||
{
|
||||
name: "nil is valid",
|
||||
pod: podtest.MakePod("pod", podtest.SetSecurityContext(&core.PodSecurityContext{
|
||||
SELinuxChangePolicy: nil,
|
||||
})),
|
||||
allowOnlyRecursive: false,
|
||||
wantErrs: nil,
|
||||
},
|
||||
{
|
||||
name: "Recursive is always valid",
|
||||
pod: podtest.MakePod("pod", podtest.SetSecurityContext(&core.PodSecurityContext{
|
||||
SELinuxChangePolicy: ptr.To(core.SELinuxChangePolicyRecursive),
|
||||
})),
|
||||
allowOnlyRecursive: false,
|
||||
wantErrs: nil,
|
||||
},
|
||||
{
|
||||
name: "MountOption is not valid when AllowOnlyRecursiveSELinuxChangePolicy",
|
||||
pod: podtest.MakePod("pod", podtest.SetSecurityContext(&core.PodSecurityContext{
|
||||
SELinuxChangePolicy: ptr.To(core.SELinuxChangePolicyMountOption),
|
||||
})),
|
||||
allowOnlyRecursive: true,
|
||||
wantErrs: field.ErrorList{
|
||||
field.NotSupported(
|
||||
field.NewPath("spec", "securityContext", "seLinuxChangePolicy"),
|
||||
core.PodSELinuxChangePolicy("MountOption"),
|
||||
[]string{"Recursive"}),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "MountOption is valid when not AllowOnlyRecursiveSELinuxChangePolicy",
|
||||
pod: podtest.MakePod("pod", podtest.SetSecurityContext(&core.PodSecurityContext{
|
||||
SELinuxChangePolicy: ptr.To(core.SELinuxChangePolicyMountOption),
|
||||
})),
|
||||
allowOnlyRecursive: false,
|
||||
wantErrs: nil,
|
||||
},
|
||||
{
|
||||
name: "invalid value",
|
||||
pod: podtest.MakePod("pod", podtest.SetSecurityContext(&core.PodSecurityContext{
|
||||
SELinuxChangePolicy: ptr.To(core.PodSELinuxChangePolicy("InvalidValue")),
|
||||
})),
|
||||
allowOnlyRecursive: false,
|
||||
wantErrs: field.ErrorList{
|
||||
field.NotSupported(field.NewPath("spec", "securityContext", "seLinuxChangePolicy"),
|
||||
core.PodSELinuxChangePolicy("InvalidValue"),
|
||||
[]string{"MountOption", "Recursive"}),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
opts := PodValidationOptions{
|
||||
AllowOnlyRecursiveSELinuxChangePolicy: tc.allowOnlyRecursive,
|
||||
}
|
||||
errs := ValidatePodSpec(&tc.pod.Spec, &tc.pod.ObjectMeta, field.NewPath("spec"), opts)
|
||||
if len(errs) == 0 {
|
||||
errs = nil
|
||||
}
|
||||
if diff := cmp.Diff(tc.wantErrs, errs); diff != "" {
|
||||
t.Errorf("unexpected field errors (-want, +got):\n%s", diff)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user