-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Additional file handling for randomly generated machine-ids #11185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
e81c47f
53141bf
8eae3c0
3ef7444
aed26a3
b08cc3b
7164855
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2043,13 +2043,39 @@ static int machine_id_read_and_sanitize(char *path, | |
| return 0; | ||
| } | ||
|
|
||
| int write_uuid_to_file(char* filename, char* uuid) { | ||
| int fd; | ||
| size_t uuid_len; | ||
|
|
||
| if (filename == NULL || uuid == NULL) { | ||
| return FLB_FALSE; | ||
| } | ||
|
|
||
| /* write uuid to file */ | ||
| fd = flb_open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); | ||
| if (fd == -1) { | ||
| return FLB_FALSE; | ||
| } | ||
|
|
||
| uuid_len = strlen(uuid); | ||
|
|
||
| if (flb_write(fd, uuid, uuid_len) != uuid_len) { | ||
| flb_close(fd); | ||
| return FLB_FALSE; | ||
| } | ||
|
|
||
| flb_close(fd); | ||
| return FLB_TRUE; | ||
| } | ||
|
|
||
| int flb_utils_get_machine_id(char **out_id, size_t *out_size) | ||
| { | ||
| int ret; | ||
| char *id; | ||
| size_t bytes; | ||
| char *uuid; | ||
| int fallback = FLB_FALSE; | ||
| char *fallback_id_file = "machine-id"; //should reside in current working directory | ||
|
|
||
| #ifdef __linux__ | ||
| char *dbus_var = "/var/lib/dbus/machine-id"; | ||
|
|
@@ -2084,6 +2110,24 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) | |
| return 0; | ||
| } | ||
| } | ||
|
|
||
| if (access(fallback_id_file, F_OK) == 0) | ||
| { | ||
| ret = machine_id_read_and_sanitize(fallback_id_file, &id, &bytes); | ||
| if(ret == 0) | ||
| { | ||
| if (bytes == 0) { | ||
| /* guid is somewhat corrupted */ | ||
| fallback = FLB_TRUE; | ||
| goto fallback; | ||
| } | ||
| } | ||
|
|
||
| *out_id = id; | ||
| *out_size = bytes; | ||
| return 0; | ||
|
||
| } | ||
|
|
||
| #elif defined(__FreeBSD__) || defined(__NetBSD__) || \ | ||
| defined(__OpenBSD__) || defined(__DragonFly__) | ||
|
|
||
|
|
@@ -2175,7 +2219,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) | |
| #endif | ||
|
|
||
| fallback: | ||
|
|
||
| flb_warn("falling back on random machine UUID"); | ||
|
|
||
| /* generate a random uuid */ | ||
|
|
@@ -2185,15 +2229,22 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) | |
| return -1; | ||
| } | ||
| ret = flb_utils_uuid_v4_gen(uuid); | ||
|
|
||
| if (ret == 0) { | ||
|
|
||
| ret = write_uuid_to_file(fallback_id_file, uuid); | ||
| if (ret != 0){ | ||
| //writing failed, next uuid generation will be random again | ||
| //write a message and return | ||
| flb_warn("failed to write machine-id to file %s", fallback_id_file); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| *out_id = uuid; | ||
| *out_size = strlen(uuid); | ||
| if (fallback == FLB_TRUE) { | ||
| return 2; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback file path is unreliable — use an absolute or well-known directory.
Using
"machine-id"without a directory path means the file will be created in the current working directory, which is unpredictable and environment-dependent. This can lead to:Consider using a well-defined location such as
/var/lib/fluent-bit/machine-id,/tmp/fluent-bit-machine-id, or a configurable path.Example fix:
Note: You may need to ensure the directory exists or handle directory creation.
📝 Committable suggestion
🤖 Prompt for AI Agents