-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathin_calyptia_fleet.c
2415 lines (1947 loc) · 61.9 KB
/
in_calyptia_fleet.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-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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <sys/stat.h>
#include <msgpack.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_custom.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_strptime.h>
#include <fluent-bit/flb_reload.h>
#include <fluent-bit/flb_lib.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/config_format/flb_cf_fluentbit.h>
#include <fluent-bit/flb_base64.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/calyptia/calyptia_constants.h>
#include "in_calyptia_fleet.h"
/* Glob support */
#ifndef _MSC_VER
#include <glob.h>
#endif
#ifdef _WIN32
#include <Windows.h>
#include <strsafe.h>
#define PATH_MAX MAX_PATH
#endif
#define DEFAULT_INTERVAL_SEC "15"
#define DEFAULT_INTERVAL_NSEC "0"
#define DEFAULT_MAX_HTTP_BUFFER_SIZE "10485760"
static int fleet_cur_chdir(struct flb_in_calyptia_fleet_config *ctx);
static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
time_t timestamp);
#ifndef FLB_SYSTEM_WINDOWS
static int is_link(const char *path) {
struct stat st = { 0 };
if (lstat(path, &st) != 0) {
return -1;
}
if ((st.st_mode & S_IFMT) == S_IFLNK) {
return FLB_TRUE;
}
return FLB_FALSE;
}
#else
/* symlinks are too difficult to use on win32 so we skip their use entirely. */
static int is_link(const char *path) {
return FLB_FALSE;
}
#endif
static char *find_case_header(struct flb_http_client *cli, const char *header)
{
char *ptr;
char *headstart;
headstart = strstr(cli->resp.data, "\r\n");
if (headstart == NULL) {
return NULL;
}
/* Lookup the beginning of the header */
for (ptr = headstart; ptr != NULL && ptr+2 < cli->resp.payload; ptr = strstr(ptr, "\r\n")) {
if (ptr + 4 < cli->resp.payload && strcmp(ptr, "\r\n\r\n") == 0) {
return NULL;
}
ptr+=2;
/* no space left for header */
if (ptr + strlen(header)+2 >= cli->resp.payload) {
return NULL;
}
/* matched header and the delimiter */
if (strncasecmp(ptr, header, strlen(header)) == 0) {
if (ptr[strlen(header)] == ':' && ptr[strlen(header)+1] == ' ') {
return ptr;
}
}
}
return NULL;
}
/* Try to find a header value in the buffer. Copied from flb_http_client.c. */
static int case_header_lookup(struct flb_http_client *cli,
const char *header, int header_len,
const char **out_val, int *out_len)
{
char *ptr;
char *crlf;
char *end;
if (!cli->resp.data) {
return -1;
}
ptr = find_case_header(cli, header);
end = strstr(cli->resp.data, "\r\n\r\n");
if (!ptr) {
if (end) {
/* The headers are complete but the header is not there */
return -1;
}
/* We need more data */
return -1;
}
/* Exclude matches in the body */
if (end && ptr > end) {
return -1;
}
/* Lookup CRLF (end of line \r\n) */
crlf = strstr(ptr, "\r\n");
if (!crlf) {
return -1;
}
/* sanity check that the header_len does not exceed the headers. */
if (ptr + header_len + 2 > end) {
return -1;
}
ptr += header_len + 2;
*out_val = ptr;
*out_len = (crlf - ptr);
return 0;
}
static flb_sds_t generate_base_fleet_directory(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t *fleet_dir)
{
if (fleet_dir == NULL) {
return NULL;
}
if (*fleet_dir == NULL) {
*fleet_dir = flb_sds_create_size(CALYPTIA_MAX_DIR_SIZE);
if (*fleet_dir == NULL) {
return NULL;
}
}
/* Ensure we have a valid value */
if (ctx->config_dir == NULL) {
ctx->config_dir = FLEET_DEFAULT_CONFIG_DIR;
}
if (ctx->fleet_name != NULL) {
return flb_sds_printf(fleet_dir, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s",
ctx->config_dir, ctx->machine_id, ctx->fleet_name);
}
return flb_sds_printf(fleet_dir, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s",
ctx->config_dir, ctx->machine_id, ctx->fleet_id);
}
flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *fname)
{
flb_sds_t cfgname = NULL;
flb_sds_t ret;
if (ctx == NULL || fname == NULL) {
return NULL;
}
if (generate_base_fleet_directory(ctx, &cfgname) == NULL) {
return NULL;
}
if (ctx->fleet_config_legacy_format) {
ret = flb_sds_printf(&cfgname, PATH_SEPARATOR "%s.conf", fname);
}
else {
ret = flb_sds_printf(&cfgname, PATH_SEPARATOR "%s.yaml", fname);
}
if (ret == NULL) {
flb_sds_destroy(cfgname);
return NULL;
}
return cfgname;
}
static flb_sds_t time_fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, time_t t)
{
char s_last_modified[32];
snprintf(s_last_modified, sizeof(s_last_modified)-1, "%d", (int)t);
return fleet_config_filename(ctx, s_last_modified);
}
static int is_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_config *cfg)
{
flb_sds_t cfgnewname;
int ret = FLB_FALSE;
if (cfg == NULL) {
return FLB_FALSE;
}
if (cfg->conf_path_file == NULL) {
return FLB_FALSE;
}
cfgnewname = new_fleet_config_filename(ctx);
if (cfgnewname == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
if (strcmp(cfgnewname, cfg->conf_path_file) == 0) {
ret = FLB_TRUE;
}
flb_sds_destroy(cfgnewname);
return ret;
}
static int is_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_config *cfg)
{
flb_sds_t cfgcurname;
int ret = FLB_FALSE;
if (cfg == NULL) {
return FLB_FALSE;
}
if (cfg->conf_path_file == NULL) {
return FLB_FALSE;
}
cfgcurname = cur_fleet_config_filename(ctx);
if (cfgcurname == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
if (strcmp(cfgcurname, cfg->conf_path_file) == 0) {
ret = FLB_TRUE;
}
flb_sds_destroy(cfgcurname);
return ret;
}
static int is_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_config *cfg)
{
flb_sds_t cfgcurname;
int ret = FLB_FALSE;
if (cfg == NULL) {
return FLB_FALSE;
}
if (cfg->conf_path_file == NULL) {
return FLB_FALSE;
}
cfgcurname = old_fleet_config_filename(ctx);
if (cfgcurname == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
if (strcmp(cfgcurname, cfg->conf_path_file) == 0) {
ret = FLB_TRUE;
}
flb_sds_destroy(cfgcurname);
return ret;
}
static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config *ctx, const char *path)
{
char *fname;
char *end;
long val;
if (path == NULL || ctx == NULL) {
return FLB_FALSE;
}
fname = strrchr(path, PATH_SEPARATOR[0]);
if (fname == NULL) {
return FLB_FALSE;
}
fname++;
errno = 0;
val = strtol(fname, &end, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
return FLB_FALSE;
}
if (ctx->fleet_config_legacy_format) {
if (strcmp(end, ".conf") == 0) {
return FLB_TRUE;
}
}
else if (strcmp(end, ".yaml") == 0) {
return FLB_TRUE;
}
return FLB_FALSE;
}
static int is_timestamped_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_config *cfg)
{
if (cfg == NULL) {
return FLB_FALSE;
}
if (cfg->conf_path_file == NULL) {
return FLB_FALSE;
}
return is_timestamped_fleet_config_path(ctx, cfg->conf_path_file);
}
static int is_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_config *cfg)
{
if (cfg == NULL) {
return FLB_FALSE;
}
if (cfg->conf_path_file == NULL) {
return FLB_FALSE;
}
return is_new_fleet_config(ctx, cfg) ||
is_cur_fleet_config(ctx, cfg) ||
is_old_fleet_config(ctx, cfg) ||
is_timestamped_fleet_config(ctx, cfg);
}
static int exists_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
int ret = FLB_FALSE;
flb_sds_t cfgnewname;
cfgnewname = new_fleet_config_filename(ctx);
if (cfgnewname == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
ret = access(cfgnewname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE;
flb_sds_destroy(cfgnewname);
return ret;
}
static int exists_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t cfgcurname;
int ret = FLB_FALSE;
cfgcurname = cur_fleet_config_filename(ctx);
if (cfgcurname == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
ret = access(cfgcurname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE;
flb_sds_destroy(cfgcurname);
return ret;
}
static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
int ret = FLB_FALSE;
flb_sds_t cfgoldname;
cfgoldname = old_fleet_config_filename(ctx);
if (cfgoldname == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
ret = access(cfgoldname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE;
flb_sds_destroy(cfgoldname);
return ret;
}
static int exists_header_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
int ret = FLB_FALSE;
flb_sds_t cfgheadername;
cfgheadername = hdr_fleet_config_filename(ctx);
if (cfgheadername == NULL) {
flb_plg_error(ctx->ins, "unable to allocate configuration name");
return FLB_FALSE;
}
ret = access(cfgheadername, F_OK) == 0 ? FLB_TRUE : FLB_FALSE;
flb_sds_destroy(cfgheadername);
return ret;
}
static void *do_reload(void *data)
{
struct reload_ctx *reload = (struct reload_ctx *)data;
if (reload == NULL) {
return NULL;
}
/* avoid reloading the current configuration... just use our new one! */
flb_context_set(reload->flb);
reload->flb->config->enable_hot_reload = FLB_TRUE;
if (reload->flb->config->conf_path_file) {
flb_sds_destroy(reload->flb->config->conf_path_file);
}
reload->flb->config->conf_path_file = reload->cfg_path;
sleep(5);
#ifndef FLB_SYSTEM_WINDOWS
flb_free(reload);
kill(getpid(), SIGHUP);
#else
/* using the refactor that placed `bin_restarting` inside
* `flb_config` to use it as a messaging mechanism instead of
* GenerateConsoleCtrlEvent to overcome the fact that windows
* services do not have a console and therefore cannot
* react to console events or handle them (fixes sc-112185).
*/
reload->flb->config->bin_restarting = FLB_RELOAD_IN_PROGRESS;
flb_free(reload);
#endif
return NULL;
}
static int test_config_is_valid(struct flb_in_calyptia_fleet_config *ctx,
flb_sds_t cfgpath)
{
return FLB_TRUE;
struct flb_cf *conf;
int ret = FLB_FALSE;
if (cfgpath == NULL) {
return FLB_FALSE;
}
conf = flb_cf_create();
if (conf == NULL) {
flb_plg_debug(ctx->ins, "unable to create config during validation test: %s",
cfgpath);
goto config_init_error;
}
conf = flb_cf_create_from_file(conf, cfgpath);
if (conf == NULL) {
flb_plg_debug(ctx->ins,
"unable to create config from file during validation test: %s",
cfgpath);
goto cf_create_from_file_error;
}
ret = FLB_TRUE;
cf_create_from_file_error:
flb_cf_destroy(conf);
config_init_error:
return ret;
}
static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx,
const char *cfgpath,
long *config_timestamp)
{
char *ext = NULL;
long timestamp;
char realname[CALYPTIA_MAX_DIR_SIZE] = {0};
char *fname;
ssize_t len;
if (ctx == NULL || config_timestamp == NULL || cfgpath == NULL) {
return FLB_FALSE;
}
switch (is_link(cfgpath)) {
/* Prevent undefined references due to use of readlink */
#ifndef FLB_SYSTEM_WINDOWS
case FLB_TRUE:
len = readlink(cfgpath, realname, sizeof(realname));
if (len > sizeof(realname)) {
return FLB_FALSE;
}
break;
#endif /* FLB_SYSTEM_WINDOWS */
case FLB_FALSE:
strncpy(realname, cfgpath, sizeof(realname)-1);
break;
default:
flb_errno();
return FLB_FALSE;
}
fname = basename(realname);
flb_plg_debug(ctx->ins, "parsing configuration timestamp from path: %s", fname);
errno = 0;
timestamp = strtol(fname, &ext, 10);
if ((errno == ERANGE && (timestamp == LONG_MAX || timestamp == LONG_MIN)) ||
(errno != 0 && timestamp == 0)) {
flb_errno();
return FLB_FALSE;
}
/* unable to parse the timstamp */
if (errno == ERANGE) {
return FLB_FALSE;
}
*config_timestamp = timestamp;
return FLB_TRUE;
}
static int parse_config_timestamp(struct flb_in_calyptia_fleet_config *ctx,
long *config_timestamp)
{
flb_ctx_t *flb_ctx = flb_context_get();
if (ctx == NULL || config_timestamp == NULL) {
return FLB_FALSE;
}
return parse_config_name_timestamp(ctx, flb_ctx->config->conf_path_file, config_timestamp);
}
static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cfgpath)
{
struct reload_ctx *reload;
pthread_t pth;
pthread_attr_t ptha;
flb_ctx_t *flb = flb_context_get();
if (parse_config_name_timestamp(ctx, cfgpath, &ctx->config_timestamp) != FLB_TRUE) {
flb_sds_destroy(cfgpath);
return FLB_FALSE;
}
reload = flb_calloc(1, sizeof(struct reload_ctx));
reload->flb = flb;
reload->cfg_path = cfgpath;
if (ctx->collect_fd > 0) {
flb_input_collector_pause(ctx->collect_fd, ctx->ins);
}
if (flb == NULL) {
flb_plg_error(ctx->ins, "unable to get fluent-bit context.");
if (ctx->collect_fd > 0) {
flb_input_collector_resume(ctx->collect_fd, ctx->ins);
}
flb_sds_destroy(cfgpath);
return FLB_FALSE;
}
/* fix execution in valgrind...
* otherwise flb_reload errors out with:
* [error] [reload] given flb context is NULL
*/
flb_plg_info(ctx->ins, "loading configuration from %s.", reload->cfg_path);
if (test_config_is_valid(ctx, reload->cfg_path) == FLB_FALSE) {
flb_plg_error(ctx->ins, "unable to load configuration.");
if (ctx->collect_fd > 0) {
flb_input_collector_resume(ctx->collect_fd, ctx->ins);
}
flb_sds_destroy(cfgpath);
return FLB_FALSE;
}
if (fleet_cur_chdir(ctx) == -1) {
flb_errno();
flb_plg_error(ctx->ins, "unable to change to configuration directory");
}
fleet_cur_chdir(ctx);
pthread_attr_init(&ptha);
pthread_attr_setdetachstate(&ptha, PTHREAD_CREATE_DETACHED);
pthread_create(&pth, &ptha, do_reload, reload);
return FLB_TRUE;
}
static msgpack_object *msgpack_lookup_map_key(msgpack_object *obj, const char *keyname)
{
int idx;
msgpack_object_kv *cur;
msgpack_object_str *key;
if (obj == NULL || keyname == NULL) {
return NULL;
}
if (obj->type != MSGPACK_OBJECT_MAP) {
return NULL;
}
for (idx = 0; idx < obj->via.map.size; idx++) {
cur = &obj->via.map.ptr[idx];
if (cur->key.type != MSGPACK_OBJECT_STR) {
continue;
}
key = &cur->key.via.str;
if (key->size != strlen(keyname)) {
continue;
}
if (strncmp(key->ptr, keyname, key->size) == 0) {
return &cur->val;
}
}
return NULL;
}
static msgpack_object *msgpack_lookup_array_offset(msgpack_object *obj, size_t offset)
{
if (obj == NULL) {
return NULL;
}
if (obj->type != MSGPACK_OBJECT_ARRAY) {
return NULL;
}
if (obj->via.array.size <= offset) {
return NULL;
}
return &obj->via.array.ptr[offset];
}
static flb_sds_t parse_api_key_json(struct flb_in_calyptia_fleet_config *ctx,
char *payload, size_t size)
{
int ret;
int out_size;
char *pack;
struct flb_pack_state pack_state;
size_t off = 0;
msgpack_unpacked result;
msgpack_object *tmp;
flb_sds_t project_id = NULL;
if (ctx == NULL || payload == NULL) {
return NULL;
}
/* Initialize packer */
flb_pack_state_init(&pack_state);
/* Pack JSON as msgpack */
ret = flb_pack_json_state(payload, size,
&pack, &out_size, &pack_state);
flb_pack_state_reset(&pack_state);
/* Handle exceptions */
if (ret == FLB_ERR_JSON_PART || ret == FLB_ERR_JSON_INVAL || ret == -1) {
flb_plg_warn(ctx->ins, "invalid JSON message, skipping");
return NULL;
}
msgpack_unpacked_init(&result);
while (msgpack_unpack_next(&result, pack, out_size, &off) == MSGPACK_UNPACK_SUCCESS) {
tmp = msgpack_lookup_map_key(&result.data, "ProjectID");
if (tmp == NULL) {
flb_plg_error(ctx->ins, "unable to find fleet by name");
msgpack_unpacked_destroy(&result);
return NULL;
}
if (tmp->type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "invalid fleet ID data type");
msgpack_unpacked_destroy(&result);
return NULL;
}
project_id = flb_sds_create_len(tmp->via.str.ptr, tmp->via.str.size);
break;
}
msgpack_unpacked_destroy(&result);
flb_free(pack);
return project_id;
}
static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx,
char *payload, size_t size)
{
int ret;
int out_size;
char *pack;
struct flb_pack_state pack_state;
size_t off = 0;
msgpack_unpacked result;
msgpack_object *map;
msgpack_object *fleet;
if (ctx == NULL || payload == NULL) {
return -1;
}
/* Initialize packer */
flb_pack_state_init(&pack_state);
/* Pack JSON as msgpack */
ret = flb_pack_json_state(payload, size,
&pack, &out_size, &pack_state);
flb_pack_state_reset(&pack_state);
/* Handle exceptions */
if (ret == FLB_ERR_JSON_PART || ret == FLB_ERR_JSON_INVAL || ret == -1) {
flb_plg_warn(ctx->ins, "invalid JSON message, skipping");
return -1;
}
msgpack_unpacked_init(&result);
while (msgpack_unpack_next(&result, pack, out_size, &off) == MSGPACK_UNPACK_SUCCESS) {
map = msgpack_lookup_array_offset(&result.data, 0);
if (map == NULL) {
break;
}
fleet = msgpack_lookup_map_key(map, "id");
if (fleet == NULL) {
flb_plg_error(ctx->ins, "unable to find fleet by name");
break;
}
if (fleet->type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "unable to find fleet by name");
break;
}
ctx->fleet_id = flb_sds_create_len(fleet->via.str.ptr, fleet->via.str.size);
ctx->fleet_id_found = FLB_TRUE;
break;
}
msgpack_unpacked_destroy(&result);
flb_free(pack);
if (ctx->fleet_id == NULL) {
return -1;
}
return 0;
}
static flb_sds_t get_project_id_from_api_key(struct flb_in_calyptia_fleet_config *ctx)
{
unsigned char encoded[256];
unsigned char token[512] = {0};
char *api_token_sep;
size_t tlen;
size_t elen;
int ret;
if (ctx == NULL) {
return NULL;
}
api_token_sep = strchr(ctx->api_key, '.');
if (api_token_sep == NULL) {
return NULL;
}
elen = api_token_sep-ctx->api_key;
elen = elen + (4 - (elen % 4));
if (elen > sizeof(encoded)) {
flb_plg_error(ctx->ins, "API Token is too large");
return NULL;
}
memset(encoded, '=', sizeof(encoded));
memcpy(encoded, ctx->api_key, api_token_sep-ctx->api_key);
ret = flb_base64_decode(token, sizeof(token)-1, &tlen,
encoded, elen);
if (ret != 0) {
return NULL;
}
return parse_api_key_json(ctx, (char *)token, tlen);
}
static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config *ctx,
flb_sds_t url)
{
int ret = -1;
size_t b_sent;
struct flb_connection *u_conn;
struct flb_http_client *client;
if (ctx == NULL || url == NULL) {
return NULL;
}
u_conn = flb_upstream_conn_get(ctx->u);
if (u_conn == NULL) {
flb_plg_error(ctx->ins, "unable to get upstream connection");
return NULL;
}
client = flb_http_client(u_conn, FLB_HTTP_GET, url, NULL, 0,
ctx->ins->host.name, ctx->ins->host.port, NULL, 0);
if (!client) {
flb_plg_error(ctx->ins, "unable to create http client");
goto http_client_error;
}
flb_http_buffer_size(client, ctx->max_http_buffer_size);
flb_http_add_header(client,
CALYPTIA_HEADERS_PROJECT, sizeof(CALYPTIA_HEADERS_PROJECT) - 1,
ctx->api_key, flb_sds_len(ctx->api_key));
ret = flb_http_do(client, &b_sent);
if (ret != 0) {
flb_plg_error(ctx->ins, "http do error");
goto http_do_error;
}
if (client->resp.status != 200) {
flb_plg_error(ctx->ins, "search http status code error: %d", client->resp.status);
goto http_do_error;
}
if (client->resp.payload_size <= 0) {
flb_plg_error(ctx->ins, "empty response");
goto http_do_error;
}
flb_upstream_conn_release(u_conn);
return client;
http_do_error:
flb_http_client_destroy(client);
http_client_error:
flb_upstream_conn_release(u_conn);
return NULL;
}
static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ctx,
struct flb_config *config)
{
struct flb_http_client *client;
flb_sds_t url;
flb_sds_t project_id;
if (ctx == NULL || config == NULL) {
return -1;
}
project_id = get_project_id_from_api_key(ctx);
if (project_id == NULL) {
return -1;
}
url = flb_sds_create_size(CALYPTIA_MAX_DIR_SIZE);
if (url == NULL) {
flb_sds_destroy(project_id);
return -1;
}
flb_sds_printf(&url, CALYPTIA_ENDPOINT_FLEET_BY_NAME,
project_id, ctx->fleet_name);
client = fleet_http_do(ctx, url);
flb_sds_destroy(url);
if (!client) {
flb_sds_destroy(project_id);
return -1;
}
if (parse_fleet_search_json(ctx, client->resp.payload, client->resp.payload_size) == -1) {
flb_plg_error(ctx->ins, "unable to find fleet: %s", ctx->fleet_name);
flb_http_client_destroy(client);
flb_sds_destroy(project_id);
return -1;
}
flb_http_client_destroy(client);
flb_sds_destroy(project_id);
if (ctx->fleet_id == NULL) {
return -1;
}
return 0;
}
static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx,
flb_sds_t url,
const char *hdr,
const char *dst,
time_t *time_last_modified)
{
struct flb_http_client *client;
size_t len;
FILE *fp;
int ret = -1;
const char *fbit_last_modified;
struct flb_tm tm_last_modified = { 0 };
int fbit_last_modified_len;
time_t last_modified;
flb_sds_t fname;
if (ctx == NULL || url == NULL) {
return -1;
}
client = fleet_http_do(ctx, url);
if (client == NULL) {
return -1;
}
ret = case_header_lookup(client, "Last-modified", strlen("Last-modified"),
&fbit_last_modified, &fbit_last_modified_len);
if (ret < 0) {
goto client_error;
}
if (dst == NULL) {