Coverage Report

Created: 2025-01-28 07:34

/src/fluent-bit/plugins/out_kinesis_streams/kinesis.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 <monkey/mk_core.h>
38
#include <msgpack.h>
39
#include <string.h>
40
#include <stdio.h>
41
42
#include "kinesis.h"
43
#include "kinesis_api.h"
44
45
static struct flb_aws_header content_type_header = {
46
    .key = "Content-Type",
47
    .key_len = 12,
48
    .val = "application/x-amz-json-1.1",
49
    .val_len = 26,
50
};
51
52
static int cb_kinesis_init(struct flb_output_instance *ins,
53
                              struct flb_config *config, void *data)
54
0
{
55
0
    const char *tmp;
56
0
    char *session_name = NULL;
57
0
    struct flb_kinesis *ctx = NULL;
58
0
    int ret;
59
0
    (void) config;
60
0
    (void) data;
61
62
0
    ctx = flb_calloc(1, sizeof(struct flb_kinesis));
63
0
    if (!ctx) {
64
0
        flb_errno();
65
0
        return -1;
66
0
    }
67
68
0
    ctx->ins = ins;
69
70
    /* Populate context with config map defaults and incoming properties */
71
0
    ret = flb_output_config_map_set(ins, (void *) ctx);
72
0
    if (ret == -1) {
73
0
        flb_plg_error(ctx->ins, "configuration error");
74
0
        goto error;
75
0
    }
76
77
0
    tmp = flb_output_get_property("stream", ins);
78
0
    if (tmp) {
79
0
        ctx->stream_name = tmp;
80
0
    } else {
81
0
        flb_plg_error(ctx->ins, "'stream' is a required field");
82
0
        goto error;
83
0
    }
84
85
0
    tmp = flb_output_get_property("time_key", ins);
86
0
    if (tmp) {
87
0
        ctx->time_key = tmp;
88
0
    }
89
90
0
    tmp = flb_output_get_property("time_key_format", ins);
91
0
    if (tmp) {
92
0
        ctx->time_key_format = tmp;
93
0
    } else {
94
0
        ctx->time_key_format = DEFAULT_TIME_KEY_FORMAT;
95
0
    }
96
97
0
    tmp = flb_output_get_property("log_key", ins);
98
0
    if (tmp) {
99
0
        ctx->log_key = tmp;
100
0
    }
101
102
0
    if (ctx->log_key && ctx->time_key) {
103
0
        flb_plg_error(ctx->ins, "'time_key' and 'log_key' can not be used together");
104
0
        goto error;
105
0
    }
106
107
0
    tmp = flb_output_get_property("endpoint", ins);
108
0
    if (tmp) {
109
0
        ctx->custom_endpoint = FLB_TRUE;
110
0
        ctx->endpoint = removeProtocol((char *) tmp, "https://");
111
0
    }
112
0
    else {
113
0
        ctx->custom_endpoint = FLB_FALSE;
114
0
    }
115
116
0
    tmp = flb_output_get_property("sts_endpoint", ins);
117
0
    if (tmp) {
118
0
        ctx->sts_endpoint = (char *) tmp;
119
0
    }
120
    /*
121
     * Sets the port number for the Kinesis output plugin.
122
     *
123
     * This function uses the port number already set in the output instance's host structure.
124
     * If the port is not set (0), the default HTTPS port is used.
125
     *
126
     * @param ins The output instance.
127
     * @param ctx The Kinesis output plugin context.
128
     */
129
0
    flb_plg_debug(ins, "Retrieved port from ins->host.port: %d", ins->host.port);
130
    
131
0
    if (ins->host.port >= FLB_KINESIS_MIN_PORT && ins->host.port <= FLB_KINESIS_MAX_PORT) {
132
0
        ctx->port = ins->host.port;
133
0
        flb_plg_debug(ins, "Setting port to: %d", ctx->port);
134
0
    }
135
0
    else if (ins->host.port == 0) {
136
0
        ctx->port = FLB_KINESIS_DEFAULT_HTTPS_PORT;
137
0
        flb_plg_debug(ins, "Port not set. Using default HTTPS port: %d", ctx->port);
138
0
    }
139
0
    else {
140
0
        flb_plg_error(ins, "Invalid port number: %d. Must be between %d and %d", 
141
0
                      ins->host.port, FLB_KINESIS_MIN_PORT, FLB_KINESIS_MAX_PORT);
142
0
        goto error;
143
0
    }
144
145
0
    tmp = flb_output_get_property("log_key", ins);
146
0
    if (tmp) {
147
0
        ctx->log_key = tmp;
148
0
    }
149
150
0
    tmp = flb_output_get_property("region", ins);
151
0
    if (tmp) {
152
0
        ctx->region = tmp;
153
0
    } else {
154
0
        flb_plg_error(ctx->ins, "'region' is a required field");
155
0
        goto error;
156
0
    }
157
158
0
    tmp = flb_output_get_property("role_arn", ins);
159
0
    if (tmp) {
160
0
        ctx->role_arn = tmp;
161
0
    }
162
163
    /* one tls instance for provider, one for cw client */
164
0
    ctx->cred_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
165
0
                                   FLB_TRUE,
166
0
                                   ins->tls_debug,
167
0
                                   ins->tls_vhost,
168
0
                                   ins->tls_ca_path,
169
0
                                   ins->tls_ca_file,
170
0
                                   ins->tls_crt_file,
171
0
                                   ins->tls_key_file,
172
0
                                   ins->tls_key_passwd);
