diff --git a/pkg/security/podsecuritypolicy/provider.go b/pkg/security/podsecuritypolicy/provider.go index bcb6e33d237..b8f0bc5353d 100644 --- a/pkg/security/podsecuritypolicy/provider.go +++ b/pkg/security/podsecuritypolicy/provider.go @@ -250,6 +250,12 @@ func (s *simpleProvider) ValidateContainerSecurityContext(pod *api.Pod, containe allErrs = append(allErrs, s.hasInvalidHostPort(&c, idxPath)...) } + containersPath = fldPath.Child("initContainers") + for idx, c := range pod.Spec.InitContainers { + idxPath := containersPath.Index(idx) + allErrs = append(allErrs, s.hasInvalidHostPort(&c, idxPath)...) + } + if !s.psp.Spec.HostPID && pod.Spec.SecurityContext.HostPID { allErrs = append(allErrs, field.Invalid(fldPath.Child("hostPID"), pod.Spec.SecurityContext.HostPID, "Host PID is not allowed to be used")) } diff --git a/plugin/pkg/admission/security/podsecuritypolicy/admission_test.go b/plugin/pkg/admission/security/podsecuritypolicy/admission_test.go index 4ea65d5d57e..27b9a6448ce 100644 --- a/plugin/pkg/admission/security/podsecuritypolicy/admission_test.go +++ b/plugin/pkg/admission/security/podsecuritypolicy/admission_test.go @@ -394,8 +394,11 @@ func TestAdmitHostPorts(t *testing.T) { }, } - for k, v := range tests { - testPSPAdmit(k, v.psps, v.pod, v.shouldPass, v.expectedPSP, t) + for i := 0; i < 2; i++ { + for k, v := range tests { + v.pod.Spec.Containers, v.pod.Spec.InitContainers = v.pod.Spec.InitContainers, v.pod.Spec.Containers + testPSPAdmit(k, v.psps, v.pod, v.shouldPass, v.expectedPSP, t) + } } }