Skip to content
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
5c5cd42
feat(agent): retrieve info about installed packages
hahuja2 Aug 9, 2024
4d36457
feat(agent): ensure composer exists before retrieving info
hahuja2 Aug 9, 2024
ba9a11c
feat(agent): initial prototype
hahuja2 Aug 16, 2024
d220611
feat(agent): remove unnecessary code
hahuja2 Aug 16, 2024
7dc5fda
chore(agent): remove include directive
hahuja2 Aug 16, 2024
0a5dcb2
refactor: cleanup the code a bit
lavarou Sep 5, 2024
4402552
fix memory management issues
lavarou Sep 6, 2024
55b74b8
add basic autoloader detection tests
lavarou Sep 6, 2024
d058eb3
refactor: cleanup the code a lot
lavarou Sep 11, 2024
9aadc6b
make tests pass on PHPs >= 8.0
lavarou Sep 12, 2024
fab25b4
autoloader tests should not have any errors
lavarou Sep 12, 2024
e7f5ea8
enhance tests with mocked composer's runtime api
lavarou Sep 12, 2024
291930c
adjust list of magic files
lavarou Sep 12, 2024
cddb8dd
add copyright and description to autoloaders mocks
lavarou Sep 12, 2024
bd871c2
update doc strings in mock of composer's runtime API
lavarou Sep 12, 2024
07911e0
do something useful with all that package data
lavarou Sep 12, 2024
aae7ca4
add ini setting to control use of composer api
lavarou Sep 12, 2024
2ebd005
refactor some more
lavarou Sep 12, 2024
bbc453b
refactor some more
lavarou Sep 12, 2024
675fe7b
fix autoloader tests without composer
lavarou Sep 13, 2024
97e8332
fix memleak
lavarou Sep 13, 2024
40fde89
use 'pretty' version for package version
lavarou Sep 16, 2024
b867248
only report packages that have version set
lavarou Sep 16, 2024
a313703
improve integration tests
lavarou Sep 16, 2024
b814457
refactor: simplify interaction with Composer
lavarou Sep 16, 2024
de06513
feat: filter out root package
lavarou Sep 17, 2024
4cc07ca
fix: package info from composer wins [WIP]
lavarou Sep 17, 2024
d6964ee
fix: package info from composer wins [UNIT TESTS]
lavarou Sep 17, 2024
69377b1
fixup! feat: filter out root package
lavarou Sep 17, 2024
3fe0711
don't call composer when package detection is off
lavarou Sep 17, 2024
8e50756
refactor: rename ini for composer api use
lavarou Sep 17, 2024
c69efc6
fixup! improve integration tests
lavarou Sep 17, 2024
76334f2
feat(testing): Adds support for composer api package detection
mfulb Sep 16, 2024
b925fed
chore: Disables debugging output
mfulb Sep 17, 2024
06a7733
only legacy code creates package version metric
lavarou Sep 17, 2024
b2f8567
refactor package version metric creation
lavarou Sep 18, 2024
d270d36
fixup! refactor package version metric creation
lavarou Sep 18, 2024
ead97fa
cleanup code before code review
lavarou Sep 19, 2024
cb6f920
cleanup code before code review
lavarou Sep 19, 2024
724ee00
clang-format code before code review
lavarou Sep 19, 2024
54b657a
fix failing fw_support unit tests
lavarou Sep 19, 2024
be12b2f
chore(agent): Updates comment
mfulb Sep 19, 2024
7e0c996
feat(agent): Adds package supportability metric
mfulb Sep 19, 2024
b5ec0a7
fix failing fw_support integration tests
lavarou Sep 19, 2024
c013997
add autoloader tests with broken composer install
lavarou Sep 20, 2024
f9b42e4
add packages tests with broken composer install
lavarou Sep 20, 2024
c53299d
fix(agent): Adds mongodb package supportability metric
mfulb Sep 20, 2024
199adcb
fix(agent): Adds package major number metric for lumen
mfulb Sep 20, 2024
0ab342b
tests: ensure last composer version wins
lavarou Sep 24, 2024
d53ea28
tests: nr_hashmap_get->nr_php_packages_get_package
lavarou Sep 24, 2024
cdfe1e1
style: clang-format axiom/tests/test_php_packages.c
lavarou Sep 24, 2024
b4d8a8f
chore: fix spelling mistakes in comments
lavarou Sep 24, 2024
7fd363e
fix initialization of zend_result variable
lavarou Sep 24, 2024
80fdbdd
use const qualifier for const variable
lavarou Sep 24, 2024
fd3f639
fix memleak - don't leave `vendor_path` dangling
lavarou Sep 24, 2024
af1170a
format invalid installed packages for debug log
lavarou Sep 24, 2024
cb83165
add more assert like NULL checks
lavarou Sep 24, 2024
388c5d0
ensure pointer is not NULL before writing to it
lavarou Sep 24, 2024
7a5c773
cleanup composer mock
lavarou Sep 24, 2024
3a356ea
drop use of `NRSAFESTR` when accessing package_name and package_version
lavarou Sep 24, 2024
ab4f401
Revert "use const qualifier for const variable"
lavarou Sep 24, 2024
a73bbda
enhance tests with mocked composer's runtime api
lavarou Sep 24, 2024
f6b438a
enhance composer detection tests
lavarou Sep 24, 2024
5a73606
enhance tests with mocked composer's runtime api
lavarou Sep 24, 2024
51f6d43
Revert "drop use of `NRSAFESTR` when accessing ..."
lavarou Sep 25, 2024
1fda499
feat(agent): improve package major version metric creation (#965)
mfulb Sep 26, 2024
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
3 changes: 2 additions & 1 deletion agent/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ if test "$PHP_NEWRELIC" = "yes"; then
fw_zend2.c fw_zend.c"
LIBRARIES="lib_aws_sdk_php.c lib_monolog.c lib_doctrine2.c lib_guzzle3.c \
lib_guzzle4.c lib_guzzle6.c lib_guzzle_common.c \
lib_mongodb.c lib_phpunit.c lib_predis.c lib_zend_http.c"
lib_mongodb.c lib_phpunit.c lib_predis.c lib_zend_http.c \
lib_composer.c"
PHP_NEW_EXTENSION(newrelic, $FRAMEWORKS $LIBRARIES $NEWRELIC_AGENT, $ext_shared,, \\$(NEWRELIC_CFLAGS))

PHP_SUBST(NEWRELIC_CFLAGS)
Expand Down
13 changes: 13 additions & 0 deletions agent/fw_drupal.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "util_memory.h"
#include "util_strings.h"

#define PHP_PACKAGE_NAME "drupal/drupal"

/*
* Set the Web Transaction (WT) name to "(cached page)"
*
Expand Down Expand Up @@ -879,4 +881,15 @@ void nr_drupal_enable(TSRMLS_D) {
nr_php_user_function_add_declared_callback(
NR_PSTR("drupal_http_request"), nr_drupal_replace_http_request TSRMLS_CC);
#endif

if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
PHP_PACKAGE_VERSION_UNKNOWN);
}

nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, NULL,
nr_php_packages_get_package(NRPRG(txn)->php_packages,
PHP_PACKAGE_NAME));

}
7 changes: 4 additions & 3 deletions agent/fw_drupal8.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ NR_PHP_WRAPPER_END
void nr_drupal_version() {
zval* zval_version = NULL;
zend_class_entry* class_entry = NULL;
nr_php_package_t* p = NULL;

class_entry = nr_php_find_class("drupal");
if (NULL == class_entry) {
Expand All @@ -687,10 +688,10 @@ void nr_drupal_version() {
if (nr_php_is_zval_valid_string(zval_version)) {
char* version = Z_STRVAL_P(zval_version);
if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
p = nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
}
nr_fw_support_add_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
version);
nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, version, p);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (and similar changes in other fw_*.c modules) is required so that if composer api is enabled, the major version value, used in the metric name, will be from package info collected using composer api rather than from the package itself (from static VERSION constant or calling some version of get_version method in the package). The former method yields more useful and accurate results.

}

nr_php_zval_free(&zval_version);
Expand Down
1 change: 1 addition & 0 deletions agent/fw_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern void nr_phpunit_enable(TSRMLS_D);
extern void nr_predis_enable(TSRMLS_D);
extern void nr_zend_http_enable(TSRMLS_D);
extern void nr_monolog_enable(TSRMLS_D);
extern void nr_composer_handle_autoload(const char* filename);

/* Vulnerability Management Packages */
extern void nr_drupal_version(void);
Expand Down
8 changes: 7 additions & 1 deletion agent/fw_laminas3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "util_logging.h"
#include "util_memory.h"

#define PHP_PACKAGE_NAME "laminas/laminas-mvc"

/*
* Laminas is a rebranding of Zend, but the logic remains the same,
* it is simply a name change and corresponds directly to Zend 3.x.
Expand Down Expand Up @@ -163,7 +165,11 @@ void nr_laminas3_enable(TSRMLS_D) {
nr_laminas3_name_the_wt TSRMLS_CC);

if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), "laminas/laminas-mvc",
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
PHP_PACKAGE_VERSION_UNKNOWN);
}
nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, NULL,
nr_php_packages_get_package(NRPRG(txn)->php_packages,
PHP_PACKAGE_NAME));
}
5 changes: 3 additions & 2 deletions agent/fw_laravel.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ NR_PHP_WRAPPER(nr_laravel_application_construct) {
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
;
char* version = NULL;
nr_php_package_t* p = NULL;

NR_UNUSED_SPECIALFN;
(void)wraprec;
Expand All @@ -961,10 +962,10 @@ NR_PHP_WRAPPER(nr_laravel_application_construct) {

if (NRINI(vulnerability_management_package_detection_enabled)) {
// Add php package to transaction
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
p = nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
}
nr_fw_support_add_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
version);
version, p);

if (version) {
nrl_debug(NRL_FRAMEWORK, "Laravel version is " NRP_FMT, NRP_PHP(version));
Expand Down
10 changes: 9 additions & 1 deletion agent/fw_lumen.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
#include "php_wrapper.h"
#include "php_hash.h"
#include "fw_hooks.h"
#include "fw_support.h"
#include "util_logging.h"
#include "util_memory.h"
#include "util_strings.h"

#define PHP_PACKAGE_NAME "laravel/lumen-framework"

/*
* Sets the web transaction name. If strip_base == true,
* leading class path components will be stripped.
Expand Down Expand Up @@ -232,7 +235,12 @@ void nr_lumen_enable(TSRMLS_D) {
#endif

if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), "laravel/lumen-framework",
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
PHP_PACKAGE_VERSION_UNKNOWN);
}

nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, NULL,
nr_php_packages_get_package(NRPRG(txn)->php_packages,
PHP_PACKAGE_NAME));
}
5 changes: 3 additions & 2 deletions agent/fw_slim.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ NR_PHP_WRAPPER_END
NR_PHP_WRAPPER(nr_slim_application_construct) {
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS);
char* version = NULL;
nr_php_package_t* p = NULL;

NR_UNUSED_SPECIALFN;
(void)wraprec;
Expand All @@ -160,11 +161,11 @@ NR_PHP_WRAPPER(nr_slim_application_construct) {

if (NRINI(vulnerability_management_package_detection_enabled)) {
// Add php package to transaction
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
p = nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
}

nr_fw_support_add_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
version);
version, p);

nr_free(version);
nr_php_scope_release(&this_var);
Expand Down
26 changes: 21 additions & 5 deletions agent/fw_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,39 @@ void nr_fw_support_add_logging_supportability_metric(nrtxn_t* txn,
void nr_fw_support_add_package_supportability_metric(
nrtxn_t* txn,
const char* package_name,
const char* package_version) {
if (NULL == txn || NULL == package_name || NULL == package_version) {
const char* package_version,
nr_php_package_t* p) {
if (NULL == txn || NULL == package_name) {
return;
}

char* metname = NULL;
char major_version[MAJOR_VERSION_LENGTH] = {0};
const char* version = package_version;

// override provided package_version only if:
// - php_package is provided
// - its version is not NULL
// - its version is not PHP_PACKAGE_VERSION_UNKNOWN
if (NULL != p && NULL != p->package_version
&& 0 != nr_strcmp(p->package_version, PHP_PACKAGE_VERSION_UNKNOWN)) {
version = p->package_version;
}

// only generate metric if version is known
if (NULL == version || 0 == nr_strcmp(version, PHP_PACKAGE_VERSION_UNKNOWN)) {
return;
}

/* The below for loop checks if the major version of the package is more than
* one digit and keeps looping until a '.' is encountered or one of the
* conditions is met.
*/
for (int i = 0; package_version[i] && i < MAJOR_VERSION_LENGTH - 1; i++) {
if ('.' == package_version[i]) {
for (int i = 0; version[i] && i < MAJOR_VERSION_LENGTH - 1; i++) {
if ('.' == version[i]) {
break;
}
major_version[i] = package_version[i];
major_version[i] = version[i];
}

if (NR_FW_UNSET == NRINI(force_framework)) {
Expand Down
5 changes: 4 additions & 1 deletion agent/fw_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define FW_SUPPORT_HDR

#include "php_user_instrument.h"
#include "nr_php_packages.h"

extern void nr_php_framework_add_supportability_metric(
const char* framework_name,
Expand Down Expand Up @@ -44,11 +45,13 @@ extern void nr_fw_support_add_logging_supportability_metric(
* Params : 1. Transaction object
* 2. Package name
* 3. Package version
* 4. PHP package reported for vulnerability management
*
*/
extern void nr_fw_support_add_package_supportability_metric(
nrtxn_t* txn,
const char* package_name,
const char* package_version);
const char* package_version,
nr_php_package_t* p);

#endif /* FW_SUPPORT_HDR */
9 changes: 8 additions & 1 deletion agent/fw_symfony4.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "fw_support.h"
#include "fw_symfony_common.h"

#define PHP_PACKAGE_NAME "symfony/http-kernel"

NR_PHP_WRAPPER(nr_symfony4_exception) {
int priority = nr_php_error_get_priority(E_ERROR);
zval* event = NULL;
Expand Down Expand Up @@ -277,7 +279,12 @@ void nr_symfony4_enable(TSRMLS_D) {
#endif

if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), "symfony/http-kernel",
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
PHP_PACKAGE_VERSION_UNKNOWN);
}

nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, NULL,
nr_php_packages_get_package(NRPRG(txn)->php_packages,
PHP_PACKAGE_NAME));
}
7 changes: 4 additions & 3 deletions agent/fw_wordpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,15 +806,16 @@ void nr_wordpress_version() {
zval retval;
int result
= zend_eval_string(func_string, &retval, "Get Wordpress Version");
nr_php_package_t* p = NULL;
// Add php package to transaction
if (SUCCESS == result) {
if (nr_php_is_zval_valid_string(&retval)) {
char* version = Z_STRVAL(retval);
if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
p = nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
}
nr_fw_support_add_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
version);
nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, version, p);
}
zval_dtor(&retval);
}
Expand Down
11 changes: 11 additions & 0 deletions agent/fw_yii.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "util_memory.h"
#include "util_strings.h"