173
174
0
    if (!ctx->cred_tls) {
175
0
        flb_plg_error(ctx->ins, "Failed to create tls context");
176
0
        goto error;
177
0
    }
178
179
0
    ctx->client_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
180
0
                                     FLB_TRUE,
181
0
                                     ins->tls_debug,
182
0
                                     ins->tls_vhost,
183
0
                                     ins->tls_ca_path,
184
0
                                     ins->tls_ca_file,
185
0
                                     ins->tls_crt_file,
186
0
                                     ins->tls_key_file,
187
0
                                     ins->tls_key_passwd);
188
0
    if (!ctx->client_tls) {
189
0
        flb_plg_error(ctx->ins, "Failed to create tls context");
190
0
        goto error;
191
0
    }
192
193
0
    ctx->aws_provider = flb_standard_chain_provider_create(config,
194
0
                                                           ctx->cred_tls,
195
0
                                                           (char *) ctx->region,
196
0
                                                           ctx->sts_endpoint,
197
0
                                                           NULL,
198
0
                                                           flb_aws_client_generator(),
199
0
                                                           ctx->profile);
200
0
    if (!ctx->aws_provider) {
201
0
        flb_plg_error(ctx->ins, "Failed to create AWS Credential Provider");
202
0
        goto error;
203
0
    }
204
205
0
    ctx->uuid = flb_sts_session_name();
206
0
    if (!ctx->uuid) {
207
0
        flb_plg_error(ctx->ins,
208
0
                      "Failed to generate plugin instance UUID");
209
0
        goto error;
210
0
    }
211
212
0
    if(ctx->role_arn) {
213
        /* set up sts assume role provider */
214
0
        session_name = flb_sts_session_name();
215
0
        if (!session_name) {
216
0
            flb_plg_error(ctx->ins,
217
0
                          "Failed to generate random STS session name");
218
0
            goto error;
219
0
        }
220
221
        /* STS provider needs yet another separate TLS instance */
222
0
        ctx->sts_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
223
0
                                      FLB_TRUE,
224
0
                                      ins->tls_debug,
225
0
                                      ins->tls_vhost,
226
0
                                      ins->tls_ca_path,
227
0
                                      ins->tls_ca_file,
228
0
                                      ins->tls_crt_file,
229
0
                                      ins->tls_key_file,
230
0
                                      ins->tls_key_passwd);
231
0
        if (!ctx->sts_tls) {
232
0
            flb_errno();
233
0
            goto error;
234
0
        }
