|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2enode |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/onsi/ginkgo/v2" |
| 24 | + "github.com/onsi/gomega" |
| 25 | + gomegatypes "github.com/onsi/gomega/types" |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/kubernetes/test/e2e/feature" |
| 29 | + "k8s.io/kubernetes/test/e2e/framework" |
| 30 | + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 31 | + e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" |
| 32 | + "k8s.io/kubernetes/test/e2e/nodefeature" |
| 33 | + testutils "k8s.io/kubernetes/test/utils" |
| 34 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 35 | + admissionapi "k8s.io/pod-security-admission/api" |
| 36 | +) |
| 37 | + |
| 38 | +var falseVar = false |
| 39 | + |
| 40 | +var _ = SIGDescribe("DefaultProcMount [LinuxOnly]", framework.WithNodeConformance(), func() { |
| 41 | + f := framework.NewDefaultFramework("proc-mount-default-test") |
| 42 | + f.NamespacePodSecurityLevel = admissionapi.LevelBaseline |
| 43 | + |
| 44 | + ginkgo.It("will mask proc mounts by default", func(ctx context.Context) { |
| 45 | + testProcMount(ctx, f, v1.DefaultProcMount, gomega.BeNumerically(">=", 10), gomega.BeNumerically(">=", 7)) |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +var _ = SIGDescribe("ProcMount [LinuxOnly]", nodefeature.ProcMountType, nodefeature.UserNamespacesSupport, feature.UserNamespacesSupport, func() { |
| 50 | + f := framework.NewDefaultFramework("proc-mount-baseline-test") |
| 51 | + f.NamespacePodSecurityLevel = admissionapi.LevelBaseline |
| 52 | + |
| 53 | + f.It("will fail to unmask proc mounts if not privileged", func(ctx context.Context) { |
| 54 | + if !supportsUserNS(ctx, f) { |
| 55 | + e2eskipper.Skipf("runtime does not support user namespaces") |
| 56 | + } |
| 57 | + pmt := v1.UnmaskedProcMount |
| 58 | + podClient := e2epod.NewPodClient(f) |
| 59 | + _, err := podClient.PodInterface.Create(ctx, &v1.Pod{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{Name: "proc-mount-pod"}, |
| 61 | + Spec: v1.PodSpec{ |
| 62 | + Containers: []v1.Container{ |
| 63 | + { |
| 64 | + Name: "test-container-1", |
| 65 | + Image: imageutils.GetE2EImage(imageutils.BusyBox), |
| 66 | + Command: []string{"/bin/sleep"}, |
| 67 | + Args: []string{"10000"}, |
| 68 | + SecurityContext: &v1.SecurityContext{ |
| 69 | + ProcMount: &pmt, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + HostUsers: &falseVar, |
| 74 | + }, |
| 75 | + }, metav1.CreateOptions{}) |
| 76 | + gomega.Expect(err).To(gomega.HaveOccurred()) |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +var _ = SIGDescribe("ProcMount [LinuxOnly]", nodefeature.ProcMountType, nodefeature.UserNamespacesSupport, feature.UserNamespacesSupport, func() { |
| 81 | + f := framework.NewDefaultFramework("proc-mount-privileged-test") |
| 82 | + |
| 83 | + f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged |
| 84 | + |
| 85 | + f.It("will unmask proc mounts if requested", func(ctx context.Context) { |
| 86 | + if !supportsUserNS(ctx, f) { |
| 87 | + e2eskipper.Skipf("runtime does not support user namespaces") |
| 88 | + } |
| 89 | + testProcMount(ctx, f, v1.UnmaskedProcMount, gomega.Equal(1), gomega.BeZero()) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +func testProcMount(ctx context.Context, f *framework.Framework, pmt v1.ProcMountType, expectedLines gomegatypes.GomegaMatcher, expectedReadOnly gomegatypes.GomegaMatcher) { |
| 94 | + ginkgo.By("creating a target pod") |
| 95 | + podClient := e2epod.NewPodClient(f) |
| 96 | + pod := podClient.CreateSync(ctx, &v1.Pod{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{Name: "proc-mount-pod"}, |
| 98 | + Spec: v1.PodSpec{ |
| 99 | + Containers: []v1.Container{ |
| 100 | + { |
| 101 | + Name: "test-container-1", |
| 102 | + Image: imageutils.GetE2EImage(imageutils.BusyBox), |
| 103 | + Command: []string{"/bin/sleep"}, |
| 104 | + Args: []string{"10000"}, |
| 105 | + SecurityContext: &v1.SecurityContext{ |
| 106 | + ProcMount: &pmt, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + HostUsers: &falseVar, |
| 111 | + }, |
| 112 | + }) |
| 113 | + |
| 114 | + _, err := testutils.PodRunningReady(pod) |
| 115 | + framework.ExpectNoError(err) |
| 116 | + |
| 117 | + output := e2epod.ExecCommandInContainer(f, pod.Name, pod.Spec.Containers[0].Name, "/bin/sh", "-ec", "mount | grep /proc") |
| 118 | + ginkgo.By(output) |
| 119 | + lines := strings.Split(output, "\n") |
| 120 | + gomega.Expect(len(lines)).To(expectedLines) |
| 121 | + gomega.Expect(strings.Count(output, "(ro")).To(expectedReadOnly) |
| 122 | +} |
| 123 | + |
| 124 | +func supportsUserNS(ctx context.Context, f *framework.Framework) bool { |
| 125 | + nodeList, err := f.ClientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) |
| 126 | + framework.ExpectNoError(err) |
| 127 | + // Assuming that there is only one node, because this is a node e2e test. |
| 128 | + gomega.Expect(nodeList.Items).To(gomega.HaveLen(1)) |
| 129 | + node := nodeList.Items[0] |
| 130 | + for _, rc := range node.Status.RuntimeHandlers { |
| 131 | + if rc.Name == "" && rc.Features != nil && *rc.Features.UserNamespaces { |
| 132 | + return true |
| 133 | + } |
| 134 | + } |
| 135 | + return false |
| 136 | +} |
0 commit comments