@@ -53,6 +53,9 @@ profile cri-validate-apparmor-test-audit-write flags=(attach_disconnected) {
53
53
`
54
54
)
55
55
56
+ // The AppArmor profile to the CRI via the deprecated apparmor_profile field
57
+ // in favor of the newer structured apparmor field.
58
+ // CRI provides the AppArmor profile via both fields to maintain backwards compatibility.
56
59
var _ = framework .KubeDescribe ("AppArmor" , func () {
57
60
f := framework .NewDefaultCRIFramework ()
58
61
@@ -66,7 +69,7 @@ var _ = framework.KubeDescribe("AppArmor", func() {
66
69
Expect (loadTestProfiles ()).NotTo (HaveOccurred ())
67
70
})
68
71
69
- Context ("runtime should support apparmor " , func () {
72
+ Context ("runtime should support depracated apparmor_profile field " , func () {
70
73
var sandboxID string
71
74
var sandboxConfig * runtimeapi.PodSandboxConfig
72
75
@@ -81,38 +84,143 @@ var _ = framework.KubeDescribe("AppArmor", func() {
81
84
rc .RemovePodSandbox (context .TODO (), sandboxID )
82
85
})
83
86
84
- It ("should fail with an unloaded profile" , func () {
85
- profile := apparmorProfileNamePrefix + "non-existent-profile"
87
+ It ("should fail with an unloaded apparmor_profile" , func () {
88
+ profile := & runtimeapi.LinuxContainerSecurityContext {
89
+ ApparmorProfile : apparmorProfileNamePrefix + "non-existent-profile" ,
90
+ }
86
91
containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , false )
87
92
Expect (containerID ).To (BeEmpty ())
88
93
})
89
94
90
- It ("should enforce a profile blocking writes" , func () {
91
- profile := apparmorProfileNamePrefix + "cri-validate-apparmor-test-deny-write"
95
+ It ("should enforce a apparmor_profile blocking writes" , func () {
96
+ profile := & runtimeapi.LinuxContainerSecurityContext {
97
+ ApparmorProfile : apparmorProfileNamePrefix + "cri-validate-apparmor-test-deny-write" ,
98
+ }
92
99
containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , true )
93
100
checkContainerApparmor (rc , containerID , false )
94
101
})
95
102
96
- It ("should enforce a permissive profile" , func () {
97
- profile := apparmorProfileNamePrefix + "cri-validate-apparmor-test-audit-write"
103
+ It ("should enforce a permissive depracated profile" , func () {
104
+ profile := & runtimeapi.LinuxContainerSecurityContext {
105
+ ApparmorProfile : apparmorProfileNamePrefix + "cri-validate-apparmor-test-audit-write" ,
106
+ }
107
+ containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , true )
108
+ checkContainerApparmor (rc , containerID , true )
109
+ })
110
+ })
111
+
112
+ Context ("runtime should support apparmor field" , func () {
113
+ var sandboxID string
114
+ var sandboxConfig * runtimeapi.PodSandboxConfig
115
+
116
+ BeforeEach (func () {
117
+ sandboxID , sandboxConfig = framework .CreatePodSandboxForContainer (rc )
118
+ })
119
+
120
+ AfterEach (func () {
121
+ By ("stop PodSandbox" )
122
+ rc .StopPodSandbox (context .TODO (), sandboxID )
123
+ By ("delete PodSandbox" )
124
+ rc .RemovePodSandbox (context .TODO (), sandboxID )
125
+ })
126
+
127
+ It ("should fail with an unloaded apparmor_profile" , func () {
128
+ profile := & runtimeapi.LinuxContainerSecurityContext {
129
+ Apparmor : & runtimeapi.SecurityProfile {
130
+ ProfileType : runtimeapi .SecurityProfile_Localhost ,
131
+ LocalhostRef : apparmorProfileNamePrefix + "non-existent-profile" ,
132
+ },
133
+ }
134
+ containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , false )
135
+ Expect (containerID ).To (BeEmpty ())
136
+ })
137
+
138
+ It ("should enforce a apparmor_profile blocking writes" , func () {
139
+ profile := & runtimeapi.LinuxContainerSecurityContext {
140
+ Apparmor : & runtimeapi.SecurityProfile {
141
+ ProfileType : runtimeapi .SecurityProfile_Localhost ,
142
+ LocalhostRef : apparmorProfileNamePrefix + "cri-validate-apparmor-test-deny-write" ,
143
+ },
144
+ }
145
+ containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , true )
146
+ checkContainerApparmor (rc , containerID , false )
147
+ })
148
+
149
+ It ("should enforce a permissive depracated profile" , func () {
150
+ profile := & runtimeapi.LinuxContainerSecurityContext {
151
+ Apparmor : & runtimeapi.SecurityProfile {
152
+ ProfileType : runtimeapi .SecurityProfile_Localhost ,
153
+ LocalhostRef : apparmorProfileNamePrefix + "cri-validate-apparmor-test-audit-write" ,
154
+ },
155
+ }
156
+ containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , true )
157
+ checkContainerApparmor (rc , containerID , true )
158
+ })
159
+ })
160
+
161
+ Context ("runtime should prefer new apparmor field" , func () {
162
+ var sandboxID string
163
+ var sandboxConfig * runtimeapi.PodSandboxConfig
164
+
165
+ BeforeEach (func () {
166
+ sandboxID , sandboxConfig = framework .CreatePodSandboxForContainer (rc )
167
+ })
168
+
169
+ AfterEach (func () {
170
+ By ("stop PodSandbox" )
171
+ rc .StopPodSandbox (context .TODO (), sandboxID )
172
+ By ("delete PodSandbox" )
173
+ rc .RemovePodSandbox (context .TODO (), sandboxID )
174
+ })
175
+
176
+ It ("should fail with an unloaded apparmor_profile" , func () {
177
+ profile := & runtimeapi.LinuxContainerSecurityContext {
178
+ ApparmorProfile : apparmorProfileNamePrefix + "non-existent-profile" ,
179
+ Apparmor : & runtimeapi.SecurityProfile {
180
+ ProfileType : runtimeapi .SecurityProfile_Localhost ,
181
+ LocalhostRef : apparmorProfileNamePrefix + "non-existent-profile" ,
182
+ },
183
+ }
184
+ containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , false )
185
+ Expect (containerID ).To (BeEmpty ())
186
+ })
187
+
188
+ It ("should enforce a apparmor_profile blocking writes" , func () {
189
+ profile := & runtimeapi.LinuxContainerSecurityContext {
190
+ ApparmorProfile : apparmorProfileNamePrefix + "non-existent-profile" ,
191
+ Apparmor : & runtimeapi.SecurityProfile {
192
+ ProfileType : runtimeapi .SecurityProfile_Localhost ,
193
+ LocalhostRef : apparmorProfileNamePrefix + "cri-validate-apparmor-test-deny-write" ,
194
+ },
195
+ }
196
+ containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , true )
197
+ checkContainerApparmor (rc , containerID , false )
198
+ })
199
+
200
+ It ("should work with apparmor profile" , func () {
201
+ profile := & runtimeapi.LinuxContainerSecurityContext {
202
+ ApparmorProfile : apparmorProfileNamePrefix + "non-existent-profile" ,
203
+ Apparmor : & runtimeapi.SecurityProfile {
204
+ ProfileType : runtimeapi .SecurityProfile_Localhost ,
205
+ LocalhostRef : apparmorProfileNamePrefix + "cri-validate-apparmor-test-audit-write" ,
206
+ },
207
+ }
98
208
containerID := createContainerWithAppArmor (rc , ic , sandboxID , sandboxConfig , profile , true )
99
209
checkContainerApparmor (rc , containerID , true )
100
210
})
101
211
})
102
212
}
103
213
})
104
214
105
- func createContainerWithAppArmor (rc internalapi.RuntimeService , ic internalapi.ImageManagerService , sandboxID string , sandboxConfig * runtimeapi.PodSandboxConfig , profile string , shouldSucceed bool ) string {
215
+ func createContainerWithAppArmor (rc internalapi.RuntimeService , ic internalapi.ImageManagerService , sandboxID string , sandboxConfig * runtimeapi.PodSandboxConfig , profile * runtimeapi. LinuxContainerSecurityContext , shouldSucceed bool ) string {
106
216
By ("create a container with apparmor" )
107
217
containerName := "apparmor-test-" + framework .NewUUID ()
108
218
containerConfig := & runtimeapi.ContainerConfig {
109
219
Metadata : framework .BuildContainerMetadata (containerName , framework .DefaultAttempt ),
110
220
Image : & runtimeapi.ImageSpec {Image : framework .TestContext .TestImageList .DefaultTestContainerImage },
111
221
Command : []string {"touch" , "/tmp/foo" },
112
222
Linux : & runtimeapi.LinuxContainerConfig {
113
- SecurityContext : & runtimeapi.LinuxContainerSecurityContext {
114
- ApparmorProfile : profile ,
115
- },
223
+ SecurityContext : profile ,
116
224
},
117
225
}
118
226
0 commit comments