Skip to content

Commit cd0fbdf

Browse files
committed
out_es: add cloud_apikey configuration
This patch adds support for the Elastic Cloud API Keys. It updates the elastic search plugin to add a cloud_apikey configuration option. Usage: [OUTPUT] name es Cloud_Id elastic_cloud_id Cloud_Apikey elastic_apikey Signed-off-by: Soedarsono <[email protected]>
1 parent 92b9053 commit cd0fbdf

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

plugins/out_es/es.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ static flb_sds_t add_aws_auth(struct flb_http_client *c,
7575
}
7676
#endif /* FLB_HAVE_AWS */
7777

78+
static int es_http_add_cloud_apikey(struct flb_http_client *c,
79+
const char *apikey)
80+
{
81+
flb_sds_t header;
82+
int ret = 0;
83+
84+
if (apikey == NULL) {
85+
return -1;
86+
}
87+
88+
header = flb_sds_create_size(256 + 7);
89+
header = flb_sds_printf(&header, "%s %s", "ApiKey", apikey);
90+
91+
ret = flb_http_add_header(c, "Authorization", 13,
92+
header, flb_sds_len(header));
93+
94+
flb_sds_destroy(header);
95+
96+
return ret;
97+
}
98+
7899
static int es_pack_map_content(msgpack_packer *tmp_pck,
79100
msgpack_object map,
80101
struct flb_elasticsearch *ctx)
@@ -868,11 +889,17 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
868889
flb_http_add_header(c, "Content-Type", 12, "application/x-ndjson", 20);
869890

870891
if (ctx->http_user && ctx->http_passwd) {
892+
flb_plg_debug(ctx->ins, "using http basic auth");
871893
flb_http_basic_auth(c, ctx->http_user, ctx->http_passwd);
872894
}
873895
else if (ctx->cloud_user && ctx->cloud_passwd) {
896+
flb_plg_debug(ctx->ins, "using elastic cloud auth");
874897
flb_http_basic_auth(c, ctx->cloud_user, ctx->cloud_passwd);
875898
}
899+
else if (ctx->cloud_apikey) {
900+
flb_plg_debug(ctx->ins, "using elastic cloud apikey");
901+
es_http_add_cloud_apikey(c, ctx->cloud_apikey);
902+
}
876903

877904
#ifdef FLB_HAVE_AWS
878905
if (ctx->has_aws_auth == FLB_TRUE) {
@@ -926,7 +953,7 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
926953
/*
927954
* If trace_error is set, trace the actual
928955
* response from Elasticsearch explaining the problem.
929-
* Trace_Output can be used to see the request.
956+
* Trace_Output can be used to see the request.
930957
*/
931958
if (pack_size < 4000) {
932959
flb_plg_debug(ctx->ins, "error caused by: Input\n%.*s\n",
@@ -1023,7 +1050,7 @@ static struct flb_config_map config_map[] = {
10231050
"Set payload compression mechanism. Option available is 'gzip'"
10241051
},
10251052

1026-
/* Cloud Authentication */
1053+
/* Elastic Cloud Authentication */
10271054
{
10281055
FLB_CONFIG_MAP_STR, "cloud_id", NULL,
10291056
0, FLB_FALSE, 0,
@@ -1034,6 +1061,11 @@ static struct flb_config_map config_map[] = {
10341061
0, FLB_FALSE, 0,
10351062
"Elastic cloud authentication credentials"
10361063
},
1064+
{
1065+
FLB_CONFIG_MAP_STR, "cloud_apikey", NULL,
1066+
0, FLB_FALSE, 0,
1067+
"Elastic cloud apikey credentials"
1068+
},
10371069

10381070
/* AWS Authentication */
10391071
#ifdef FLB_HAVE_AWS

plugins/out_es/es.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct flb_elasticsearch {
5050
char *cloud_user;
5151
char *cloud_passwd;
5252

53+
/* Elastic Cloud Apikey */
54+
char *cloud_apikey;
55+
5356
/* AWS Auth */
5457
#ifdef FLB_HAVE_AWS
5558
int has_aws_auth;

plugins/out_es/es_conf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ struct flb_elasticsearch *flb_es_conf_create(struct flb_output_instance *ins,
224224
set_cloud_credentials(ctx, tmp);
225225
}
226226

227+
/* handle cloud_apikey */
228+
tmp = flb_output_get_property("cloud_apikey", ins);
229+
if (tmp) {
230+
ctx->cloud_apikey = flb_strdup(tmp);
231+
}
232+
227233
/* use TLS ? */
228234
if (ins->use_tls == FLB_TRUE) {
229235
io_flags = FLB_IO_TLS;
@@ -531,6 +537,7 @@ int flb_es_conf_destroy(struct flb_elasticsearch *ctx)
531537

532538
flb_free(ctx->cloud_passwd);
533539
flb_free(ctx->cloud_user);
540+
flb_free(ctx->cloud_apikey);
534541
flb_free(ctx);
535542

536543
return 0;

0 commit comments

Comments
 (0)