@@ -57,23 +57,46 @@ var _ = SIGDescribe("AppArmor", framework.WithNodeConformance(), func() {
57
57
f := framework .NewDefaultFramework ("apparmor-test" )
58
58
f .NamespacePodSecurityLevel = admissionapi .LevelPrivileged
59
59
60
- ginkgo .It ("should reject an unloaded profile" , func (ctx context.Context ) {
61
- status := runAppArmorTest (ctx , f , false , v1 .DeprecatedAppArmorBetaProfileNamePrefix + "non-existent-profile" )
60
+ ginkgo .It ("should reject an unloaded profile with annotation " , func (ctx context.Context ) {
61
+ status := runAppArmorTest (ctx , f , false , v1 .DeprecatedAppArmorBetaProfileNamePrefix + "non-existent-profile" , false )
62
62
gomega .Expect (status .ContainerStatuses [0 ].State .Waiting .Message ).To (gomega .ContainSubstring ("apparmor" ))
63
63
})
64
- ginkgo .It ("should enforce a profile blocking writes" , func (ctx context.Context ) {
65
- status := runAppArmorTest (ctx , f , true , v1 .DeprecatedAppArmorBetaProfileNamePrefix + apparmorProfilePrefix + "deny-write" )
64
+ ginkgo .It ("should reject an unloaded profile with field" , func (ctx context.Context ) {
65
+ status := runAppArmorTest (ctx , f , false , "non-existent-profile" , true )
66
+ gomega .Expect (status .ContainerStatuses [0 ].State .Waiting .Message ).To (gomega .ContainSubstring ("apparmor" ))
67
+ })
68
+ ginkgo .It ("should enforce a profile blocking writes with annotation" , func (ctx context.Context ) {
69
+ status := runAppArmorTest (ctx , f , true , v1 .DeprecatedAppArmorBetaProfileNamePrefix + apparmorProfilePrefix + "deny-write" , false )
70
+ if len (status .ContainerStatuses ) == 0 {
71
+ framework .Failf ("Unexpected pod status: %s" , dump .Pretty (status ))
72
+ return
73
+ }
74
+ state := status .ContainerStatuses [0 ].State .Terminated
75
+ gomega .Expect (state ).ToNot (gomega .BeNil (), "ContainerState: %+v" , status .ContainerStatuses [0 ].State )
76
+ gomega .Expect (state .ExitCode ).To (gomega .Not (gomega .BeZero ()), "ContainerStateTerminated: %+v" , state )
77
+ })
78
+ ginkgo .It ("should enforce a profile blocking writes with field" , func (ctx context.Context ) {
79
+ status := runAppArmorTest (ctx , f , true , apparmorProfilePrefix + "deny-write" , true )
66
80
if len (status .ContainerStatuses ) == 0 {
67
81
framework .Failf ("Unexpected pod status: %s" , dump .Pretty (status ))
68
82
return
69
83
}
70
84
state := status .ContainerStatuses [0 ].State .Terminated
71
85
gomega .Expect (state ).ToNot (gomega .BeNil (), "ContainerState: %+v" , status .ContainerStatuses [0 ].State )
72
86
gomega .Expect (state .ExitCode ).To (gomega .Not (gomega .BeZero ()), "ContainerStateTerminated: %+v" , state )
73
-
74
87
})
75
- ginkgo .It ("should enforce a permissive profile" , func (ctx context.Context ) {
76
- status := runAppArmorTest (ctx , f , true , v1 .DeprecatedAppArmorBetaProfileNamePrefix + apparmorProfilePrefix + "audit-write" )
88
+ ginkgo .It ("should enforce a permissive profile with annotations" , func (ctx context.Context ) {
89
+ status := runAppArmorTest (ctx , f , true , v1 .DeprecatedAppArmorBetaProfileNamePrefix + apparmorProfilePrefix + "audit-write" , false )
90
+ if len (status .ContainerStatuses ) == 0 {
91
+ framework .Failf ("Unexpected pod status: %s" , dump .Pretty (status ))
92
+ return
93
+ }
94
+ state := status .ContainerStatuses [0 ].State .Terminated
95
+ gomega .Expect (state ).ToNot (gomega .BeNil (), "ContainerState: %+v" , status .ContainerStatuses [0 ].State )
96
+ gomega .Expect (state .ExitCode ).To (gomega .BeZero (), "ContainerStateTerminated: %+v" , state )
97
+ })
98
+ ginkgo .It ("should enforce a permissive profile with field" , func (ctx context.Context ) {
99
+ status := runAppArmorTest (ctx , f , true , apparmorProfilePrefix + "audit-write" , true )
77
100
if len (status .ContainerStatuses ) == 0 {
78
101
framework .Failf ("Unexpected pod status: %s" , dump .Pretty (status ))
79
102
return
@@ -88,8 +111,13 @@ var _ = SIGDescribe("AppArmor", framework.WithNodeConformance(), func() {
88
111
f := framework .NewDefaultFramework ("apparmor-test" )
89
112
f .NamespacePodSecurityLevel = admissionapi .LevelPrivileged
90
113
91
- ginkgo .It ("should reject a pod with an AppArmor profile" , func (ctx context.Context ) {
92
- status := runAppArmorTest (ctx , f , false , v1 .DeprecatedAppArmorBetaProfileRuntimeDefault )
114
+ ginkgo .It ("should reject a pod with an AppArmor profile in annotation" , func (ctx context.Context ) {
115
+ status := runAppArmorTest (ctx , f , false , v1 .DeprecatedAppArmorBetaProfileRuntimeDefault , false )
116
+ expectRejection (status )
117
+ })
118
+
119
+ ginkgo .It ("should reject a pod with an AppArmor profile in field" , func (ctx context.Context ) {
120
+ status := runAppArmorTest (ctx , f , false , v1 .DeprecatedAppArmorBetaProfileRuntimeDefault , true )
93
121
expectRejection (status )
94
122
})
95
123
})
@@ -149,8 +177,8 @@ func loadTestProfiles() error {
149
177
return nil
150
178
}
151
179
152
- func runAppArmorTest (ctx context.Context , f * framework.Framework , shouldRun bool , profile string ) v1.PodStatus {
153
- pod := createPodWithAppArmor (ctx , f , profile )
180
+ func runAppArmorTest (ctx context.Context , f * framework.Framework , shouldRun bool , profile string , useField bool ) v1.PodStatus {
181
+ pod := createPodWithAppArmor (ctx , f , profile , useField )
154
182
if shouldRun {
155
183
// The pod needs to start before it stops, so wait for the longer start timeout.
156
184
framework .ExpectNoError (e2epod .WaitTimeoutForPodNoLongerRunningInNamespace (ctx ,
@@ -207,13 +235,10 @@ func runAppArmorTest(ctx context.Context, f *framework.Framework, shouldRun bool
207
235
return p .Status
208
236
}
209
237
210
- func createPodWithAppArmor (ctx context.Context , f * framework.Framework , profile string ) * v1.Pod {
238
+ func createPodWithAppArmor (ctx context.Context , f * framework.Framework , profile string , useField bool ) * v1.Pod {
211
239
pod := & v1.Pod {
212
240
ObjectMeta : metav1.ObjectMeta {
213
241
Name : fmt .Sprintf ("test-apparmor-%s" , strings .Replace (profile , "/" , "-" , - 1 )),
214
- Annotations : map [string ]string {
215
- v1 .DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "test" : profile ,
216
- },
217
242
},
218
243
Spec : v1.PodSpec {
219
244
Containers : []v1.Container {{
@@ -224,6 +249,28 @@ func createPodWithAppArmor(ctx context.Context, f *framework.Framework, profile
224
249
RestartPolicy : v1 .RestartPolicyNever ,
225
250
},
226
251
}
252
+
253
+ if useField {
254
+ if profile == v1 .DeprecatedAppArmorBetaProfileRuntimeDefault {
255
+ pod .Spec .Containers [0 ].SecurityContext = & v1.SecurityContext {
256
+ AppArmorProfile : & v1.AppArmorProfile {
257
+ Type : v1 .AppArmorProfileTypeRuntimeDefault ,
258
+ },
259
+ }
260
+ } else {
261
+ pod .Spec .Containers [0 ].SecurityContext = & v1.SecurityContext {
262
+ AppArmorProfile : & v1.AppArmorProfile {
263
+ Type : v1 .AppArmorProfileTypeLocalhost ,
264
+ LocalhostProfile : & profile ,
265
+ },
266
+ }
267
+ }
268
+ } else {
269
+ pod .Annotations = map [string ]string {
270
+ v1 .DeprecatedAppArmorBetaContainerAnnotationKeyPrefix + "test" : profile ,
271
+ }
272
+ }
273
+
227
274
return e2epod .NewPodClient (f ).Create (ctx , pod )
228
275
}
229
276
0 commit comments