k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/node/apparmor.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package node 18 19 import ( 20 "context" 21 22 v1 "k8s.io/api/core/v1" 23 "k8s.io/kubernetes/test/e2e/framework" 24 e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl" 25 e2epod "k8s.io/kubernetes/test/e2e/framework/pod" 26 e2esecurity "k8s.io/kubernetes/test/e2e/framework/security" 27 e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" 28 admissionapi "k8s.io/pod-security-admission/api" 29 30 "github.com/onsi/ginkgo/v2" 31 ) 32 33 var _ = SIGDescribe("AppArmor", func() { 34 f := framework.NewDefaultFramework("apparmor") 35 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 36 37 ginkgo.Context("load AppArmor profiles", func() { 38 ginkgo.BeforeEach(func(ctx context.Context) { 39 e2eskipper.SkipIfAppArmorNotSupported() 40 e2esecurity.LoadAppArmorProfiles(ctx, f.Namespace.Name, f.ClientSet) 41 }) 42 ginkgo.AfterEach(func(ctx context.Context) { 43 if !ginkgo.CurrentSpecReport().Failed() { 44 return 45 } 46 e2ekubectl.LogFailedContainers(ctx, f.ClientSet, f.Namespace.Name, framework.Logf) 47 }) 48 49 ginkgo.It("should enforce an AppArmor profile specified on the pod", func(ctx context.Context) { 50 pod := e2esecurity.AppArmorTestPod(f.Namespace.Name, false, true) 51 e2esecurity.RunAppArmorTestPod(ctx, pod, f.ClientSet, e2epod.NewPodClient(f), true) 52 }) 53 54 ginkgo.It("should enforce an AppArmor profile specified on the container", func(ctx context.Context) { 55 pod := e2esecurity.AppArmorTestPod(f.Namespace.Name, false, true) 56 // Move AppArmor profile to the container. 57 pod.Spec.Containers[0].SecurityContext = &v1.SecurityContext{ 58 AppArmorProfile: pod.Spec.SecurityContext.AppArmorProfile, 59 } 60 pod.Spec.SecurityContext = nil 61 62 e2esecurity.RunAppArmorTestPod(ctx, pod, f.ClientSet, e2epod.NewPodClient(f), true) 63 }) 64 65 ginkgo.It("should enforce an AppArmor profile specified in annotations", func(ctx context.Context) { 66 pod := e2esecurity.AppArmorTestPod(f.Namespace.Name, false, true) 67 // Move AppArmor profile to the annotations. 68 profile := pod.Spec.SecurityContext.AppArmorProfile 69 key := v1.DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + pod.Spec.Containers[0].Name 70 pod.Annotations = map[string]string{ 71 key: v1.DeprecatedAppArmorBetaProfileNamePrefix + *profile.LocalhostProfile, 72 } 73 pod.Spec.SecurityContext = nil 74 75 e2esecurity.RunAppArmorTestPod(ctx, pod, f.ClientSet, e2epod.NewPodClient(f), true) 76 }) 77 78 ginkgo.It("can disable an AppArmor profile, using unconfined", func(ctx context.Context) { 79 pod := e2esecurity.AppArmorTestPod(f.Namespace.Name, true, true) 80 e2esecurity.RunAppArmorTestPod(ctx, pod, f.ClientSet, e2epod.NewPodClient(f), true) 81 }) 82 }) 83 })