Coverage Report

Created: 2023-03-10 07:33

/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-2022 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
122
0
    tmp = flb_output_get_property("log_key", ins);
123
0
    if (tmp) {
124
0
        ctx->log_key = tmp;
125
0
    }
126
127
0
    tmp = flb_output_get_property("region", ins);
128
0
    if (tmp) {
129
0
        ctx->region = tmp;
130
0
    } else {
131
0
        flb_plg_error(ctx->ins, "'region' is a required field");
132
0
        goto error;
133
0
    }
134
135
0
    tmp = flb_output_get_property("role_arn", ins);
136
0
    if (tmp) {
137
0
        ctx->role_arn = tmp;
138
0
    }
139
140
    /* one tls instance for provider, one for cw client */
141
0
    ctx->cred_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
142
0
                                   FLB_TRUE,
143
0
                                   ins->tls_debug,
144
0
                                   ins->tls_vhost,
145
0
                                   ins->tls_ca_path,
146
0
                                   ins->tls_ca_file,
147
0
                                   ins->tls_crt_file,
148
0
                                   ins->tls_key_file,
149
0
                                   ins->tls_key_passwd);
150
151
0
    if (!ctx->cred_tls) {
152
0
        flb_plg_error(ctx->ins, "Failed to create tls context");
153
0
        goto error;
154
0
    }
155
156
0
    ctx->client_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
157
0
                                     FLB_TRUE,
158
0
                                     ins->tls_debug,
159
0
                                     ins->tls_vhost,
160
0
                                     ins->tls_ca_path,
161
0
                                     ins->tls_ca_file,
162
0
                                     ins->tls_crt_file,
163
0
                                     ins->tls_key_file,
164
0
                                     ins->tls_key_passwd);
165
0
    if (!ctx->client_tls) {
166
0
        flb_plg_error(ctx->ins, "Failed to create tls context");
167
0
        goto error;
168
0
    }
169
170
0
    ctx->aws_provider = flb_standard_chain_provider_create(config,
171
0
                                                           ctx->cred_tls,
172
0
                                                           (char *) ctx->region,
173
0
                                                           ctx->sts_endpoint,
174
0
                                                           NULL,
175
0
                                                           flb_aws_client_generator());
176
0
    if (!ctx->aws_provider) {
177
0
        flb_plg_error(ctx->ins, "Failed to create AWS Credential Provider");
178
0
        goto error;
179
0
    }
180
181
0
    ctx->uuid = flb_sts_session_name();
182
0
    if (!ctx->uuid) {
183
0
        flb_plg_error(ctx->ins,
184
0
                      "Failed to generate plugin instance UUID");
185
0
        goto error;
186
0
    }
187
188
0
    if(ctx->role_arn) {
189
        /* set up sts assume role provider */
190
0
        session_name = flb_sts_session_name();
191
0
        if (!session_name) {
192
0
            flb_plg_error(ctx->ins,
193
0
                          "Failed to generate random STS session name");
194
0
            goto error;
195
0
        }
196
197
        /* STS provider needs yet another separate TLS instance */
198
0
        ctx->sts_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
199
0
                                      FLB_TRUE,
200
0
                                      ins->tls_debug,
201
0
                                      ins->tls_vhost,
202
0
                                      ins->tls_ca_path,
203
0
                                      ins->tls_ca_file,
204
0
                                      ins->tls_crt_file,
205
0
                                      ins->tls_key_file,
206
0
                                      ins->tls_key_passwd);
207
0
        if (!ctx->sts_tls) {
208
0
            flb_errno();
209
0
            goto error;
210
0
        }
211
212
0
        ctx->base_aws_provider = ctx->aws_provider;
213
214
0
        ctx->aws_provider = flb_sts_provider_create(config,
215
0
                                                    ctx->sts_tls,
216
0
                                                    ctx->base_aws_provider,
217
0
                                                    (char *) ctx->external_id,
218
0
                                                    (char *) ctx->role_arn,
219
0
                                                    session_name,
220
0
                                                    (char *) ctx->region,
221
0
                                                    ctx->sts_endpoint,
222
0
                                                    NULL,
223
0
                                                    flb_aws_client_generator());
224
0
        if (!ctx->aws_provider) {
225
0
            flb_plg_error(ctx->ins,
226
0
                          "Failed to create AWS STS Credential Provider");
227
0
            goto error;
228
0
        }
229
        /* session name can freed after provider is created */
230
0
        flb_free(session_name);
231
0
        session_name = NULL;
232
0
    }
233
234
    /* initialize credentials and set to sync mode */
235
0
    ctx->aws_provider->provider_vtable->sync(ctx->aws_provider);
236
0
    ctx->aws_provider->provider_vtable->init(ctx->aws_provider);
237
0
    ctx->aws_provider->provider_vtable->upstream_set(ctx->aws_provider, ctx->ins);
238
239
0
    if (ctx->endpoint == NULL) {
240
0
        ctx->endpoint = flb_aws_endpoint("kinesis", (char *) ctx->region);
241
0
        if (!ctx->endpoint) {
242
0
            goto error;
243
0
        }
244
0
    }
