security_context.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package windows
  14. import (
  15. "context"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/uuid"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. testutils "k8s.io/kubernetes/test/utils"
  21. imageutils "k8s.io/kubernetes/test/utils/image"
  22. "github.com/onsi/ginkgo"
  23. )
  24. const runAsUserNameContainerName = "run-as-username-container"
  25. var _ = SIGDescribe("[Feature:Windows] SecurityContext RunAsUserName", func() {
  26. f := framework.NewDefaultFramework("windows-run-as-username")
  27. ginkgo.It("should be able create pods and run containers with a given username", func() {
  28. ginkgo.By("Creating 2 pods: 1 with the default user, and one with a custom one.")
  29. podDefault := runAsUserNamePod(nil)
  30. f.TestContainerOutput("check default user", podDefault, 0, []string{"ContainerUser"})
  31. podUserName := runAsUserNamePod(toPtr("ContainerAdministrator"))
  32. f.TestContainerOutput("check set user", podUserName, 0, []string{"ContainerAdministrator"})
  33. })
  34. ginkgo.It("should not be able to create pods with unknown usernames", func() {
  35. ginkgo.By("Creating a pod with an invalid username")
  36. podInvalid := f.PodClient().Create(runAsUserNamePod(toPtr("FooLish")))
  37. framework.Logf("Waiting for pod %s to enter the error state.", podInvalid.Name)
  38. framework.ExpectNoError(f.WaitForPodTerminated(podInvalid.Name, ""))
  39. podInvalid, _ = f.PodClient().Get(context.TODO(), podInvalid.Name, metav1.GetOptions{})
  40. podTerminatedReason := testutils.TerminatedContainers(podInvalid)[runAsUserNameContainerName]
  41. if podTerminatedReason != "ContainerCannotRun" && podTerminatedReason != "StartError" {
  42. framework.Failf("The container terminated reason was supposed to be: 'ContainerCannotRun' or 'StartError', not: '%q'", podTerminatedReason)
  43. }
  44. })
  45. ginkgo.It("should override SecurityContext username if set", func() {
  46. ginkgo.By("Creating a pod with 2 containers with different username configurations.")
  47. pod := runAsUserNamePod(toPtr("ContainerAdministrator"))
  48. pod.Spec.Containers[0].SecurityContext.WindowsOptions.RunAsUserName = toPtr("ContainerUser")
  49. pod.Spec.Containers = append(pod.Spec.Containers, v1.Container{
  50. Name: "run-as-username-new-container",
  51. Image: imageutils.GetE2EImage(imageutils.NonRoot),
  52. Command: []string{"cmd", "/S", "/C", "echo %username%"},
  53. })
  54. f.TestContainerOutput("check overridden username", pod, 0, []string{"ContainerUser"})
  55. f.TestContainerOutput("check pod SecurityContext username", pod, 1, []string{"ContainerAdministrator"})
  56. })
  57. })
  58. func runAsUserNamePod(username *string) *v1.Pod {
  59. podName := "run-as-username-" + string(uuid.NewUUID())
  60. return &v1.Pod{
  61. ObjectMeta: metav1.ObjectMeta{
  62. Name: podName,
  63. },
  64. Spec: v1.PodSpec{
  65. Containers: []v1.Container{
  66. {
  67. Name: runAsUserNameContainerName,
  68. Image: imageutils.GetE2EImage(imageutils.NonRoot),
  69. Command: []string{"cmd", "/S", "/C", "echo %username%"},
  70. SecurityContext: &v1.SecurityContext{
  71. WindowsOptions: &v1.WindowsSecurityContextOptions{
  72. RunAsUserName: username,
  73. },
  74. },
  75. },
  76. },
  77. SecurityContext: &v1.PodSecurityContext{
  78. WindowsOptions: &v1.WindowsSecurityContextOptions{
  79. RunAsUserName: username,
  80. },
  81. },
  82. RestartPolicy: v1.RestartPolicyNever,
  83. },
  84. }
  85. }
  86. func toPtr(s string) *string {
  87. return &s
  88. }