#define PHP_PACKAGE_NAME "yiisoft/yii2"
/*
* Yii1: Set the web transaction name from the controllerId + actionId combo.
*
Expand Down Expand Up @@ -221,4 +222,14 @@ void nr_yii2_enable(TSRMLS_D) {
nr_php_wrap_user_function(NR_PSTR("yii\\base\\ErrorHandler::logException"),
nr_yii2_error_handler_wrapper TSRMLS_CC);
#endif

if (NRINI(vulnerability_management_package_detection_enabled)) {
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME,
PHP_PACKAGE_VERSION_UNKNOWN);
}

nr_fw_support_add_package_supportability_metric(
NRPRG(txn), PHP_PACKAGE_NAME, NULL,
nr_php_packages_get_package(NRPRG(txn)->php_packages,
PHP_PACKAGE_NAME));
}
5 changes: 3 additions & 2 deletions agent/lib_aws_sdk_php.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void nr_lib_aws_sdk_php_handle_version() {
zval* zval_version = NULL;
zend_class_entry* class_entry = NULL;
char* version = NULL;
nr_php_package_t* p = NULL;

class_entry = nr_php_find_class("aws\\sdk");
if (NULL != class_entry) {
Expand All @@ -67,10 +68,10 @@ void nr_lib_aws_sdk_php_handle_version() {
}
if (NRINI(vulnerability_management_package_detection_enabled)) {
/* Add php package to transaction */
nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
p = nr_txn_add_php_package(NRPRG(txn), PHP_PACKAGE_NAME, version);
}
nr_fw_support_add_package_supportability_metric(NRPRG(txn), PHP_PACKAGE_NAME,
version);
version, p);
nr_php_zval_free(&zval_version);
}

Expand Down
Loading
Loading