Skip to content

Commit 0b15c25

Browse files
cosmo0920edsiper
authored andcommitted
out_splunk: Migrate to use record_accesor pattern for extracting token
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 7be40e6 commit 0b15c25

File tree

3 files changed

+35
-55
lines changed

3 files changed

+35
-55
lines changed

plugins/out_splunk/splunk.c

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -347,67 +347,24 @@ static inline int splunk_metrics_format(struct flb_output_instance *ins,
347347

348348

349349
/* 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)
350+
static flb_sds_t extract_hec_token(struct flb_splunk *ctx, msgpack_object map,
351+
char *tag, int tag_len)
351352
{
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;
363353
flb_sds_t hec_token;
364354

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-
}
355+
/* Extract HEC token (map which is from metadata lookup) */
356+
if (ctx->event_sourcetype_key) {
357+
hec_token = flb_ra_translate(ctx->ra_metadata_auth_key, tag, tag_len,
358+
map, NULL);
359+
if (hec_token) {
360+
return hec_token;
398361
}
399-
}
400362

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;
363+
flb_plg_debug(ctx->ins, "Could not find hec_token in metadata");
364+
return NULL;
407365
}
408366

409-
410-
flb_plg_debug(ctx->ins, "Could not find hec_token in metadata");
367+
flb_plg_debug(ctx->ins, "Could not find a record accessor definition of hec_token");
411368
return NULL;
412369
}
413370

@@ -458,7 +415,7 @@ static inline int splunk_format(const void *in_buf, size_t in_bytes,
458415

459416
map = *log_event.body;
460417
metadata = *log_event.metadata;
461-
metadata_hec_token = extract_hec_token(ctx, &metadata);
418+
metadata_hec_token = extract_hec_token(ctx, metadata, tag, tag_len);
462419

463420
if (metadata_hec_token != NULL) {
464421
/* Currently, in_splunk implementation permits to

plugins/out_splunk/splunk.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ struct flb_splunk {
9898
/* Token Auth (via metadata) */
9999
flb_sds_t metadata_auth_header;
100100

101+
/* Metadata of Splunk Authentication */
102+
flb_sds_t metadata_auth_key;
103+
struct flb_record_accessor *ra_metadata_auth_key;
104+
101105
/* Channel identifier */
102106
flb_sds_t channel;
103107
size_t channel_len;

plugins/out_splunk/splunk_conf.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ struct flb_splunk *flb_splunk_conf_create(struct flb_output_instance *ins,
262262
}
263263
}
264264

265+
/* Currently, Splunk HEC token is stored in a fixed key, hec_token. */
266+
ctx->metadata_auth_key = "hec_token";
267+
if (ctx->metadata_auth_key) {
268+
ctx->ra_metadata_auth_key = flb_ra_create(ctx->metadata_auth_key, FLB_TRUE);
269+
if (!ctx->ra_metadata_auth_key) {
270+
flb_plg_error(ctx->ins,
271+
"cannot create record accessor for "
272+
"metadata_auth_key pattern: '%s'",
273+
ctx->event_host);
274+
flb_splunk_conf_destroy(ctx);
275+
return NULL;
276+
}
277+
}
278+
279+
265280
/* channel */
266281
if (ctx->channel != NULL) {
267282
ctx->channel_len = flb_sds_len(ctx->channel);
@@ -306,6 +321,10 @@ int flb_splunk_conf_destroy(struct flb_splunk *ctx)
306321
flb_ra_destroy(ctx->ra_event_index_key);
307322
}
308323

324+
if (ctx->ra_metadata_auth_key) {
325+
flb_ra_destroy(ctx->ra_metadata_auth_key);
326+
}
327+
309328
event_fields_destroy(ctx);
310329

311330
flb_free(ctx);

0 commit comments

Comments
 (0)