235
236
0
        ctx->base_aws_provider = ctx->aws_provider;
237
238
0
        ctx->aws_provider = flb_sts_provider_create(config,
239
0
                                                    ctx->sts_tls,
240
0
                                                    ctx->base_aws_provider,
241
0
                                                    (char *) ctx->external_id,
242
0
                                                    (char *) ctx->role_arn,
243
0
                                                    session_name,
244
0
                                                    (char *) ctx->region,
245
0
                                                    ctx->sts_endpoint,
246
0
                                                    NULL,
247
0
                                                    flb_aws_client_generator());
248
0
        if (!ctx->aws_provider) {
249
0
            flb_plg_error(ctx->ins,
250
0
                          "Failed to create AWS STS Credential Provider");
251
0
            goto error;
252
0
        }
253
        /* session name can freed after provider is created */
254
0
        flb_free(session_name);
255
0
        session_name = NULL;
256
0
    }
257
258
    /* initialize credentials and set to sync mode */
259
0
    ctx->aws_provider->provider_vtable->sync(ctx->aws_provider);
260
0
    ctx->aws_provider->provider_vtable->init(ctx->aws_provider);
261
0
    ctx->aws_provider->provider_vtable->upstream_set(ctx->aws_provider, ctx->ins);
262
263
0
    if (ctx->endpoint == NULL) {
264
0
        ctx->endpoint = flb_aws_endpoint("kinesis", (char *) ctx->region);
265
0
        if (!ctx->endpoint) {
266
0
            goto error;
267
0
        }
268
0
    }
269
270
0
    struct flb_aws_client_generator *generator = flb_aws_client_generator();
271
0
    ctx->kinesis_client = generator->create();
272
0
    if (!ctx->kinesis_client) {
273
0
        goto error;
274
0
    }
275
0
    ctx->kinesis_client->name = "kinesis_client";
276
0
    ctx->kinesis_client->has_auth = FLB_TRUE;
277
0
    ctx->kinesis_client->provider = ctx->aws_provider;
278
0
    ctx->kinesis_client->region = (char *) ctx->region;
279
0
    ctx->kinesis_client->retry_requests = ctx->retry_requests;
280
0
    ctx->kinesis_client->service = "kinesis";
281
0
    ctx->kinesis_client->port = ctx->port;
282
0
    ctx->kinesis_client->flags = 0;
283
0
    ctx->kinesis_client->proxy = NULL;
284
0
    ctx->kinesis_client->static_headers = &content_type_header;
285
0
    ctx->kinesis_client->static_headers_len = 1;
286
287
0
    struct flb_upstream *upstream = flb_upstream_create(config, ctx->endpoint,
288
0
                                                        ctx->port, FLB_IO_TLS,
289
0
                                                        ctx->client_tls);
290
0
    if (!upstream) {
291
0
        flb_plg_error(ctx->ins, "Connection initialization error");
292
0
        goto error;
293
0
    }
294
295
0
    ctx->kinesis_client->upstream = upstream;
296
0
    flb_output_upstream_set(upstream, ctx->ins);
297
298
0
    ctx->kinesis_client->host = ctx->endpoint;
299
300
    /* Export context */
301
0
    flb_output_set_context(ins, ctx);
302
303
0
    return 0;
304
305
0
error:
306
0
    flb_free(session_name);
307
0
    flb_plg_error(ctx->ins, "Initialization failed");
308
0
    flb_kinesis_ctx_destroy(ctx);
309
0
    return -1;
