7
7
"fmt"
8
8
"time"
9
9
10
+ "github.com/kubearmor/KubeArmor/protobuf"
10
11
"github.com/kubearmor/KubeArmor/tests/util"
12
+
11
13
. "github.com/kubearmor/KubeArmor/tests/util"
12
14
. "github.com/onsi/ginkgo/v2"
13
15
. "github.com/onsi/gomega"
@@ -72,11 +74,15 @@ var _ = Describe("Smoke", func() {
72
74
Expect (sout ).To (MatchRegexp ("apt.*Permission denied" ))
73
75
74
76
// check policy violation alert
75
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
77
+ expect := protobuf.Alert {
78
+ PolicyName : "ksp-wordpress-block-process" ,
79
+ Severity : "3" ,
80
+ }
81
+
82
+ // check policy violation alert
83
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
76
84
Expect (err ).To (BeNil ())
77
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
78
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-wordpress-block-process" ))
79
- Expect (alerts [0 ].Severity ).To (Equal ("3" ))
85
+ Expect (res .Found ).To (BeTrue ())
80
86
})
81
87
82
88
It ("can block execution of access to sensitive file with abs path" , func () {
@@ -98,13 +104,15 @@ var _ = Describe("Smoke", func() {
98
104
Expect (sout ).To (MatchRegexp ("wp-config.php.*Permission denied" ))
99
105
100
106
// check policy violation alert
101
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
107
+ expect := protobuf.Alert {
108
+ PolicyName : "ksp-wordpress-block-config" ,
109
+ Severity : "10" ,
110
+ Message : "blocked access to wordpress configuration file" ,
111
+ }
112
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
102
113
Expect (err ).To (BeNil ())
103
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
104
- fmt .Printf ("%+v\n " , alerts [0 ])
105
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-wordpress-block-config" ))
106
- Expect (alerts [0 ].Severity ).To (Equal ("10" ))
107
- Expect (alerts [0 ].Message ).To (Equal ("blocked access to wordpress configuration file" ))
114
+ Expect (res .Found ).To (BeTrue ())
115
+
108
116
})
109
117
110
118
It ("can block execution of access to sensitive file with rel path" , func () {
@@ -126,13 +134,14 @@ var _ = Describe("Smoke", func() {
126
134
Expect (sout ).To (MatchRegexp ("wp-config.php.*Permission denied" ))
127
135
128
136
// check policy violation alert
129
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
130
- Expect (err ).To (BeNil ())
131
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
132
- fmt .Printf ("%+v\n " , alerts [0 ])
133
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-wordpress-block-config" ))
134
- Expect (alerts [0 ].Severity ).To (Equal ("10" ))
135
- Expect (alerts [0 ].Message ).To (Equal ("blocked access to wordpress configuration file" ))
137
+ expect := protobuf.Alert {
138
+ PolicyName : "ksp-wordpress-block-config" ,
139
+ Severity : "10" ,
140
+ Message : "blocked access to wordpress configuration file" ,
141
+ }
142
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
143
+ Expect (err ).To (BeNil ())
144
+ Expect (res .Found ).To (BeTrue ())
136
145
})
137
146
138
147
It ("can block execution of access to service account token" , func () {
@@ -154,11 +163,13 @@ var _ = Describe("Smoke", func() {
154
163
Expect (sout ).To (MatchRegexp ("token.*Permission denied" ))
155
164
156
165
// check policy violation alert
157
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
166
+ expect := protobuf.Alert {
167
+ PolicyName : "ksp-wordpress-block-sa" ,
168
+ Severity : "7" ,
169
+ }
170
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
158
171
Expect (err ).To (BeNil ())
159
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
160
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-wordpress-block-sa" ))
161
- Expect (alerts [0 ].Severity ).To (Equal ("7" ))
172
+ Expect (res .Found ).To (BeTrue ())
162
173
})
163
174
164
175
It ("allow access for service account token to only cat" , func () {
@@ -181,12 +192,14 @@ var _ = Describe("Smoke", func() {
181
192
Expect (sout ).To (MatchRegexp ("token.*Permission denied" ))
182
193
183
194
// check policy violation alert
184
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
195
+ expect := protobuf.Alert {
196
+ PolicyName : "ksp-wordpress-lenient-allow-sa" ,
197
+ Severity : "7" ,
198
+ Source : "head" ,
199
+ }
200
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
185
201
Expect (err ).To (BeNil ())
186
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
187
- fmt .Printf ("---Alert---\n %s" , alerts [0 ].String ())
188
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-wordpress-lenient-allow-sa" ))
189
- Expect (alerts [0 ].Severity ).To (Equal ("7" ))
202
+ Expect (res .Found ).To (BeTrue ())
190
203
191
204
// trigger normal operations permitted by policy
192
205
sout , _ , err = K8sExecInPod (wp , "wordpress-mysql" ,
@@ -205,9 +218,14 @@ var _ = Describe("Smoke", func() {
205
218
Expect (sout ).To (Not (ContainSubstring ("Permission denied" )))
206
219
207
220
// check for no policy violation alert
208
- _ , alerts , err = KarmorGetLogs (3 * time .Second , 1 )
209
- Expect (err ).To (BeNil ())
210
- Expect (len (alerts )).To (BeNumerically ("==" , 0 ))
221
+ expect = protobuf.Alert {
222
+ PolicyName : "ksp-wordpress-lenient-allow-sa" ,
223
+ Severity : "7" ,
224
+ Source : "cat" ,
225
+ }
226
+ res , err = KarmorGetTargetAlert (5 * time .Second , & expect )
227
+ Expect (err ).To (BeNil ())
228
+ Expect (res .Found ).To (BeFalse ())
211
229
})
212
230
213
231
It ("can audit access to sensitive data path" , func () {
@@ -229,12 +247,13 @@ var _ = Describe("Smoke", func() {
229
247
fmt .Printf ("OUTPUT: %s\n " , sout )
230
248
231
249
// check policy violation alert
232
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
250
+ expect := protobuf.Alert {
251
+ PolicyName : "ksp-mysql-audit-dir" ,
252
+ Severity : "5" ,
253
+ }
254
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
233
255
Expect (err ).To (BeNil ())
234
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
235
- fmt .Printf ("---Alert---\n %s" , alerts [0 ].String ())
236
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-mysql-audit-dir" ))
237
- Expect (alerts [0 ].Severity ).To (Equal ("5" ))
256
+ Expect (res .Found ).To (BeTrue ())
238
257
239
258
_ , _ , err = K8sExecInPod (sql , "wordpress-mysql" ,
240
259
[]string {"bash" , "-c" , fmt .Sprintf ("rm %s" , fname )})
@@ -283,10 +302,13 @@ var _ = Describe("Smoke", func() {
283
302
Expect (sout ).To (ContainSubstring ("Permission denied" ))
284
303
285
304
// check policy violation alert
286
- _ , alerts , err := KarmorGetLogs (5 * time .Second , 1 )
305
+ expect := protobuf.Alert {
306
+ PolicyName : "ksp-wordpress-block-mount-file" ,
307
+ Severity : "5" ,
308
+ }
309
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
287
310
Expect (err ).To (BeNil ())
288
- Expect (alerts [0 ].PolicyName ).To (Equal ("ksp-wordpress-block-mount-file" ))
289
- Expect (alerts [0 ].Severity ).To (Equal ("5" ))
311
+ Expect (res .Found ).To (BeTrue ())
290
312
})
291
313
It ("will allow use of tcp network protocol by curl and bash" , func () {
292
314
err := util .AnnotateNS ("wordpress-mysql" , "kubearmor-network-posture" , "audit" )
@@ -323,11 +345,13 @@ var _ = Describe("Smoke", func() {
323
345
Expect (sout ).To (ContainSubstring ("http://www.google.com/" ))
324
346
325
347
// check alert
326
- _ , alerts , err = KarmorGetLogs (5 * time .Second , 1 )
348
+ expect := protobuf.Alert {
349
+ PolicyName : "DefaultPosture" ,
350
+ Result : "Passed" ,
351
+ }
352
+ res , err := KarmorGetTargetAlert (5 * time .Second , & expect )
327
353
Expect (err ).To (BeNil ())
328
- Expect (len (alerts )).To (BeNumerically (">=" , 1 ))
329
- Expect (alerts [0 ].PolicyName ).To (Equal ("DefaultPosture" ))
330
- Expect (alerts [0 ].Result ).To (Equal ("Passed" ))
354
+ Expect (res .Found ).To (BeTrue ())
331
355
})
332
356
})
333
357
})
0 commit comments