Skip to content

Commit 0986d3d

Browse files
authored
in_forward: support unix_perm (fluent#5511)
* tests: runtime: in_forward: add permission test Signed-off-by: Takahiro Yamashita <[email protected]> * in_forward: support 'unix_perm' Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent 7b1c19a commit 0986d3d

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

plugins/in_forward/fw.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#ifdef FLB_HAVE_UNIX_SOCKET
2727
#include <sys/socket.h>
2828
#include <sys/un.h>
29+
#include <sys/stat.h>
2930
#endif
3031

3132
#include "fw.h"
@@ -60,6 +61,16 @@ static int fw_unix_create(struct flb_in_fw_config *ctx)
6061
return -1;
6162
}
6263

64+
if (ctx->unix_perm_str) {
65+
if (chmod(address.sun_path, ctx->unix_perm)) {
66+
flb_errno();
67+
flb_plg_error(ctx->ins, "cannot set permission on '%s' to %04o",
68+
address.sun_path, ctx->unix_perm);
69+
close(fd);
70+
return -1;
71+
}
72+
}
73+
6374
if (listen(fd, 5) != 0) {
6475
flb_errno();
6576
close(fd);
@@ -216,6 +227,11 @@ static struct flb_config_map config_map[] = {
216227
0, FLB_TRUE, offsetof(struct flb_in_fw_config, unix_path),
217228
"The path to unix socket to receive a Forward message."
218229
},
230+
{
231+
FLB_CONFIG_MAP_STR, "unix_perm", (char *)NULL,
232+
0, FLB_TRUE, offsetof(struct flb_in_fw_config, unix_perm_str),
233+
"Set the permissions for the UNIX socket"
234+
},
219235
{
220236
FLB_CONFIG_MAP_SIZE, "buffer_chunk_size", FLB_IN_FW_CHUNK_SIZE,
221237
0, FLB_TRUE, offsetof(struct flb_in_fw_config, buffer_chunk_size),

plugins/in_forward/fw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct flb_in_fw_config {
3636

3737
/* Unix Socket (TCP only) */
3838
char *unix_path; /* Unix path for socket */
39+
unsigned int unix_perm; /* Permission for socket */
40+
flb_sds_t unix_perm_str; /* Permission (config map) */
3941

4042
int coll_fd;
4143
struct mk_list connections; /* List of active connections */

plugins/in_forward/fw_config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ struct flb_in_fw_config *fw_config_init(struct flb_input_instance *i_ins)
5353
snprintf(tmp, sizeof(tmp) - 1, "%d", i_ins->host.port);
5454
config->tcp_port = flb_strdup(tmp);
5555
}
56+
else {
57+
/* Unix socket mode */
58+
if (config->unix_perm_str) {
59+
config->unix_perm = strtol(config->unix_perm_str, NULL, 8) & 07777;
60+
}
61+
}
5662

5763
if (!config->unix_path) {
5864
flb_debug("[in_fw] Listen='%s' TCP_Port=%s",

tests/runtime/in_forward.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,110 @@ void flb_test_unix_path()
459459
flb_socket_close(fd);
460460
test_ctx_destroy(ctx);
461461
}
462+
463+
464+
void flb_test_unix_perm()
465+
{
466+
struct flb_lib_out_cb cb_data;
467+
struct test_ctx *ctx;
468+
struct sockaddr_un sun;
469+
flb_sockfd_t fd;
470+
int ret;
471+
int num;
472+
ssize_t w_size;
473+
char *unix_path = "in_forward_unix";
474+
struct stat sb;
475+
476+
char *buf;
477+
size_t size;
478+
479+
clear_output_num();
480+
481+
cb_data.cb = cb_check_result_json;
482+
cb_data.data = "\"test\":\"msg\"";
483+
484+
ctx = test_ctx_create(&cb_data);
485+
if (!TEST_CHECK(ctx != NULL)) {
486+
TEST_MSG("test_ctx_create failed");
487+
exit(EXIT_FAILURE);
488+
}
489+
490+
ret = flb_input_set(ctx->flb, ctx->i_ffd,
491+
"unix_path", unix_path,
492+
"unix_perm", "0600",
493+
NULL);
494+
TEST_CHECK(ret == 0);
495+
496+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
497+
"match", "test",
498+
"format", "json",
499+
NULL);
500+
TEST_CHECK(ret == 0);
501+
502+
/* Start the engine */
503+
ret = flb_start(ctx->flb);
504+
TEST_CHECK(ret == 0);
505+
506+
/* waiting to create socket */
507+
flb_time_msleep(200);
508+
509+
memset(&sun, 0, sizeof(sun));
510+
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
511+
if (!TEST_CHECK(fd >= 0)) {
512+
TEST_MSG("failed to socket %s, errno=%d", unix_path, errno);
513+
unlink(unix_path);
514+
exit(EXIT_FAILURE);
515+
}
516+
517+
sun.sun_family = AF_LOCAL;
518+
strcpy(sun.sun_path, unix_path);
519+
ret = connect(fd, (const struct sockaddr *)&sun, sizeof(sun));
520+
if (!TEST_CHECK(ret >= 0)) {
521+
TEST_MSG("failed to connect, errno=%d", errno);
522+
flb_socket_close(fd);
523+
unlink(unix_path);
524+
exit(EXIT_FAILURE);
525+
}
526+
create_simple_json(&buf, &size);
527+
w_size = send(fd, buf, size, 0);
528+
flb_free(buf);
529+
if (!TEST_CHECK(w_size == size)) {
530+
TEST_MSG("failed to write to %s", unix_path);
531+
flb_socket_close(fd);
532+
unlink(unix_path);
533+
exit(EXIT_FAILURE);
534+
}
535+
536+
/* waiting to flush */
537+
flb_time_msleep(1500);
538+
539+
num = get_output_num();
540+
if (!TEST_CHECK(num > 0)) {
541+
TEST_MSG("no outputs");
542+
}
543+
544+
545+
/* File permission */
546+
ret = stat(unix_path, &sb);
547+
if (!TEST_CHECK(ret == 0)) {
548+
TEST_MSG("stat failed. errno=%d", errno);
549+
test_ctx_destroy(ctx);
550+
exit(EXIT_FAILURE);
551+
}
552+
553+
if (!TEST_CHECK((sb.st_mode & S_IRWXO) == 0)) {
554+
TEST_MSG("Permssion(others) error. val=0x%x",sb.st_mode & S_IRWXO);
555+
}
556+
if (!TEST_CHECK((sb.st_mode & S_IRWXG) == 0)) {
557+
TEST_MSG("Permssion(group) error. val=0x%x",sb.st_mode & S_IRWXG);
558+
}
559+
if (!TEST_CHECK((sb.st_mode & S_IRWXU) == (S_IRUSR | S_IWUSR))) {
560+
TEST_MSG("Permssion(user) error. val=0x%x",sb.st_mode & S_IRWXU);
561+
}
562+
563+
flb_socket_close(fd);
564+
test_ctx_destroy(ctx);
565+
}
462566
#endif /* FLB_HAVE_UNIX_SOCKET */
463567

464568

@@ -468,6 +572,7 @@ TEST_LIST = {
468572
{"tag_prefix", flb_test_tag_prefix},
469573
#ifdef FLB_HAVE_UNIX_SOCKET
470574
{"unix_path", flb_test_unix_path},
575+
{"unix_perm", flb_test_unix_perm},
471576
#endif
472577
{NULL, NULL}
473578
};

0 commit comments

Comments
 (0)