Skip to content

Commit dd20b37

Browse files
out_es: add apikey to available auth types
Allow for using an API key as an authentication type to elastic.
1 parent 067c064 commit dd20b37

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

plugins/out_es/es.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "es.h"
3737
#include "es_conf.h"
3838
#include "es_bulk.h"
39+
#include "fluent-bit/flb_sds.h"
3940
#include "murmur3.h"
4041

4142
struct flb_output_plugin out_es_plugin;
@@ -885,6 +886,11 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
885886
else if (ctx->cloud_user && ctx->cloud_passwd) {
886887
flb_http_basic_auth(c, ctx->cloud_user, ctx->cloud_passwd);
887888
}
889+
else if (ctx->http_api_key) {
890+
flb_sds_t header_value = flb_sds_cat(flb_sds_create("ApiKey "), ctx->http_api_key, strlen(ctx->http_api_key));
891+
flb_http_add_header(c, "Authorization", 11, header_value, strlen(header_value));
892+
flb_sds_destroy(header_value);
893+
}
888894

889895
#ifdef FLB_HAVE_AWS
890896
if (ctx->has_aws_auth == FLB_TRUE) {

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@
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 *p;
15+
char *out_js = res_data;
16+
char *auth_header = data;
17+
18+
/* Check if the Authorization header is properly set */
19+
TEST_CHECK(res_data != NULL);
20+
21+
/* Print test debugging info */
22+
flb_debug("[api key test] %s, data: %s", auth_header, out_js);
23+
24+
flb_free(res_data);
25+
}
26+
27+
1028
static void cb_check_write_op_index(void *ctx, int ffd,
1129
int res_ret, void *res_data,
1230
size_t res_size, void *data)
@@ -722,6 +740,51 @@ void flb_test_div0()
722740
flb_destroy(ctx);
723741
}
724742

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

726789
static void cb_check_long_index(void *ctx, int ffd,
727790
int res_ret, void *res_data, size_t res_size,
@@ -1012,6 +1075,7 @@ TEST_LIST = {
10121075
{"tag_key" , flb_test_tag_key },
10131076
{"replace_dots" , flb_test_replace_dots },
10141077
{"id_key" , flb_test_id_key },
1078+
{"http_api_key" , flb_test_http_api_key },
10151079
{"logstash_prefix_separator" , flb_test_logstash_prefix_separator },
10161080
{"response_success" , flb_test_response_success },
10171081
{"response_successes", flb_test_response_successes },

0 commit comments

Comments
 (0)