/src/fluent-bit/plugins/out_kinesis_firehose/firehose.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2024 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_compat.h> |
21 | | #include <fluent-bit/flb_info.h> |
22 | | #include <fluent-bit/flb_output.h> |
23 | | #include <fluent-bit/flb_utils.h> |
24 | | #include <fluent-bit/flb_slist.h> |
25 | | #include <fluent-bit/flb_time.h> |
26 | | #include <fluent-bit/flb_pack.h> |
27 | | #include <fluent-bit/flb_config_map.h> |
28 | | #include <fluent-bit/flb_output_plugin.h> |
29 | | |
30 | | #include <fluent-bit/flb_sds.h> |
31 | | #include <fluent-bit/flb_aws_credentials.h> |
32 | | #include <fluent-bit/flb_aws_util.h> |
33 | | #include <fluent-bit/flb_mem.h> |
34 | | #include <fluent-bit/flb_http_client.h> |
35 | | #include <fluent-bit/flb_utils.h> |
36 | | |
37 | | #include <fluent-bit/aws/flb_aws_compress.h> |
38 | | |
39 | | #include <monkey/mk_core.h> |
40 | | #include <msgpack.h> |
41 | | #include <string.h> |
42 | | #include <stdio.h> |
43 | | |
44 | | #include "firehose.h" |
45 | | #include "firehose_api.h" |
46 | | |
47 | | static struct flb_aws_header content_type_header = { |
48 | | .key = "Content-Type", |
49 | | .key_len = 12, |
50 | | .val = "application/x-amz-json-1.1", |
51 | | .val_len = 26, |
52 | | }; |
53 | | |
54 | | static int cb_firehose_init(struct flb_output_instance *ins, |
55 | | struct flb_config *config, void *data) |
56 | 0 | { |
57 | 0 | const char *tmp; |
58 | 0 | char *session_name = NULL; |
59 | 0 | struct flb_firehose *ctx = NULL; |
60 | 0 | int ret; |
61 | 0 | (void) config; |
62 | 0 | (void) data; |
63 | |
|
64 | 0 | ctx = flb_calloc(1, sizeof(struct flb_firehose)); |
65 | 0 | if (!ctx) { |
66 | 0 | flb_errno(); |
67 | 0 | return -1; |
68 | 0 | } |
69 | | |
70 | 0 | ctx->ins = ins; |
71 | | |
72 | | /* Populate context with config map defaults and incoming properties */ |
73 | 0 | ret = flb_output_config_map_set(ins, (void *) ctx); |
74 | 0 | if (ret == -1) { |
75 | 0 | flb_plg_error(ctx->ins, "configuration error"); |
76 | 0 | goto error; |
77 | 0 | } |
78 | | |
79 | 0 | tmp = flb_output_get_property("delivery_stream", ins); |
80 | 0 | if (tmp) { |
81 | 0 | ctx->delivery_stream = tmp; |
82 | 0 | } else { |
83 | 0 | flb_plg_error(ctx->ins, "'delivery_stream' is a required field"); |
84 | 0 | goto error; |
85 | 0 | } |
86 | | |
87 | 0 | tmp = flb_output_get_property("time_key", ins); |
88 | 0 | if (tmp) { |
89 | 0 | ctx->time_key = tmp; |
90 | 0 | } |
91 | |
|
92 | 0 | tmp = flb_output_get_property("time_key_format", ins); |
93 | 0 | if (tmp) { |
94 | 0 | ctx->time_key_format = tmp; |
95 | 0 | } else { |
96 | 0 | ctx->time_key_format = DEFAULT_TIME_KEY_FORMAT; |
97 | 0 | } |
98 | |
|
99 | 0 | tmp = flb_output_get_property("log_key", ins); |
100 | 0 | if (tmp) { |
101 | 0 | ctx->log_key = tmp; |
102 | 0 | } |
103 | |
|
104 | 0 | if (ctx->log_key && ctx->time_key) { |
105 | 0 | flb_plg_error(ctx->ins, "'time_key' and 'log_key' can not be used together"); |
106 | 0 | goto error; |
107 | 0 | } |
108 | | |
109 | 0 | tmp = flb_output_get_property("endpoint", ins); |
110 | 0 | if (tmp) { |
111 | 0 | ctx->custom_endpoint = FLB_TRUE; |
112 | 0 | ctx->endpoint = removeProtocol((char *) tmp, "https://"); |
113 | 0 | } |
114 | 0 | else { |
115 | 0 | ctx->custom_endpoint = FLB_FALSE; |
116 | 0 | } |
117 | |
|
118 | 0 | tmp = flb_output_get_property("sts_endpoint", ins); |
119 | 0 | if (tmp) { |
120 | 0 | ctx->sts_endpoint = (char *) tmp; |
121 | 0 | } |
122 | |
|
123 | 0 | tmp = flb_output_get_property("compression", ins); |
124 | 0 | if (tmp) { |
125 | 0 | ret = flb_aws_compression_get_type(tmp); |
126 | 0 | if (ret == -1) { |
127 | 0 | flb_plg_error(ctx->ins, "unknown compression: %s", tmp); |
128 | 0 | goto error; |
129 | 0 | } |
130 | 0 | ctx->compression = ret; |
131 | 0 | } |
132 | | |
133 | 0 | tmp = flb_output_get_property("log_key", ins); |
134 | 0 | if (tmp) { |
135 | 0 | ctx->log_key = tmp; |
136 | 0 | } |
137 | |
|
138 | 0 | tmp = flb_output_get_property("region", ins); |
139 | 0 | if (tmp) { |
140 | 0 | ctx->region = tmp; |
141 | 0 | } else { |
142 | 0 | flb_plg_error(ctx->ins, "'region' is a required field"); |
143 | 0 | goto error; |
144 | 0 | } |
145 | | |
146 | 0 | tmp = flb_output_get_property("role_arn", ins); |
147 | 0 | if (tmp) { |
148 | 0 | ctx->role_arn = tmp; |
149 | 0 | } |
150 | | |
151 | | /* one tls instance for provider, one for cw client */ |
152 | 0 | ctx->cred_tls = flb_tls_create(FLB_TLS_CLIENT_MODE, |
153 | 0 | FLB_TRUE, |
154 | 0 | ins->tls_debug, |
155 | 0 | ins->tls_vhost, |
156 | 0 | ins->tls_ca_path, |
157 | 0 | ins->tls_ca_file, |
158 | 0 | ins->tls_crt_file, |
159 | 0 | ins->tls_key_file, |
160 | 0 | ins->tls_key_passwd); |
161 | |
|
162 | 0 | if (!ctx->cred_tls) { |
163 | 0 | flb_plg_error(ctx->ins, "Failed to create tls context"); |
164 | 0 | goto error; |
165 | 0 | } |
166 | | |
167 | 0 | ctx->client_tls = flb_tls_create(FLB_TLS_CLIENT_MODE, |
168 | 0 | FLB_TRUE, |
169 | 0 | ins->tls_debug, |
170 | 0 | ins->tls_vhost, |
171 | 0 | ins->tls_ca_path, |
172 | 0 | ins->tls_ca_file, |
173 | 0 | ins->tls_crt_file, |
174 | 0 | ins->tls_key_file, |
175 | 0 | ins->tls_key_passwd); |
176 | 0 | if (!ctx->client_tls) { |
177 | 0 | flb_plg_error(ctx->ins, "Failed to create tls context"); |
178 | 0 | goto error; |
179 | 0 | } |
180 | | |
181 | 0 | ctx->aws_provider = flb_standard_chain_provider_create(config, |
182 | 0 | ctx->cred_tls, |
183 | 0 | (char *) ctx->region, |
184 | 0 | ctx->sts_endpoint, |
185 | 0 | NULL, |
186 | 0 | flb_aws_client_generator(), |
187 | 0 | ctx->profile); |
188 | 0 | if (!ctx->aws_provider) { |
189 | 0 | flb_plg_error(ctx->ins, "Failed to create AWS Credential Provider"); |
190 | 0 | goto error; |
191 | 0 | } |
192 | | |
193 | 0 | if(ctx->role_arn) { |
194 | | /* set up sts assume role provider */ |
195 | 0 | session_name = flb_sts_session_name(); |
196 | 0 | if (!session_name) { |
197 | 0 | flb_plg_error(ctx->ins, |
198 | 0 | "Failed to generate random STS session name"); |
199 | 0 | goto error; |
200 | 0 | } |
201 | | |
202 | | /* STS provider needs yet another separate TLS instance */ |
203 | 0 | ctx->sts_tls = flb_tls_create(FLB_TLS_CLIENT_MODE, |
204 | 0 | FLB_TRUE, |
205 | 0 | ins->tls_debug, |
206 | 0 | ins->tls_vhost, |
207 | 0 | ins->tls_ca_path, |
208 | 0 | ins->tls_ca_file, |
209 | 0 | ins->tls_crt_file, |
210 | 0 | ins->tls_key_file, |
211 | 0 | ins->tls_key_passwd); |
212 | 0 | if (!ctx->sts_tls) { |
213 | 0 | flb_errno(); |
214 | 0 | goto error; |
215 | 0 | } |
216 | | |
217 | 0 | ctx->base_aws_provider = ctx->aws_provider; |
218 | |
|
219 | 0 | ctx->aws_provider = flb_sts_provider_create(config, |
220 | 0 | ctx->sts_tls, |
221 | 0 | ctx->base_aws_provider, |
222 | 0 | (char *) ctx->external_id, |
223 | 0 | (char *) ctx->role_arn, |
224 | 0 | session_name, |
225 | 0 | (char *) ctx->region, |
226 | 0 | ctx->sts_endpoint, |
227 | 0 | NULL, |
228 | 0 | flb_aws_client_generator()); |
229 | 0 | if (!ctx->aws_provider) { |
230 | 0 | flb_plg_error(ctx->ins, |
231 | 0 | "Failed to create AWS STS Credential Provider"); |
232 | 0 | goto error; |
233 | 0 | } |
234 | | /* session name can freed after provider is created */ |
235 | 0 | flb_free(session_name); |
236 | 0 | session_name = NULL; |
237 | 0 | } |
238 | | |
239 | | /* initialize credentials and set to sync mode */ |
240 | 0 | ctx->aws_provider->provider_vtable->sync(ctx->aws_provider); |
241 | 0 | ctx->aws_provider->provider_vtable->init(ctx->aws_provider); |
242 | 0 | ctx->aws_provider->provider_vtable->upstream_set(ctx->aws_provider, ctx->ins); |
243 | |
|
244 | 0 | if (ctx->endpoint == NULL) { |
245 | 0 | ctx->endpoint = flb_aws_endpoint("firehose", (char *) ctx->region); |
246 | 0 | if (!ctx->endpoint) { |
247 | 0 | goto error; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | 0 | struct flb_aws_client_generator *generator = flb_aws_client_generator(); |
252 | 0 | ctx->firehose_client = generator->create(); |
253 | 0 | if (!ctx->firehose_client) { |
254 | 0 | goto error; |
255 | 0 | } |
256 | 0 | ctx->firehose_client->name = "firehose_client"; |
257 | 0 | ctx->firehose_client->has_auth = FLB_TRUE; |
258 | 0 | ctx->firehose_client->provider = ctx->aws_provider; |
259 | 0 | ctx->firehose_client->region = (char *) ctx->region; |
260 | 0 | ctx->firehose_client->retry_requests = ctx->retry_requests; |
261 | 0 | ctx->firehose_client->service = "firehose"; |
262 | 0 | ctx->firehose_client->port = 443; |
263 | 0 | ctx->firehose_client->flags = 0; |
264 | 0 | ctx->firehose_client->proxy = NULL; |
265 | 0 | ctx->firehose_client->static_headers = &content_type_header; |
266 | 0 | ctx->firehose_client->static_headers_len = 1; |
267 | |
|
268 | 0 | struct flb_upstream *upstream = flb_upstream_create(config, ctx->endpoint, |
269 | 0 | 443, FLB_IO_TLS, |
270 | 0 | ctx->client_tls); |
271 | 0 | if (!upstream) { |
272 | 0 | flb_plg_error(ctx->ins, "Connection initialization error"); |
273 | 0 | goto error; |
274 | 0 | } |
275 | | |
276 | 0 | ctx->firehose_client->upstream = upstream; |
277 | 0 | flb_output_upstream_set(upstream, ctx->ins); |
278 | |
|
279 | 0 | ctx->firehose_client->host = ctx->endpoint; |
280 | | |
281 | | /* Export context */ |
282 | 0 | flb_output_set_context(ins, ctx); |
283 | |
|
284 | 0 | return 0; |
285 | | |
286 | 0 | error: |
287 | 0 | flb_free(session_name); |
288 | 0 | flb_plg_error(ctx->ins, "Initialization failed"); |
289 | 0 | flb_firehose_ctx_destroy(ctx); |
290 | 0 | return -1; |
291 | 0 | } |
292 | | |
293 | | struct flush *new_flush_buffer() |
294 | 0 | { |
295 | 0 | struct flush *buf; |
296 | | |
297 | |
|
298 | 0 | buf = flb_calloc(1, sizeof(struct flush)); |
299 | 0 | if (!buf) { |
300 | 0 | flb_errno(); |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | 0 | buf->tmp_buf = flb_malloc(sizeof(char) * PUT_RECORD_BATCH_PAYLOAD_SIZE); |
305 | 0 | if (!buf->tmp_buf) { |
306 | 0 | flb_errno(); |
307 | 0 | flush_destroy(buf); |
308 | 0 | return NULL; |
309 | 0 | } |
310 | 0 | buf->tmp_buf_size = PUT_RECORD_BATCH_PAYLOAD_SIZE; |
311 | |
|
312 | 0 | buf->events = flb_malloc(sizeof(struct firehose_event) * MAX_EVENTS_PER_PUT); |
313 | 0 | if (!buf->events) { |
314 | 0 | flb_errno(); |
315 | 0 | flush_destroy(buf); |
316 | 0 | return NULL; |
317 | 0 | } |
318 | 0 | buf->events_capacity = MAX_EVENTS_PER_PUT; |
319 | |
|
320 | 0 | return buf; |
321 | 0 | } |
322 | | |
323 | | static void cb_firehose_flush(struct flb_event_chunk *event_chunk, |
324 | | struct flb_output_flush *out_flush, |
325 | | struct flb_input_instance *i_ins, |
326 | | void *out_context, |
327 | | struct flb_config *config) |
328 | 0 | { |
329 | 0 | struct flb_firehose *ctx = out_context; |
330 | 0 | int ret; |
331 | 0 | struct flush *buf; |
332 | 0 | (void) i_ins; |
333 | 0 | (void) config; |
334 | |
|
335 | 0 | buf = new_flush_buffer(); |
336 | 0 | if (!buf) { |
337 | 0 | flb_plg_error(ctx->ins, "Failed to construct flush buffer"); |
338 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
339 | 0 | } |
340 | | |
341 | 0 | ret = process_and_send_records(ctx, buf, |
342 | 0 | event_chunk->data, event_chunk->size); |
343 | 0 | if (ret < 0) { |
344 | 0 | flb_plg_error(ctx->ins, "Failed to send records"); |
345 | 0 | flush_destroy(buf); |
346 | 0 | FLB_OUTPUT_RETURN(FLB_RETRY); |
347 | 0 | } |
348 | | |
349 | 0 | flb_plg_debug(ctx->ins, "Processed %d records, sent %d to %s", |
350 | 0 | buf->records_processed, buf->records_sent, ctx->delivery_stream); |
351 | 0 | flush_destroy(buf); |
352 | |
|
353 | 0 | FLB_OUTPUT_RETURN(FLB_OK); |
354 | 0 | } |
355 | | |
356 | | void flb_firehose_ctx_destroy(struct flb_firehose *ctx) |
357 | 0 | { |
358 | 0 | if (ctx != NULL) { |
359 | 0 | if (ctx->base_aws_provider) { |
360 | 0 | flb_aws_provider_destroy(ctx->base_aws_provider); |
361 | 0 | } |
362 | |
|
363 | 0 | if (ctx->aws_provider) { |
364 | 0 | flb_aws_provider_destroy(ctx->aws_provider); |
365 | 0 | } |
366 | |
|
367 | 0 | if (ctx->cred_tls) { |
368 | 0 | flb_tls_destroy(ctx->cred_tls); |
369 | 0 | } |
370 | |
|
371 | 0 | if (ctx->sts_tls) { |
372 | 0 | flb_tls_destroy(ctx->sts_tls); |
373 | 0 | } |
374 | |
|
375 | 0 | if (ctx->client_tls) { |
376 | 0 | flb_tls_destroy(ctx->client_tls); |
377 | 0 | } |
378 | |
|
379 | 0 | if (ctx->firehose_client) { |
380 | 0 | flb_aws_client_destroy(ctx->firehose_client); |
381 | 0 | } |
382 | |
|
383 | 0 | if (ctx->custom_endpoint == FLB_FALSE) { |
384 | 0 | flb_free(ctx->endpoint); |
385 | 0 | } |
386 | |
|
387 | 0 | flb_free(ctx); |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | static int cb_firehose_exit(void *data, struct flb_config *config) |
392 | 0 | { |
393 | 0 | struct flb_firehose *ctx = data; |
394 | |
|
395 | 0 | flb_firehose_ctx_destroy(ctx); |
396 | 0 | return 0; |
397 | 0 | } |
398 | | |
399 | | /* Configuration properties map */ |
400 | | static struct flb_config_map config_map[] = { |
401 | | { |
402 | | FLB_CONFIG_MAP_STR, "region", NULL, |
403 | | 0, FLB_TRUE, offsetof(struct flb_firehose, region), |
404 | | "The AWS region of your delivery stream" |
405 | | }, |
406 | | |
407 | | { |
408 | | FLB_CONFIG_MAP_STR, "delivery_stream", NULL, |
409 | | 0, FLB_TRUE, offsetof(struct flb_firehose, delivery_stream), |
410 | | "Firehose delivery stream name" |
411 | | }, |
412 | | |
413 | | { |
414 | | FLB_CONFIG_MAP_STR, "time_key", NULL, |
415 | | 0, FLB_TRUE, offsetof(struct flb_firehose, time_key), |
416 | | "Add the timestamp to the record under this key. By default the timestamp " |
417 | | "from Fluent Bit will not be added to records sent to Kinesis." |
418 | | }, |
419 | | |
420 | | { |
421 | | FLB_CONFIG_MAP_STR, "time_key_format", NULL, |
422 | | 0, FLB_TRUE, offsetof(struct flb_firehose, time_key_format), |
423 | | "strftime compliant format string for the timestamp; for example, " |
424 | | "the default is '%Y-%m-%dT%H:%M:%S'. This option is used with time_key. " |
425 | | }, |
426 | | |
427 | | { |
428 | | FLB_CONFIG_MAP_STR, "role_arn", NULL, |
429 | | 0, FLB_TRUE, offsetof(struct flb_firehose, role_arn), |
430 | | "ARN of an IAM role to assume (ex. for cross account access)." |
431 | | }, |
432 | | |
433 | | { |
434 | | FLB_CONFIG_MAP_STR, "endpoint", NULL, |
435 | | 0, FLB_FALSE, 0, |
436 | | "Specify a custom endpoint for the Firehose API" |
437 | | }, |
438 | | |
439 | | { |
440 | | FLB_CONFIG_MAP_STR, "sts_endpoint", NULL, |
441 | | 0, FLB_TRUE, offsetof(struct flb_firehose, sts_endpoint), |
442 | | "Custom endpoint for the STS API." |
443 | | }, |
444 | | |
445 | | { |
446 | | FLB_CONFIG_MAP_STR, "external_id", NULL, |
447 | | 0, FLB_TRUE, offsetof(struct flb_firehose, external_id), |
448 | | "Specify an external ID for the STS API, can be used with the role_arn parameter if your role " |
449 | | "requires an external ID." |
450 | | }, |
451 | | |
452 | | { |
453 | | FLB_CONFIG_MAP_STR, "compression", NULL, |
454 | | 0, FLB_FALSE, 0, |
455 | | "Compression type for Firehose records. Each log record is individually compressed " |
456 | | "and sent to Firehose. 'gzip' and 'arrow' are the supported values. " |
457 | | "'arrow' is only an available if Apache Arrow was enabled at compile time. " |
458 | | "Defaults to no compression." |
459 | | }, |
460 | | |
461 | | { |
462 | | FLB_CONFIG_MAP_STR, "log_key", NULL, |
463 | | 0, FLB_TRUE, offsetof(struct flb_firehose, log_key), |
464 | | "By default, the whole log record will be sent to Firehose. " |
465 | | "If you specify a key name with this option, then only the value of " |
466 | | "that key will be sent to Firehose. For example, if you are using " |
467 | | "the Fluentd Docker log driver, you can specify `log_key log` and only " |
468 | | "the log message will be sent to Firehose." |
469 | | }, |
470 | | |
471 | | { |
472 | | FLB_CONFIG_MAP_BOOL, "auto_retry_requests", "true", |
473 | | 0, FLB_TRUE, offsetof(struct flb_firehose, retry_requests), |
474 | | "Immediately retry failed requests to AWS services once. This option " |
475 | | "does not affect the normal Fluent Bit retry mechanism with backoff. " |
476 | | "Instead, it enables an immediate retry with no delay for networking " |
477 | | "errors, which may help improve throughput when there are transient/random " |
478 | | "networking issues." |
479 | | }, |
480 | | |
481 | | { |
482 | | FLB_CONFIG_MAP_STR, "profile", NULL, |
483 | | 0, FLB_TRUE, offsetof(struct flb_firehose, profile), |
484 | | "AWS Profile name. AWS Profiles can be configured with AWS CLI and are usually stored in " |
485 | | "$HOME/.aws/ directory." |
486 | | }, |
487 | | /* EOF */ |
488 | | {0} |
489 | | }; |
490 | | |
491 | | /* Plugin registration */ |
492 | | struct flb_output_plugin out_kinesis_firehose_plugin = { |
493 | | .name = "kinesis_firehose", |
494 | | .description = "Send logs to Amazon Kinesis Firehose", |
495 | | .cb_init = cb_firehose_init, |
496 | | .cb_flush = cb_firehose_flush, |
497 | | .cb_exit = cb_firehose_exit, |
498 | | .workers = 1, |
499 | | .flags = 0, |
500 | | |
501 | | /* Configuration */ |
502 | | .config_map = config_map, |
503 | | }; |