17
17
get_file_contents , get_first_pod_name ,
18
18
get_ingress_nginx_template_conf ,
19
19
get_last_reload_time , get_pods_amount ,
20
- get_service_endpoint , get_test_file_name ,
20
+ clear_file_contents , get_test_file_name ,
21
21
scale_deployment , wait_before_test ,
22
22
wait_until_all_pods_are_ready ,
23
- write_to_json )
23
+ write_to_json , get_pod_name_that_contains )
24
24
from suite .yaml_utils import get_first_ingress_host_from_yaml
25
25
26
26
src_ing_yaml = f"{ TEST_DATA } /appprotect/appprotect-ingress.yaml"
@@ -84,8 +84,13 @@ def appprotect_setup(
84
84
src_pol_yaml = f"{ TEST_DATA } /appprotect/{ ap_policy } .yaml"
85
85
pol_name = create_ap_policy_from_yaml (kube_apis .custom_objects , src_pol_yaml , test_namespace )
86
86
87
+ print ("------------------------- Deploy syslog server ---------------------------" )
88
+ src_syslog_yaml = f"{ TEST_DATA } /appprotect/syslog.yaml"
89
+ create_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
90
+
87
91
def fin ():
88
92
print ("Clean up:" )
93
+ delete_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
89
94
delete_ap_policy (kube_apis .custom_objects , pol_name , test_namespace )
90
95
delete_ap_logconf (kube_apis .custom_objects , log_name , test_namespace )
91
96
delete_common_app (kube_apis , "simple" , test_namespace )
@@ -297,6 +302,7 @@ def test_ap_enable_false_policy_incorrect(
297
302
delete_items_from_yaml (kube_apis , src_ing_yaml , test_namespace )
298
303
assert_valid_responses (response )
299
304
305
+ @pytest .mark .flaky (max_runs = 3 )
300
306
def test_ap_sec_logs_on (
301
307
self ,
302
308
request ,
@@ -309,15 +315,9 @@ def test_ap_sec_logs_on(
309
315
"""
310
316
Test corresponding log entries with correct policy (includes setting up a syslog server as defined in syslog.yaml)
311
317
"""
312
- src_syslog_yaml = f"{ TEST_DATA } /appprotect/syslog.yaml"
313
318
log_loc = "/var/log/messages"
314
-
315
- create_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
316
-
317
319
syslog_dst = f"syslog-svc.{ test_namespace } "
318
-
319
- # items[-1] because syslog pod is last one to spin-up
320
- syslog_pod = kube_apis .v1 .list_namespaced_pod (test_namespace ).items [- 1 ].metadata .name
320
+ syslog_pod = get_pod_name_that_contains (kube_apis .v1 , test_namespace , "syslog-" )
321
321
322
322
create_ingress_with_ap_annotations (
323
323
kube_apis , src_ing_yaml , test_namespace , ap_policy , "True" , "True" , f"{ syslog_dst } :514"
@@ -333,9 +333,9 @@ def test_ap_sec_logs_on(
333
333
appprotect_setup .req_url + "/<script>" , headers ={"host" : ingress_host }, verify = False
334
334
)
335
335
print (response_block .text )
336
- log_contents = ""
336
+ log_contents_block = ""
337
337
retry = 0
338
- while "ASM:attack_type" not in log_contents and retry <= 60 :
338
+ while "ASM:attack_type" not in log_contents_block and retry <= 30 :
339
339
log_contents_block = get_file_contents (
340
340
kube_apis .v1 , log_loc , syslog_pod , test_namespace
341
341
)
@@ -354,7 +354,7 @@ def test_ap_sec_logs_on(
354
354
log_contents = get_file_contents (kube_apis .v1 , log_loc , syslog_pod , test_namespace )
355
355
356
356
delete_items_from_yaml (kube_apis , src_ing_yaml , test_namespace )
357
- delete_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
357
+ clear_file_contents (kube_apis . v1 , log_loc , syslog_pod , test_namespace )
358
358
359
359
assert_invalid_responses (response_block )
360
360
assert (
@@ -384,15 +384,8 @@ def test_ap_pod_startup(
384
384
"""
385
385
Log pod startup time while scaling up from 0 to 1
386
386
"""
387
- src_syslog_yaml = f"{ TEST_DATA } /appprotect/syslog.yaml"
388
- create_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
389
-
390
387
syslog_dst = f"syslog-svc.{ test_namespace } "
391
388
392
- # FIXME this is not used
393
- # items[-1] because syslog pod is last one to spin-up
394
- # syslog_pod = kube_apis.v1.list_namespaced_pod(test_namespace).items[-1].metadata.name
395
-
396
389
create_ingress_with_ap_annotations (
397
390
kube_apis , src_ing_yaml , test_namespace , ap_policy , "True" , "True" , f"{ syslog_dst } :514"
398
391
)
@@ -408,29 +401,27 @@ def test_ap_pod_startup(
408
401
wait_before_test ()
409
402
num = scale_deployment (kube_apis .v1 , kube_apis .apps_v1_api , "nginx-ingress" , ns , 1 )
410
403
delete_items_from_yaml (kube_apis , src_ing_yaml , test_namespace )
411
- delete_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
412
404
413
405
assert num is None
414
406
407
+ @pytest .mark .flaky (max_runs = 3 )
415
408
def test_ap_multi_sec_logs (
416
409
self , request , kube_apis , crd_ingress_controller_with_ap , appprotect_setup , test_namespace
417
410
):
418
411
"""
419
412
Test corresponding log entries with multiple log destinations (in this case, two syslog servers)
420
413
"""
421
- src_syslog_yaml = f"{ TEST_DATA } /appprotect/syslog.yaml"
422
414
src_syslog2_yaml = f"{ TEST_DATA } /appprotect/syslog2.yaml"
423
415
log_loc = "/var/log/messages"
424
416
425
- print ("Create two syslog servers" )
426
- create_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
417
+ print ("Create a second syslog server" )
427
418
create_items_from_yaml (kube_apis , src_syslog2_yaml , test_namespace )
428
419
429
420
syslog_dst = f"syslog-svc.{ test_namespace } "
430
421
syslog2_dst = f"syslog2-svc.{ test_namespace } "
431
422
432
- syslog_pod = kube_apis .v1 . list_namespaced_pod ( test_namespace ). items [ - 2 ]. metadata . name
433
- syslog2_pod = kube_apis .v1 . list_namespaced_pod ( test_namespace ). items [ - 1 ]. metadata . name
423
+ syslog_pod = get_pod_name_that_contains ( kube_apis .v1 , test_namespace , "syslog-" )
424
+ syslog2_pod = get_pod_name_that_contains ( kube_apis .v1 , test_namespace , "syslog2" )
434
425
435
426
with open (src_ing_yaml ) as f :
436
427
doc = yaml .safe_load (f )
@@ -454,6 +445,7 @@ def test_ap_multi_sec_logs(
454
445
455
446
ingress_host = get_first_ingress_host_from_yaml (src_ing_yaml )
456
447
448
+ wait_before_test (30 )
457
449
ensure_response_from_backend (appprotect_setup .req_url , ingress_host , check404 = True )
458
450
459
451
print ("----------------------- Send request ----------------------" )
@@ -480,8 +472,8 @@ def test_ap_multi_sec_logs(
480
472
reload_times [f"{ request .node .name } " ] = f"last reload duration: { reload_ms } ms"
481
473
482
474
delete_items_from_yaml (kube_apis , src_ing_yaml , test_namespace )
483
- delete_items_from_yaml (kube_apis , src_syslog_yaml , test_namespace )
484
475
delete_items_from_yaml (kube_apis , src_syslog2_yaml , test_namespace )
476
+ clear_file_contents (kube_apis .v1 , log_loc , syslog_pod , test_namespace )
485
477
486
478
assert_invalid_responses (response )
487
479
# check logs in dest. #1 i.e. syslog server #1
@@ -501,6 +493,7 @@ def test_ap_multi_sec_logs(
501
493
and 'outcome="REJECTED"' in log2_contents
502
494
)
503
495
496
+ @pytest .mark .flaky (max_runs = 3 )
504
497
def test_ap_enable_true_policy_correct_uds (
505
498
self , request , kube_apis , crd_ingress_controller_with_ap , appprotect_setup , test_namespace
506
499
):
0 commit comments