310
0
}
311
312
static struct flush *new_flush_buffer(const char *tag, int tag_len)
313
0
{
314
0
    struct flush *buf;
315
316
317
0
    buf = flb_calloc(1, sizeof(struct flush));
318
0
    if (!buf) {
319
0
        flb_errno();
320
0
        return NULL;
321
0
    }
322
323
0
    buf->tmp_buf = flb_malloc(sizeof(char) * PUT_RECORDS_PAYLOAD_SIZE);
324
0
    if (!buf->tmp_buf) {
325
0
        flb_errno();
326
0
        kinesis_flush_destroy(buf);
327
0
        return NULL;
328
0
    }
329
0
    buf->tmp_buf_size = PUT_RECORDS_PAYLOAD_SIZE;
330
331
0
    buf->events = flb_malloc(sizeof(struct kinesis_event) * MAX_EVENTS_PER_PUT);
332
0
    if (!buf->events) {
333
0
        flb_errno();
334
0
        kinesis_flush_destroy(buf);
335
0
        return NULL;
336
0
    }
337
0
    buf->events_capacity = MAX_EVENTS_PER_PUT;
338
    
339
0
    buf->tag = tag;
340
0
    buf->tag_len = tag_len;
341
342
0
    return buf;
343
0
}
344
345
static void cb_kinesis_flush(struct flb_event_chunk *event_chunk,
346
                             struct flb_output_flush *out_flush,
347
                             struct flb_input_instance *i_ins,
348
                             void *out_context,
349
                             struct flb_config *config)
