Skip to content

Commit 8107bf4

Browse files
out_es: Prevent a SIGSEGV when ctx->index is NULL
If `index` field is blank in the configuration, and `Logstash_Format` and `Generate_ID` are both `off`, a SIGSEGV can occur when trying to generate the index for a set of messages. Signed-off-by: Todd Kennedy <[email protected]>
1 parent eaa4692 commit 8107bf4

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

plugins/out_es/es.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@ static int cb_es_init(struct flb_output_instance *ins,
627627
return -1;
628628
}
629629

630+
if (ctx->index == NULL && ctx->logstash_format == FLB_FALSE && ctx->generate_id == FLB_FALSE) {
631+
flb_plg_error(ins, "cannot initialize plugin, index is not set and logstash_format and generate_id are both off");
632+
return -1;
633+
}
634+
630635
flb_plg_debug(ctx->ins, "host=%s port=%i uri=%s index=%s type=%s",
631636
ins->host.name, ins->host.port, ctx->uri,
632637
ctx->index, ctx->type);
@@ -937,7 +942,7 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
937942
/*
938943
* If trace_error is set, trace the actual
939944
* response from Elasticsearch explaining the problem.
940-
* Trace_Output can be used to see the request.
945+
* Trace_Output can be used to see the request.
941946
*/
942947
if (pack_size < 4000) {
943948
flb_plg_debug(ctx->ins, "error caused by: Input\n%.*s\n",

tests/runtime/out_elasticsearch.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,47 @@ void flb_test_write_operation_upsert()
347347
flb_destroy(ctx);
348348
}
349349

350+
void flb_test_null_index()
351+
{
352+
int ret;
353+
int size = sizeof(JSON_ES) - 1;
354+
flb_ctx_t *ctx;
355+
int in_ffd;
356+
int out_ffd;
357+
/* Create context, flush every second (some checks omitted here) */
358+
ctx = flb_create();
359+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
360+
361+
/* Lib input mode */
362+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
363+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
364+
365+
/* Elasticsearch output */
366+
out_ffd = flb_output(ctx, (char *) "es", NULL);
367+
flb_output_set(ctx, out_ffd,
368+
"match", "test",
369+
NULL);
370+
371+
/* Override defaults of index and type */
372+
flb_output_set(ctx, out_ffd,
373+
"index", "",
374+
"type", "type_test",
375+
NULL);
376+
377+
/* Enable test mode */
378+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
379+
cb_check_index_type,
380+
NULL, NULL);
381+
382+
/* Start */
383+
ret = flb_start(ctx);
384+
TEST_CHECK(ret == -1);
385+
386+
sleep(2);
387+
flb_stop(ctx);
388+
flb_destroy(ctx);
389+
}
390+
350391
void flb_test_index_type()
351392
{
352393
int ret;

0 commit comments

Comments
 (0)