@@ -8,15 +8,20 @@ class TestSecConfig(case.TestCase):
88 requires_waf = True
99
1010 def apply_config (self , conf_name ):
11- conf_path = Path (__file__ ).parent / f' ./conf/http_{ conf_name } .conf'
11+ conf_path = Path (__file__ ).parent / f" ./conf/http_{ conf_name } .conf"
1212 conf_text = conf_path .read_text ()
1313 status , log_lines = self .orch .nginx_replace_config (
1414 conf_text , conf_path .name )
1515 self .assertEqual (0 , status , log_lines )
1616
17+ def _test_config (self , conf_name ):
18+ conf_path = Path (__file__ ).parent / f"./conf/http_{ conf_name } .conf"
19+ conf_text = conf_path .read_text ()
20+ return self .orch .nginx_replace_config (conf_text , conf_path .name )
21+
1722 def get_appsec_data (self ):
1823 self .orch .reload_nginx ()
19- log_lines = self .orch .sync_service (' agent' )
24+ log_lines = self .orch .sync_service (" agent" )
2025 entries = [
2126 entry for entry in (formats .parse_trace (line )
2227 for line in log_lines ) if entry is not None
@@ -25,141 +30,140 @@ def get_appsec_data(self):
2530 for entry in entries :
2631 for trace in entry :
2732 for span in trace :
28- if span .get (' meta' , {}).get (' _dd.appsec.json' ):
29- return json .loads (span [' meta' ][ ' _dd.appsec.json' ])
30- self .failureException (' No _dd.appsec.json found in traces' )
33+ if span .get (" meta" , {}).get (" _dd.appsec.json" ):
34+ return json .loads (span [" meta" ][ " _dd.appsec.json" ])
35+ self .failureException (" No _dd.appsec.json found in traces" )
3136
3237 def test_custom_templates (self ):
33- templ_json_path = Path (__file__ ).parent / ' ./conf/templ.json'
34- templ_html_path = Path (__file__ ).parent / ' ./conf/templ.html'
35- self .orch .nginx_replace_file (' /tmp/templ.json' ,
38+ templ_json_path = Path (__file__ ).parent / " ./conf/templ.json"
39+ templ_html_path = Path (__file__ ).parent / " ./conf/templ.html"
40+ self .orch .nginx_replace_file (" /tmp/templ.json" ,
3641 templ_json_path .read_text ())
37- self .orch .nginx_replace_file (' /tmp/templ.html' ,
42+ self .orch .nginx_replace_file (" /tmp/templ.html" ,
3843 templ_html_path .read_text ())
3944
40- self .apply_config (' custom_blocking_templates' )
45+ self .apply_config (" custom_blocking_templates" )
4146
4247 headers = {
43- ' User-Agent' : ' dd-test-scanner-log-block' ,
44- ' Accept' : ' text/html'
48+ " User-Agent" : " dd-test-scanner-log-block" ,
49+ " Accept" : " text/html"
4550 }
4651 status , headers , body = self .orch .send_nginx_http_request (
47- ' /http' , 80 , headers )
52+ " /http" , 80 , headers )
4853 self .assertEqual (status , 403 )
4954 # find content-type header:
5055 ct = next ((v for k , v in headers if k .lower () == "content-type" ), None )
51- self .assertEqual (ct , ' text/html;charset=utf-8' )
52- self .assertTrue (' My custom blocking response' in body )
56+ self .assertEqual (ct , " text/html;charset=utf-8" )
57+ self .assertTrue (" My custom blocking response" in body )
5358
5459 headers = {
55- ' User-Agent' : ' dd-test-scanner-log-block' ,
56- ' Accept' : ' text/json'
60+ " User-Agent" : " dd-test-scanner-log-block" ,
61+ " Accept" : " text/json"
5762 }
5863 status , headers , body = self .orch .send_nginx_http_request (
59- ' /http' , 80 , headers )
64+ " /http" , 80 , headers )
6065 self .assertEqual (status , 403 )
6166 ct = next ((v for k , v in headers if k .lower () == "content-type" ), None )
62- self .assertEqual (ct , ' application/json' )
67+ self .assertEqual (ct , " application/json" )
6368 self .assertEqual (
6469 body ,
6570 '{"error": "blocked", "details": "my custom json response"}\n ' )
6671
6772 def test_appsec_fully_disabled (self ):
68- self .apply_config (' appsec_fully_disabled' )
73+ self .apply_config (" appsec_fully_disabled" )
6974
7075 headers = {
71- ' User-Agent' : ' dd-test-scanner-log-block' ,
72- ' Accept' : ' text/json'
76+ " User-Agent" : " dd-test-scanner-log-block" ,
77+ " Accept" : " text/json"
7378 }
74- status , _ , _ = self .orch .send_nginx_http_request ('/' , 80 , headers )
79+ status , _ , _ = self .orch .send_nginx_http_request ("/" , 80 , headers )
7580 self .assertEqual (status , 200 )
7681
7782 def test_bad_custom_template (self ):
78- self .apply_config ('bad_template_file' )
79-
80- msg = self .orch .wait_for_log_message (
81- 'nginx' ,
82- '.*Initialising security library failed.*' ,
83- timeout_secs = 5 )
83+ # We can't afford to shutdown workers
84+ status , log_lines = self ._test_config ("bad_template_file" )
85+ self .assertNotEqual (0 , status , log_lines )
8486 self .assertTrue (
85- 'Failed to open file: /file/that/does/not/exist' in msg )
87+ any ('Failed to open file: "/file/that/does/not/exist"' in line
88+ for line in log_lines ))
8689
8790 def test_bad_rules_file (self ):
88- self .apply_config ('bad_rules_file' )
89-
90- msg = self .orch .wait_for_log_message (
91- 'nginx' ,
92- '.*Initialising security library failed.*' ,
93- timeout_secs = 5 )
94- self .assertTrue ('Failed to open file: /bad/rules/file' in msg )
91+ status , log_lines = self ._test_config ("bad_rules_file" )
92+ self .assertNotEqual (0 , status , log_lines )
93+ self .assertTrue (
94+ any ('Failed to open file: "/bad/rules/file' in line
95+ for line in log_lines ))
9596
9697 def test_bad_pool_name (self ):
97- conf_path = Path (__file__ ).parent / 'conf/http_bad_thread_pool.conf'
98- conf_text = conf_path .read_text ()
99- status , log_lines = self .orch .nginx_replace_config (
100- conf_text , conf_path .name )
98+ status , log_lines = self ._test_config ("bad_thread_pool" )
10199 self .assertNotEqual (0 , status , log_lines )
102100
103101 self .assertTrue (
104102 any ('datadog_waf_thread_pool_name: "bad_thread_pool" not found' in
105103 line for line in log_lines ))
106104
107105 def test_multiple_pools (self ):
108- self .apply_config (' multiple_thread_pools' )
106+ self .apply_config (" multiple_thread_pools" )
109107
110- headers = {' User-Agent' : ' dd-test-scanner-log-block' }
108+ headers = {" User-Agent" : " dd-test-scanner-log-block" }
111109 status , _ , _ = self .orch .send_nginx_http_request (
112- ' /http/a' , 80 , headers )
110+ " /http/a" , 80 , headers )
113111 self .assertEqual (status , 403 )
114112
115- headers = {' User-Agent' : ' dd-test-scanner-log-block' }
113+ headers = {" User-Agent" : " dd-test-scanner-log-block" }
116114 status , _ , _ = self .orch .send_nginx_http_request (
117- ' /local/' , 80 , headers )
115+ " /local/" , 80 , headers )
118116 self .assertEqual (status , 403 )
119117
120- headers = {' User-Agent' : ' dd-test-scanner-log-block' }
118+ headers = {" User-Agent" : " dd-test-scanner-log-block" }
121119 status , _ , _ = self .orch .send_nginx_http_request (
122- ' /unmonitored/index.html' , 80 , headers )
120+ " /unmonitored/index.html" , 80 , headers )
123121 self .assertEqual (status , 200 )
124122
125123 def test_custom_obfuscation (self ):
126- waf_path = Path (__file__ ).parent / ' ./conf/waf.json'
124+ waf_path = Path (__file__ ).parent / " ./conf/waf.json"
127125 waf_text = waf_path .read_text ()
128- self .orch .nginx_replace_file (' /tmp/waf.json' , waf_text )
126+ self .orch .nginx_replace_file (" /tmp/waf.json" , waf_text )
129127
130- self .apply_config (' custom_obfuscation' )
128+ self .apply_config (" custom_obfuscation" )
131129
132130 # Redaction by key
133131 # datadog_appsec_obfuscation_key_regex my.special.key;
134132 status , _ , _ = self .orch .send_nginx_http_request (
135- ' /http/?my_special_key=matched+value' , 80 )
133+ " /http/?my_special_key=matched+value" , 80 )
136134 appsec_data = self .get_appsec_data ()
137135 self .assertEqual (
138- appsec_data ['triggers' ][0 ]['rule_matches' ][0 ]['parameters' ][0 ]
139- ['value' ], '<Redacted>' )
136+ appsec_data ["triggers" ][0 ]["rule_matches" ][0 ]["parameters" ][0 ]
137+ ["value" ],
138+ "<Redacted>" ,
139+ )
140140
141141 # Redaction by value
142142 # datadog_appsec_obfuscation_value_regex \Az.*;
143143 status , _ , _ = self .orch .send_nginx_http_request (
144- ' /http/?the+key=z_matched+value' , 80 )
144+ " /http/?the+key=z_matched+value" , 80 )
145145 appsec_data = self .get_appsec_data ()
146146 self .assertEqual (
147- appsec_data ['triggers' ][0 ]['rule_matches' ][0 ]['parameters' ][0 ]
148- ['value' ], '<Redacted>' )
147+ appsec_data ["triggers" ][0 ]["rule_matches" ][0 ]["parameters" ][0 ]
148+ ["value" ],
149+ "<Redacted>" ,
150+ )
149151
150152 def test_no_obfuscation (self ):
151- waf_path = Path (__file__ ).parent / ' ./conf/waf.json'
153+ waf_path = Path (__file__ ).parent / " ./conf/waf.json"
152154 waf_text = waf_path .read_text ()
153- self .orch .nginx_replace_file (' /tmp/waf.json' , waf_text )
155+ self .orch .nginx_replace_file (" /tmp/waf.json" , waf_text )
154156
155- self .apply_config (' no_obfuscation' )
157+ self .apply_config (" no_obfuscation" )
156158
157- self .orch .sync_service (' agent' )
159+ self .orch .sync_service (" agent" )
158160
159161 # No redaction by key
160162 status , _ , _ = self .orch .send_nginx_http_request (
161- ' /http/?password=matched+value' , 80 )
163+ " /http/?password=matched+value" , 80 )
162164 appsec_data = self .get_appsec_data ()
163165 self .assertEqual (
164- appsec_data ['triggers' ][0 ]['rule_matches' ][0 ]['parameters' ][0 ]
165- ['value' ], 'matched value' )
166+ appsec_data ["triggers" ][0 ]["rule_matches" ][0 ]["parameters" ][0 ]
167+ ["value" ],
168+ "matched value" ,
169+ )
0 commit comments