Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_es: add cloud_apikey configuration #7935

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions plugins/out_es/es.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ static flb_sds_t add_aws_auth(struct flb_http_client *c,
}
#endif /* FLB_HAVE_AWS */

static int es_http_add_cloud_apikey(struct flb_http_client *c,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some opportunities for refactoring http auths in the flb_http_client library, but should probably be tackled in another PR.

const char *apikey)
{
flb_sds_t header;
int ret = 0;

if (apikey == NULL) {
return -1;
}

header = flb_sds_create_size(256 + 7);
header = flb_sds_printf(&header, "%s %s", "ApiKey", apikey);

ret = flb_http_add_header(c, "Authorization", 13,
header, flb_sds_len(header));

flb_sds_destroy(header);

return ret;
}

static int es_pack_map_content(msgpack_packer *tmp_pck,
msgpack_object map,
struct flb_elasticsearch *ctx)
Expand Down Expand Up @@ -868,11 +889,17 @@ static void cb_es_flush(struct flb_event_chunk *event_chunk,
flb_http_add_header(c, "Content-Type", 12, "application/x-ndjson", 20);

if (ctx->http_user && ctx->http_passwd) {
flb_plg_debug(ctx->ins, "using http basic auth");
flb_http_basic_auth(c, ctx->http_user, ctx->http_passwd);
}
else if (ctx->cloud_user && ctx->cloud_passwd) {
flb_plg_debug(ctx->ins, "using elastic cloud auth");
flb_http_basic_auth(c, ctx->cloud_user, ctx->cloud_passwd);
}
else if (ctx->cloud_apikey) {
flb_plg_debug(ctx->ins, "using elastic cloud apikey");
es_http_add_cloud_apikey(c, ctx->cloud_apikey);
}

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

/* Cloud Authentication */
/* Elastic Cloud Authentication */
{
FLB_CONFIG_MAP_STR, "cloud_id", NULL,
0, FLB_FALSE, 0,
Expand All @@ -1034,6 +1061,11 @@ static struct flb_config_map config_map[] = {
0, FLB_FALSE, 0,
"Elastic cloud authentication credentials"
},
{
FLB_CONFIG_MAP_STR, "cloud_apikey", NULL,
0, FLB_FALSE, 0,
"Elastic cloud apikey credentials"
},

/* AWS Authentication */
#ifdef FLB_HAVE_AWS
Expand Down
3 changes: 3 additions & 0 deletions plugins/out_es/es.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ struct flb_elasticsearch {
char *cloud_user;
char *cloud_passwd;

/* Elastic Cloud Apikey */
char *cloud_apikey;

/* AWS Auth */
#ifdef FLB_HAVE_AWS
int has_aws_auth;
Expand Down
7 changes: 7 additions & 0 deletions plugins/out_es/es_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ struct flb_elasticsearch *flb_es_conf_create(struct flb_output_instance *ins,
set_cloud_credentials(ctx, tmp);
}

/* handle cloud_apikey */
tmp = flb_output_get_property("cloud_apikey", ins);
if (tmp) {
ctx->cloud_apikey = flb_strdup(tmp);
}

/* use TLS ? */
if (ins->use_tls == FLB_TRUE) {
io_flags = FLB_IO_TLS;
Expand Down Expand Up @@ -531,6 +537,7 @@ int flb_es_conf_destroy(struct flb_elasticsearch *ctx)

flb_free(ctx->cloud_passwd);
flb_free(ctx->cloud_user);
flb_free(ctx->cloud_apikey);
flb_free(ctx);

return 0;
Expand Down
Loading