apparmor.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright 2017 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. api "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/labels"
  19. "k8s.io/kubernetes/pkg/security/apparmor"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. imageutils "k8s.io/kubernetes/test/utils/image"
  22. . "github.com/onsi/gomega"
  23. )
  24. const (
  25. appArmorProfilePrefix = "e2e-apparmor-test-"
  26. appArmorAllowedPath = "/expect_allowed_write"
  27. appArmorDeniedPath = "/expect_permission_denied"
  28. loaderLabelKey = "name"
  29. loaderLabelValue = "e2e-apparmor-loader"
  30. )
  31. // AppArmorDistros are distros with AppArmor support
  32. var AppArmorDistros = []string{"gci", "ubuntu"}
  33. func IsAppArmorSupported() bool {
  34. return framework.NodeOSDistroIs(AppArmorDistros...)
  35. }
  36. func SkipIfAppArmorNotSupported() {
  37. framework.SkipUnlessNodeOSDistroIs(AppArmorDistros...)
  38. }
  39. func LoadAppArmorProfiles(f *framework.Framework) {
  40. createAppArmorProfileCM(f)
  41. createAppArmorProfileLoader(f)
  42. }
  43. // CreateAppArmorTestPod creates a pod that tests apparmor profile enforcement. The pod exits with
  44. // an error code if the profile is incorrectly enforced. If runOnce is true the pod will exit after
  45. // a single test, otherwise it will repeat the test every 1 second until failure.
  46. func CreateAppArmorTestPod(f *framework.Framework, unconfined bool, runOnce bool) *api.Pod {
  47. profile := "localhost/" + appArmorProfilePrefix + f.Namespace.Name
  48. testCmd := fmt.Sprintf(`
  49. if touch %[1]s; then
  50. echo "FAILURE: write to %[1]s should be denied"
  51. exit 1
  52. elif ! touch %[2]s; then
  53. echo "FAILURE: write to %[2]s should be allowed"
  54. exit 2
  55. elif [[ $(< /proc/self/attr/current) != "%[3]s" ]]; then
  56. echo "FAILURE: not running with expected profile %[3]s"
  57. echo "found: $(cat /proc/self/attr/current)"
  58. exit 3
  59. fi`, appArmorDeniedPath, appArmorAllowedPath, appArmorProfilePrefix+f.Namespace.Name)
  60. if unconfined {
  61. profile = apparmor.ProfileNameUnconfined
  62. testCmd = `
  63. if cat /proc/sysrq-trigger 2>&1 | grep 'Permission denied'; then
  64. echo 'FAILURE: reading /proc/sysrq-trigger should be allowed'
  65. exit 1
  66. elif [[ $(< /proc/self/attr/current) != "unconfined" ]]; then
  67. echo 'FAILURE: not running with expected profile unconfined'
  68. exit 2
  69. fi`
  70. }
  71. if !runOnce {
  72. testCmd = fmt.Sprintf(`while true; do
  73. %s
  74. sleep 1
  75. done`, testCmd)
  76. }
  77. loaderAffinity := &api.Affinity{
  78. PodAffinity: &api.PodAffinity{
  79. RequiredDuringSchedulingIgnoredDuringExecution: []api.PodAffinityTerm{{
  80. Namespaces: []string{f.Namespace.Name},
  81. LabelSelector: &metav1.LabelSelector{
  82. MatchLabels: map[string]string{loaderLabelKey: loaderLabelValue},
  83. },
  84. TopologyKey: "kubernetes.io/hostname",
  85. }},
  86. },
  87. }
  88. pod := &api.Pod{
  89. ObjectMeta: metav1.ObjectMeta{
  90. GenerateName: "test-apparmor-",
  91. Annotations: map[string]string{
  92. apparmor.ContainerAnnotationKeyPrefix + "test": profile,
  93. },
  94. Labels: map[string]string{
  95. "test": "apparmor",
  96. },
  97. },
  98. Spec: api.PodSpec{
  99. Affinity: loaderAffinity,
  100. Containers: []api.Container{{
  101. Name: "test",
  102. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  103. Command: []string{"sh", "-c", testCmd},
  104. }},
  105. RestartPolicy: api.RestartPolicyNever,
  106. },
  107. }
  108. if runOnce {
  109. pod = f.PodClient().Create(pod)
  110. framework.ExpectNoError(framework.WaitForPodSuccessInNamespace(
  111. f.ClientSet, pod.Name, f.Namespace.Name))
  112. var err error
  113. pod, err = f.PodClient().Get(pod.Name, metav1.GetOptions{})
  114. framework.ExpectNoError(err)
  115. } else {
  116. pod = f.PodClient().CreateSync(pod)
  117. framework.ExpectNoError(f.WaitForPodReady(pod.Name))
  118. }
  119. // Verify Pod affinity colocated the Pods.
  120. loader := getRunningLoaderPod(f)
  121. Expect(pod.Spec.NodeName).To(Equal(loader.Spec.NodeName))
  122. return pod
  123. }
  124. func createAppArmorProfileCM(f *framework.Framework) {
  125. profileName := appArmorProfilePrefix + f.Namespace.Name
  126. profile := fmt.Sprintf(`#include <tunables/global>
  127. profile %s flags=(attach_disconnected) {
  128. #include <abstractions/base>
  129. file,
  130. deny %s w,
  131. audit %s w,
  132. }
  133. `, profileName, appArmorDeniedPath, appArmorAllowedPath)
  134. cm := &api.ConfigMap{
  135. ObjectMeta: metav1.ObjectMeta{
  136. Name: "apparmor-profiles",
  137. Namespace: f.Namespace.Name,
  138. },
  139. Data: map[string]string{
  140. profileName: profile,
  141. },
  142. }
  143. _, err := f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(cm)
  144. framework.ExpectNoError(err, "Failed to create apparmor-profiles ConfigMap")
  145. }
  146. func createAppArmorProfileLoader(f *framework.Framework) {
  147. True := true
  148. One := int32(1)
  149. loader := &api.ReplicationController{
  150. ObjectMeta: metav1.ObjectMeta{
  151. Name: "apparmor-loader",
  152. Namespace: f.Namespace.Name,
  153. },
  154. Spec: api.ReplicationControllerSpec{
  155. Replicas: &One,
  156. Template: &api.PodTemplateSpec{
  157. ObjectMeta: metav1.ObjectMeta{
  158. Labels: map[string]string{loaderLabelKey: loaderLabelValue},
  159. },
  160. Spec: api.PodSpec{
  161. Containers: []api.Container{{
  162. Name: "apparmor-loader",
  163. Image: imageutils.GetE2EImage(imageutils.AppArmorLoader),
  164. Args: []string{"-poll", "10s", "/profiles"},
  165. SecurityContext: &api.SecurityContext{
  166. Privileged: &True,
  167. },
  168. VolumeMounts: []api.VolumeMount{{
  169. Name: "sys",
  170. MountPath: "/sys",
  171. ReadOnly: true,
  172. }, {
  173. Name: "apparmor-includes",
  174. MountPath: "/etc/apparmor.d",
  175. ReadOnly: true,
  176. }, {
  177. Name: "profiles",
  178. MountPath: "/profiles",
  179. ReadOnly: true,
  180. }},
  181. }},
  182. Volumes: []api.Volume{{
  183. Name: "sys",
  184. VolumeSource: api.VolumeSource{
  185. HostPath: &api.HostPathVolumeSource{
  186. Path: "/sys",
  187. },
  188. },
  189. }, {
  190. Name: "apparmor-includes",
  191. VolumeSource: api.VolumeSource{
  192. HostPath: &api.HostPathVolumeSource{
  193. Path: "/etc/apparmor.d",
  194. },
  195. },
  196. }, {
  197. Name: "profiles",
  198. VolumeSource: api.VolumeSource{
  199. ConfigMap: &api.ConfigMapVolumeSource{
  200. LocalObjectReference: api.LocalObjectReference{
  201. Name: "apparmor-profiles",
  202. },
  203. },
  204. },
  205. }},
  206. },
  207. },
  208. },
  209. }
  210. _, err := f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(loader)
  211. framework.ExpectNoError(err, "Failed to create apparmor-loader ReplicationController")
  212. // Wait for loader to be ready.
  213. getRunningLoaderPod(f)
  214. }
  215. func getRunningLoaderPod(f *framework.Framework) *api.Pod {
  216. label := labels.SelectorFromSet(labels.Set(map[string]string{loaderLabelKey: loaderLabelValue}))
  217. pods, err := framework.WaitForPodsWithLabelScheduled(f.ClientSet, f.Namespace.Name, label)
  218. framework.ExpectNoError(err, "Failed to schedule apparmor-loader Pod")
  219. pod := &pods.Items[0]
  220. framework.ExpectNoError(framework.WaitForPodRunningInNamespace(f.ClientSet, pod), "Failed to run apparmor-loader Pod")
  221. return pod
  222. }