245
246
0
    struct flb_aws_client_generator *generator = flb_aws_client_generator();
247
0
    ctx->kinesis_client = generator->create();
248
0
    if (!ctx->kinesis_client) {
249
0
        goto error;
250
0
    }
251
0
    ctx->kinesis_client->name = "kinesis_client";
252
0
    ctx->kinesis_client->has_auth = FLB_TRUE;
253
0
    ctx->kinesis_client->provider = ctx->aws_provider;
254
0
    ctx->kinesis_client->region = (char *) ctx->region;
255
0
    ctx->kinesis_client->retry_requests = ctx->retry_requests;
256
0
    ctx->kinesis_client->service = "kinesis";
257
0
    ctx->kinesis_client->port = 443;
258
0
    ctx->kinesis_client->flags = 0;
259
0
    ctx->kinesis_client->proxy = NULL;
260
0
    ctx->kinesis_client->static_headers = &content_type_header;
261
0
    ctx->kinesis_client->static_headers_len = 1;
262
263
0
    struct flb_upstream *upstream = flb_upstream_create(config, ctx->endpoint,
264
0
                                                        443, FLB_IO_TLS,
265
0
                                                        ctx->client_tls);
266
0
    if (!upstream) {
267
0
        flb_plg_error(ctx->ins, "Connection initialization error");
268
0
        goto error;
269
0
    }
270
271
0
    ctx->kinesis_client->upstream = upstream;
272
0
    flb_output_upstream_set(upstream, ctx->ins);
273
274
0
    ctx->kinesis_client->host = ctx->endpoint;
275
276
    /* Export context */
277
0
    flb_output_set_context(ins, ctx);
278
279
0
    return 0;
280
281
0
error:
282
0
    flb_free(session_name);
283
0
    flb_plg_error(ctx->ins, "Initialization failed");
284
0
    flb_kinesis_ctx_destroy(ctx);
285
0
    return -1;
286
0
}
287
288
static struct flush *new_flush_buffer(const char *tag, int tag_len)
289
0
{
290
0
    struct flush *buf;
291
292
293
0
    buf = flb_calloc(1, sizeof(struct flush));
294
0
    if (!buf) {
295
0
        flb_errno();
296
0
        return NULL;
297
0
    }
298
299
0
    buf->tmp_buf = flb_malloc(sizeof(char) * PUT_RECORDS_PAYLOAD_SIZE);
300
0
    if (!buf->tmp_buf) {
301
0
        flb_errno();
302
0
        kinesis_flush_destroy(buf);
303
0
        return NULL;
304
0
    }
305
0
    buf->tmp_buf_size = PUT_RECORDS_PAYLOAD_SIZE;
306
307
0
    buf->events = flb_malloc(sizeof(struct kinesis_event) * MAX_EVENTS_PER_PUT);
308
0
    if (!buf->events) {
309
0
        flb_errno();
310
0
        kinesis_flush_destroy(buf);
311
0
        return NULL;
312
0
    }
313
0
    buf->events_capacity = MAX_EVENTS_PER_PUT;
314
    
315
0
    buf->tag = tag;
316
0
    buf->tag_len = tag_len;
317
318
0
    return buf;
319
0
}
320
321
static void cb_kinesis_flush(struct flb_event_chunk *event_chunk,
322
                             struct flb_output_flush *out_flush,
323
                             struct flb_input_instance *i_ins,
324
                             void *out_context,
325
                             struct flb_config *config)
