Skip to content

Commit 59d31dc

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 09214eb commit 59d31dc

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)
@@ -875,11 +896,17 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
875896
flb_http_add_header(c, "Content-Type", 12, "application/x-ndjson", 20);
876897

877898
if (ctx->http_user && ctx->http_passwd) {
899+
flb_plg_debug(ctx->ins, "using http basic auth");
878900
flb_http_basic_auth(c, ctx->http_user, ctx->http_passwd);
879901
}
880902
else if (ctx->cloud_user && ctx->cloud_passwd) {
903+
flb_plg_debug(ctx->ins, "using elastic cloud auth");
881904
flb_http_basic_auth(c, ctx->cloud_user, ctx->cloud_passwd);
882905
}
906+
else if (ctx->cloud_apikey) {
907+
flb_plg_debug(ctx->ins, "using elastic cloud apikey");
908+
es_http_add_cloud_apikey(c, ctx->cloud_apikey);
909+
}
883910

884911
#ifdef FLB_HAVE_AWS
885912
if (ctx->has_aws_auth == FLB_TRUE) {
@@ -937,7 +964,7 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
937964
/*
938965
* If trace_error is set, trace the actual
939966
* response from Elasticsearch explaining the problem.
940-
* Trace_Output can be used to see the request.
967+
* Trace_Output can be used to see the request.
941968
*/
942969
if (pack_size < 4000) {
943970
flb_plg_debug(ctx->ins, "error caused by: Input\n%.*s\n",
@@ -1102,7 +1129,7 @@ static struct flb_config_map config_map[] = {
11021129
"Set payload compression mechanism. Option available is 'gzip'"
11031130
},
11041131

1105-
/* Cloud Authentication */
1132+
/* Elastic Cloud Authentication */
11061133
{
11071134
FLB_CONFIG_MAP_STR, "cloud_id", NULL,
11081135
0, FLB_FALSE, 0,
@@ -1113,6 +1140,11 @@ static struct flb_config_map config_map[] = {
11131140
0, FLB_FALSE, 0,
11141141
"Elastic cloud authentication credentials"
11151142
},
1143+
{
1144+
FLB_CONFIG_MAP_STR, "cloud_apikey", NULL,
1145+
0, FLB_FALSE, 0,
1146+
"Elastic cloud apikey credentials"
1147+
},
11161148

11171149
/* AWS Authentication */
11181150
#ifdef FLB_HAVE_AWS

plugins/out_es/es.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ struct flb_elasticsearch {
5959
char *cloud_user;
6060
char *cloud_passwd;
6161

62+
/* Elastic Cloud Apikey */
63+
char *cloud_apikey;
64+
6265
/* AWS Auth */
6366
#ifdef FLB_HAVE_AWS
6467
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)