Skip to content

Commit

Permalink
Take into consideration appsec envs on the tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
estringana committed Mar 7, 2024
1 parent 7f6fc56 commit da51431
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 147 deletions.
4 changes: 4 additions & 0 deletions ext/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ static void dd_ini_env_to_ini_name(const zai_str env_name, zai_config_name *ini_
memcpy(ini_name->ptr, "datadog.", sizeof("datadog.") - 1);
ini_name->len = env_name.len + DD_TO_DATADOG_INC;

if (env_name.ptr == strstr(env_name.ptr, "DD_APPSEC_")) {
ini_name->ptr[sizeof("datadog.appsec") - 1] = '.';
}

if (env_name.ptr == strstr(env_name.ptr, "DD_TRACE_")) {
ini_name->ptr[sizeof("datadog.trace") - 1] = '.';
}
Expand Down
54 changes: 5 additions & 49 deletions tests/ext/appsec/sca_flag_is_sent_01.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,13 @@ if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing s
DD_TRACE_GENERATE_ROOT_SPAN=0
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
--INI--
datadog.trace.agent_url="file://{PWD}/simple-telemetry.out"
--FILE--
<?php

DDTrace\start_span();
$root = DDTrace\root_span();

$root->service = 'simple-telemetry-app';
$root->meta['env'] = 'test-env';

DDTrace\close_span();

// At this stage, the service and env are stored to be used by telemetry
dd_trace_serialize_closed_spans();

dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/simple-telemetry.out')) {
$batches = [];
foreach (file(__DIR__ . '/simple-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
array_push($batches, ...($json["request_type"] == "message-batch" ? $json["payload"] : [$json]));
}
}
$found = array_filter($batches, function ($json) {
if ($json["request_type"] !== "app-started") {
return false;
}
foreach($json["payload"]["configuration"] as $configuration) {
if ($configuration["name"] == "appsec_sca_enabled") {
var_dump($configuration);
return true;
}
}
return false;
});
if (count($found) == 1) {
var_dump("Sent");
break;
}
}
}

?>
datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_01-telemetry.out"
--FILE_EXTERNAL--
sca_test.inc
--EXPECT--
array(3) {
["name"]=>
string(18) "appsec_sca_enabled"
string(18) "appsec.sca_enabled"
["value"]=>
string(5) "false"
["origin"]=>
Expand All @@ -71,4 +27,4 @@ string(4) "Sent"
--CLEAN--
<?php

@unlink(__DIR__ . '/simple-telemetry.out');
@unlink(__DIR__ . '/sca_flag_is_sent_01-telemetry.out');
54 changes: 5 additions & 49 deletions tests/ext/appsec/sca_flag_is_sent_02.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,13 @@ DD_TRACE_GENERATE_ROOT_SPAN=0
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
DD_APPSEC_SCA_ENABLED=true
--INI--
datadog.trace.agent_url="file://{PWD}/simple-telemetry.out"
--FILE--
<?php

DDTrace\start_span();
$root = DDTrace\root_span();

$root->service = 'simple-telemetry-app';
$root->meta['env'] = 'test-env';

DDTrace\close_span();

// At this stage, the service and env are stored to be used by telemetry
dd_trace_serialize_closed_spans();

dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/simple-telemetry.out')) {
$batches = [];
foreach (file(__DIR__ . '/simple-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
array_push($batches, ...($json["request_type"] == "message-batch" ? $json["payload"] : [$json]));
}
}
$found = array_filter($batches, function ($json) {
if ($json["request_type"] !== "app-started") {
return false;
}
foreach($json["payload"]["configuration"] as $configuration) {
if ($configuration["name"] == "appsec_sca_enabled") {
var_dump($configuration);
return true;
}
}
return false;
});
if (count($found) == 1) {
var_dump("Sent");
break;
}
}
}

?>
datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_02-telemetry.out"
--FILE_EXTERNAL--
sca_test.inc
--EXPECT--
array(3) {
["name"]=>
string(18) "appsec_sca_enabled"
string(18) "appsec.sca_enabled"
["value"]=>
string(4) "true"
["origin"]=>
Expand All @@ -72,4 +28,4 @@ string(4) "Sent"
--CLEAN--
<?php

@unlink(__DIR__ . '/simple-telemetry.out');
@unlink(__DIR__ . '/sca_flag_is_sent_02-telemetry.out');
54 changes: 5 additions & 49 deletions tests/ext/appsec/sca_flag_is_sent_03.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,13 @@ DD_TRACE_GENERATE_ROOT_SPAN=0
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
DD_APPSEC_SCA_ENABLED=false
--INI--
datadog.trace.agent_url="file://{PWD}/simple-telemetry.out"
--FILE--
<?php

DDTrace\start_span();
$root = DDTrace\root_span();

$root->service = 'simple-telemetry-app';
$root->meta['env'] = 'test-env';

DDTrace\close_span();

// At this stage, the service and env are stored to be used by telemetry
dd_trace_serialize_closed_spans();

dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/simple-telemetry.out')) {
$batches = [];
foreach (file(__DIR__ . '/simple-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
array_push($batches, ...($json["request_type"] == "message-batch" ? $json["payload"] : [$json]));
}
}
$found = array_filter($batches, function ($json) {
if ($json["request_type"] !== "app-started") {
return false;
}
foreach($json["payload"]["configuration"] as $configuration) {
if ($configuration["name"] == "appsec_sca_enabled") {
var_dump($configuration);
return true;
}
}
return false;
});
if (count($found) == 1) {
var_dump("Sent");
break;
}
}
}

