Skip to content

Commit a88db4e

Browse files
committed
add support for csec php agent
1 parent 9409457 commit a88db4e

11 files changed

+155
-1
lines changed

agent/config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ if test "$PHP_NEWRELIC" = "yes"; then
220220
php_pdo_mysql.c php_pdo_pgsql.c php_pgsql.c php_psr7.c php_redis.c \
221221
php_rinit.c php_rshutdown.c php_samplers.c php_stack.c \
222222
php_stacked_segment.c php_txn.c php_user_instrument.c \
223-
php_user_instrument_hashmap.c php_vm.c php_wrapper.c"
223+
php_user_instrument_hashmap.c php_vm.c php_wrapper.c csec_metadata.c"
224224
FRAMEWORKS="fw_cakephp.c fw_codeigniter.c fw_drupal8.c \
225225
fw_drupal.c fw_drupal_common.c fw_joomla.c fw_kohana.c \
226226
fw_laminas3.c fw_laravel.c fw_laravel_queue.c fw_lumen.c \

agent/csec_metadata.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "csec_metadata.h"
2+
#include "util_strings.h"
3+
#include "php_hash.h"
4+
#include "php_api_internal.h"
5+
6+
static void nr_csec_php_add_assoc_string_const(zval* arr,
7+
const char* key,
8+
const char* value) {
9+
char* val = NULL;
10+
11+
if (NULL == arr || NULL == key || NULL == value) {
12+
return;
13+
}
14+
15+
val = nr_strdup(value);
16+
nr_php_add_assoc_string(arr, key, val);
17+
nr_free(val);
18+
}
19+
20+
#ifdef TAGS
21+
void zif_newrelic_get_security_metadata(void); /* ctags landing pad only */
22+
void newrelic_get_security_metadata(void); /* ctags landing pad only */
23+
#endif
24+
PHP_FUNCTION(newrelic_get_security_metadata) {
25+
26+
NR_UNUSED_RETURN_VALUE;
27+
NR_UNUSED_RETURN_VALUE_PTR;
28+
NR_UNUSED_RETURN_VALUE_USED;
29+
NR_UNUSED_THIS_PTR;
30+
NR_UNUSED_EXECUTE_DATA;
31+
32+
array_init(return_value);
33+
34+
nr_csec_php_add_assoc_string_const(return_value, KEY_ENTITY_NAME, nr_app_get_entity_name(NRPRG(app)));
35+
nr_csec_php_add_assoc_string_const(return_value, KEY_ENTITY_TYPE, nr_app_get_entity_type(NRPRG(app)));
36+
nr_csec_php_add_assoc_string_const(return_value, KEY_ENTITY_GUID, nr_app_get_entity_guid(NRPRG(app)));
37+
nr_csec_php_add_assoc_string_const(return_value, KEY_HOSTNAME, nr_app_get_host_name(NRPRG(app)));
38+
nr_csec_php_add_assoc_string_const(return_value, KEY_LICENSE, NRPRG(license).value);
39+
40+
if (NRPRG(app)) {
41+
nr_csec_php_add_assoc_string_const(return_value, KEY_AGENT_RUN_ID, NRPRG(app)->agent_run_id);
42+
nr_csec_php_add_assoc_string_const(return_value, KEY_ACCOUNT_ID, NRPRG(app)->account_id);
43+
nr_csec_php_add_assoc_string_const(return_value, KEY_PLICENSE, NRPRG(app)->plicense);
44+
int high_security = NRPRG(app)->info.high_security;
45+
add_assoc_long(return_value, KEY_HIGH_SECURITY, (long)high_security);
46+
}
47+
48+
}

agent/csec_metadata.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "php_agent.h"
2+
#include "util_hashmap.h"
3+
4+
#define KEY_ENTITY_NAME "entity.name"
5+
#define KEY_ENTITY_TYPE "entity.type"
6+
#define KEY_ENTITY_GUID "entity.guid"
7+
#define KEY_HOSTNAME "hostname"
8+
#define KEY_AGENT_RUN_ID "agent.run.id"
9+
#define KEY_ACCOUNT_ID "account.id"
10+
#define KEY_LICENSE "license"
11+
#define KEY_PLICENSE "plicense"
12+
#define KEY_HIGH_SECURITY "high_security"

