privileged.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2016 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 common
  14. import (
  15. "fmt"
  16. "github.com/onsi/ginkgo"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. imageutils "k8s.io/kubernetes/test/utils/image"
  21. )
  22. // PrivilegedPodTestConfig is configuration struct for privileged pod test
  23. // TODO: Merge with tests in security_context.go
  24. type PrivilegedPodTestConfig struct {
  25. f *framework.Framework
  26. privilegedPod string
  27. privilegedContainer string
  28. notPrivilegedContainer string
  29. pod *v1.Pod
  30. }
  31. var _ = framework.KubeDescribe("PrivilegedPod [NodeConformance]", func() {
  32. config := &PrivilegedPodTestConfig{
  33. f: framework.NewDefaultFramework("e2e-privileged-pod"),
  34. privilegedPod: "privileged-pod",
  35. privilegedContainer: "privileged-container",
  36. notPrivilegedContainer: "not-privileged-container",
  37. }
  38. ginkgo.It("should enable privileged commands [LinuxOnly]", func() {
  39. // Windows does not support privileged containers.
  40. ginkgo.By("Creating a pod with a privileged container")
  41. config.createPods()
  42. ginkgo.By("Executing in the privileged container")
  43. config.run(config.privilegedContainer, true)
  44. ginkgo.By("Executing in the non-privileged container")
  45. config.run(config.notPrivilegedContainer, false)
  46. })
  47. })
  48. func (c *PrivilegedPodTestConfig) run(containerName string, expectSuccess bool) {
  49. cmd := []string{"ip", "link", "add", "dummy1", "type", "dummy"}
  50. reverseCmd := []string{"ip", "link", "del", "dummy1"}
  51. stdout, stderr, err := c.f.ExecCommandInContainerWithFullOutput(
  52. c.privilegedPod, containerName, cmd...)
  53. msg := fmt.Sprintf("cmd %v, stdout %q, stderr %q", cmd, stdout, stderr)
  54. if expectSuccess {
  55. framework.ExpectNoError(err, msg)
  56. // We need to clean up the dummy link that was created, as it
  57. // leaks out into the node level -- yuck.
  58. _, _, err := c.f.ExecCommandInContainerWithFullOutput(
  59. c.privilegedPod, containerName, reverseCmd...)
  60. framework.ExpectNoError(err,
  61. fmt.Sprintf("could not remove dummy1 link: %v", err))
  62. } else {
  63. framework.ExpectError(err, msg)
  64. }
  65. }
  66. func (c *PrivilegedPodTestConfig) createPodsSpec() *v1.Pod {
  67. isPrivileged := true
  68. notPrivileged := false
  69. return &v1.Pod{
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: c.privilegedPod,
  72. Namespace: c.f.Namespace.Name,
  73. },
  74. Spec: v1.PodSpec{
  75. Containers: []v1.Container{
  76. {
  77. Name: c.privilegedContainer,
  78. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  79. ImagePullPolicy: v1.PullIfNotPresent,
  80. SecurityContext: &v1.SecurityContext{Privileged: &isPrivileged},
  81. Command: []string{"/bin/sleep", "10000"},
  82. },
  83. {
  84. Name: c.notPrivilegedContainer,
  85. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  86. ImagePullPolicy: v1.PullIfNotPresent,
  87. SecurityContext: &v1.SecurityContext{Privileged: &notPrivileged},
  88. Command: []string{"/bin/sleep", "10000"},
  89. },
  90. },
  91. },
  92. }
  93. }
  94. func (c *PrivilegedPodTestConfig) createPods() {
  95. podSpec := c.createPodsSpec()
  96. c.pod = c.f.PodClient().CreateSync(podSpec)
  97. }