Skip to content

Commit bc35940

Browse files
committed
expose app metadata for csec via global symbol
Rather than providing a PHP user function to expose app metadata to PHP userland, export a global symbol for cseg agent to use.
1 parent c2f0349 commit bc35940

File tree

5 files changed

+64
-55
lines changed

5 files changed

+64
-55
lines changed

agent/csec_metadata.c

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
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-
}
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
145

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) {
6+
#include "csec_metadata.h"
257

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;
8+
#include "util_memory.h"
319

32-
array_init(return_value);
10+
#include "nr_axiom.h"
11+
#include "nr_agent.h"
12+
#include "nr_app.h"
13+
#include "php_includes.h"
14+
#include "php_compat.h"
15+
#include "php_newrelic.h"
3316

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);
17+
int nr_php_csec_get_metadata(nr_php_csec_metadata_t* csec_metadata) {
18+
if (NULL == csec_metadata) {
19+
return -1;
20+
}
3921

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);
22+
if (NULL == NRPRG(app)) {
23+
return -2;
4624
}
4725

26+
csec_metadata->high_security = NRPRG(app)->info.high_security;
27+
csec_metadata->entity_name = nr_strdup(nr_app_get_entity_name(NRPRG(app)));
28+
csec_metadata->entity_type = nr_strdup(nr_app_get_entity_type(NRPRG(app)));
29+
csec_metadata->entity_guid = nr_strdup(nr_app_get_entity_guid(NRPRG(app)));
30+
csec_metadata->host_name = nr_strdup(nr_app_get_host_name(NRPRG(app)));
31+
csec_metadata->agent_run_id = nr_strdup(NRPRG(app)->agent_run_id);
32+
csec_metadata->account_id = nr_strdup(NRPRG(app)->account_id);
33+
csec_metadata->license = nr_strdup(NRPRG(license).value);
34+
csec_metadata->plicense = nr_strdup(NRPRG(app)->plicense);
35+
36+
return 0;
4837
}

agent/csec_metadata.h

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
#include "php_agent.h"
2-
#include "util_hashmap.h"
1+
/*
2+
* Copyright 2020 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
35

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"
6+
#ifndef CSEC_METADATA_H
7+
#define CSEC_METADATA_H
8+
9+
typedef struct _nr_php_csec_metadata_t {
10+
int high_security; /* Indicates if high security been set locally for this
11+
application */
12+
char* license; /* License key provided */
13+
char* plicense; /* Printable license (abbreviated for security) */
14+
char* host_name; /* Local host name reported to the daemon */
15+
char* entity_name; /* Entity name related to this application */
16+
char* entity_type; /* Entity type */
17+
char* account_id; /* Security : Added for getting account id */
18+
char* entity_guid; /* Entity guid related to this application */
19+
char* agent_run_id; /* The collector's agent run ID; assigned from the
20+
New Relic backend */
21+
} nr_php_csec_metadata_t;
22+
23+
/*
24+
* Purpose : Return app meta data by populating nr_php_csec_metadata_t
25+
* structure. The caller is responsible for freeing the memory
26+
* allocated for the strings in the structure.
27+
*
28+
* Params : Pointer to a nr_php_csec_metadata_t structure
29+
*
30+
* Returns : 0 for success
31+
* -1 for invalid input
32+
* -2 for invalid internal state
33+
*/
34+
extern int nr_php_csec_get_metadata(nr_php_csec_metadata_t*);
35+
#endif

agent/export.syms

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
get_module
2+
nr_php_csec_get_metadata

agent/php_api_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
extern PHP_FUNCTION(newrelic_get_request_metadata);
1818

19-
extern PHP_FUNCTION(newrelic_get_security_metadata);
20-
2119
#ifdef ENABLE_TESTING_API
2220

2321
/*

agent/php_newrelic.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,9 @@ static zend_function_entry newrelic_functions[] = {
343343
#ifdef PHP8
344344
PHP_FE(newrelic_get_linking_metadata, newrelic_arginfo_void)
345345
PHP_FE(newrelic_get_trace_metadata, newrelic_arginfo_void)
346-
PHP_FE(newrelic_get_security_metadata, newrelic_arginfo_void)
347346
#else
348347
PHP_FE(newrelic_get_linking_metadata, 0)
349348
PHP_FE(newrelic_get_trace_metadata, 0)
350-
PHP_FE(newrelic_get_security_metadata, 0)
351349
#endif /* PHP 8 */
352350
/*
353351
* Integration test helpers

0 commit comments

Comments
 (0)