agent/php_api_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
extern PHP_FUNCTION(newrelic_get_request_metadata);
1818

19+
extern PHP_FUNCTION(newrelic_get_security_metadata);
20+
1921
#ifdef ENABLE_TESTING_API
2022

2123
/*

agent/php_globals.h

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ typedef struct _nrphpglobals_t {
6666
int instrument_internal; /* newrelic.transaction_tracer.internal_functions_enabled
6767
*/
6868
int high_security; /* newrelic.high_security */
69+
bool nr_security_agent_enabled; /* newrelic.security.agent.enabled */
70+
bool nr_security_enabled; /* newrelic.security.enabled */
6971

7072
int apache_major; /* Apache major version */
7173
int apache_minor; /* Apache minor version */

agent/php_minit.c

+7
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ PHP_MINIT_FUNCTION(newrelic) {
719719
nr_wordpress_minit();
720720
nr_php_set_opcode_handlers();
721721

722+
if (!NR_PHP_PROCESS_GLOBALS(nr_security_agent_enabled) || !NR_PHP_PROCESS_GLOBALS(nr_security_enabled) || NR_PHP_PROCESS_GLOBALS(high_security)) {
723+
nrl_info(NRL_INIT, "New Relic Security is completely disabled by one of the user provided config `newrelic.security.enabled`, `newrelic.security.agent.enabled` or `newrelic.high_security`. Not loading security capabilities.");
724+
nrl_debug(NRL_INIT, "newrelic.security.agent.enabled : %s", NR_PHP_PROCESS_GLOBALS(nr_security_enabled) ? "true" : "false");
725+
nrl_debug(NRL_INIT, "newrelic.security.enabled : %s", NR_PHP_PROCESS_GLOBALS(nr_security_agent_enabled) ? "true" : "false");
726+
nrl_debug(NRL_INIT, "newrelic.high_security : %s", NR_PHP_PROCESS_GLOBALS(high_security) ? "true" : "false");
727+
}
728+
722729
nrl_debug(NRL_INIT, "MINIT processing done");
723730
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO /* PHP 7.4+ */
724731
NR_PHP_PROCESS_GLOBALS(zend_offset) = zend_get_resource_handle(dummy);

agent/php_newrelic.c

+2
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,11 @@ static zend_function_entry newrelic_functions[] = {
342342
#ifdef PHP8
343343
PHP_FE(newrelic_get_linking_metadata, newrelic_arginfo_void)
344344
PHP_FE(newrelic_get_trace_metadata, newrelic_arginfo_void)
345+
PHP_FE(newrelic_get_security_metadata, newrelic_arginfo_void)
345346
#else
346347
PHP_FE(newrelic_get_linking_metadata, 0)
347348
PHP_FE(newrelic_get_trace_metadata, 0)
349+
PHP_FE(newrelic_get_security_metadata, 0)
348350
#endif /* PHP 8 */
349351
/*
350352
* Integration test helpers

agent/php_nrini.c

+64
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,58 @@ static PHP_INI_MH(nr_high_security_mh) {
515515
return SUCCESS;
516516
}
517517

518+
static PHP_INI_MH(nr_security_enabled_mh) {
519+
int val;
520+
521+
(void)entry;
522+
(void)NEW_VALUE_LEN;
523+
(void)mh_arg1;
524+
(void)mh_arg2;
525+
(void)mh_arg3;
526+
(void)stage;
527+
NR_UNUSED_TSRMLS;
528+
529+
val = nr_bool_from_str(NEW_VALUE);
530+
531+
if (-1 == val) {
532+
return FAILURE;
533+
}
534+
535+
if (val) {
536+
NR_PHP_PROCESS_GLOBALS(nr_security_enabled) = true;
537+
} else {
538+
NR_PHP_PROCESS_GLOBALS(nr_security_enabled) = false;
539+
}
540+
541+
return SUCCESS;
542+
}
543+
544+
static PHP_INI_MH(nr_security_agent_enabled_mh) {
545+
int val;
546+
547+
(void)entry;
548+
(void)NEW_VALUE_LEN;
549+
(void)mh_arg1;
550+
(void)mh_arg2;
551+
(void)mh_arg3;
552+
(void)stage;
553+
NR_UNUSED_TSRMLS;
554+
555+
val = nr_bool_from_str(NEW_VALUE);
556+
557+
if (-1 == val) {
558+
return FAILURE;
559+
}
560+
561+
if (val) {
562+
NR_PHP_PROCESS_GLOBALS(nr_security_agent_enabled) = true;
563+
} else {
564+
NR_PHP_PROCESS_GLOBALS(nr_security_agent_enabled) = false;
565+
}
566+
567+
return SUCCESS;
568+
}
569+
518570
static PHP_INI_MH(nr_preload_framework_library_detection_mh) {
519571
int val;
520572

@@ -2009,6 +2061,18 @@ PHP_INI_ENTRY_EX("newrelic.high_security",
20092061
nr_high_security_mh,
20102062
0)
20112063

2064+
PHP_INI_ENTRY_EX("newrelic.security.agent.enabled",
2065+
"0",
2066+
NR_PHP_SYSTEM,
2067+
nr_security_agent_enabled_mh,
2068+
0)
2069+
2070+
PHP_INI_ENTRY_EX("newrelic.security.enabled",
2071+
"0",
2072+
NR_PHP_SYSTEM,
2073+
nr_security_enabled_mh,
2074+
0)
2075+
20122076
/*
20132077
* Feature flag handling.
20142078
*/

axiom/cmd_appinfo_transmit.c

+15
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ nr_status_t nr_cmd_appinfo_process_reply(const uint8_t* data,
287287
int reply_len;
288288
const char* reply_json;
289289
const char* entity_guid;
290+
const char* account_id; /* Csec : Added for extracting account_id */
290291

291292
if ((NULL == data) || (0 == len)) {
292293
return NR_FAILURE;
@@ -384,6 +385,20 @@ nr_status_t nr_cmd_appinfo_process_reply(const uint8_t* data,
384385
app->entity_guid = NULL;
385386
}
386387

388+
/*
389+
* Csec : Added for extracting account_id
390+
*/
391+
nr_free(app->account_id);
392+
account_id = nro_get_hash_string(app->connect_reply, "account_id", NULL);
393+
if (NULL != account_id) {
394+
app->account_id = nr_strdup(account_id);
395+
} else {
396+
app->account_id = NULL;
397+
}
398+
/*
399+
* Csec : Added for extracting account_id
400+
*/
401+
387402
nrl_debug(NRL_ACCT, "APPINFO reply full app='%.*s' agent_run_id=%s",
388403
NRP_APPNAME(app->info.appname), app->agent_run_id);
389404

axiom/nr_app.c

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ void nr_app_destroy(nrapp_t** app_ptr) {
121121
nr_free(app->host_name);
122122
nr_free(app->entity_guid);
123123
nr_free(app->entity_name);
124+
nr_free(app->account_id);
124125
nr_rules_destroy(&app->url_rules);
125126
nr_rules_destroy(&app->txn_rules);
126127
nr_segment_terms_destroy(&app->segment_terms);

axiom/nr_app.h

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ typedef struct _nrapp_t {
105105
char* host_name; /* Local host name reported to the daemon */
106106
char* entity_name; /* Entity name related to this application */
107107
char* entity_guid; /* Entity guid related to this application */
108+
char* account_id; /* Security : Added for getting account id */
108109
time_t last_daemon_query; /* Used by agent: Last time we queried daemon about
109110
this app */
110111
int failed_daemon_query_count; /* Used by agent: Number of times daemon query

0 commit comments

Comments
 (0)