Skip to content
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

Add schema extraction enabling flag #2441

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appsec/src/extension/commands/client_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static dd_result _pack_command(

double se_sample_rate = get_global_DD_API_SECURITY_REQUEST_SAMPLE_RATE();
if (se_sample_rate >= MIN_SE_SAMPLE_RATE) {
mpack_write_bool(w, true);
mpack_write_bool(w, get_global_DD_EXPERIMENTAL_API_SECURITY_ENABLED());

dd_mpack_write_lstr(w, "sample_rate");
mpack_write(w, se_sample_rate);
Expand Down
1 change: 1 addition & 0 deletions appsec/src/extension/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern bool runtime_config_first_init;
CONFIG(CUSTOM(STRING), DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING, "safe", .parser = dd_parse_automated_user_events_tracking) \
CONFIG(STRING, DD_APPSEC_HTTP_BLOCKED_TEMPLATE_HTML, "") \
CONFIG(STRING, DD_APPSEC_HTTP_BLOCKED_TEMPLATE_JSON, "") \
CONFIG(BOOL, DD_EXPERIMENTAL_API_SECURITY_ENABLED, "false") \
CONFIG(DOUBLE, DD_API_SECURITY_REQUEST_SAMPLE_RATE, "0.1")
// clang-format on

Expand Down
11 changes: 11 additions & 0 deletions appsec/tests/extension/inc/mock_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ function get_commands() {
}
}

function get_command($command) {
$commands = $this->get_commands();
foreach($commands as $c) {
if ($c[0] == $command) {
return $c;
}
}

return [];
}

function print_commands($sort = true) {
$commands = $this->get_commands();
if (!is_array($commands)) {
Expand Down
Binary file modified appsec/tests/extension/rinit_rshutdown_basic.phpt
Binary file not shown.
28 changes: 28 additions & 0 deletions appsec/tests/extension/schema_extraction_01.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Default schema extraction configurations
--FILE--
<?php
use function datadog\appsec\testing\{rinit,rshutdown,get_formatted_runtime_id};

include __DIR__ . '/inc/mock_helper.php';

$helper = Helper::createInitedRun([
response_list(response_request_init(['ok', []]))
]);

var_dump(rinit());
var_dump(rshutdown());

$clientInit = $helper->get_command('client_init');

var_dump($clientInit[1][5]['schema_extraction']);
?>
--EXPECTF--
bool(true)
bool(true)
array(2) {
["enabled"]=>
bool(false)
["sample_rate"]=>
float(0.1)
}
31 changes: 31 additions & 0 deletions appsec/tests/extension/schema_extraction_02.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Schema extraction configured
--ENV--
DD_EXPERIMENTAL_API_SECURITY_ENABLED=true
DD_API_SECURITY_REQUEST_SAMPLE_RATE=0.5
--FILE--
<?php
use function datadog\appsec\testing\{rinit,rshutdown,get_formatted_runtime_id};

include __DIR__ . '/inc/mock_helper.php';

$helper = Helper::createInitedRun([
response_list(response_request_init(['ok', []]))
]);

var_dump(rinit());
var_dump(rshutdown());

$clientInit = $helper->get_command('client_init');

var_dump($clientInit[1][5]['schema_extraction']);
?>
--EXPECTF--
bool(true)
bool(true)
array(2) {
["enabled"]=>
bool(true)
["sample_rate"]=>
float(0.5)
}
31 changes: 31 additions & 0 deletions appsec/tests/extension/schema_extraction_03.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Schema extraction is disabled when sample rate is 0
--ENV--
DD_EXPERIMENTAL_API_SECURITY_ENABLED=true
DD_API_SECURITY_REQUEST_SAMPLE_RATE=0
--FILE--
<?php
use function datadog\appsec\testing\{rinit,rshutdown,get_formatted_runtime_id};

include __DIR__ . '/inc/mock_helper.php';

$helper = Helper::createInitedRun([
response_list(response_request_init(['ok', []]))
]);

var_dump(rinit());
var_dump(rshutdown());

$clientInit = $helper->get_command('client_init');

var_dump($clientInit[1][5]['schema_extraction']);
?>
--EXPECTF--
bool(true)
bool(true)
array(2) {
["enabled"]=>
bool(false)
["sample_rate"]=>
float(0)
}
Loading