Skip to content

Commit 610a125

Browse files
tkennedy1-godaddyedsiper
authored andcommitted
out_es: add apikey to available auth types
Allow for using an API key as an authentication type to elastic. Signed-off-by: Todd Kennedy <[email protected]>
1 parent c51a9e4 commit 610a125

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

plugins/out_es/es.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <fluent-bit/flb_record_accessor.h>
3030
#include <fluent-bit/flb_ra_key.h>
3131
#include <fluent-bit/flb_log_event_decoder.h>
32+
#include <fluent-bit/flb_log.h>
33+
#include <fluent-bit/flb_sds.h>
3234
#include <msgpack.h>
3335

3436
#include <time.h>
@@ -822,6 +824,7 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
822824
struct flb_http_client *c;
823825
flb_sds_t signature = NULL;
824826
int compressed = FLB_FALSE;
827+
flb_sds_t header_line = NULL;
825828

826829
/* Get upstream connection */
827830
u_conn = flb_upstream_conn_get(ctx->u);
@@ -885,6 +888,23 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
885888
else if (ctx->cloud_user && ctx->cloud_passwd) {
886889
flb_http_basic_auth(c, ctx->cloud_user, ctx->cloud_passwd);
887890
}
891+
else if (ctx->http_api_key) {
892+
header_line = flb_sds_printf(NULL, "ApiKey %s", ctx->http_api_key);
893+
if (header_line == NULL) {
894+
flb_plg_error(ctx->ins, "failed to format API key auth header");
895+
goto retry;
896+
}
897+
898+
if (flb_http_add_header(c,
899+
FLB_HTTP_HEADER_AUTH, strlen(FLB_HTTP_HEADER_AUTH),
900+
header_line, flb_sds_len(header_line)) != 0) {
901+
flb_plg_error(ctx->ins, "failed to add API key auth header");
902+
flb_sds_destroy(header_line);
903+
goto retry;
904+
}
905+
906+
flb_sds_destroy(header_line);
907+
}
888908

889909
#ifdef FLB_HAVE_AWS
890910
if (ctx->has_aws_auth == FLB_TRUE) {
@@ -1099,6 +1119,11 @@ static struct flb_config_map config_map[] = {
10991119
0, FLB_TRUE, offsetof(struct flb_elasticsearch, http_passwd),
11001120
"Password for user defined in HTTP_User"
11011121
},
1122+
{
1123+
FLB_CONFIG_MAP_STR, "http_api_key", NULL,
1124+
0, FLB_TRUE, offsetof(struct flb_elasticsearch, http_api_key),
1125+
"Base-64 encoded API key credential for Elasticsearch"
1126+
},
11021127

11031128
/* HTTP Compression */
11041129
{
@@ -1288,7 +1313,6 @@ static struct flb_config_map config_map[] = {
12881313
0, FLB_TRUE, offsetof(struct flb_elasticsearch, trace_error),
12891314
"When enabled print the Elasticsearch exception to stderr (for diag only)"
12901315
},
1291-
12921316
/* EOF */
12931317
{0}
12941318
};

plugins/out_es/es.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct flb_elasticsearch {
5454
/* HTTP Auth */
5555
char *http_user;
5656
char *http_passwd;
57+
char *http_api_key;
5758

5859
/* Elastic Cloud Auth */
5960
char *cloud_user;

tests/runtime/out_elasticsearch.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
#include "data/es/json_es.h" /* JSON_ES */
88

99

10+
static void cb_check_http_api_key(void *ctx, int ffd,
11+
int res_ret, void *res_data,
12+
size_t res_size, void *data)
13+
{
14+
char *api_key = data;
15+
16+
TEST_CHECK(api_key != NULL);
17+
TEST_CHECK(strlen(api_key) > 0);
18+
19+
TEST_CHECK(strcmp(api_key, "my-api-key-for-elasticsearch") == 0);
20+
21+
flb_free(res_data);
22+
}
23+
24+
1025
static void cb_check_write_op_index(void *ctx, int ffd,
1126
int res_ret, void *res_data,
1227
size_t res_size, void *data)
@@ -722,6 +737,51 @@ void flb_test_div0()
722737
flb_destroy(ctx);
723738
}
724739

740+
void flb_test_http_api_key()
741+
{
742+
int ret;
743+
int size = sizeof(JSON_ES) - 1;
744+
flb_ctx_t *ctx;
745+
int in_ffd;
746+
int out_ffd;
747+
char *api_key = "my-api-key-for-elasticsearch";
748+
749+
/* Create context, flush every second (some checks omitted here) */
750+
ctx = flb_create();
751+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
752+
753+
/* Lib input mode */
754+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
755+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
756+
757+
/* Elasticsearch output */
758+
out_ffd = flb_output(ctx, (char *) "es", NULL);
759+
flb_output_set(ctx, out_ffd,
760+
"match", "test",
761+
NULL);
762+
763+
/* Configure http_api_key */
764+
flb_output_set(ctx, out_ffd,
765+
"http_api_key", api_key,
766+
NULL);
767+
768+
/* Enable test mode */
769+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
770+
cb_check_http_api_key,
771+
api_key, NULL);
772+
773+
/* Start */
774+
ret = flb_start(ctx);
775+
TEST_CHECK(ret == 0);
776+
777+
/* Ingest data sample */
778+
flb_lib_push(ctx, in_ffd, (char *) JSON_ES, size);
779+
780+
sleep(2);
781+
flb_stop(ctx);
782+
flb_destroy(ctx);
783+
}
784+
725785

726786
static void cb_check_long_index(void *ctx, int ffd,
727787
int res_ret, void *res_data, size_t res_size,
@@ -1012,6 +1072,7 @@ TEST_LIST = {
10121072
{"tag_key" , flb_test_tag_key },
10131073
{"replace_dots" , flb_test_replace_dots },
10141074
{"id_key" , flb_test_id_key },
1075+
{"http_api_key" , flb_test_http_api_key },
10151076
{"logstash_prefix_separator" , flb_test_logstash_prefix_separator },
10161077
{"response_success" , flb_test_response_success },
10171078
{"response_successes", flb_test_response_successes },

0 commit comments

Comments
 (0)