Skip to content
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

out_es: ensure integrity of already recorded logs #2026

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 67 additions & 13 deletions plugins/out_es/es.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ static int cb_es_init(struct flb_output_instance *ins,
static int elasticsearch_error_check(struct flb_elasticsearch *ctx,
struct flb_http_client *c)
{
int i;
int i, j, k;
int ret;
int check = FLB_TRUE;
int check = FLB_FALSE;
int root_type;
char *out_buf;
size_t off = 0;
Expand All @@ -499,6 +499,9 @@ static int elasticsearch_error_check(struct flb_elasticsearch *ctx,
msgpack_object root;
msgpack_object key;
msgpack_object val;
msgpack_object item;
msgpack_object item_key;
msgpack_object item_val;

/*
* Check if our payload is complete: there is such situations where
Expand Down Expand Up @@ -550,11 +553,7 @@ static int elasticsearch_error_check(struct flb_elasticsearch *ctx,
goto done;
}

if (key.via.str.size != 6) {
continue;
}

if (strncmp(key.via.str.ptr, "errors", 6) == 0) {
if (key.via.str.size == 6 && strncmp(key.via.str.ptr, "errors", 6) == 0) {
val = root.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_BOOLEAN) {
flb_plg_error(ctx->ins, "unexpected 'error' value type=%i",
Expand All @@ -564,17 +563,72 @@ static int elasticsearch_error_check(struct flb_elasticsearch *ctx,
}

/* If error == false, we are OK (no errors = FLB_FALSE) */
if (val.via.boolean) {
/* there is an error */
check = FLB_TRUE;
goto done;
}
else {
if (!val.via.boolean) {
/* no errors */
check = FLB_FALSE;
goto done;
}
}
else if (key.via.str.size == 5 && strncmp(key.via.str.ptr, "items", 5) == 0) {
val = root.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_ARRAY) {
flb_plg_error(ctx->ins, "unexpected 'items' value type=%i",
val.type);
check = FLB_TRUE;
goto done;
}

for (j = 0; j < val.via.array.size; j++) {
item = val.via.array.ptr[j];
if (item.type != MSGPACK_OBJECT_MAP) {
flb_plg_error(ctx->ins, "unexpected 'item' outer value type=%i",
item.type);
check = FLB_TRUE;
goto done;
}

if (item.via.map.size != 1) {
flb_plg_error(ctx->ins, "unexpected 'item' size=%i",
item.via.map.size);
check = FLB_TRUE;
goto done;
}

item = item.via.map.ptr[0].val;
if (item.type != MSGPACK_OBJECT_MAP) {
flb_plg_error(ctx->ins, "unexpected 'item' inner value type=%i",
item.type);
check = FLB_TRUE;
goto done;
}

for (k = 0; k < item.via.map.size; k++) {
item_key = item.via.map.ptr[k].key;
if (item_key.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "unexpected key type=%i",
item_key.type);
check = FLB_TRUE;
goto done;
}

if (item_key.via.str.size == 6 && strncmp(item_key.via.str.ptr, "status", 6) == 0) {
item_val = item.via.map.ptr[k].val;

if (item_val.type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
flb_plg_error(ctx->ins, "unexpected 'status' value type=%i",
item_val.type);
check = FLB_TRUE;
goto done;
}
/* Check for errors other than version conflict (document already exists) */
if (item_val.via.i64 != 409) {
check = FLB_TRUE;
goto done;
}
}
}
}
}
}

done:
Expand Down
4 changes: 2 additions & 2 deletions plugins/out_es/es_bulk.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#define ES_BULK_CHUNK 4096 /* Size of buffer chunks */
#define ES_BULK_HEADER 165 /* ES Bulk API prefix line */
#define ES_BULK_INDEX_FMT "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_ID "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT "{\"create\":{\"_index\":\"%s\",\"_type\":\"%s\"}}\n"
#define ES_BULK_INDEX_FMT_ID "{\"create\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\"}}\n"

struct es_bulk {
char *ptr;
Expand Down