Skip to content

Commit 7be40e6

Browse files
cosmo0920edsiper
authored andcommitted
out_splunk: Handle HEC token from metadata
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 8fb66c4 commit 7be40e6

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

plugins/out_splunk/splunk.c

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,87 @@ static inline int splunk_metrics_format(struct flb_output_instance *ins,
345345
}
346346
#endif
347347

348+
349+
/* implements functionality to get auth_header from msgpack map (metadata) */
350+
static flb_sds_t extract_hec_token(struct flb_splunk *ctx, msgpack_object *map)
351+
{
352+
size_t map_size = map->via.map.size;
353+
msgpack_object_kv *kv;
354+
msgpack_object key;
355+
msgpack_object val;
356+
char *key_str = NULL;
357+
char *val_str = NULL;
358+
size_t key_str_size = 0;
359+
size_t val_str_size = 0;
360+
int j;
361+
int check = FLB_FALSE;
362+
int found = FLB_FALSE;
363+
flb_sds_t hec_token;
364+
365+
kv = map->via.map.ptr;
366+
367+
for(j=0; j < map_size; j++) {
368+
check = FLB_FALSE;
369+
found = FLB_FALSE;
370+
key = (kv+j)->key;
371+
if (key.type == MSGPACK_OBJECT_BIN) {
372+
key_str = (char *) key.via.bin.ptr;
373+
key_str_size = key.via.bin.size;
374+
check = FLB_TRUE;
375+
}
376+
if (key.type == MSGPACK_OBJECT_STR) {
377+
key_str = (char *) key.via.str.ptr;
378+
key_str_size = key.via.str.size;
379+
check = FLB_TRUE;
380+
}
381+
382+
if (check == FLB_TRUE) {
383+
if (strncmp("hec_token", key_str, key_str_size) == 0) {
384+
val = (kv+j)->val;
385+
if (val.type == MSGPACK_OBJECT_BIN) {
386+
val_str = (char *) val.via.bin.ptr;
387+
val_str_size = val.via.str.size;
388+
found = FLB_TRUE;
389+
break;
390+
}
391+
if (val.type == MSGPACK_OBJECT_STR) {
392+
val_str = (char *) val.via.str.ptr;
393+
val_str_size = val.via.str.size;
394+
found = FLB_TRUE;
395+
break;
396+
}
397+
}
398+
}
399+
}
400+
401+
if (found == FLB_TRUE) {
402+
hec_token = flb_sds_create_len(val_str, val_str_size);
403+
if (!hec_token) {
404+
return NULL;
405+
}
406+
return hec_token;
407+
}
408+
409+
410+
flb_plg_debug(ctx->ins, "Could not find hec_token in metadata");
411+
return NULL;
412+
}
413+
348414
static inline int splunk_format(const void *in_buf, size_t in_bytes,
349415
char *tag, int tag_len,
350416
char **out_buf, size_t *out_size,
351417
struct flb_splunk *ctx)
352418
{
353419
int ret;
354420
msgpack_object map;
421+
msgpack_object metadata;
355422
msgpack_sbuffer mp_sbuf;
356423
msgpack_packer mp_pck;
357424
char *err;
358425
flb_sds_t tmp;
359426
flb_sds_t record;
360427
flb_sds_t json_out;
428+
flb_sds_t metadata_hec_token = NULL;
361429
struct flb_log_event_decoder log_decoder;
362430
struct flb_log_event log_event;
363431

@@ -378,6 +446,8 @@ static inline int splunk_format(const void *in_buf, size_t in_bytes,
378446
return -1;
379447
}
380448

449+
ctx->metadata_auth_header = NULL;
450+
381451
while ((ret = flb_log_event_decoder_next(
382452
&log_decoder,
383453
&log_event)) == FLB_EVENT_DECODER_SUCCESS) {
@@ -387,6 +457,19 @@ static inline int splunk_format(const void *in_buf, size_t in_bytes,
387457
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
388458

389459
map = *log_event.body;
460+
metadata = *log_event.metadata;
461+
metadata_hec_token = extract_hec_token(ctx, &metadata);
462+
463+
if (metadata_hec_token != NULL) {
464+
/* Currently, in_splunk implementation permits to
465+
* specify only one splunk token per one instance.
466+
* So, it should be valid if storing only last value of
467+
* splunk token per one chunk. */
468+
if (ctx->metadata_auth_header != NULL) {
469+
cfl_sds_destroy(ctx->metadata_auth_header);
470+
}
471+
ctx->metadata_auth_header = metadata_hec_token;
472+
}
390473

391474
if (ctx->event_key) {
392475
/* Pack the value of a event key */
@@ -644,6 +727,10 @@ static void cb_splunk_flush(struct flb_event_chunk *event_chunk,
644727
if (ctx->http_user && ctx->http_passwd) {
645728
flb_http_basic_auth(c, ctx->http_user, ctx->http_passwd);
646729
}
730+
else if (ctx->metadata_auth_header) {
731+
flb_http_add_header(c, "Authorization", 13,
732+
ctx->metadata_auth_header, flb_sds_len(ctx->metadata_auth_header));
733+
}
647734
else if (ctx->auth_header) {
648735
flb_http_add_header(c, "Authorization", 13,
649736
ctx->auth_header, flb_sds_len(ctx->auth_header));
@@ -711,6 +798,9 @@ static void cb_splunk_flush(struct flb_event_chunk *event_chunk,
711798
}
712799

713800
/* Cleanup */
801+
if (ctx->metadata_auth_header != NULL) {
802+
cfl_sds_destroy(ctx->metadata_auth_header);
803+
}
714804
flb_http_client_destroy(c);
715805
flb_upstream_conn_release(u_conn);
716806
FLB_OUTPUT_RETURN(ret);
@@ -817,7 +907,8 @@ static struct flb_config_map config_map[] = {
817907
{
818908
FLB_CONFIG_MAP_STR, "splunk_token", NULL,
819909
0, FLB_FALSE, 0,
820-
"Specify the Authentication Token for the HTTP Event Collector interface."
910+
"Specify the Authentication Token for the HTTP Event Collector interface. "
911+
"If event metadata contains a splunk_token, it will be prioritized to use instead of this token."
821912
},
822913

823914
{

plugins/out_splunk/splunk.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ struct flb_splunk {
9595

9696
/* Token Auth */
9797
flb_sds_t auth_header;
98+
/* Token Auth (via metadata) */
99+
flb_sds_t metadata_auth_header;
98100

99101
/* Channel identifier */
100102
flb_sds_t channel;

plugins/out_splunk/splunk_conf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ struct flb_splunk *flb_splunk_conf_create(struct flb_output_instance *ins,
240240
return NULL;
241241
}
242242

243+
ctx->metadata_auth_header = NULL;
243244
/* No http_user is set, fallback to splunk_token, if splunk_token is unset, fail. */
244245
if (!ctx->http_user) {
245246
/* Splunk Auth Token */

0 commit comments

Comments
 (0)