Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
73 changes: 73 additions & 0 deletions src/config_format/flb_cf_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ enum state {
STATE_SECTION, /* top level */
STATE_SECTION_KEY,
STATE_SECTION_VAL,
STATE_SECTION_VAL_LIST,

STATE_SERVICE, /* 'service' section */
STATE_INCLUDE, /* 'includes' section */
Expand Down Expand Up @@ -268,6 +269,8 @@ static char *state_str(enum state val)
return "section-key";
case STATE_SECTION_VAL:
return "section-value";
case STATE_SECTION_VAL_LIST:
return "section-value-list";
case STATE_SERVICE:
return "service";
case STATE_INCLUDE:
Expand Down Expand Up @@ -885,6 +888,7 @@ static int consume_event(struct flb_cf *conf, struct local_ctx *ctx,
char *value;
struct flb_cf_env_var *keyval;
char *last_included;
flb_sds_t normalized_key;

last_included = state_get_last(ctx);

Expand Down Expand Up @@ -1961,6 +1965,75 @@ static int consume_event(struct flb_cf *conf, struct local_ctx *ctx,
return YAML_FAILURE;
}

if (state->state != STATE_SECTION_KEY) {
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;
case YAML_SEQUENCE_START_EVENT:
normalized_key = flb_cf_key_translate(conf, state->key, flb_sds_len(state->key));
if (normalized_key == NULL) {
flb_error("unable to normalize key");
return YAML_FAILURE;
}

ret = strcasecmp(normalized_key, "parsers_file");
flb_sds_destroy(normalized_key);

if (state->section != SECTION_SERVICE || ret != 0) {
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}

state = state_push_key(ctx, STATE_SECTION_VAL_LIST, state->key);
if (state == NULL) {
flb_error("unable to allocate state");
return YAML_FAILURE;
}
break;
default:
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}
break;

case STATE_SECTION_VAL_LIST:
switch(event->type) {
case YAML_SCALAR_EVENT:
value = (char *) event->data.scalar.value;

if (state->cf_section == NULL) {
flb_error("no section to register key value to");
return YAML_FAILURE;
}

if (flb_cf_section_property_add(conf, state->cf_section->properties,
state->key, flb_sds_len(state->key),
value, strlen(value)) == NULL) {
flb_error("unable to add property");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return YAML_FAILURE;
}
break;
case YAML_SEQUENCE_END_EVENT:
state = state_pop(ctx);

if (state == NULL) {
flb_error("no state left");
return YAML_FAILURE;
}

if (state->state != STATE_SECTION_VAL) {
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
}

state = state_pop(ctx);

if (state == NULL) {
flb_error("no state left");
return YAML_FAILURE;
}

if (state->state != STATE_SECTION_KEY) {
yaml_error_event(ctx, state, event);
return YAML_FAILURE;
Expand Down
50 changes: 50 additions & 0 deletions tests/internal/config_format_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define FLB_004 FLB_TESTS_CONF_PATH "/stream_processor.yaml"
#define FLB_005 FLB_TESTS_CONF_PATH "/plugins.yaml"
#define FLB_006 FLB_TESTS_CONF_PATH "/upstream.yaml"
#define FLB_PARSERS_LIST FLB_TESTS_CONF_PATH "/parsers/parsers-list.yaml"
#define FLB_PARSERS_LIST_CAMEL_CASE FLB_TESTS_CONF_PATH "/parsers/parsers-list-camel-case.yaml"

#define FLB_000_WIN FLB_TESTS_CONF_PATH "\\fluent-bit-windows.yaml"
#define FLB_BROKEN_PLUGIN_VARIANT FLB_TESTS_CONF_PATH "/broken_plugin_variant.yaml"
Expand Down Expand Up @@ -322,6 +324,52 @@ static void test_parser_conf()
flb_config_exit(config);
}

static void test_parser_conf_list_file(char *path)
{
struct flb_cf *cf;
struct flb_config *config;
int ret;
int cnt;

cf = flb_cf_yaml_create(NULL, path, NULL, 0);
TEST_CHECK(cf != NULL);
if (!cf) {
exit(EXIT_FAILURE);
}

config = flb_config_init();
TEST_CHECK(config != NULL);
config->conf_path = flb_strdup(FLB_TESTS_CONF_PATH "/parsers/");

/* Count the parsers registered automatically by fluent-bit */
cnt = mk_list_size(&config->parsers);

ret = flb_config_load_config_format(config, cf);
if (ret != 0) {
exit(EXIT_FAILURE);
}

if (!TEST_CHECK(mk_list_size(&config->parsers) == cnt + 2)) {
TEST_MSG("Section number error. Got=%d expect=%d",
mk_list_size(&config->parsers),
cnt + 2);
}

flb_cf_dump(cf);
flb_cf_destroy(cf);
flb_config_exit(config);
}

static void test_parser_conf_list(void)
{
test_parser_conf_list_file(FLB_PARSERS_LIST);
}

static void test_parser_conf_list_camel_case(void)
{
test_parser_conf_list_file(FLB_PARSERS_LIST_CAMEL_CASE);
}

static inline int check_camel_to_snake(char *input, char *output)
{
int len;
Expand Down Expand Up @@ -880,6 +928,8 @@ TEST_LIST = {
{ "slist odd", test_slist_odd},
{ "slist even", test_slist_even},
{ "parsers file conf", test_parser_conf},
{ "parsers file conf list", test_parser_conf_list},
{ "parsers file conf list camel case", test_parser_conf_list_camel_case},
{ "camel_case_key", test_camel_case_key},
{ "processors", test_processors},
{ "parsers_and_multiline_parsers", test_parsers_and_multiline_parsers},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[PARSER]
Name extra
Format regex
Regex ^(?<message>.*)$
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
service:
parsersFile:
- parsers.conf
- parsers-extra.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
service:
parsers_file:
- parsers.conf
- parsers-extra.conf
Loading