...
1
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
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
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 })
84
View as plain text