mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-08-08 23:37:11 +00:00
Update the procMount test expectations to match the intentional PSA
policy relaxation introduced in commit e8bd3f629d.
As of Kubernetes 1.35+, Pod Security Admission Baseline policy
allows UnmaskedProcMount for pods with user namespaces (hostUsers:
false). This was an intentional change to support nested container
use cases while maintaining security through user namespace isolation.
The test "will fail to unmask proc mounts if not privileged" was
written before this relaxation and expected Baseline level to reject
UnmaskedProcMount. Since Baseline now allows it (for user namespace
pods), the test needs to use Restricted level instead, which
unconditionally blocks UnmaskedProcMount regardless of user namespace
settings.
Changes:
- Change PSA level from Baseline to Restricted
- Update test name to clarify it's testing Restricted level behavior
- Update framework name from "proc-mount-baseline-test" to
"proc-mount-restricted-test"
Fixes the ci-crio-userns-e2e-serial test failure that started occurring
when runtimes began reporting user namespace support.
Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
136 lines
4.6 KiB
Go
136 lines
4.6 KiB
Go
/*
|
|
Copyright 2024 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package e2enode
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/onsi/ginkgo/v2"
|
|
"github.com/onsi/gomega"
|
|
gomegatypes "github.com/onsi/gomega/types"
|
|
v1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/kubernetes/test/e2e/feature"
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
|
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
|
|
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
|
|
testutils "k8s.io/kubernetes/test/utils"
|
|
imageutils "k8s.io/kubernetes/test/utils/image"
|
|
admissionapi "k8s.io/pod-security-admission/api"
|
|
)
|
|
|
|
var falseVar = false
|
|
|
|
var _ = SIGDescribe("DefaultProcMount [LinuxOnly]", framework.WithNodeConformance(), func() {
|
|
f := framework.NewDefaultFramework("proc-mount-default-test")
|
|
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
|
|
|
|
ginkgo.It("will mask proc mounts by default", func(ctx context.Context) {
|
|
testProcMount(ctx, f, v1.DefaultProcMount, true, gomega.BeNumerically(">", 1), gomega.BeNumerically(">", 0))
|
|
})
|
|
})
|
|
|
|
var _ = SIGDescribe("ProcMount [LinuxOnly]", feature.ProcMountType, feature.UserNamespacesSupport, func() {
|
|
f := framework.NewDefaultFramework("proc-mount-restricted-test")
|
|
f.NamespacePodSecurityLevel = admissionapi.LevelRestricted
|
|
|
|
f.It("will fail to unmask proc mounts at restricted level", func(ctx context.Context) {
|
|
if !supportsUserNS(ctx, f) {
|
|
e2eskipper.Skipf("runtime does not support user namespaces")
|
|
}
|
|
pmt := v1.UnmaskedProcMount
|
|
podClient := e2epod.NewPodClient(f)
|
|
_, err := podClient.PodInterface.Create(ctx, &v1.Pod{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "proc-mount-pod"},
|
|
Spec: v1.PodSpec{
|
|
Containers: []v1.Container{
|
|
{
|
|
Name: "test-container-1",
|
|
Image: imageutils.GetE2EImage(imageutils.BusyBox),
|
|
Command: []string{"/bin/sleep"},
|
|
Args: []string{"10000"},
|
|
SecurityContext: &v1.SecurityContext{
|
|
ProcMount: &pmt,
|
|
},
|
|
},
|
|
},
|
|
HostUsers: &falseVar,
|
|
},
|
|
}, metav1.CreateOptions{})
|
|
gomega.Expect(err).To(gomega.HaveOccurred())
|
|
})
|
|
})
|
|
|
|
var _ = SIGDescribe("ProcMount [LinuxOnly]", feature.ProcMountType, feature.UserNamespacesSupport, func() {
|
|
f := framework.NewDefaultFramework("proc-mount-privileged-test")
|
|
|
|
f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
|
|
|
|
f.It("will unmask proc mounts if requested", func(ctx context.Context) {
|
|
if !supportsUserNS(ctx, f) {
|
|
e2eskipper.Skipf("runtime does not support user namespaces")
|
|
}
|
|
testProcMount(ctx, f, v1.UnmaskedProcMount, false, gomega.Equal(1), gomega.BeZero())
|
|
})
|
|
})
|
|
|
|
func testProcMount(ctx context.Context, f *framework.Framework, pmt v1.ProcMountType, hostUsers bool, expectedLines gomegatypes.GomegaMatcher, expectedReadOnly gomegatypes.GomegaMatcher) {
|
|
ginkgo.By("creating a target pod")
|
|
podClient := e2epod.NewPodClient(f)
|
|
pod := podClient.CreateSync(ctx, &v1.Pod{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "proc-mount-pod"},
|
|
Spec: v1.PodSpec{
|
|
Containers: []v1.Container{
|
|
{
|
|
Name: "test-container-1",
|
|
Image: imageutils.GetE2EImage(imageutils.BusyBox),
|
|
Command: []string{"/bin/sleep"},
|
|
Args: []string{"10000"},
|
|
SecurityContext: &v1.SecurityContext{
|
|
ProcMount: &pmt,
|
|
},
|
|
},
|
|
},
|
|
HostUsers: &hostUsers,
|
|
},
|
|
})
|
|
|
|
_, err := testutils.PodRunningReady(pod)
|
|
framework.ExpectNoError(err)
|
|
|
|
output := e2epod.ExecCommandInContainer(f, pod.Name, pod.Spec.Containers[0].Name, "/bin/sh", "-ec", "mount | grep /proc")
|
|
ginkgo.By(output)
|
|
lines := strings.Split(output, "\n")
|
|
gomega.Expect(len(lines)).To(expectedLines)
|
|
gomega.Expect(strings.Count(output, "(ro")).To(expectedReadOnly)
|
|
}
|
|
|
|
func supportsUserNS(ctx context.Context, f *framework.Framework) bool {
|
|
nodeList, err := f.ClientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
|
|
framework.ExpectNoError(err)
|
|
// Assuming that there is only one node, because this is a node e2e test.
|
|
gomega.Expect(nodeList.Items).To(gomega.HaveLen(1))
|
|
node := nodeList.Items[0]
|
|
for _, rc := range node.Status.RuntimeHandlers {
|
|
if rc.Name == "" && rc.Features != nil && *rc.Features.UserNamespaces {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|