Skip to content

Commit

Permalink
Merge pull request #1044 from criblio/feat/1025-psm-uuid
Browse files Browse the repository at this point in the history
1025 Add machine ID and UUID to process start message
  • Loading branch information
michalbiesek authored Jul 21, 2022
2 parents 2a692c2 + 7a08f1e commit 431fb7a
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/schemas/definitions/data.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@
"type": "string",
"enum": ["debug", "info", "warning", "error", "none"]
},
"machine_id" : {
"title": "machine_id",
"description": "A unique identifier for a machine.",
"type": "string",
},
"maxeventpersec": {
"title": "maxeventpersec",
"description": "Specifies a rate limit for events. See `scope.yml`.",
Expand Down Expand Up @@ -625,6 +630,11 @@
"type": "string",
"const": "thread"
},
"uuid" : {
"title": "uuid",
"description": "A unique identifier for a process.",
"type": "string",
},
"validateserver": {
"title": "validateserver",
"description": "Specifies whether to validate the TLS server certificate. See `scope.yml`.",
Expand Down
12 changes: 10 additions & 2 deletions docs/schemas/event_start_msg.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "object",
"title": "AppScope Start message",
"description": "Structure of the start message",
"examples": [{"format":"ndjson","info":{"process":{"libscopever":"v1.1.0","pid":35673,"ppid":3390,"gid":1000,"groupname":"test_user","uid":1000,"username":"test_user","hostname":"test_user","procname":"ls","cmd":"ls --color=auto","id":"test_user-ls-ls --color=auto"},"configuration":{"current":{"metric":{"enable":"true","transport":{"type":"udp","host":"127.0.0.1","port":"8125","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"statsd","statsdprefix":"","statsdmaxlen":512,"verbosity":4},"watch":[{"type":"fs"},{"type":"net"},{"type":"http"},{"type":"dns"},{"type":"process"},{"type":"statsd"}]},"libscope":{"log":{"level":"info","transport":{"type":"file","path":"/tmp/scope.log","buffering":"line"}},"configevent":"true","summaryperiod":10,"commanddir":"/tmp"},"event":{"enable":"true","transport":{"type":"tcp","host":"127.0.0.1","port":"9109","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"ndjson","maxeventpersec":10000,"enhancefs":"true"},"watch":[{"type":"file","name":"(\\/logs?\\/)|(\\.log$)|(\\.log[.\\d])","field":".*","value":".*"},{"type":"console","name":"(stdout)|(stderr)","field":".*","value":".*","allowbinary":"true"},{"type":"http","name":".*","field":".*","value":".*","headers":[]},{"type":"net","name":".*","field":".*","value":".*"},{"type":"fs","name":".*","field":".*","value":".*"},{"type":"dns","name":".*","field":".*","value":".*"}]},"payload":{"enable":"false","dir":"/tmp"},"tags":{},"protocol":[],"cribl":{"enable":"false","transport":{"type":"edge"},"authtoken":""}}},"environment":{}}}],
"examples": [{"format":"ndjson","info":{"process":{"libscopever":"v1.1.0","pid":35673,"ppid":3390,"gid":1000,"groupname":"test_user","uid":1000,"username":"test_user","hostname":"test_user","procname":"ls","cmd":"ls --color=auto","id":"test_user-ls-ls --color=auto","machine_id":"a1e2ada5a5b1b273b4b5c0c2c1c4f5d1","uuid":"da845a9b-a55d-4c42-893d-08b54ee6e999"},"configuration":{"current":{"metric":{"enable":"true","transport":{"type":"udp","host":"127.0.0.1","port":"8125","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"statsd","statsdprefix":"","statsdmaxlen":512,"verbosity":4},"watch":[{"type":"fs"},{"type":"net"},{"type":"http"},{"type":"dns"},{"type":"process"},{"type":"statsd"}]},"libscope":{"log":{"level":"info","transport":{"type":"file","path":"/tmp/scope.log","buffering":"line"}},"configevent":"true","summaryperiod":10,"commanddir":"/tmp"},"event":{"enable":"true","transport":{"type":"tcp","host":"127.0.0.1","port":"9109","tls":{"enable":"false","validateserver":"true","cacertpath":""}},"format":{"type":"ndjson","maxeventpersec":10000,"enhancefs":"true"},"watch":[{"type":"file","name":"(\\/logs?\\/)|(\\.log$)|(\\.log[.\\d])","field":".*","value":".*"},{"type":"console","name":"(stdout)|(stderr)","field":".*","value":".*","allowbinary":"true"},{"type":"http","name":".*","field":".*","value":".*","headers":[]},{"type":"net","name":".*","field":".*","value":".*"},{"type":"fs","name":".*","field":".*","value":".*"},{"type":"dns","name":".*","field":".*","value":".*"}]},"payload":{"enable":"false","dir":"/tmp"},"tags":{},"protocol":[],"cribl":{"enable":"false","transport":{"type":"edge"},"authtoken":""}}},"environment":{}}}],
"required": [
"format",
"info"
Expand Down Expand Up @@ -44,7 +44,9 @@
"hostname",
"procname",
"cmd",
"id"
"id",
"machine_id",
"uuid"
],
"properties": {
"libscopever": {
Expand Down Expand Up @@ -79,6 +81,12 @@
},
"id": {
"$ref": "definitions/data.schema.json#/$defs/id"
},
"machine_id": {
"$ref": "definitions/data.schema.json#/$defs/machine_id"
},
"uuid": {
"$ref": "definitions/data.schema.json#/$defs/uuid"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions os/linux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ coretest: $(C_FILES) $(YAML_AR) $(JSON_AR) $(TEST_LIB)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/cfgtest cfgtest.o cfg.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/transporttest transporttest.o transport.o scopestdlib.o dbg.o log.o fn.o utils.o os.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/logtest logtest.o log.o transport.o scopestdlib.o dbg.o fn.o utils.o os.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/utilstest utilstest.o scopestdlib.o dbg.o fn.o utils.o os.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/mtctest mtctest.o mtc.o log.o transport.o mtcformat.o strset.o com.o ctl.o evtformat.o cfg.o cfgutils.o scopestdlib.o dbg.o circbuf.o linklist.o fn.o utils.o os.o test.o report.o search.o httpagg.o state.o httpstate.o metriccapture.o plattime.o $(TEST_AR) $(TEST_LD_FLAGS) -Wl,--wrap=cfgLogStreamEnable
$(CC) $(TEST_CFLAGS) -o test/$(OS)/evtformattest evtformattest.o evtformat.o log.o transport.o mtcformat.o strset.o scopestdlib.o dbg.o cfg.o com.o ctl.o mtc.o circbuf.o cfgutils.o linklist.o fn.o utils.o os.o test.o report.o search.o httpagg.o state.o httpstate.o metriccapture.o plattime.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/ctltest ctltest.o ctl.o log.o transport.o scopestdlib.o dbg.o cfgutils.o cfg.o com.o mtc.o evtformat.o mtcformat.o strset.o circbuf.o linklist.o fn.o utils.o os.o test.o report.o search.o httpagg.o state.o httpstate.o metriccapture.o plattime.o $(TEST_AR) $(TEST_LD_FLAGS) -Wl,--wrap=cbufGet
Expand Down
5 changes: 3 additions & 2 deletions src/com.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ jsonProcessObject(proc_id_t *proc)
if (!(cJSON_AddStringToObjLN(root, "cmd", proc->cmd))) goto err;
}
if (!(cJSON_AddStringToObjLN(root, "id", proc->id))) goto err;
// starttime

if (!(cJSON_AddStringToObjLN(root, "machine_id", proc->machine_id))) goto err;
if (!(cJSON_AddStringToObjLN(root, "uuid", proc->uuid))) goto err;

return root;
err:
if (root) cJSON_Delete(root);
Expand Down
15 changes: 15 additions & 0 deletions src/scopestdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ extern void* scopelibc_shmat(int, const void *, int);
extern int scopelibc_shmdt(const void *);
extern int scopelibc_shmget(key_t, size_t, int);
extern int scopelibc_sched_getcpu(void);
extern int scopelibc_rand(void);
extern void scopelibc_srand(unsigned int);

static int g_go_static;

Expand Down Expand Up @@ -1156,3 +1158,16 @@ scope___ctype_tolower_loc(void)
{
return scopelibc___ctype_tolower_loc();
}

int
scope_rand(void)
{
return scopelibc_rand();
}

void
scope_srand(unsigned int seed)
{
scopelibc_srand(seed);
}

3 changes: 3 additions & 0 deletions src/scopestdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@ void* scope_shmat(int, const void *, int);
int scope_shmdt(const void *);
int scope_shmget(key_t, size_t, int);
int scope_sched_getcpu(void);
int scope_rand(void);
void scope_srand(unsigned int);


#endif // __SCOPE_STDLIB_H__
4 changes: 4 additions & 0 deletions src/scopetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ typedef enum {CFG_MTC_FS,

#define MAX_HOSTNAME 255
#define MAX_PROCNAME 128
#define MACHINE_ID_LEN 32
#define UUID_LEN 36
#define DEFAULT_CMD_SIZE 32
#define MAX_ID 512
#define MAX_CGROUP 512
Expand All @@ -55,6 +57,8 @@ typedef struct
char cgroup[MAX_CGROUP];
char *username;
char *groupname;
char machine_id[MACHINE_ID_LEN + 1];
char uuid[UUID_LEN + 1];
} proc_id_t;

#define TRUE 1
Expand Down
155 changes: 155 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "scopestdlib.h"
Expand All @@ -10,6 +11,12 @@
#include "dbg.h"
#include "runtimecfg.h"

#define MAC_ADDR_LEN 17
#define ZERO_MACHINE_ID "00000000000000000000000000000000"

static int createMachineID(char *string);
static int getMacAddr(char *string);

rtconfig g_cfg = {0};

unsigned int
Expand Down Expand Up @@ -203,3 +210,151 @@ sigSafeNanosleep(const struct timespec *req)

return rv;
}

// Generate a UUID v4 string using a random generator
void
setUUID(char *string)
{
if (string == NULL) {
scopeLogError("ERROR: setUUIDv4: Null string");
return;
}

unsigned char key[16];
static bool seeded = FALSE;

if (!seeded) {
scope_srand((unsigned int)scope_time(NULL));
seeded = TRUE;
}

for (int i = 0; i < 16; i++) {
key[i] = (unsigned char)scope_rand() % 255;
}

key[6] = 0x40 | (key[6] & 0xf); // Set version to 4
key[8] = 0x80 | (key[8] & 0x3f); // Set variant to 8

scope_snprintf(string, UUID_LEN + 1,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
key[0], key[1], key[2], key[3],
key[4], key[5], key[6], key[7],
key[8], key[9], key[10], key[11],
key[12], key[13], key[14], key[15]);
}

// Get the Machine ID, or if not available, create one
void
setMachineID(char *string)
{
if (string == NULL) {
scopeLogError("ERROR: setMachineID: Null string");
return;
}
scope_strncpy(string, ZERO_MACHINE_ID, MACHINE_ID_LEN + 1);

char buf[MACHINE_ID_LEN + 1] = {0};
FILE *fp;

// Try to get a machine id from /etc
if ((fp = scope_fopen("/etc/machine-id", "r")) != NULL) {
if (scope_fgets(buf, sizeof(buf), fp) == NULL) {
scopeLogInfo("INFO: setMachineID: Could not read Machine ID from file /etc/machine-id");
}
scope_fclose(fp);
}

if (scope_strlen(buf) != MACHINE_ID_LEN) {
scopeLogInfo("INFO: setMachineID: Machine ID not found or unexpected length. Creating one.");
if (createMachineID(buf)) {
scopeLogError("ERROR: setMachineID: Error creating Machine ID");
return;
}
}

scope_strncpy(string, buf, MACHINE_ID_LEN + 1);
}

// Create a Machine ID from a mac address
static int
createMachineID(char *string)
{
if (string == NULL) return 1;

char mac_addr[MAC_ADDR_LEN];
if (getMacAddr(mac_addr)) {
scopeLogError("ERROR: createMachineID: getMacAddr");
return 1;
}

scope_snprintf(string, MACHINE_ID_LEN + 1,
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3],
mac_addr[4], mac_addr[5], mac_addr[6], mac_addr[7],
mac_addr[8], mac_addr[9], mac_addr[10], mac_addr[11],
mac_addr[12], mac_addr[13], mac_addr[14], mac_addr[15]);
return 0;
}

// Get the machine's physical MAC address
static int
getMacAddr(char *string)
{
DIR *d;
struct dirent *dir;
struct stat buf;
char mac_buf[MAC_ADDR_LEN];
char dir_path[256];
char link_path[256];
char addr_path[256];
bool found = FALSE;

d = scope_opendir("/sys/class/net/");
if (!d) return 1;

// Check if interface eth exists
// Otherwise find an interface that does not contain "virtual" in the soft link
while ((dir = scope_readdir(d)) != NULL) {
scope_sprintf(dir_path, "/sys/class/net/%s", dir->d_name);

if (scope_strstr(dir->d_name, "eth") != 0) {
found = TRUE;
break;
}

if (scope_lstat(dir_path, &buf) != 0) {
break;
}
if (S_ISLNK(buf.st_mode)) {
(void)scope_readlink(dir_path, link_path, sizeof(link_path));
if (scope_strstr(link_path, "virtual") == NULL) {
found = TRUE;
break;
}
}
}
scope_closedir(d);

if (!found) {
scopeLogError("Error: getMacAddr: No physical interface found");
return 1;
}

scope_sprintf(addr_path, "%s/address", dir_path);

FILE *fp;
if ((fp = scope_fopen(addr_path, "r")) == NULL) {
scopeLogError("Error: getMacAddr: No address file found");
return 1;
}
if (scope_fgets(mac_buf, sizeof(mac_buf), fp) == NULL) {
scopeLogError("Error: getMacAddr: No address found in file");
scope_fclose(fp);
return 1;
}
scope_fclose(fp);

scope_strncpy(string, mac_buf, MAC_ADDR_LEN + 1);
return 0;
}

3 changes: 3 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ int endsWith(const char *string, const char *substring);

int sigSafeNanosleep(const struct timespec *req);

void setUUID(char *string);
void setMachineID(char *string);

#endif // __UTILS_H__
3 changes: 3 additions & 0 deletions src/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,9 @@ init(void)
setProcId(&g_proc);
setPidEnv(g_proc.pid);

setMachineID(g_proc.machine_id);
setUUID(g_proc.uuid);

// initEnv() will set this TRUE if it detects `scope_attach_PID.env` in
// `/dev/shm` with our PID indicating we were injected into a running
// process.
Expand Down
1 change: 1 addition & 0 deletions test/execute.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ run_test test/${OS}/cfgutilstest
run_test test/${OS}/cfgtest
run_test test/${OS}/transporttest
run_test test/${OS}/logtest
run_test test/${OS}/utilstest
run_test test/${OS}/mtctest
run_test test/${OS}/evtformattest
run_test test/${OS}/ctltest
Expand Down
Loading

0 comments on commit 431fb7a

Please sign in to comment.