forked from fluent/fluent-bit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3.c
2469 lines (2182 loc) · 80.4 KB
/
s3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_slist.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_aws_util.h>
#include <fluent-bit/aws/flb_aws_compress.h>
#include <fluent-bit/flb_hash.h>
#include <fluent-bit/flb_crypto.h>
#include <fluent-bit/flb_signv4.h>
#include <fluent-bit/flb_scheduler.h>
#include <fluent-bit/flb_gzip.h>
#include <fluent-bit/flb_base64.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <msgpack.h>
#include "s3.h"
#include "s3_store.h"
#define DEFAULT_S3_PORT 443
#define DEFAULT_S3_INSECURE_PORT 80
static int construct_request_buffer(struct flb_s3 *ctx, flb_sds_t new_data,
struct s3_file *chunk,
char **out_buf, size_t *out_size);
static int s3_put_object(struct flb_s3 *ctx, const char *tag, time_t create_time,
char *body, size_t body_size);
static int put_all_chunks(struct flb_s3 *ctx);
static void cb_s3_upload(struct flb_config *ctx, void *data);
static struct multipart_upload *get_upload(struct flb_s3 *ctx,
const char *tag, int tag_len);
static struct multipart_upload *create_upload(struct flb_s3 *ctx,
const char *tag, int tag_len);
static void remove_from_queue(struct upload_queue *entry);
static struct flb_aws_header content_encoding_header = {
.key = "Content-Encoding",
.key_len = 16,
.val = "gzip",
.val_len = 4,
};
static struct flb_aws_header content_type_header = {
.key = "Content-Type",
.key_len = 12,
.val = "",
.val_len = 0,
};
static struct flb_aws_header canned_acl_header = {
.key = "x-amz-acl",
.key_len = 9,
.val = "",
.val_len = 0,
};
static struct flb_aws_header content_md5_header = {
.key = "Content-MD5",
.key_len = 11,
.val = "",
.val_len = 0,
};
static struct flb_aws_header storage_class_header = {
.key = "x-amz-storage-class",
.key_len = 19,
.val = "",
.val_len = 0,
};
static char *mock_error_response(char *error_env_var)
{
char *err_val = NULL;
char *error = NULL;
int len = 0;
err_val = getenv(error_env_var);
if (err_val != NULL && strlen(err_val) > 0) {
error = flb_malloc(strlen(err_val) + sizeof(char));
if (error == NULL) {
flb_errno();
return NULL;
}
len = strlen(err_val);
memcpy(error, err_val, len);
error[len] = '\0';
return error;
}
return NULL;
}
int s3_plugin_under_test()
{
if (getenv("FLB_S3_PLUGIN_UNDER_TEST") != NULL) {
return FLB_TRUE;
}
return FLB_FALSE;
}
int create_headers(struct flb_s3 *ctx, char *body_md5,
struct flb_aws_header **headers, int *num_headers,
int multipart_upload)
{
int n = 0;
int headers_len = 0;
struct flb_aws_header *s3_headers = NULL;
if (ctx->content_type != NULL) {
headers_len++;
}
if (ctx->compression == FLB_AWS_COMPRESS_GZIP) {
headers_len++;
}
if (ctx->canned_acl != NULL) {
headers_len++;
}
if (body_md5 != NULL && strlen(body_md5) && multipart_upload == FLB_FALSE) {
headers_len++;
}
if (ctx->storage_class != NULL) {
headers_len++;
}
if (headers_len == 0) {
*num_headers = headers_len;
*headers = s3_headers;
return 0;
}
s3_headers = flb_malloc(sizeof(struct flb_aws_header) * headers_len);
if (s3_headers == NULL) {
flb_errno();
return -1;
}
if (ctx->content_type != NULL) {
s3_headers[n] = content_type_header;
s3_headers[n].val = ctx->content_type;
s3_headers[n].val_len = strlen(ctx->content_type);
n++;
}
if (ctx->compression == FLB_AWS_COMPRESS_GZIP) {
s3_headers[n] = content_encoding_header;
n++;
}
if (ctx->canned_acl != NULL) {
s3_headers[n] = canned_acl_header;
s3_headers[n].val = ctx->canned_acl;
s3_headers[n].val_len = strlen(ctx->canned_acl);
n++;
}
if (body_md5 != NULL && strlen(body_md5) && multipart_upload == FLB_FALSE) {
s3_headers[n] = content_md5_header;
s3_headers[n].val = body_md5;
s3_headers[n].val_len = strlen(body_md5);
n++;
}
if (ctx->storage_class != NULL) {
s3_headers[n] = storage_class_header;
s3_headers[n].val = ctx->storage_class;
s3_headers[n].val_len = strlen(ctx->storage_class);
}
*num_headers = headers_len;
*headers = s3_headers;
return 0;
};
struct flb_http_client *mock_s3_call(char *error_env_var, char *api)
{
/* create an http client so that we can set the response */
struct flb_http_client *c = NULL;
char *error = mock_error_response(error_env_var);
char *resp;
int len;
c = flb_calloc(1, sizeof(struct flb_http_client));
if (!c) {
flb_errno();
flb_free(error);
return NULL;
}
mk_list_init(&c->headers);
if (error != NULL) {
c->resp.status = 400;
/* resp.data is freed on destroy, payload is supposed to reference it */
c->resp.data = error;
c->resp.payload = c->resp.data;
c->resp.payload_size = strlen(error);
}
else {
c->resp.status = 200;
c->resp.payload = "";
c->resp.payload_size = 0;
if (strcmp(api, "CreateMultipartUpload") == 0) {
/* mocked success response */
c->resp.payload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\n"
"<Bucket>example-bucket</Bucket>\n"
"<Key>example-object</Key>\n"
"<UploadId>VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA</UploadId>\n"
"</InitiateMultipartUploadResult>";
c->resp.payload_size = strlen(c->resp.payload);
}
else if (strcmp(api, "UploadPart") == 0) {
/* mocked success response */
resp = "Date: Mon, 1 Nov 2010 20:34:56 GMT\n"
"ETag: \"b54357faf0632cce46e942fa68356b38\"\n"
"Content-Length: 0\n"
"Connection: keep-alive\n"
"Server: AmazonS3";
/* since etag is in the headers, this code uses resp.data */
len = strlen(resp);
c->resp.data = flb_malloc(len + 1);
if (!c->resp.data) {
flb_errno();
return NULL;
}
memcpy(c->resp.data, resp, len);
c->resp.data[len] = '\0';
c->resp.data_size = len;
}
else {
c->resp.payload = "";
c->resp.payload_size = 0;
}
}
return c;
}
static flb_sds_t concat_path(char *p1, char *p2)
{
flb_sds_t dir;
flb_sds_t tmp;
dir = flb_sds_create_size(64);
tmp = flb_sds_printf(&dir, "%s/%s", p1, p2);
if (!tmp) {
flb_errno();
flb_sds_destroy(dir);
return NULL;
}
dir = tmp;
return dir;
}
/* Reads in index value from metadata file and sets seq_index to value */
static int read_seq_index(char *seq_index_file, uint64_t *seq_index)
{
FILE *fp;
int ret;
fp = fopen(seq_index_file, "r");
if (fp == NULL) {
flb_errno();
return -1;
}
ret = fscanf(fp, "%"PRIu64, seq_index);
if (ret != 1) {
flb_errno();
return -1;
}
fclose(fp);
return 0;
}
/* Writes index value to metadata file */
static int write_seq_index(char *seq_index_file, uint64_t seq_index)
{
FILE *fp;
int ret;
fp = fopen(seq_index_file, "w+");
if (fp == NULL) {
flb_errno();
return -1;
}
ret = fprintf(fp, "%"PRIu64, seq_index);
if (ret < 0) {
flb_errno();
return -1;
}
fclose(fp);
return 0;
}
static int init_seq_index(void *context) {
int ret;
const char *tmp;
char tmp_buf[1024];
struct flb_s3 *ctx = context;
ctx->key_fmt_has_seq_index = FLB_TRUE;
ctx->stream_metadata = flb_fstore_stream_create(ctx->fs, "sequence");
if (!ctx->stream_metadata) {
flb_plg_error(ctx->ins, "could not initialize metadata stream");
flb_fstore_destroy(ctx->fs);
ctx->fs = NULL;
return -1;
}
/* Construct directories and file path names */
ctx->metadata_dir = flb_sds_create(ctx->stream_metadata->path);
if (ctx->metadata_dir == NULL) {
flb_plg_error(ctx->ins, "Failed to create metadata path");
flb_errno();
return -1;
}
tmp = "/index_metadata";
ret = flb_sds_cat_safe(&ctx->metadata_dir, tmp, strlen(tmp));
if (ret < 0) {
flb_plg_error(ctx->ins, "Failed to create metadata path");
flb_errno();
return -1;
}
ctx->seq_index_file = flb_sds_create(ctx->metadata_dir);
if (ctx->seq_index_file == NULL) {
flb_plg_error(ctx->ins, "Failed to create sequential index file path");
flb_errno();
return -1;
}
tmp = "/seq_index_";
ret = flb_sds_cat_safe(&ctx->seq_index_file, tmp, strlen(tmp));
if (ret < 0) {
flb_plg_error(ctx->ins, "Failed to create sequential index file path");
flb_errno();
return -1;
}
sprintf(tmp_buf, "%d", ctx->ins->id);
ret = flb_sds_cat_safe(&ctx->seq_index_file, tmp_buf, strlen(tmp_buf));
if (ret < 0) {
flb_plg_error(ctx->ins, "Failed to create sequential index file path");
flb_errno();
return -1;
}
/* Create directory path if it doesn't exist */
ret = mkdir(ctx->metadata_dir, 0700);
if (ret < 0 && errno != EEXIST) {
flb_plg_error(ctx->ins, "Failed to create metadata directory");
return -1;
}
/* Check if index file doesn't exist and set index value */
if (access(ctx->seq_index_file, F_OK) != 0) {
ctx->seq_index = 0;
ret = write_seq_index(ctx->seq_index_file, ctx->seq_index);
if (ret < 0) {
flb_plg_error(ctx->ins, "Failed to write to sequential index metadata file");
return -1;
}
}
else {
ret = read_seq_index(ctx->seq_index_file, &ctx->seq_index);
if (ret < 0) {
flb_plg_error(ctx->ins, "Failed to read from sequential index "
"metadata file");
return -1;
}
flb_plg_info(ctx->ins, "Successfully recovered index. "
"Continuing at index=%d", ctx->seq_index);
}
return 0;
}
void multipart_upload_destroy(struct multipart_upload *m_upload)
{
int i;
flb_sds_t etag;
if (!m_upload) {
return;
}
if (m_upload->s3_key) {
flb_sds_destroy(m_upload->s3_key);
}
if (m_upload->tag) {
flb_sds_destroy(m_upload->tag);
}
if (m_upload->upload_id) {
flb_sds_destroy(m_upload->upload_id);
}
for (i = 0; i < m_upload->part_number; i++) {
etag = m_upload->etags[i];
if (etag) {
flb_sds_destroy(etag);
}
}
flb_free(m_upload);
}
static void s3_context_destroy(struct flb_s3 *ctx)
{
struct mk_list *head;
struct mk_list *tmp;
struct multipart_upload *m_upload;
struct upload_queue *upload_contents;
if (!ctx) {
return;
}
if (ctx->base_provider) {
flb_aws_provider_destroy(ctx->base_provider);
}
if (ctx->provider) {
flb_aws_provider_destroy(ctx->provider);
}
if (ctx->provider_tls) {
flb_tls_destroy(ctx->provider_tls);
}
if (ctx->sts_provider_tls) {
flb_tls_destroy(ctx->sts_provider_tls);
}
if (ctx->s3_client) {
flb_aws_client_destroy(ctx->s3_client);
}
if (ctx->client_tls) {
flb_tls_destroy(ctx->client_tls);
}
if (ctx->free_endpoint == FLB_TRUE) {
flb_free(ctx->endpoint);
}
if (ctx->buffer_dir) {
flb_sds_destroy(ctx->buffer_dir);
}
if (ctx->metadata_dir) {
flb_sds_destroy(ctx->metadata_dir);
}
if (ctx->seq_index_file) {
flb_sds_destroy(ctx->seq_index_file);
}
/* Remove uploads */
mk_list_foreach_safe(head, tmp, &ctx->uploads) {
m_upload = mk_list_entry(head, struct multipart_upload, _head);
mk_list_del(&m_upload->_head);
multipart_upload_destroy(m_upload);
}
mk_list_foreach_safe(head, tmp, &ctx->upload_queue) {
upload_contents = mk_list_entry(head, struct upload_queue, _head);
s3_store_file_delete(ctx, upload_contents->upload_file);
multipart_upload_destroy(upload_contents->m_upload_file);
remove_from_queue(upload_contents);
}
flb_free(ctx);
}
static int cb_s3_init(struct flb_output_instance *ins,
struct flb_config *config, void *data)
{
int ret;
flb_sds_t tmp_sds;
int async_flags;
int len;
char *role_arn = NULL;
char *session_name;
const char *tmp;
struct flb_s3 *ctx = NULL;
struct flb_aws_client_generator *generator;
(void) config;
(void) data;
char *ep;
struct flb_split_entry *tok;
struct mk_list *split;
int list_size;
ctx = flb_calloc(1, sizeof(struct flb_s3));
if (!ctx) {
flb_errno();
return -1;
}
ctx->ins = ins;
mk_list_init(&ctx->uploads);
mk_list_init(&ctx->upload_queue);
ctx->retry_time = 0;
ctx->upload_queue_success = FLB_FALSE;
/* Export context */
flb_output_set_context(ins, ctx);
/* initialize config map */
ret = flb_output_config_map_set(ins, (void *) ctx);
if (ret == -1) {
return -1;
}
/* Date key */
ctx->date_key = ctx->json_date_key;
tmp = flb_output_get_property("json_date_key", ins);
if (tmp) {
/* Just check if we have to disable it */
if (flb_utils_bool(tmp) == FLB_FALSE) {
ctx->date_key = NULL;
}
}
/* Date format for JSON output */
ctx->json_date_format = FLB_PACK_JSON_DATE_ISO8601;
tmp = flb_output_get_property("json_date_format", ins);
if (tmp) {
ret = flb_pack_to_json_date_type(tmp);
if (ret == -1) {
flb_plg_error(ctx->ins, "invalid json_date_format '%s'. ", tmp);
return -1;
}
else {
ctx->json_date_format = ret;
}
}
tmp = flb_output_get_property("bucket", ins);
if (!tmp) {
flb_plg_error(ctx->ins, "'bucket' is a required parameter");
return -1;
}
tmp = flb_output_get_property("chunk_buffer_dir", ins);
if (tmp) {
len = strlen(tmp);
if (tmp[len - 1] == '/' || tmp[len - 1] == '\\') {
flb_plg_error(ctx->ins, "'chunk_buffer_dir' can not end in a / or \\");
return -1;
}
}
/*
* store_dir is the user input, buffer_dir is what the code uses
* We append the bucket name to the dir, to support multiple instances
* of this plugin using the same buffer dir
*/
tmp_sds = concat_path(ctx->store_dir, ctx->bucket);
if (!tmp_sds) {
flb_plg_error(ctx->ins, "Could not construct buffer path");
return -1;
}
ctx->buffer_dir = tmp_sds;
/* Initialize local storage */
ret = s3_store_init(ctx);
if (ret == -1) {
flb_plg_error(ctx->ins, "Failed to initialize S3 storage: %s",
ctx->store_dir);
return -1;
}
tmp = flb_output_get_property("s3_key_format", ins);
if (tmp) {
if (tmp[0] != '/') {
flb_plg_error(ctx->ins, "'s3_key_format' must start with a '/'");
return -1;
}
if (strstr((char *) tmp, "$INDEX")) {
ret = init_seq_index(ctx);
if (ret < 0) {
return -1;
}
}
if (strstr((char *) tmp, "$UUID")) {
ctx->key_fmt_has_uuid = FLB_TRUE;
}
}
/* validate 'total_file_size' */
if (ctx->file_size <= 0) {
flb_plg_error(ctx->ins, "Failed to parse total_file_size %s", tmp);
return -1;
}
if (ctx->file_size < 1000000) {
flb_plg_error(ctx->ins, "total_file_size must be at least 1MB");
return -1;
}
if (ctx->file_size > MAX_FILE_SIZE) {
flb_plg_error(ctx->ins, "Max total_file_size is %s bytes", MAX_FILE_SIZE_STR);
return -1;
}
flb_plg_info(ctx->ins, "Using upload size %lu bytes", ctx->file_size);
if (ctx->use_put_object == FLB_FALSE && ctx->file_size < 2 * MIN_CHUNKED_UPLOAD_SIZE) {
flb_plg_info(ctx->ins,
"total_file_size is less than 10 MB, will use PutObject API");
ctx->use_put_object = FLB_TRUE;
}
tmp = flb_output_get_property("compression", ins);
if (tmp) {
ret = flb_aws_compression_get_type(tmp);
if (ret == -1) {
flb_plg_error(ctx->ins, "unknown compression: %s", tmp);
return -1;
}
if (ctx->use_put_object == FLB_FALSE && ctx->compression == FLB_AWS_COMPRESS_ARROW) {
flb_plg_error(ctx->ins,
"use_put_object must be enabled when Apache Arrow is enabled");
return -1;
}
ctx->compression = ret;
}
tmp = flb_output_get_property("content_type", ins);
if (tmp) {
ctx->content_type = (char *) tmp;
}
if (ctx->use_put_object == FLB_FALSE) {
/* upload_chunk_size */
if (ctx->upload_chunk_size <= 0) {
flb_plg_error(ctx->ins, "Failed to parse upload_chunk_size %s", tmp);
return -1;
}
if (ctx->upload_chunk_size > ctx->file_size) {
flb_plg_error(ctx->ins,
"upload_chunk_size can not be larger than total_file_size");
return -1;
}
if (ctx->upload_chunk_size < MIN_CHUNKED_UPLOAD_SIZE) {
flb_plg_error(ctx->ins, "upload_chunk_size must be at least 5,242,880 bytes");
return -1;
}
if (ctx->compression == FLB_AWS_COMPRESS_GZIP) {
if(ctx->upload_chunk_size > MAX_CHUNKED_UPLOAD_COMPRESS_SIZE) {
flb_plg_error(ctx->ins, "upload_chunk_size in compressed multipart upload cannot exceed 5GB");
return -1;
}
} else {
if (ctx->upload_chunk_size > MAX_CHUNKED_UPLOAD_SIZE) {
flb_plg_error(ctx->ins, "Max upload_chunk_size is 50MB");
return -1;
}
}
}
if (ctx->upload_chunk_size != MIN_CHUNKED_UPLOAD_SIZE &&
(ctx->upload_chunk_size * 2) > ctx->file_size) {
flb_plg_error(ctx->ins, "total_file_size is less than 2x upload_chunk_size");
return -1;
}
if (ctx->use_put_object == FLB_TRUE) {
/*
* code internally uses 'upload_chunk_size' as the unit for each Put,
* regardless of which API is used to send data
*/
ctx->upload_chunk_size = ctx->file_size;
if (ctx->file_size > MAX_FILE_SIZE_PUT_OBJECT) {
flb_plg_error(ctx->ins, "Max total_file_size is 50M when use_put_object is enabled");
return -1;
}
}
tmp = flb_output_get_property("endpoint", ins);
if (tmp) {
ctx->insecure = strncmp(tmp, "http://", 7) == 0 ? FLB_TRUE : FLB_FALSE;
if (ctx->insecure == FLB_TRUE) {
ep = removeProtocol((char *) tmp, "http://");
}
else {
ep = removeProtocol((char *) tmp, "https://");
}
split = flb_utils_split((const char *)ep, ':', 1);
if (!split) {
flb_errno();
return -1;
}
list_size = mk_list_size(split);
if (list_size > 2) {
flb_plg_error(ctx->ins, "Failed to split endpoint");
flb_utils_split_free(split);
return -1;
}
tok = mk_list_entry_first(split, struct flb_split_entry, _head);
ctx->endpoint = flb_strndup(tok->value, tok->len);
if (!ctx->endpoint) {
flb_errno();
flb_utils_split_free(split);
return -1;
}
ctx->free_endpoint = FLB_TRUE;
if (list_size == 2) {
tok = mk_list_entry_next(&tok->_head, struct flb_split_entry, _head, split);
ctx->port = atoi(tok->value);
}
else {
ctx->port = ctx->insecure == FLB_TRUE ? DEFAULT_S3_INSECURE_PORT : DEFAULT_S3_PORT;
}
flb_utils_split_free(split);
}
else {
/* default endpoint for the given region */
ctx->endpoint = flb_aws_endpoint("s3", ctx->region);
ctx->insecure = FLB_FALSE;
ctx->port = DEFAULT_S3_PORT;
ctx->free_endpoint = FLB_TRUE;
if (!ctx->endpoint) {
flb_plg_error(ctx->ins, "Could not construct S3 endpoint");
return -1;
}
}
tmp = flb_output_get_property("sts_endpoint", ins);
if (tmp) {
ctx->sts_endpoint = (char *) tmp;
}
tmp = flb_output_get_property("canned_acl", ins);
if (tmp) {
ctx->canned_acl = (char *) tmp;
}
tmp = flb_output_get_property("storage_class", ins);
if (tmp) {
ctx->storage_class = (char *) tmp;
}
if (ctx->insecure == FLB_FALSE) {
ctx->client_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
ins->tls_verify,
ins->tls_debug,
ins->tls_vhost,
ins->tls_ca_path,
ins->tls_ca_file,
ins->tls_crt_file,
ins->tls_key_file,
ins->tls_key_passwd);
if (!ctx->client_tls) {
flb_plg_error(ctx->ins, "Failed to create tls context");
return -1;
}
}
/* AWS provider needs a separate TLS instance */
ctx->provider_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
FLB_TRUE,
ins->tls_debug,
ins->tls_vhost,
ins->tls_ca_path,
ins->tls_ca_file,
ins->tls_crt_file,
ins->tls_key_file,
ins->tls_key_passwd);
if (!ctx->provider_tls) {
flb_errno();
return -1;
}
ctx->provider = flb_standard_chain_provider_create(config,
ctx->provider_tls,
ctx->region,
ctx->sts_endpoint,
NULL,
flb_aws_client_generator());
if (!ctx->provider) {
flb_plg_error(ctx->ins, "Failed to create AWS Credential Provider");
return -1;
}
tmp = flb_output_get_property("role_arn", ins);
if (tmp) {
/* Use the STS Provider */
ctx->base_provider = ctx->provider;
role_arn = (char *) tmp;
/* STS provider needs yet another separate TLS instance */
ctx->sts_provider_tls = flb_tls_create(FLB_TLS_CLIENT_MODE,
FLB_TRUE,
ins->tls_debug,
ins->tls_vhost,
ins->tls_ca_path,
ins->tls_ca_file,
ins->tls_crt_file,
ins->tls_key_file,
ins->tls_key_passwd);
if (!ctx->sts_provider_tls) {
flb_errno();
return -1;
}
session_name = flb_sts_session_name();
if (!session_name) {
flb_plg_error(ctx->ins, "Failed to create aws iam role "
"session name");
flb_errno();
return -1;
}
ctx->provider = flb_sts_provider_create(config,
ctx->sts_provider_tls,
ctx->base_provider,
ctx->external_id,
role_arn,
session_name,
ctx->region,
ctx->sts_endpoint,
NULL,
flb_aws_client_generator());
flb_free(session_name);
if (!ctx->provider) {
flb_plg_error(ctx->ins, "Failed to create AWS STS Credential "
"Provider");
return -1;
}
}
/* read any remaining buffers from previous (failed) executions */
ctx->has_old_buffers = s3_store_has_data(ctx);
ctx->has_old_uploads = s3_store_has_uploads(ctx);
/* Multipart */
multipart_read_uploads_from_fs(ctx);
if (mk_list_size(&ctx->uploads) > 0) {
/* note that these should be sent */
ctx->has_old_uploads = FLB_TRUE;
}
/* create S3 client */
generator = flb_aws_client_generator();
ctx->s3_client = generator->create();
if (!ctx->s3_client) {
return -1;
}
ctx->s3_client->name = "s3_client";
ctx->s3_client->has_auth = FLB_TRUE;
ctx->s3_client->provider = ctx->provider;
ctx->s3_client->region = ctx->region;
ctx->s3_client->service = "s3";
ctx->s3_client->port = ctx->port;
ctx->s3_client->flags = 0;
ctx->s3_client->proxy = NULL;
ctx->s3_client->s3_mode = S3_MODE_SIGNED_PAYLOAD;
ctx->s3_client->retry_requests = ctx->retry_requests;
if (ctx->insecure == FLB_TRUE) {
ctx->s3_client->upstream = flb_upstream_create(config, ctx->endpoint, ctx->port,
FLB_IO_TCP, NULL);
} else {
ctx->s3_client->upstream = flb_upstream_create(config, ctx->endpoint, ctx->port,
FLB_IO_TLS, ctx->client_tls);
}
if (!ctx->s3_client->upstream) {
flb_plg_error(ctx->ins, "Connection initialization error");
return -1;
}
flb_output_upstream_set(ctx->s3_client->upstream, ctx->ins);
ctx->s3_client->host = ctx->endpoint;
/* set to sync mode and initialize credentials */
ctx->provider->provider_vtable->sync(ctx->provider);
ctx->provider->provider_vtable->init(ctx->provider);
ctx->timer_created = FLB_FALSE;
ctx->timer_ms = (int) (ctx->upload_timeout / 6) * 1000;
if (ctx->timer_ms > UPLOAD_TIMER_MAX_WAIT) {
ctx->timer_ms = UPLOAD_TIMER_MAX_WAIT;
}
else if (ctx->timer_ms < UPLOAD_TIMER_MIN_WAIT) {
ctx->timer_ms = UPLOAD_TIMER_MIN_WAIT;
}
/* init must use sync mode */
async_flags = flb_stream_get_flags(&ctx->s3_client->upstream->base);
flb_stream_disable_async_mode(&ctx->s3_client->upstream->base);
/* clean up any old buffers found on startup */
if (ctx->has_old_buffers == FLB_TRUE) {
flb_plg_info(ctx->ins,
"Sending locally buffered data from previous "
"executions to S3; buffer=%s",
ctx->fs->root_path);
ctx->has_old_buffers = FLB_FALSE;
ret = put_all_chunks(ctx);
if (ret < 0) {
ctx->has_old_buffers = FLB_TRUE;
flb_plg_error(ctx->ins,
"Failed to send locally buffered data left over "
"from previous executions; will retry. Buffer=%s",
ctx->fs->root_path);
}
}
/* clean up any old uploads found on start up */
if (ctx->has_old_uploads == FLB_TRUE) {
flb_plg_info(ctx->ins,
"Completing multipart uploads from previous "
"executions to S3; buffer=%s",
ctx->stream_upload->path);
ctx->has_old_uploads = FLB_FALSE;
/*
* we don't need to worry if this fails; it will retry each
* time the upload callback is called
*/
cb_s3_upload(config, ctx);
}
if (ctx->use_put_object == FLB_TRUE) {
/*
* Run S3 in async mode.
* Multipart uploads don't work with async mode right now in high throughput
* cases. Its not clear why. Realistically, the performance of sync mode
* will be sufficient for most users, and long term we can do the work
* to enable async if needed.
*/
flb_stream_set_flags(&ctx->s3_client->upstream->base, async_flags);
}
/* this is done last since in the previous block we make calls to AWS */
ctx->provider->provider_vtable->upstream_set(ctx->provider, ctx->ins);
return 0;
}
/*
* return value is one of FLB_OK, FLB_RETRY, FLB_ERROR
*
* Chunk is allowed to be NULL
*/
static int upload_data(struct flb_s3 *ctx, struct s3_file *chunk,
struct multipart_upload *m_upload,
char *body, size_t body_size,
const char *tag, int tag_len)
{
int init_upload = FLB_FALSE;
int complete_upload = FLB_FALSE;
int size_check = FLB_FALSE;
int part_num_check = FLB_FALSE;
int timeout_check = FLB_FALSE;
time_t create_time;
int ret;
void *payload_buf = NULL;
size_t payload_size = 0;
size_t preCompress_size = 0;
if (ctx->compression == FLB_AWS_COMPRESS_GZIP) {
/* Map payload */
ret = flb_aws_compression_compress(ctx->compression, body, body_size, &payload_buf, &payload_size);
if (ret == -1) {
flb_plg_error(ctx->ins, "Failed to compress data");
return FLB_RETRY;