security_context.go 8.2 KB

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