350
0
{
351
0
    struct flb_kinesis *ctx = out_context;
352
0
    int ret;
353
0
    struct flush *buf;
354
0
    (void) i_ins;
355
0
    (void) config;
356
357
0
    buf = new_flush_buffer(event_chunk->tag, flb_sds_len(event_chunk->tag));
358
0
    if (!buf) {
359
0
        flb_plg_error(ctx->ins, "Failed to construct flush buffer");
360
0
        FLB_OUTPUT_RETURN(FLB_RETRY);
361
0
    }
362
363
0
    ret = process_and_send_to_kinesis(ctx, buf,
364
0
                                      event_chunk->data,
365
0
                                      event_chunk->size);
366
0
    if (ret < 0) {
367
0
        flb_plg_error(ctx->ins, "Failed to send records to kinesis");
368
0
        kinesis_flush_destroy(buf);
369
0
        FLB_OUTPUT_RETURN(FLB_RETRY);
370
0
    }
371
372
0
    flb_plg_debug(ctx->ins, "Processed %d records, sent %d to %s",
373
0
                 buf->records_processed, buf->records_sent, ctx->stream_name);
374
0
    kinesis_flush_destroy(buf);
375
376
0
    FLB_OUTPUT_RETURN(FLB_OK);
377
0
}
378
379
void flb_kinesis_ctx_destroy(struct flb_kinesis *ctx)
380
0
{
381
0
    if (ctx != NULL) {
382
0
        if (ctx->base_aws_provider) {
383
0
            flb_aws_provider_destroy(ctx->base_aws_provider);
384
0
        }
385
386
0
        if (ctx->aws_provider) {
387
0
            flb_aws_provider_destroy(ctx->aws_provider);
388
0
        }
389
390
0
        if (ctx->cred_tls) {
391
0
            flb_tls_destroy(ctx->cred_tls);
392
0
        }
393
394
0
        if (ctx->sts_tls) {
395
0
            flb_tls_destroy(ctx->sts_tls);
396
0
        }
397
398
0
        if (ctx->client_tls) {
399
0
            flb_tls_destroy(ctx->client_tls);
400
0
        }
401
402
0
        if (ctx->kinesis_client) {
403
0
            flb_aws_client_destroy(ctx->kinesis_client);
404
0
        }
405
406
0
        if (ctx->custom_endpoint == FLB_FALSE) {
407
0
            flb_free(ctx->endpoint);
408
0
        }
409
410
0
        if (ctx->uuid) {
411
0
            flb_free(ctx->uuid);
412
0
        }
413
414
0
        flb_free(ctx);
415
0
    }
416
0
}
417
418
static int cb_kinesis_exit(void *data, struct flb_config *config)
419
0
{
420
0
    struct flb_kinesis *ctx = data;
421
422
0
    flb_kinesis_ctx_destroy(ctx);
423
0
    return 0;
424
0
}
425
426
/* Configuration properties map */
427
static struct flb_config_map config_map[] = {
428
    {
429
     FLB_CONFIG_MAP_STR, "region", NULL,
430
     0, FLB_TRUE, offsetof(struct flb_kinesis, region),
431
     "The AWS region of your kinesis stream"
432
    },
433
434
    {
435
     FLB_CONFIG_MAP_STR, "stream", NULL,
436
     0, FLB_TRUE, offsetof(struct flb_kinesis, stream_name),
437
     "Kinesis stream name"
438
    },
439
440
    {
441
     FLB_CONFIG_MAP_STR, "time_key", NULL,
442
     0, FLB_TRUE, offsetof(struct flb_kinesis, time_key),
443
     "Add the timestamp to the record under this key. By default the timestamp "
444
     "from Fluent Bit will not be added to records sent to Kinesis."
445
    },
446
447
    {
448
     FLB_CONFIG_MAP_STR, "time_key_format", NULL,
449
     0, FLB_TRUE, offsetof(struct flb_kinesis, time_key_format),
450
     "strftime compliant format string for the timestamp; for example, "
451
     "the default is '%Y-%m-%dT%H:%M:%S'. This option is used with time_key. "
452
    },
453
454
    {
455
     FLB_CONFIG_MAP_STR, "role_arn", NULL,
456
     0, FLB_TRUE, offsetof(struct flb_kinesis, role_arn),
457
     "ARN of an IAM role to assume (ex. for cross account access)."
458
    },
459
460
    {
461
     FLB_CONFIG_MAP_STR, "endpoint", NULL,
462
     0, FLB_FALSE, 0,
463
     "Specify a custom endpoint for the Kinesis API"
464
    },
465
466
    {
467
     FLB_CONFIG_MAP_STR, "sts_endpoint", NULL,
468
     0, FLB_TRUE, offsetof(struct flb_kinesis, sts_endpoint),
469
    "Custom endpoint for the STS API."
470
    },
471
472
    {
473
     FLB_CONFIG_MAP_STR, "external_id", NULL,
474
     0, FLB_TRUE, offsetof(struct flb_kinesis, external_id),
475
     "Specify an external ID for the STS API, can be used with the role_arn parameter if your role "
476
     "requires an external ID."
477
    },
478
479
    {
480
     FLB_CONFIG_MAP_STR, "log_key", NULL,
481
     0, FLB_TRUE, offsetof(struct flb_kinesis, log_key),
482
     "By default, the whole log record will be sent to Kinesis. "
483
     "If you specify a key name with this option, then only the value of "
484
     "that key will be sent to Kinesis. For example, if you are using "
485
     "the Fluentd Docker log driver, you can specify `log_key log` and only "
486
     "the log message will be sent to Kinesis."
487
    },
488
489
    {
490
     FLB_CONFIG_MAP_BOOL, "auto_retry_requests", "true",
491
     0, FLB_TRUE, offsetof(struct flb_kinesis, retry_requests),
492
     "Immediately retry failed requests to AWS services once. This option "
493
     "does not affect the normal Fluent Bit retry mechanism with backoff. "
494
     "Instead, it enables an immediate retry with no delay for networking "
495
     "errors, which may help improve throughput when there are transient/random "
496
     "networking issues."
497
    },
498
499
    {
500
     FLB_CONFIG_MAP_STR, "profile", NULL,
501
     0, FLB_TRUE, offsetof(struct flb_kinesis, profile),
502
     "AWS Profile name. AWS Profiles can be configured with AWS CLI and are usually stored in "
503
     "$HOME/.aws/ directory."
504
    },
505
506
    /* EOF */
507
    {0}
508
};
509
510
/* Plugin registration */
511
struct flb_output_plugin out_kinesis_streams_plugin = {
512
    .name         = "kinesis_streams",
513
    .description  = "Send logs to Amazon Kinesis Streams",
514
    .cb_init      = cb_kinesis_init,
515
    .cb_flush     = cb_kinesis_flush,
516
    .cb_exit      = cb_kinesis_exit,
517
    .workers      = 1,
518
    .flags        = 0,
519
520
    /* Configuration */
521
    .config_map     = config_map,
522
};