?>
datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_03-telemetry.out"
--FILE_EXTERNAL--
sca_test.inc
--EXPECT--
array(3) {
["name"]=>
string(18) "appsec_sca_enabled"
string(18) "appsec.sca_enabled"
["value"]=>
string(5) "false"
["origin"]=>
Expand All @@ -72,4 +28,4 @@ string(4) "Sent"
--CLEAN--
<?php

@unlink(__DIR__ . '/simple-telemetry.out');
@unlink(__DIR__ . '/sca_flag_is_sent_03-telemetry.out');
31 changes: 31 additions & 0 deletions tests/ext/appsec/sca_flag_is_sent_04.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
DD_APPSEC_SCA_ENABLED is set by INI
--DESCRIPTION--
This configuration is used by the backend to display/charge customers
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing sensitive test - valgrind is too slow');
?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
--INI--
datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_04-telemetry.out"
datadog.appsec.sca_enabled=1
--FILE_EXTERNAL--
sca_test.inc
--EXPECT--
array(3) {
["name"]=>
string(18) "appsec.sca_enabled"
["value"]=>
string(1) "1"
["origin"]=>
string(6) "EnvVar"
}
string(4) "Sent"
--CLEAN--
<?php

@unlink(__DIR__ . '/sca_flag_is_sent_04-telemetry.out');
31 changes: 31 additions & 0 deletions tests/ext/appsec/sca_flag_is_sent_05.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
DD_APPSEC_SCA_ENABLED is set by INI
--DESCRIPTION--
This configuration is used by the backend to display/charge customers
--SKIPIF--
<?php
if (getenv('PHP_PEAR_RUNTESTS') === '1') die("skip: pecl run-tests does not support {PWD}");
if (getenv('USE_ZEND_ALLOC') === '0' && !getenv("SKIP_ASAN")) die('skip timing sensitive test - valgrind is too slow');
?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
DD_INSTRUMENTATION_TELEMETRY_ENABLED=1
--INI--
datadog.trace.agent_url="file://{PWD}/sca_flag_is_sent_05-telemetry.out"
datadog.appsec.sca_enabled=0
--FILE_EXTERNAL--
sca_test.inc
--EXPECT--
array(3) {
["name"]=>
string(18) "appsec.sca_enabled"
["value"]=>
string(1) "0"
["origin"]=>
string(6) "EnvVar"
}
string(4) "Sent"
--CLEAN--
<?php

@unlink(__DIR__ . '/sca_flag_is_sent_05-telemetry.out');
46 changes: 46 additions & 0 deletions tests/ext/appsec/sca_test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
DDTrace\start_span();
$root = DDTrace\root_span();

$id = basename(__FILE__, ".php");

$root->service = $id.'-telemetry-app';
$root->meta['env'] = 'test-env';

DDTrace\close_span();

// At this stage, the service and env are stored to be used by telemetry
dd_trace_serialize_closed_spans();

dd_trace_internal_fn("finalize_telemetry");

for ($i = 0; $i < 100; ++$i) {
usleep(100000);
if (file_exists(__DIR__ . '/'.$id.'-telemetry.out')) {
$batches = [];
foreach (file(__DIR__ . '/'.$id.'-telemetry.out') as $l) {
if ($l) {
$json = json_decode($l, true);
array_push($batches, ...($json["request_type"] == "message-batch" ? $json["payload"] : [$json]));
}
}
$found = array_filter($batches, function ($json) {
if ($json["request_type"] !== "app-started") {
return false;
}
foreach($json["payload"]["configuration"] as $configuration) {
if (str_contains($configuration["name"], "sca_enabled")) {
var_dump($configuration);
return true;
}
}
return false;
});
if (count($found) == 1) {
var_dump("Sent");
break;
}
}
}

?>

0 comments on commit da51431

Please sign in to comment.