|
| 1 | +/* |
| 2 | +Copyright 2019 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 windows |
| 18 | + |
| 19 | +import ( |
| 20 | + v1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 23 | + "k8s.io/kubernetes/test/e2e/framework" |
| 24 | + e2elog "k8s.io/kubernetes/test/e2e/framework/log" |
| 25 | + testutils "k8s.io/kubernetes/test/utils" |
| 26 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 27 | + |
| 28 | + "github.com/onsi/ginkgo" |
| 29 | +) |
| 30 | + |
| 31 | +const runAsUserNameContainerName = "run-as-username-container" |
| 32 | + |
| 33 | +var _ = SIGDescribe("[Feature:Windows] SecurityContext RunAsUserName", func() { |
| 34 | + f := framework.NewDefaultFramework("windows-run-as-username") |
| 35 | + |
| 36 | + ginkgo.It("should be able create pods and run containers with a given username", func() { |
| 37 | + ginkgo.By("Creating 2 pods: 1 with the default user, and one with a custom one.") |
| 38 | + podDefault := runAsUserNamePod(nil) |
| 39 | + f.TestContainerOutput("check default user", podDefault, 0, []string{"ContainerUser"}) |
| 40 | + |
| 41 | + podUserName := runAsUserNamePod(toPtr("ContainerAdministrator")) |
| 42 | + f.TestContainerOutput("check set user", podUserName, 0, []string{"ContainerAdministrator"}) |
| 43 | + }) |
| 44 | + |
| 45 | + ginkgo.It("should not be able to create pods with unknown usernames", func() { |
| 46 | + ginkgo.By("Creating a pod with an invalid username") |
| 47 | + podInvalid := f.PodClient().Create(runAsUserNamePod(toPtr("FooLish"))) |
| 48 | + |
| 49 | + e2elog.Logf("Waiting for pod %s to enter the error state.", podInvalid.Name) |
| 50 | + framework.ExpectNoError(f.WaitForPodTerminated(podInvalid.Name, "")) |
| 51 | + |
| 52 | + podInvalid, _ = f.PodClient().Get(podInvalid.Name, metav1.GetOptions{}) |
| 53 | + podTerminatedReason := testutils.TerminatedContainers(podInvalid)[runAsUserNameContainerName] |
| 54 | + if "ContainerCannotRun" != podTerminatedReason { |
| 55 | + e2elog.Failf("The container terminated reason was supposed to be: 'ContainerCannotRun', not: '%q'", podTerminatedReason) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + ginkgo.It("should override SecurityContext username if set", func() { |
| 60 | + ginkgo.By("Creating a pod with 2 containers with different username configurations.") |
| 61 | + |
| 62 | + pod := runAsUserNamePod(toPtr("ContainerAdministrator")) |
| 63 | + pod.Spec.Containers[0].SecurityContext.WindowsOptions.RunAsUserName = toPtr("ContainerUser") |
| 64 | + pod.Spec.Containers = append(pod.Spec.Containers, v1.Container{ |
| 65 | + Name: "run-as-username-new-container", |
| 66 | + Image: imageutils.GetE2EImage(imageutils.NonRoot), |
| 67 | + Command: []string{"cmd", "/S", "/C", "echo %username%"}, |
| 68 | + }) |
| 69 | + |
| 70 | + f.TestContainerOutput("check overridden username", pod, 0, []string{"ContainerUser"}) |
| 71 | + f.TestContainerOutput("check pod SecurityContext username", pod, 1, []string{"ContainerAdministrator"}) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +func runAsUserNamePod(username *string) *v1.Pod { |
| 76 | + podName := "run-as-username-" + string(uuid.NewUUID()) |
| 77 | + return &v1.Pod{ |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: podName, |
| 80 | + }, |
| 81 | + Spec: v1.PodSpec{ |
| 82 | + Containers: []v1.Container{ |
| 83 | + { |
| 84 | + Name: runAsUserNameContainerName, |
| 85 | + Image: imageutils.GetE2EImage(imageutils.NonRoot), |
| 86 | + Command: []string{"cmd", "/S", "/C", "echo %username%"}, |
| 87 | + SecurityContext: &v1.SecurityContext{ |
| 88 | + WindowsOptions: &v1.WindowsSecurityContextOptions{ |
| 89 | + RunAsUserName: username, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + SecurityContext: &v1.PodSecurityContext{ |
| 95 | + WindowsOptions: &v1.WindowsSecurityContextOptions{ |
| 96 | + RunAsUserName: username, |
| 97 | + }, |
| 98 | + }, |
| 99 | + RestartPolicy: v1.RestartPolicyNever, |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func toPtr(s string) *string { |
| 105 | + return &s |
| 106 | +} |
0 commit comments