-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathazure_blob.c
More file actions
2114 lines (1803 loc) · 70.4 KB
/
azure_blob.c
File metadata and controls
2114 lines (1803 loc) · 70.4 KB
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-2024 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_sds.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_gzip.h>
#include <fluent-bit/flb_base64.h>
#include <fluent-bit/flb_sqldb.h>
#include <fluent-bit/flb_input_blob.h>
#include <fluent-bit/flb_log_event_decoder.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_notification.h>
#include <fluent-bit/flb_scheduler.h>
#include <fluent-bit/flb_record_accessor.h>
#include <fluent-bit/flb_ra_key.h>
#include <msgpack.h>
#include "azure_blob.h"
#include "azure_blob_db.h"
#include "azure_blob_uri.h"
#include "azure_blob_conf.h"
#include "azure_blob_appendblob.h"
#include "azure_blob_blockblob.h"
#include "azure_blob_http.h"
#include "azure_blob_store.h"
#define CREATE_BLOB 1337
/* thread_local_storage for workers */
struct worker_info {
int active_upload;
};
FLB_TLS_DEFINE(struct worker_info, worker_info);
static flb_sds_t cb_azb_msgpack_extract_log_key(void *out_context, const char *data,
uint64_t bytes)
{
struct flb_azure_blob *ctx = out_context;
flb_sds_t out_buf = NULL;
msgpack_unpacked result;
msgpack_object root;
msgpack_object map;
struct flb_record_accessor *ra = NULL;
struct flb_ra_value *rval = NULL;
size_t off = 0;
ra = flb_ra_create(ctx->log_key, FLB_FALSE);
if (!ra) {
flb_errno();
flb_plg_error(ctx->ins, "invalid record accessor pattern '%s'", ctx->log_key);
return NULL;
}
/* Unpack the data */
msgpack_unpacked_init(&result);
while (1) {
msgpack_unpack_return ret = msgpack_unpack_next(&result, data, bytes, &off);
if (ret == MSGPACK_UNPACK_SUCCESS) {
root = result.data;
if (root.type != MSGPACK_OBJECT_ARRAY) {
continue;
}
if (root.via.array.size < 2) {
flb_plg_debug(ctx->ins, "msgpack array has insufficient elements");
continue;
}
map = root.via.array.ptr[1];
/* Get value using record accessor */
rval = flb_ra_get_value_object(ra, map);
if (!rval) {
flb_plg_error(ctx->ins, "could not find field '%s'", ctx->log_key);
continue;
}
/* Convert value based on its type */
if (rval->type == FLB_RA_STRING) {
out_buf = flb_sds_create_size(rval->o.via.str.size + 1);
if (out_buf) {
flb_sds_copy(out_buf, rval->o.via.str.ptr, rval->o.via.str.size);
flb_sds_cat(out_buf, "\n", 1);
}
}
else if (rval->type == FLB_RA_FLOAT) {
out_buf = flb_sds_create_size(64);
if (out_buf) {
flb_sds_printf(&out_buf, "%f\n", rval->val.f64);
}
}
else if (rval->type == FLB_RA_INT) {
out_buf = flb_sds_create_size(64);
if (out_buf) {
flb_sds_printf(&out_buf, "%" PRId64 "\n", rval->val.i64);
}
}
else {
flb_errno();
flb_plg_error(ctx->ins, "cannot convert given value for field '%s'", ctx->log_key);
flb_ra_key_value_destroy(rval);
rval = NULL;
break;
}
/* Check if buffer allocation succeeded */
if (!out_buf) {
flb_errno();
flb_plg_error(ctx->ins, "could not allocate output buffer");
}
flb_ra_key_value_destroy(rval);
rval = NULL;
/* Successfully found and processed log_key, exit loop */
break;
}
else if (ret == MSGPACK_UNPACK_CONTINUE) {
/* Buffer exhausted or truncated data, stop processing */
flb_plg_debug(ctx->ins, "msgpack unpack needs more data or data truncated");
break;
}
else if (ret == MSGPACK_UNPACK_PARSE_ERROR) {
flb_errno();
flb_plg_error(ctx->ins, "msgpack parse error");
break;
}
else {
flb_errno();
flb_plg_error(ctx->ins, "unexpected msgpack unpack return code %d", ret);
break;
}
}
/* Clean up */
msgpack_unpacked_destroy(&result);
flb_ra_destroy(ra);
return out_buf;
}
static int azure_blob_format(struct flb_config *config,
struct flb_input_instance *ins,
void *plugin_context,
void *flush_ctx,
int event_type,
const char *tag, int tag_len,
const void *data, size_t bytes,
void **out_data, size_t *out_size)
{
flb_sds_t out_buf;
struct flb_azure_blob *ctx = plugin_context;
if (ctx->log_key) {
out_buf = cb_azb_msgpack_extract_log_key(ctx, data, bytes);
}
else {
out_buf = flb_pack_msgpack_to_json_format(data, bytes,
FLB_PACK_JSON_FORMAT_LINES,
FLB_PACK_JSON_DATE_ISO8601,
ctx->date_key,
config->json_escape_unicode);
}
if (!out_buf) {
return -1;
}
*out_data = out_buf;
*out_size = flb_sds_len(out_buf);
return 0;
}
/*
* Either new_data or chunk can be NULL, but not both
*/
static int construct_request_buffer(struct flb_azure_blob *ctx, flb_sds_t new_data,
struct azure_blob_file *upload_file,
char **out_buf, size_t *out_size)
{
char *body;
char *tmp;
size_t body_size = 0;
char *buffered_data = NULL;
size_t buffer_size = 0;
int ret;
if (new_data == NULL && upload_file == NULL) {
flb_plg_error(ctx->ins, "[construct_request_buffer] Something went wrong"
" both chunk and new_data are NULL");
return -1;
}
if (upload_file) {
ret = azure_blob_store_file_upload_read(ctx, upload_file->fsf, &buffered_data, &buffer_size);
if (ret < 0) {
flb_plg_error(ctx->ins, "Could not read locally buffered data %s",
upload_file->fsf->name);
return -1;
}
/*
* lock the upload_file from buffer list
*/
azure_blob_store_file_lock(upload_file);
body = buffered_data;
body_size = buffer_size;
}
flb_plg_debug(ctx->ins, "[construct_request_buffer] size of buffer file read %zu", buffer_size);
/*
* If new data is arriving, increase the original 'buffered_data' size
* to append the new one.
*/
if (new_data) {
body_size += flb_sds_len(new_data);
flb_plg_debug(ctx->ins, "[construct_request_buffer] size of new_data %zu", body_size);
tmp = flb_realloc(buffered_data, body_size + 1);
if (!tmp) {
flb_errno();
flb_free(buffered_data);
if (upload_file) {
azure_blob_store_file_unlock(upload_file);
}
return -1;
}
body = buffered_data = tmp;
memcpy(body + buffer_size, new_data, flb_sds_len(new_data));
if (ctx->compress_gzip == FLB_FALSE){
body[body_size] = '\0';
}
}
flb_plg_debug(ctx->ins, "[construct_request_buffer] final increased %zu", body_size);
*out_buf = body;
*out_size = body_size;
return 0;
}
void generate_random_string_blob(char *str, size_t length)
{
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const size_t charset_size = sizeof(charset) - 1;
size_t i;
size_t index;
/* Seed the random number generator with multiple sources of entropy */
unsigned int seed = (unsigned int)(time(NULL) ^ clock() ^ getpid());
srand(seed);
for (i = 0; i < length; ++i) {
index = (size_t)rand() % charset_size;
str[i] = charset[index];
}
str[length] = '\0';
}
static int create_blob(struct flb_azure_blob *ctx, char *name)
{
int ret;
size_t b_sent;
flb_sds_t uri = NULL;
struct flb_http_client *c;
struct flb_connection *u_conn;
uri = azb_uri_create_blob(ctx, name);
if (!uri) {
return FLB_RETRY;
}
if (ctx->buffering_enabled == FLB_TRUE){
ctx->u->base.flags &= ~(FLB_IO_ASYNC);
ctx->u->base.net.io_timeout = ctx->io_timeout;
}
/* Get upstream connection */
u_conn = flb_upstream_conn_get(ctx->u);
if (!u_conn) {
flb_plg_error(ctx->ins,
"cannot create upstream connection for create_append_blob");
flb_sds_destroy(uri);
return FLB_RETRY;
}
/* Create HTTP client context */
c = flb_http_client(u_conn, FLB_HTTP_PUT,
uri,
NULL, 0, NULL, 0, NULL, 0);
if (!c) {
flb_plg_error(ctx->ins, "cannot create HTTP client context");
flb_upstream_conn_release(u_conn);
flb_sds_destroy(uri);
return FLB_RETRY;
}
/* Prepare headers and authentication */
azb_http_client_setup(ctx, c, -1, FLB_TRUE,
AZURE_BLOB_CT_NONE, AZURE_BLOB_CE_NONE);
/* Send HTTP request */
ret = flb_http_do(c, &b_sent);
flb_sds_destroy(uri);
if (ret == -1) {
flb_plg_error(ctx->ins, "error sending append_blob");
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_RETRY;
}
if (c->resp.status == 201) {
/* delete "&sig=..." in the c->uri for security */
char *p = strstr(c->uri, "&sig=");
if (p) {
*p = '\0';
}
flb_plg_info(ctx->ins, "blob created successfully: %s", c->uri);
}
else {
if (c->resp.payload_size > 0) {
flb_plg_error(ctx->ins, "http_status=%i cannot create append blob\n%s",
c->resp.status, c->resp.payload);
}
else {
flb_plg_error(ctx->ins, "http_status=%i cannot create append blob",
c->resp.status);
}
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_RETRY;
}
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_OK;
}
static int delete_blob(struct flb_azure_blob *ctx, char *name)
{
int ret;
size_t b_sent;
flb_sds_t uri = NULL;
struct flb_http_client *c;
struct flb_connection *u_conn;
uri = azb_uri_create_blob(ctx, name);
if (!uri) {
return FLB_RETRY;
}
/* Get upstream connection */
u_conn = flb_upstream_conn_get(ctx->u);
if (!u_conn) {
flb_plg_error(ctx->ins,
"cannot create upstream connection for create_append_blob");
flb_sds_destroy(uri);
return FLB_RETRY;
}
/* Create HTTP client context */
c = flb_http_client(u_conn, FLB_HTTP_DELETE,
uri,
NULL, 0, NULL, 0, NULL, 0);
if (!c) {
flb_plg_error(ctx->ins, "cannot create HTTP client context");
flb_upstream_conn_release(u_conn);
flb_sds_destroy(uri);
return FLB_RETRY;
}
/* Prepare headers and authentication */
azb_http_client_setup(ctx, c, -1, FLB_TRUE,
AZURE_BLOB_CT_NONE, AZURE_BLOB_CE_NONE);
/* Send HTTP request */
ret = flb_http_do(c, &b_sent);
flb_sds_destroy(uri);
if (ret == -1) {
flb_plg_error(ctx->ins, "error sending append_blob");
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_RETRY;
}
if (c->resp.status == 201) {
/* delete "&sig=..." in the c->uri for security */
char *p = strstr(c->uri, "&sig=");
if (p) {
*p = '\0';
}
flb_plg_info(ctx->ins, "blob deleted successfully: %s", c->uri);
}
else {
if (c->resp.payload_size > 0) {
flb_plg_error(ctx->ins, "http_status=%i cannot delete append blob\n%s",
c->resp.status, c->resp.payload);
}
else {
flb_plg_error(ctx->ins, "http_status=%i cannot delete append blob",
c->resp.status);
}
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_RETRY;
}
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_OK;
}
static int http_send_blob(struct flb_config *config, struct flb_azure_blob *ctx,
flb_sds_t ref_name,
flb_sds_t uri,
flb_sds_t block_id,
int event_type,
void *data, size_t bytes)
{
int ret;
int compressed = FLB_FALSE;
int content_encoding = FLB_FALSE;
int content_type = FLB_FALSE;
size_t b_sent;
void *payload_buf;
size_t payload_size;
struct flb_http_client *c;
struct flb_connection *u_conn;
flb_plg_debug(ctx->ins, "generated blob uri ::: %s", uri);
if (ctx->buffering_enabled == FLB_TRUE){
ctx->u->base.flags &= ~(FLB_IO_ASYNC);
ctx->u->base.net.io_timeout = ctx->io_timeout;
}
/* Get upstream connection */
u_conn = flb_upstream_conn_get(ctx->u);
if (!u_conn) {
flb_plg_error(ctx->ins,
"cannot create TCP upstream connection");
return FLB_RETRY;
}
payload_buf = data;
payload_size = bytes;
/* Handle compression requests */
if (ctx->compress_gzip == FLB_TRUE || ctx->compress_blob == FLB_TRUE) {
ret = flb_gzip_compress((void *) data, bytes, &payload_buf, &payload_size);
if (ret == 0) {
compressed = FLB_TRUE;
}
else {
flb_plg_warn(ctx->ins,
"cannot gzip payload, disabling compression");
payload_buf = data;
payload_size = bytes;
}
}
/* set http header flags */
if (ctx->compress_blob == FLB_TRUE) {
content_encoding = AZURE_BLOB_CE_NONE;
content_type = AZURE_BLOB_CT_GZIP;
}
else if (compressed == FLB_TRUE) {
content_encoding = AZURE_BLOB_CE_GZIP;
content_type = AZURE_BLOB_CT_JSON;
}
/* Create HTTP client context */
c = flb_http_client(u_conn, FLB_HTTP_PUT,
uri,
payload_buf, payload_size, NULL, 0, NULL, 0);
if (!c) {
flb_plg_error(ctx->ins, "cannot create HTTP client context");
if (compressed == FLB_TRUE) {
flb_free(payload_buf);
}
flb_upstream_conn_release(u_conn);
return FLB_RETRY;
}
/* Prepare headers and authentication */
azb_http_client_setup(ctx, c, (ssize_t) payload_size, FLB_FALSE,
content_type, content_encoding);
/* Send HTTP request */
ret = flb_http_do(c, &b_sent);
/* Release compressed buffer */
if (compressed == FLB_TRUE) {
flb_free(payload_buf);
}
flb_upstream_conn_release(u_conn);
/* Validate HTTP status */
if (ret == -1) {
flb_plg_error(ctx->ins, "error sending append_blob for %s", ref_name);
return FLB_RETRY;
}
if (c->resp.status == 201) {
flb_plg_info(ctx->ins, "content uploaded successfully: %s", ref_name);
flb_http_client_destroy(c);
return FLB_OK;
}
else if (c->resp.status == 404) {
/* delete "&sig=..." in the c->uri for security */
char *p = strstr(c->uri, "&sig=");
if (p) {
*p = '\0';
}
flb_plg_info(ctx->ins, "blob not found: %s", c->uri);
flb_http_client_destroy(c);
return CREATE_BLOB;
}
else if (c->resp.payload_size > 0) {
flb_plg_error(ctx->ins, "http_status=%i cannot append content to blob\n%s",
c->resp.status, c->resp.payload);
if (strstr(c->resp.payload, "must be 0 for Create Append")) {
flb_http_client_destroy(c);
return CREATE_BLOB;
}
}
else {
flb_plg_error(ctx->ins, "cannot upload %s content to blob (http_status=%i)",
ref_name, c->resp.status);
}
flb_http_client_destroy(c);
return FLB_RETRY;
}
static int send_blob(struct flb_config *config,
struct flb_input_instance *i_ins,
struct flb_azure_blob *ctx,
int event_type,
int blob_type, char *name, uint64_t part_id,
char *tag, int tag_len, void *data, size_t bytes)
{
int ret;
uint64_t ms = 0;
flb_sds_t uri = NULL;
flb_sds_t block_id = NULL;
flb_sds_t ref_name = NULL;
void *payload_buf = data;
size_t payload_size = bytes;
char *generated_random_string;
ref_name = flb_sds_create_size(256);
if (!ref_name) {
return FLB_RETRY;
}
/* Allocate memory for the random string dynamically */
generated_random_string = flb_malloc(ctx->blob_uri_length + 1);
if (!generated_random_string) {
flb_errno();
flb_plg_error(ctx->ins, "cannot allocate memory for random string");
flb_sds_destroy(ref_name);
return FLB_RETRY;
}
if (blob_type == AZURE_BLOB_APPENDBLOB) {
uri = azb_append_blob_uri(ctx, tag);
}
else if (blob_type == AZURE_BLOB_BLOCKBLOB) {
generate_random_string_blob(generated_random_string, ctx->blob_uri_length); /* Generate the random string */
if (event_type == FLB_EVENT_TYPE_LOGS) {
block_id = azb_block_blob_id_logs(&ms);
if (!block_id) {
flb_plg_error(ctx->ins, "could not generate block id");
flb_free(generated_random_string);
cfl_sds_destroy(ref_name);
return FLB_RETRY;
}
uri = azb_block_blob_uri(ctx, tag, block_id, ms, generated_random_string);
ref_name = flb_sds_printf(&ref_name, "file=%s.%" PRIu64, name, ms);
}
else if (event_type == FLB_EVENT_TYPE_BLOBS) {
block_id = azb_block_blob_id_blob(ctx, name, part_id);
uri = azb_block_blob_uri(ctx, name, block_id, 0, generated_random_string);
ref_name = flb_sds_printf(&ref_name, "file=%s:%" PRIu64, name, part_id);
}
}
if (!uri) {
flb_free(generated_random_string);
if (block_id != NULL) {
flb_free(block_id);
}
flb_sds_destroy(ref_name);
return FLB_RETRY;
}
/* Map buffer */
payload_buf = data;
payload_size = bytes;
ret = http_send_blob(config, ctx, ref_name, uri, block_id, event_type, payload_buf, payload_size);
flb_plg_debug(ctx->ins, "http_send_blob()=%i", ret);
if (ret == FLB_OK) {
/* For Logs type, we need to commit the block right away */
if (event_type == FLB_EVENT_TYPE_LOGS) {
ret = azb_block_blob_commit_block(ctx, block_id, tag, ms, generated_random_string);
}
}
else if (ret == CREATE_BLOB) {
ret = create_blob(ctx, name);
if (ret == FLB_OK) {
ret = http_send_blob(config, ctx, ref_name, uri, block_id, event_type, payload_buf, payload_size);
}
}
flb_sds_destroy(ref_name);
if (payload_buf != data) {
flb_sds_destroy(payload_buf);
}
flb_sds_destroy(uri);
flb_free(generated_random_string);
if (block_id != NULL) {
flb_free(block_id);
}
return ret;
}
static int create_container(struct flb_azure_blob *ctx, char *name)
{
int ret;
size_t b_sent;
flb_sds_t uri;
struct flb_http_client *c;
struct flb_connection *u_conn;
if (ctx->buffering_enabled == FLB_TRUE){
ctx->u->base.flags &= ~(FLB_IO_ASYNC);
ctx->u->base.net.io_timeout = ctx->io_timeout;
}
/* Get upstream connection */
u_conn = flb_upstream_conn_get(ctx->u);
if (!u_conn) {
flb_plg_error(ctx->ins,
"cannot create upstream connection for container creation");
return FLB_FALSE;
}
/* URI */
uri = azb_uri_ensure_or_create_container(ctx);
if (!uri) {
flb_upstream_conn_release(u_conn);
return FLB_FALSE;
}
/* Create HTTP client context */
c = flb_http_client(u_conn, FLB_HTTP_PUT,
uri,
NULL, 0, NULL, 0, NULL, 0);
if (!c) {
flb_plg_error(ctx->ins, "cannot create HTTP client context");
flb_upstream_conn_release(u_conn);
return FLB_FALSE;
}
/* Prepare headers and authentication */
azb_http_client_setup(ctx, c, -1, FLB_FALSE,
AZURE_BLOB_CT_NONE, AZURE_BLOB_CE_NONE);
/* Send HTTP request */
ret = flb_http_do(c, &b_sent);
/* Release URI */
flb_sds_destroy(uri);
/* Validate http response */
if (ret == -1) {
flb_plg_error(ctx->ins, "error requesting container creation");
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_FALSE;
}
if (c->resp.status == 201) {
flb_plg_info(ctx->ins, "container '%s' created sucessfully", name);
}
else {
if (c->resp.payload_size > 0) {
flb_plg_error(ctx->ins, "cannot create container '%s'\n%s",
name, c->resp.payload);
}
else {
flb_plg_error(ctx->ins, "cannot create container '%s'\n%s",
name, c->resp.payload);
}
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_FALSE;
}
flb_http_client_destroy(c);
flb_upstream_conn_release(u_conn);
return FLB_TRUE;
}
/*
* Check that the container exists, if it doesn't and the configuration property
* auto_create_container is enabled, it will send a request to create it. If it
* could not be created, it returns FLB_FALSE.
* If auto_create_container is disabled, it will return FLB_TRUE assuming the container
* already exists.
*/
static int ensure_container(struct flb_azure_blob *ctx)
{
int ret;
int status;
size_t b_sent;
flb_sds_t uri;
struct flb_http_client *c;
struct flb_connection *u_conn;
if (!ctx->auto_create_container) {
flb_plg_info(ctx->ins, "auto_create_container is disabled, assuming container '%s' already exists",
ctx->container_name);
return FLB_TRUE;
}
uri = azb_uri_ensure_or_create_container(ctx);
if (!uri) {
flb_plg_error(ctx->ins, "cannot create container URI");
return FLB_FALSE;
}
if (ctx->buffering_enabled == FLB_TRUE){
ctx->u->base.flags &= ~(FLB_IO_ASYNC);
ctx->u->base.net.io_timeout = ctx->io_timeout;
}
/* Get upstream connection */
u_conn = flb_upstream_conn_get(ctx->u);
if (!u_conn) {
flb_plg_error(ctx->ins,
"cannot create upstream connection for container check");
flb_sds_destroy(uri);
return FLB_FALSE;
}
/* Create HTTP client context */
c = flb_http_client(u_conn, FLB_HTTP_GET,
uri,
NULL, 0, NULL, 0, NULL, 0);
if (!c) {
flb_plg_error(ctx->ins, "cannot create HTTP client context");
flb_upstream_conn_release(u_conn);
return FLB_FALSE;
}
flb_http_strip_port_from_host(c);
/* Prepare headers and authentication */
azb_http_client_setup(ctx, c, -1, FLB_FALSE,
AZURE_BLOB_CT_NONE, AZURE_BLOB_CE_NONE);
/* Send HTTP request */
ret = flb_http_do(c, &b_sent);
flb_sds_destroy(uri);
if (ret == -1) {
flb_plg_error(ctx->ins, "error requesting container properties");
flb_upstream_conn_release(u_conn);
return FLB_FALSE;
}
status = c->resp.status;
flb_http_client_destroy(c);
/* Release connection */
flb_upstream_conn_release(u_conn);
/* Request was successful, validate HTTP status code */
if (status == 404) {
/* The container was not found, try to create it */
flb_plg_info(ctx->ins, "container '%s' not found, trying to create it",
ctx->container_name);
ret = create_container(ctx, ctx->container_name);
return ret;
}
else if (status == 200) {
flb_plg_info(ctx->ins, "container '%s' already exists", ctx->container_name);
return FLB_TRUE;
}
else if (status == 403) {
flb_plg_error(ctx->ins, "failed getting container '%s', access denied",
ctx->container_name);
return FLB_FALSE;
}
flb_plg_error(ctx->ins, "get container request failed, status=%i",
status);
return FLB_FALSE;
}
static int cb_azure_blob_init(struct flb_output_instance *ins,
struct flb_config *config, void *data)
{
struct flb_azure_blob *ctx = NULL;
(void) ins;
(void) config;
(void) data;
FLB_TLS_INIT(worker_info);
ctx = flb_azure_blob_conf_create(ins, config);
if (!ctx) {
return -1;
}
if (ctx->buffering_enabled == FLB_TRUE) {
ctx->ins = ins;
ctx->retry_time = 0;
/* Initialize local storage */
int ret = azure_blob_store_init(ctx);
if (ret == -1) {
flb_plg_error(ctx->ins, "Failed to initialize kusto storage: %s",
ctx->store_dir);
return -1;
}
/* validate 'total_file_size' */
if (ctx->file_size <= 0) {
flb_plg_error(ctx->ins, "Failed to parse upload_file_size");
return -1;
}
if (ctx->file_size < 1000000) {
flb_plg_error(ctx->ins, "upload_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 must be lower than %ld bytes", MAX_FILE_SIZE);
return -1;
}
ctx->has_old_buffers = azure_blob_store_has_data(ctx);
ctx->timer_created = FLB_FALSE;
ctx->timer_ms = (int) (ctx->upload_timeout / 6) * 1000;
flb_plg_info(ctx->ins, "Using upload size %lu bytes", ctx->file_size);
}
flb_output_set_context(ins, ctx);
flb_output_set_http_debug_callbacks(ins);
return 0;
}
static int blob_chunk_register_parts(struct flb_azure_blob *ctx, uint64_t file_id, size_t total_size)
{
int ret;
int64_t parts = 0;
int64_t id;
size_t offset_start = 0;
size_t offset_end = 0;
/* generate file parts */
while (offset_start < total_size) {
offset_end = offset_start + ctx->part_size;
/* do not exceed maximum size */
if (offset_end > total_size) {
offset_end = total_size;
}
/* insert part */
ret = azb_db_file_part_insert(ctx, file_id, parts, offset_start, offset_end, &id);
if (ret == -1) {
flb_plg_error(ctx->ins, "cannot insert blob file part into database");
return -1;
}
offset_start = offset_end;
parts++;
}
return parts;
}
static int process_blob_chunk(struct flb_azure_blob *ctx, struct flb_event_chunk *event_chunk)
{
int64_t ret;
int64_t file_id;
cfl_sds_t file_path = NULL;
cfl_sds_t source = NULL;
size_t file_size;
msgpack_object map;
struct flb_log_event_decoder log_decoder;
struct flb_log_event log_event;
if (ctx->db == NULL) {
flb_plg_error(ctx->ins, "Cannot process blob because this operation requires a database.");
return -1;
}
ret = flb_log_event_decoder_init(&log_decoder,
(char *) event_chunk->data,
event_chunk->size);
if (ret != FLB_EVENT_DECODER_SUCCESS) {
flb_plg_error(ctx->ins,
"Log event decoder initialization error : %i", (int) ret);
return -1;
}
while (flb_log_event_decoder_next(&log_decoder, &log_event) == FLB_EVENT_DECODER_SUCCESS) {
map = *log_event.body;
ret = flb_input_blob_file_get_info(map, &source, &file_path, &file_size);
if (ret == -1) {
flb_plg_error(ctx->ins, "cannot get file info from blob record, skipping");
continue;
}
ret = azb_db_file_insert(ctx, source, ctx->real_endpoint, file_path, file_size);
if (ret == -1) {
flb_plg_error(ctx->ins, "cannot insert blob file into database: %s (size=%lu)",
file_path, file_size);
cfl_sds_destroy(file_path);
cfl_sds_destroy(source);
continue;
}
cfl_sds_destroy(file_path);
cfl_sds_destroy(source);
/* generate the parts by using the newest id created (ret) */
file_id = ret;
ret = blob_chunk_register_parts(ctx, file_id, file_size);
if (ret == -1) {
flb_plg_error(ctx->ins, "cannot register blob file '%s 'parts into database",
file_path);
return -1;
}
flb_plg_debug(ctx->ins, "blob file '%s' (id=%zu) registered with %zu parts",
file_path, file_id, ret);
}
flb_log_event_decoder_destroy(&log_decoder);
return 0;
}
static void cb_azb_blob_file_upload(struct flb_config *config, void *out_context)
{
int ret;
char *out_buf = NULL;
size_t out_size;
uint64_t id;
uint64_t file_id;
uint64_t part_id;
uint64_t part_delivery_attempts;
uint64_t file_delivery_attempts;
off_t offset_start;
off_t offset_end;
cfl_sds_t file_destination = NULL;
cfl_sds_t file_path = NULL;
cfl_sds_t part_ids = NULL;
cfl_sds_t source = NULL;