Add SELinuxChangePolicy validation

This commit is contained in:
Jan Safranek
2024-10-08 10:27:08 +02:00
parent 3867cb40ad
commit 6ca7b959e4
4 changed files with 282 additions and 0 deletions

View File

@@ -393,6 +393,8 @@ func GetValidationOptionsFromPodSpecAndMeta(podSpec, oldPodSpec *api.PodSpec, po
opts.AllowRelaxedEnvironmentVariableValidation = useRelaxedEnvironmentVariableValidation(podSpec, oldPodSpec)
opts.AllowRelaxedDNSSearchValidation = useRelaxedDNSSearchValidation(oldPodSpec)
opts.AllowOnlyRecursiveSELinuxChangePolicy = useOnlyRecursiveSELinuxChangePolicy(oldPodSpec)
if oldPodSpec != nil {
// if old spec has status.hostIPs downwardAPI set, we must allow it
opts.AllowHostIPsField = opts.AllowHostIPsField || hasUsedDownwardAPIFieldPathWithPodSpec(oldPodSpec, "status.hostIPs")
@@ -723,6 +725,7 @@ func dropDisabledFields(
dropPodLifecycleSleepAction(podSpec, oldPodSpec)
dropImageVolumes(podSpec, oldPodSpec)
dropSELinuxChangePolicy(podSpec, oldPodSpec)
}
func dropPodLifecycleSleepAction(podSpec, oldPodSpec *api.PodSpec) {
@@ -1361,3 +1364,34 @@ func imageVolumesInUse(podSpec *api.PodSpec) bool {
return false
}
func dropSELinuxChangePolicy(podSpec, oldPodSpec *api.PodSpec) {
if utilfeature.DefaultFeatureGate.Enabled(features.SELinuxChangePolicy) || seLinuxChangePolicyInUse(oldPodSpec) {
return
}
if podSpec == nil || podSpec.SecurityContext == nil {
return
}
podSpec.SecurityContext.SELinuxChangePolicy = nil
}
func seLinuxChangePolicyInUse(podSpec *api.PodSpec) bool {
if podSpec == nil || podSpec.SecurityContext == nil {
return false
}
return podSpec.SecurityContext.SELinuxChangePolicy != nil
}
func useOnlyRecursiveSELinuxChangePolicy(oldPodSpec *api.PodSpec) bool {
if utilfeature.DefaultFeatureGate.Enabled(features.SELinuxMount) {
// All policies are allowed
return false
}
if seLinuxChangePolicyInUse(oldPodSpec) {
// The old pod spec has *any* policy: we need to keep that object update-able.
return false
}
// No feature gate + no value in the old object -> only Recursive is allowed
return true
}