security_context.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Copyright 2015 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. /* This test check that SecurityContext parameters specified at the
  14. * pod or the container level work as intended. These tests cannot be
  15. * run when the 'SecurityContextDeny' admission controller is not used
  16. * so they are skipped by default.
  17. */
  18. package node
  19. import (
  20. "fmt"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/uuid"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. imageutils "k8s.io/kubernetes/test/utils/image"
  26. "github.com/onsi/ginkgo"
  27. "github.com/onsi/gomega"
  28. )
  29. func scTestPod(hostIPC bool, hostPID bool) *v1.Pod {
  30. podName := "security-context-" + string(uuid.NewUUID())
  31. pod := &v1.Pod{
  32. ObjectMeta: metav1.ObjectMeta{
  33. Name: podName,
  34. Labels: map[string]string{"name": podName},
  35. Annotations: map[string]string{},
  36. },
  37. Spec: v1.PodSpec{
  38. HostIPC: hostIPC,
  39. HostPID: hostPID,
  40. SecurityContext: &v1.PodSecurityContext{},
  41. Containers: []v1.Container{
  42. {
  43. Name: "test-container",
  44. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  45. },
  46. },
  47. RestartPolicy: v1.RestartPolicyNever,
  48. },
  49. }
  50. return pod
  51. }
  52. var _ = SIGDescribe("Security Context", func() {
  53. f := framework.NewDefaultFramework("security-context")
  54. ginkgo.It("should support pod.Spec.SecurityContext.SupplementalGroups [LinuxOnly]", func() {
  55. pod := scTestPod(false, false)
  56. pod.Spec.Containers[0].Command = []string{"id", "-G"}
  57. pod.Spec.SecurityContext.SupplementalGroups = []int64{1234, 5678}
  58. groups := []string{"1234", "5678"}
  59. f.TestContainerOutput("pod.Spec.SecurityContext.SupplementalGroups", pod, 0, groups)
  60. })
  61. ginkgo.It("should support pod.Spec.SecurityContext.RunAsUser [LinuxOnly]", func() {
  62. pod := scTestPod(false, false)
  63. userID := int64(1001)
  64. pod.Spec.SecurityContext.RunAsUser = &userID
  65. pod.Spec.Containers[0].Command = []string{"sh", "-c", "id"}
  66. f.TestContainerOutput("pod.Spec.SecurityContext.RunAsUser", pod, 0, []string{
  67. fmt.Sprintf("uid=%v", userID),
  68. fmt.Sprintf("gid=%v", 0),
  69. })
  70. })
  71. ginkgo.It("should support pod.Spec.SecurityContext.RunAsUser And pod.Spec.SecurityContext.RunAsGroup [LinuxOnly]", func() {
  72. pod := scTestPod(false, false)
  73. userID := int64(1001)
  74. groupID := int64(2002)
  75. pod.Spec.SecurityContext.RunAsUser = &userID
  76. pod.Spec.SecurityContext.RunAsGroup = &groupID
  77. pod.Spec.Containers[0].Command = []string{"sh", "-c", "id"}
  78. f.TestContainerOutput("pod.Spec.SecurityContext.RunAsUser", pod, 0, []string{
  79. fmt.Sprintf("uid=%v", userID),
  80. fmt.Sprintf("gid=%v", groupID),
  81. })
  82. })
  83. ginkgo.It("should support container.SecurityContext.RunAsUser [LinuxOnly]", func() {
  84. pod := scTestPod(false, false)
  85. userID := int64(1001)
  86. overrideUserID := int64(1002)
  87. pod.Spec.SecurityContext.RunAsUser = &userID
  88. pod.Spec.Containers[0].SecurityContext = new(v1.SecurityContext)
  89. pod.Spec.Containers[0].SecurityContext.RunAsUser = &overrideUserID
  90. pod.Spec.Containers[0].Command = []string{"sh", "-c", "id"}
  91. f.TestContainerOutput("pod.Spec.SecurityContext.RunAsUser", pod, 0, []string{
  92. fmt.Sprintf("uid=%v", overrideUserID),
  93. fmt.Sprintf("gid=%v", 0),
  94. })
  95. })
  96. ginkgo.It("should support container.SecurityContext.RunAsUser And container.SecurityContext.RunAsGroup [LinuxOnly]", func() {
  97. pod := scTestPod(false, false)
  98. userID := int64(1001)
  99. groupID := int64(2001)
  100. overrideUserID := int64(1002)
  101. overrideGroupID := int64(2002)
  102. pod.Spec.SecurityContext.RunAsUser = &userID
  103. pod.Spec.SecurityContext.RunAsGroup = &groupID
  104. pod.Spec.Containers[0].SecurityContext = new(v1.SecurityContext)
  105. pod.Spec.Containers[0].SecurityContext.RunAsUser = &overrideUserID
  106. pod.Spec.Containers[0].SecurityContext.RunAsGroup = &overrideGroupID
  107. pod.Spec.Containers[0].Command = []string{"sh", "-c", "id"}
  108. f.TestContainerOutput("pod.Spec.SecurityContext.RunAsUser", pod, 0, []string{
  109. fmt.Sprintf("uid=%v", overrideUserID),
  110. fmt.Sprintf("gid=%v", overrideGroupID),
  111. })
  112. })
  113. ginkgo.It("should support volume SELinux relabeling [Flaky] [LinuxOnly]", func() {
  114. testPodSELinuxLabeling(f, false, false)
  115. })
  116. ginkgo.It("should support volume SELinux relabeling when using hostIPC [Flaky] [LinuxOnly]", func() {
  117. testPodSELinuxLabeling(f, true, false)
  118. })
  119. ginkgo.It("should support volume SELinux relabeling when using hostPID [Flaky] [LinuxOnly]", func() {
  120. testPodSELinuxLabeling(f, false, true)
  121. })
  122. ginkgo.It("should support seccomp alpha unconfined annotation on the container [Feature:Seccomp] [LinuxOnly]", func() {
  123. // TODO: port to SecurityContext as soon as seccomp is out of alpha
  124. pod := scTestPod(false, false)
  125. pod.Annotations[v1.SeccompContainerAnnotationKeyPrefix+"test-container"] = "unconfined"
  126. pod.Annotations[v1.SeccompPodAnnotationKey] = v1.SeccompProfileRuntimeDefault
  127. pod.Spec.Containers[0].Command = []string{"grep", "ecc", "/proc/self/status"}
  128. f.TestContainerOutput(v1.SeccompPodAnnotationKey, pod, 0, []string{"0"}) // seccomp disabled
  129. })
  130. ginkgo.It("should support seccomp alpha unconfined annotation on the pod [Feature:Seccomp] [LinuxOnly]", func() {
  131. // TODO: port to SecurityContext as soon as seccomp is out of alpha
  132. pod := scTestPod(false, false)
  133. pod.Annotations[v1.SeccompPodAnnotationKey] = "unconfined"
  134. pod.Spec.Containers[0].Command = []string{"grep", "ecc", "/proc/self/status"}
  135. f.TestContainerOutput(v1.SeccompPodAnnotationKey, pod, 0, []string{"0"}) // seccomp disabled
  136. })
  137. ginkgo.It("should support seccomp alpha runtime/default annotation [Feature:Seccomp] [LinuxOnly]", func() {
  138. // TODO: port to SecurityContext as soon as seccomp is out of alpha
  139. pod := scTestPod(false, false)
  140. pod.Annotations[v1.SeccompContainerAnnotationKeyPrefix+"test-container"] = v1.SeccompProfileRuntimeDefault
  141. pod.Spec.Containers[0].Command = []string{"grep", "ecc", "/proc/self/status"}
  142. f.TestContainerOutput(v1.SeccompPodAnnotationKey, pod, 0, []string{"2"}) // seccomp filtered
  143. })
  144. ginkgo.It("should support seccomp default which is unconfined [Feature:Seccomp] [LinuxOnly]", func() {
  145. // TODO: port to SecurityContext as soon as seccomp is out of alpha
  146. pod := scTestPod(false, false)
  147. pod.Spec.Containers[0].Command = []string{"grep", "ecc", "/proc/self/status"}
  148. f.TestContainerOutput(v1.SeccompPodAnnotationKey, pod, 0, []string{"0"}) // seccomp disabled
  149. })
  150. })
  151. func testPodSELinuxLabeling(f *framework.Framework, hostIPC bool, hostPID bool) {
  152. // Write and read a file with an empty_dir volume
  153. // with a pod with the MCS label s0:c0,c1
  154. pod := scTestPod(hostIPC, hostPID)
  155. volumeName := "test-volume"
  156. mountPath := "/mounted_volume"
  157. pod.Spec.Containers[0].VolumeMounts = []v1.VolumeMount{
  158. {
  159. Name: volumeName,
  160. MountPath: mountPath,
  161. },
  162. }
  163. pod.Spec.Volumes = []v1.Volume{
  164. {
  165. Name: volumeName,
  166. VolumeSource: v1.VolumeSource{
  167. EmptyDir: &v1.EmptyDirVolumeSource{
  168. Medium: v1.StorageMediumDefault,
  169. },
  170. },
  171. },
  172. }
  173. pod.Spec.SecurityContext.SELinuxOptions = &v1.SELinuxOptions{
  174. Level: "s0:c0,c1",
  175. }
  176. pod.Spec.Containers[0].Command = []string{"sleep", "6000"}
  177. client := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
  178. pod, err := client.Create(pod)
  179. framework.ExpectNoError(err, "Error creating pod %v", pod)
  180. framework.ExpectNoError(framework.WaitForPodRunningInNamespace(f.ClientSet, pod))
  181. testContent := "hello"
  182. testFilePath := mountPath + "/TEST"
  183. err = f.WriteFileViaContainer(pod.Name, pod.Spec.Containers[0].Name, testFilePath, testContent)
  184. gomega.Expect(err).To(gomega.BeNil())
  185. content, err := f.ReadFileViaContainer(pod.Name, pod.Spec.Containers[0].Name, testFilePath)
  186. gomega.Expect(err).To(gomega.BeNil())
  187. gomega.Expect(content).To(gomega.ContainSubstring(testContent))
  188. foundPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{})
  189. framework.ExpectNoError(err)
  190. // Confirm that the file can be accessed from a second
  191. // pod using host_path with the same MCS label
  192. volumeHostPath := fmt.Sprintf("%s/pods/%s/volumes/kubernetes.io~empty-dir/%s", framework.TestContext.KubeVolumeDir, foundPod.UID, volumeName)
  193. ginkgo.By(fmt.Sprintf("confirming a container with the same label can read the file under --volume-dir=%s", framework.TestContext.KubeVolumeDir))
  194. pod = scTestPod(hostIPC, hostPID)
  195. pod.Spec.NodeName = foundPod.Spec.NodeName
  196. volumeMounts := []v1.VolumeMount{
  197. {
  198. Name: volumeName,
  199. MountPath: mountPath,
  200. },
  201. }
  202. volumes := []v1.Volume{
  203. {
  204. Name: volumeName,
  205. VolumeSource: v1.VolumeSource{
  206. HostPath: &v1.HostPathVolumeSource{
  207. Path: volumeHostPath,
  208. },
  209. },
  210. },
  211. }
  212. pod.Spec.Containers[0].VolumeMounts = volumeMounts
  213. pod.Spec.Volumes = volumes
  214. pod.Spec.Containers[0].Command = []string{"cat", testFilePath}
  215. pod.Spec.SecurityContext.SELinuxOptions = &v1.SELinuxOptions{
  216. Level: "s0:c0,c1",
  217. }
  218. f.TestContainerOutput("Pod with same MCS label reading test file", pod, 0, []string{testContent})
  219. // Confirm that the same pod with a different MCS
  220. // label cannot access the volume
  221. pod = scTestPod(hostIPC, hostPID)
  222. pod.Spec.Volumes = volumes
  223. pod.Spec.Containers[0].VolumeMounts = volumeMounts
  224. pod.Spec.Containers[0].Command = []string{"sleep", "6000"}
  225. pod.Spec.SecurityContext.SELinuxOptions = &v1.SELinuxOptions{
  226. Level: "s0:c2,c3",
  227. }
  228. _, err = client.Create(pod)
  229. framework.ExpectNoError(err, "Error creating pod %v", pod)
  230. err = f.WaitForPodRunning(pod.Name)
  231. framework.ExpectNoError(err, "Error waiting for pod to run %v", pod)
  232. content, err = f.ReadFileViaContainer(pod.Name, "test-container", testFilePath)
  233. gomega.Expect(content).NotTo(gomega.ContainSubstring(testContent))
  234. }