-
Notifications
You must be signed in to change notification settings - Fork 10
fix(test): rework reload_nginx
#197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "common/directives.h" | ||
|
||
#include <cassert> | ||
#include <filesystem> | ||
|
||
#include "string_util.h" | ||
|
||
namespace datadog::common { | ||
|
||
char *check_file_exists(ngx_conf_t *cf, void *post, void *data) { | ||
assert(data != nullptr); | ||
ngx_str_t *s = (ngx_str_t *)data; | ||
if (!std::filesystem::exists(nginx::to_string_view(*s))) { | ||
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Failed to open file: \"%V\"", s); | ||
return static_cast<char *>(NGX_CONF_ERROR); | ||
} | ||
return NGX_CONF_OK; | ||
} | ||
|
||
} // namespace datadog::common |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
extern "C" { | ||
#include <ngx_core.h> | ||
} | ||
|
||
namespace datadog::common { | ||
|
||
/// Checks if the file specified in the configuration exists. | ||
/// | ||
/// This function is typically used as a post-processing callback for NGINX | ||
/// configuration It verifies that the file specified in a configuration | ||
/// directive actually exists on the filesystem. | ||
char *check_file_exists(ngx_conf_t *cf, void *post, void *data); | ||
|
||
/// Post handler for checking a filepath exists. | ||
static ngx_conf_post_t ngx_conf_post_file_exists = {check_file_exists}; | ||
|
||
} // namespace datadog::common |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,20 @@ class TestSecConfig(case.TestCase): | |
requires_waf = True | ||
|
||
def apply_config(self, conf_name): | ||
conf_path = Path(__file__).parent / f'./conf/http_{conf_name}.conf' | ||
conf_path = Path(__file__).parent / f"./conf/http_{conf_name}.conf" | ||
conf_text = conf_path.read_text() | ||
status, log_lines = self.orch.nginx_replace_config( | ||
conf_text, conf_path.name) | ||
self.assertEqual(0, status, log_lines) | ||
|
||
def _test_config(self, conf_name): | ||
conf_path = Path(__file__).parent / f"./conf/http_{conf_name}.conf" | ||
conf_text = conf_path.read_text() | ||
return self.orch.nginx_replace_config(conf_text, conf_path.name) | ||
|
||
def get_appsec_data(self): | ||
self.orch.reload_nginx() | ||
log_lines = self.orch.sync_service('agent') | ||
log_lines = self.orch.sync_service("agent") | ||
entries = [ | ||
entry for entry in (formats.parse_trace(line) | ||
for line in log_lines) if entry is not None | ||
|
@@ -25,141 +30,140 @@ def get_appsec_data(self): | |
for entry in entries: | ||
for trace in entry: | ||
for span in trace: | ||
if span.get('meta', {}).get('_dd.appsec.json'): | ||
return json.loads(span['meta']['_dd.appsec.json']) | ||
self.failureException('No _dd.appsec.json found in traces') | ||
if span.get("meta", {}).get("_dd.appsec.json"): | ||
return json.loads(span["meta"]["_dd.appsec.json"]) | ||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Code Quality Violationtoo many nesting levels (...read more)Avoid to nest too many loops together. Having too many loops make your code harder to understand. Learn More |
||
self.failureException("No _dd.appsec.json found in traces") | ||
|
||
def test_custom_templates(self): | ||
templ_json_path = Path(__file__).parent / './conf/templ.json' | ||
templ_html_path = Path(__file__).parent / './conf/templ.html' | ||
self.orch.nginx_replace_file('/tmp/templ.json', | ||
templ_json_path = Path(__file__).parent / "./conf/templ.json" | ||
templ_html_path = Path(__file__).parent / "./conf/templ.html" | ||
self.orch.nginx_replace_file("/tmp/templ.json", | ||
templ_json_path.read_text()) | ||
self.orch.nginx_replace_file('/tmp/templ.html', | ||
self.orch.nginx_replace_file("/tmp/templ.html", | ||
templ_html_path.read_text()) | ||
|
||
self.apply_config('custom_blocking_templates') | ||
self.apply_config("custom_blocking_templates") | ||
|
||
headers = { | ||
'User-Agent': 'dd-test-scanner-log-block', | ||
'Accept': 'text/html' | ||
"User-Agent": "dd-test-scanner-log-block", | ||
"Accept": "text/html" | ||
} | ||
status, headers, body = self.orch.send_nginx_http_request( | ||
'/http', 80, headers) | ||
"/http", 80, headers) | ||
self.assertEqual(status, 403) | ||
# find content-type header: | ||
ct = next((v for k, v in headers if k.lower() == "content-type"), None) | ||
self.assertEqual(ct, 'text/html;charset=utf-8') | ||
self.assertTrue('My custom blocking response' in body) | ||
self.assertEqual(ct, "text/html;charset=utf-8") | ||
self.assertTrue("My custom blocking response" in body) | ||
|
||
headers = { | ||
'User-Agent': 'dd-test-scanner-log-block', | ||
'Accept': 'text/json' | ||
"User-Agent": "dd-test-scanner-log-block", | ||
"Accept": "text/json" | ||
} | ||
status, headers, body = self.orch.send_nginx_http_request( | ||
'/http', 80, headers) | ||
"/http", 80, headers) | ||
self.assertEqual(status, 403) | ||
ct = next((v for k, v in headers if k.lower() == "content-type"), None) | ||
self.assertEqual(ct, 'application/json') | ||
self.assertEqual(ct, "application/json") | ||
self.assertEqual( | ||
body, | ||
'{"error": "blocked", "details": "my custom json response"}\n') | ||
|
||
def test_appsec_fully_disabled(self): | ||
self.apply_config('appsec_fully_disabled') | ||
self.apply_config("appsec_fully_disabled") | ||
|
||
headers = { | ||
'User-Agent': 'dd-test-scanner-log-block', | ||
'Accept': 'text/json' | ||
"User-Agent": "dd-test-scanner-log-block", | ||
"Accept": "text/json" | ||
} | ||
status, _, _ = self.orch.send_nginx_http_request('/', 80, headers) | ||
status, _, _ = self.orch.send_nginx_http_request("/", 80, headers) | ||
self.assertEqual(status, 200) | ||
|
||
def test_bad_custom_template(self): | ||
self.apply_config('bad_template_file') | ||
|
||
msg = self.orch.wait_for_log_message( | ||
'nginx', | ||
'.*Initialising security library failed.*', | ||
timeout_secs=5) | ||
# We can't afford to shutdown workers | ||
status, log_lines = self._test_config("bad_template_file") | ||
self.assertNotEqual(0, status, log_lines) | ||
self.assertTrue( | ||
'Failed to open file: /file/that/does/not/exist' in msg) | ||
any('Failed to open file: "/file/that/does/not/exist"' in line | ||
for line in log_lines)) | ||
|
||
def test_bad_rules_file(self): | ||
self.apply_config('bad_rules_file') | ||
|
||
msg = self.orch.wait_for_log_message( | ||
'nginx', | ||
'.*Initialising security library failed.*', | ||
timeout_secs=5) | ||
self.assertTrue('Failed to open file: /bad/rules/file' in msg) | ||
status, log_lines = self._test_config("bad_rules_file") | ||
self.assertNotEqual(0, status, log_lines) | ||
self.assertTrue( | ||
any('Failed to open file: "/bad/rules/file' in line | ||
for line in log_lines)) | ||
|
||
def test_bad_pool_name(self): | ||
conf_path = Path(__file__).parent / 'conf/http_bad_thread_pool.conf' | ||
conf_text = conf_path.read_text() | ||
status, log_lines = self.orch.nginx_replace_config( | ||
conf_text, conf_path.name) | ||
status, log_lines = self._test_config("bad_thread_pool") | ||
self.assertNotEqual(0, status, log_lines) | ||
|
||
self.assertTrue( | ||
any('datadog_waf_thread_pool_name: "bad_thread_pool" not found' in | ||
line for line in log_lines)) | ||
|
||
def test_multiple_pools(self): | ||
self.apply_config('multiple_thread_pools') | ||
self.apply_config("multiple_thread_pools") | ||
|
||
headers = {'User-Agent': 'dd-test-scanner-log-block'} | ||
headers = {"User-Agent": "dd-test-scanner-log-block"} | ||
status, _, _ = self.orch.send_nginx_http_request( | ||
'/http/a', 80, headers) | ||
"/http/a", 80, headers) | ||
self.assertEqual(status, 403) | ||
|
||
headers = {'User-Agent': 'dd-test-scanner-log-block'} | ||
headers = {"User-Agent": "dd-test-scanner-log-block"} | ||
status, _, _ = self.orch.send_nginx_http_request( | ||
'/local/', 80, headers) | ||
"/local/", 80, headers) | ||
self.assertEqual(status, 403) | ||
|
||
headers = {'User-Agent': 'dd-test-scanner-log-block'} | ||
headers = {"User-Agent": "dd-test-scanner-log-block"} | ||
status, _, _ = self.orch.send_nginx_http_request( | ||
'/unmonitored/index.html', 80, headers) | ||
"/unmonitored/index.html", 80, headers) | ||
self.assertEqual(status, 200) | ||
|
||
def test_custom_obfuscation(self): | ||
waf_path = Path(__file__).parent / './conf/waf.json' | ||
waf_path = Path(__file__).parent / "./conf/waf.json" | ||
waf_text = waf_path.read_text() | ||
self.orch.nginx_replace_file('/tmp/waf.json', waf_text) | ||
self.orch.nginx_replace_file("/tmp/waf.json", waf_text) | ||
|
||
self.apply_config('custom_obfuscation') | ||
self.apply_config("custom_obfuscation") | ||
|
||
# Redaction by key | ||
# datadog_appsec_obfuscation_key_regex my.special.key; | ||
status, _, _ = self.orch.send_nginx_http_request( | ||
'/http/?my_special_key=matched+value', 80) | ||
"/http/?my_special_key=matched+value", 80) | ||
appsec_data = self.get_appsec_data() | ||
self.assertEqual( | ||
appsec_data['triggers'][0]['rule_matches'][0]['parameters'][0] | ||
['value'], '<Redacted>') | ||
appsec_data["triggers"][0]["rule_matches"][0]["parameters"][0] | ||
["value"], | ||
"<Redacted>", | ||
) | ||
|
||
# Redaction by value | ||
# datadog_appsec_obfuscation_value_regex \Az.*; | ||
status, _, _ = self.orch.send_nginx_http_request( | ||
'/http/?the+key=z_matched+value', 80) | ||
"/http/?the+key=z_matched+value", 80) | ||
appsec_data = self.get_appsec_data() | ||
self.assertEqual( | ||
appsec_data['triggers'][0]['rule_matches'][0]['parameters'][0] | ||
['value'], '<Redacted>') | ||
appsec_data["triggers"][0]["rule_matches"][0]["parameters"][0] | ||
["value"], | ||
"<Redacted>", | ||
) | ||
|
||
def test_no_obfuscation(self): | ||
waf_path = Path(__file__).parent / './conf/waf.json' | ||
waf_path = Path(__file__).parent / "./conf/waf.json" | ||
waf_text = waf_path.read_text() | ||
self.orch.nginx_replace_file('/tmp/waf.json', waf_text) | ||
self.orch.nginx_replace_file("/tmp/waf.json", waf_text) | ||
|
||
self.apply_config('no_obfuscation') | ||
self.apply_config("no_obfuscation") | ||
|
||
self.orch.sync_service('agent') | ||
self.orch.sync_service("agent") | ||
|
||
# No redaction by key | ||
status, _, _ = self.orch.send_nginx_http_request( | ||
'/http/?password=matched+value', 80) | ||
"/http/?password=matched+value", 80) | ||
appsec_data = self.get_appsec_data() | ||
self.assertEqual( | ||
appsec_data['triggers'][0]['rule_matches'][0]['parameters'][0] | ||
['value'], 'matched value') | ||
appsec_data["triggers"][0]["rule_matches"][0]["parameters"][0] | ||
["value"], | ||
"matched value", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
old_worker_pids
is a set, and then treated as_worker_pid
the code is doingset not in another_set
which is always true, or am I getting something wrong?The old code was looping while the intersection of the old pids and the current pids was non-empty, continuing once empty, which seems to be correct when expecting pids to stop, but was not really checking for new processes to spawn 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I know David for writing high quality code, I knew I missed something, thank you for taking the time to investigate and understand the prior code.
I intended
element not in another set
but as you mentionedold_worker_pids
is a set. It's working becausewait_until
stop waiting when the predicate isTrue
which is always the case here. 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 1ce8ad9