326
0
{
327
0
    struct flb_kinesis *ctx = out_context;
328
0
    int ret;
329
0
    struct flush *buf;
330
0
    (void) i_ins;
331
0
    (void) config;
332
333
0
    buf = new_flush_buffer(event_chunk->tag, flb_sds_len(event_chunk->tag));
334
0
    if (!buf) {
335
0
        flb_plg_error(ctx->ins, "Failed to construct flush buffer");
336
0
        FLB_OUTPUT_RETURN(FLB_RETRY);
337
0
    }
338
339
0
    ret = process_and_send_to_kinesis(ctx, buf,
340
0
                                      event_chunk->data,
341
0
                                      event_chunk->size);
342
0
    if (ret < 0) {
343
0
        flb_plg_error(ctx->ins, "Failed to send records to kinesis");
344
0
        kinesis_flush_destroy(buf);
345
0
        FLB_OUTPUT_RETURN(FLB_RETRY);
346
0
    }
347
348
0
    flb_plg_debug(ctx->ins, "Processed %d records, sent %d to %s",
349
0
                 buf->records_processed, buf->records_sent, ctx->stream_name);
350
0
    kinesis_flush_destroy(buf);
351
352
0
    FLB_OUTPUT_RETURN(FLB_OK);
353
0
}
354
355
void flb_kinesis_ctx_destroy(struct flb_kinesis *ctx)
356
0
{
357
0
    if (ctx != NULL) {
358
0
        if (ctx->base_aws_provider) {
359
0
            flb_aws_provider_destroy(ctx->base_aws_provider);
360
0
        }
361
362
0
        if (ctx->aws_provider) {
363
0
            flb_aws_provider_destroy(ctx->aws_provider);
364
0
        }
365
366
0
        if (ctx->cred_tls) {
367
0
            flb_tls_destroy(ctx->cred_tls);
368
0
        }
369
370
0
        if (ctx->sts_tls) {
371
0
            flb_tls_destroy(ctx->sts_tls);
372
0
        }
373
374
0
        if (ctx->client_tls) {
375
0
            flb_tls_destroy(ctx->client_tls);
376
0
        }
377
378
0
        if (ctx->kinesis_client) {
379
0
            flb_aws_client_destroy(ctx->kinesis_client);
380
0
        }
381
382
0
        if (ctx->custom_endpoint == FLB_FALSE) {
383
0
            flb_free(ctx->endpoint);
384
0
        }
385
386
0
        if (ctx->uuid) {
387
0
            flb_free(ctx->uuid);
388
0
        }
389
390
0
        flb_free(ctx);
391
0
    }
392
0
}
393
394
static int cb_kinesis_exit(void *data, struct flb_config *config)
395
0
{
396
0
    struct flb_kinesis *ctx = data;
397
398
0
    flb_kinesis_ctx_destroy(ctx);
399
0
    return 0;
400
0
}
401
402
/* Configuration properties map */
403
static struct flb_config_map config_map[] = {
404
    {
405
     FLB_CONFIG_MAP_STR, "region", NULL,
406
     0, FLB_TRUE, offsetof(struct flb_kinesis, region),
407
     "The AWS region of your kinesis stream"
408
    },
409
410
    {
411
     FLB_CONFIG_MAP_STR, "stream", NULL,
412
     0, FLB_TRUE, offsetof(struct flb_kinesis, stream_name),
413
     "Kinesis stream name"
414
    },
415
416
    {
417
     FLB_CONFIG_MAP_STR, "time_key", NULL,
418
     0, FLB_TRUE, offsetof(struct flb_kinesis, time_key),
419
     "Add the timestamp to the record under this key. By default the timestamp "
420
     "from Fluent Bit will not be added to records sent to Kinesis."
421
    },
422
423
    {
424
     FLB_CONFIG_MAP_STR, "time_key_format", NULL,
425
     0, FLB_TRUE, offsetof(struct flb_kinesis, time_key_format),
426
     "strftime compliant format string for the timestamp; for example, "
427
     "the default is '%Y-%m-%dT%H:%M:%S'. This option is used with time_key. "
428
    },
429
430
    {
431
     FLB_CONFIG_MAP_STR, "role_arn", NULL,
432
     0, FLB_TRUE, offsetof(struct flb_kinesis, role_arn),
433
     "ARN of an IAM role to assume (ex. for cross account access)."
434
    },
435
436
    {
437
     FLB_CONFIG_MAP_STR, "endpoint", NULL,
438
     0, FLB_FALSE, 0,
439
     "Specify a custom endpoint for the Kinesis API"
440
    },
441
442
    {
443
     FLB_CONFIG_MAP_STR, "sts_endpoint", NULL,
444
     0, FLB_TRUE, offsetof(struct flb_kinesis, sts_endpoint),
445
    "Custom endpoint for the STS API."
446
    },
447
448
    {
449
     FLB_CONFIG_MAP_STR, "external_id", NULL,
450
     0, FLB_TRUE, offsetof(struct flb_kinesis, external_id),
451
     "Specify an external ID for the STS API, can be used with the role_arn parameter if your role "
452
     "requires an external ID."
453
    },
454
455
    {
456
     FLB_CONFIG_MAP_STR, "log_key", NULL,
457
     0, FLB_TRUE, offsetof(struct flb_kinesis, log_key),
458
     "By default, the whole log record will be sent to Kinesis. "
459
     "If you specify a key name with this option, then only the value of "
460
     "that key will be sent to Kinesis. For example, if you are using "
461
     "the Fluentd Docker log driver, you can specify `log_key log` and only "
462
     "the log message will be sent to Kinesis."
463
    },
464
465
    {
466
     FLB_CONFIG_MAP_BOOL, "auto_retry_requests", "true",
467
     0, FLB_TRUE, offsetof(struct flb_kinesis, retry_requests),
468
     "Immediately retry failed requests to AWS services once. This option "
469
     "does not affect the normal Fluent Bit retry mechanism with backoff. "
470
     "Instead, it enables an immediate retry with no delay for networking "
471
     "errors, which may help improve throughput when there are transient/random "
472
     "networking issues."
473
    },
474
475
    /* EOF */
476
    {0}
477
};
478
479
/* Plugin registration */
480
struct flb_output_plugin out_kinesis_streams_plugin = {
481
    .name         = "kinesis_streams",
482
    .description  = "Send logs to Amazon Kinesis Streams",
483
    .cb_init      = cb_kinesis_init,
484
    .cb_flush     = cb_kinesis_flush,
485
    .cb_exit      = cb_kinesis_exit,
486
    .workers      = 1,
487
    .flags        = 0,
488
489
    /* Configuration */
490
    .config_map     = config_map,
491
};