@@ -88,12 +88,14 @@ static char *find_case_header(struct flb_http_client *c, const char *header)
8888
8989
9090 headstart = strstr (c -> resp .data , "\r\n" );
91+
9192 if (headstart == NULL ) {
9293 return NULL ;
9394 }
9495
9596 /* Lookup the beginning of the header */
9697 for (p = headstart ; p != NULL && p + 2 < c -> resp .payload ; p = strstr (p , "\r\n" )) {
98+
9799 if (p + 4 < c -> resp .payload && strcmp (p , "\r\n\r\n" ) == 0 ) {
98100 return NULL ;
99101 }
@@ -104,8 +106,10 @@ static char *find_case_header(struct flb_http_client *c, const char *header)
104106 if (p + strlen (header )+ 2 >= c -> resp .payload ) {
105107 return NULL ;
106108 }
109+
107110 // matched header and the delimiter
108111 if (strncasecmp (p , header , strlen (header )) == 0 ) {
112+
109113 if (p [strlen (header )] == ':' && p [strlen (header )+ 1 ] == ' ' ) {
110114 return p ;
111115 }
@@ -129,7 +133,9 @@ static int case_header_lookup(struct flb_http_client *c,
129133
130134 p = find_case_header (c , header );
131135 end = strstr (c -> resp .data , "\r\n\r\n" );
136+
132137 if (!p ) {
138+
133139 if (end ) {
134140 /* The headers are complete but the header is not there */
135141 return -1 ;
@@ -146,6 +152,7 @@ static int case_header_lookup(struct flb_http_client *c,
146152
147153 /* Lookup CRLF (end of line \r\n) */
148154 crlf = strstr (p , "\r\n" );
155+
149156 if (!crlf ) {
150157 return -1 ;
151158 }
@@ -168,9 +175,11 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,
168175 flb_sds_t cfgname ;
169176
170177 cfgname = flb_sds_create_size (4096 );
178+
171179 if (ctx -> fleet_name != NULL ) {
172180 flb_sds_printf (& cfgname , "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s.ini" , ctx -> config_dir , ctx -> fleet_name , fname );
173- } else {
181+ }
182+ else {
174183 flb_sds_printf (& cfgname , "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s.ini" , ctx -> config_dir , ctx -> fleet_id , fname );
175184 }
176185
@@ -209,7 +218,9 @@ static int is_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct
209218 if (cfg -> conf_path_file == NULL ) {
210219 return FLB_FALSE ;
211220 }
221+
212222 cfgnewname = new_fleet_config_filename (ctx );
223+
213224 if (strcmp (cfgnewname , cfg -> conf_path_file ) == 0 ) {
214225 ret = FLB_TRUE ;
215226 }
@@ -228,7 +239,9 @@ static int is_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct
228239 if (cfg -> conf_path_file == NULL ) {
229240 return FLB_FALSE ;
230241 }
242+
231243 cfgcurname = cur_fleet_config_filename (ctx );
244+
232245 if (strcmp (cfgcurname , cfg -> conf_path_file ) == 0 ) {
233246 ret = FLB_TRUE ;
234247 }
@@ -243,6 +256,7 @@ static int is_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_
243256 if (cfg -> conf_path_file == NULL ) {
244257 return FLB_FALSE ;
245258 }
259+
246260 return is_new_fleet_config (ctx , cfg ) || is_cur_fleet_config (ctx , cfg );
247261}
248262
@@ -293,16 +307,19 @@ static int test_config_is_valid(flb_sds_t cfgpath)
293307
294308
295309 config = flb_config_init ();
310+
296311 if (config == NULL ) {
297312 goto config_init_error ;
298313 }
299314
300315 cf = flb_cf_create ();
316+
301317 if (cf == NULL ) {
302318 goto cf_create_error ;
303319 }
304320
305321 cf = flb_cf_create_from_file (cf , cfgpath );
322+
306323 if (cf == NULL ) {
307324 goto cf_create_from_file_error ;
308325 }
@@ -339,6 +356,7 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf
339356 flb_plg_error (ctx -> ins , "unable to get fluent-bit context." );
340357 return FLB_FALSE ;
341358 }
359+
342360 // fix execution in valgrind...
343361 // otherwise flb_reload errors out with:
344362 // [error] [reload] given flb context is NULL
@@ -365,6 +383,7 @@ static char *tls_setting_string(int use_tls)
365383 if (use_tls ) {
366384 return "On" ;
367385 }
386+
368387 return "Off" ;
369388}
370389
@@ -405,12 +424,14 @@ static flb_sds_t parse_api_key_json(struct flb_in_calyptia_fleet_config *ctx,
405424
406425 msgpack_unpacked_init (& result );
407426 while (msgpack_unpack_next (& result , pack , out_size , & off ) == MSGPACK_UNPACK_SUCCESS ) {
427+
408428 if (result .data .type == MSGPACK_OBJECT_MAP ) {
409429 for (i = 0 ; i < result .data .via .map .size ; i ++ ) {
410430 cur = & result .data .via .map .ptr [i ];
411431 key = & cur -> key .via .str ;
412432
413433 if (strncmp (key -> ptr , "ProjectID" , key -> size ) == 0 ) {
434+
414435 if (cur -> val .type != MSGPACK_OBJECT_STR ) {
415436 flb_plg_error (ctx -> ins , "unable to find fleet by name" );
416437 msgpack_unpacked_destroy (& result );
@@ -469,14 +490,17 @@ static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx,
469490
470491 msgpack_unpacked_init (& result );
471492 while (msgpack_unpack_next (& result , pack , out_size , & off ) == MSGPACK_UNPACK_SUCCESS ) {
493+
472494 if (result .data .type == MSGPACK_OBJECT_ARRAY ) {
473495 results = & result .data .via .array ;
496+
474497 if (results -> ptr [0 ].type == MSGPACK_OBJECT_MAP ) {
475498 for (i = 0 ; i < results -> ptr [0 ].via .map .size ; i ++ ) {
476499 cur = & results -> ptr [0 ].via .map .ptr [i ];
477500 key = & cur -> key .via .str ;
478501
479502 if (strncasecmp (key -> ptr , "id" , key -> size ) == 0 ) {
503+
480504 if (cur -> val .type != MSGPACK_OBJECT_STR ) {
481505 flb_plg_error (ctx -> ins , "unable to find fleet by name" );
482506 msgpack_unpacked_destroy (& result );
@@ -499,6 +523,7 @@ static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx,
499523 if (ctx -> fleet_id == NULL ) {
500524 return -1 ;
501525 }
526+
502527 return 0 ;
503528}
504529
@@ -518,12 +543,14 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
518543 int ret ;
519544
520545 api_token_sep = strchr (ctx -> api_key , '.' );
546+
521547 if (api_token_sep == NULL ) {
522548 return -1 ;
523549 }
524550
525551 elen = api_token_sep - ctx -> api_key ;
526552 elen = elen + (4 - (elen % 4 ));
553+
527554 if (elen > sizeof (encoded )) {
528555 flb_plg_error (ctx -> ins , "API Token is too large" );
529556 return -1 ;
@@ -534,11 +561,13 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
534561
535562 ret = flb_base64_decode (token , sizeof (token )- 1 , & tlen ,
536563 encoded , elen );
564+
537565 if (ret != 0 ) {
538566 return ret ;
539567 }
540568
541569 project_id = parse_api_key_json (ctx , (char * )token , tlen );
570+
542571 if (project_id == NULL ) {
543572 return -1 ;
544573 }
@@ -549,6 +578,7 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
549578
550579 client = flb_http_client (u_conn , FLB_HTTP_GET , url , NULL , 0 ,
551580 ctx -> ins -> host .name , ctx -> ins -> host .port , NULL , 0 );
581+
552582 if (!client ) {
553583 flb_plg_error (ctx -> ins , "unable to create http client" );
554584 return -1 ;
@@ -561,6 +591,7 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
561591 ctx -> api_key , flb_sds_len (ctx -> api_key ));
562592
563593 ret = flb_http_do (client , & b_sent );
594+
564595 if (ret != 0 ) {
565596 flb_plg_error (ctx -> ins , "http do error" );
566597 return -1 ;
@@ -610,13 +641,15 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
610641 int ret = -1 ;
611642
612643 u_conn = flb_upstream_conn_get (ctx -> u );
644+
613645 if (!u_conn ) {
614646 flb_plg_error (ctx -> ins , "could not get an upstream connection to %s:%u" ,
615647 ctx -> ins -> host .name , ctx -> ins -> host .port );
616648 goto conn_error ;
617649 }
618650
619651 if (ctx -> fleet_id == NULL ) {
652+
620653 if (get_calyptia_fleet_id_by_name (ctx , u_conn , config ) == -1 ) {
621654 flb_plg_error (ctx -> ins , "unable to find fleet: %s" , ctx -> fleet_name );
622655 goto conn_error ;
@@ -631,6 +664,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
631664 client = flb_http_client (u_conn , FLB_HTTP_GET , ctx -> fleet_url ,
632665 NULL , 0 ,
633666 ctx -> ins -> host .name , ctx -> ins -> host .port , NULL , 0 );
667+
634668 if (!client ) {
635669 flb_plg_error (ins , "unable to create http client" );
636670 goto client_error ;
@@ -643,6 +677,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
643677 ctx -> api_key , flb_sds_len (ctx -> api_key ));
644678
645679 ret = flb_http_do (client , & b_sent );
680+
646681 if (ret != 0 ) {
647682 flb_plg_error (ins , "http do error" );
648683 goto http_error ;
@@ -660,6 +695,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
660695
661696 /* copy and NULL terminate the payload */
662697 data = flb_sds_create_size (client -> resp .payload_size + 1 );
698+
663699 if (!data ) {
664700 goto http_error ;
665701 }
@@ -668,6 +704,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
668704
669705 ret = case_header_lookup (client , "Last-modified" , strlen ("Last-modified" ),
670706 & fbit_last_modified , & fbit_last_modified_len );
707+
671708 if (ret == -1 ) {
672709 flb_plg_error (ctx -> ins , "unable to get last-modified header" );
673710 goto http_error ;
@@ -678,13 +715,17 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
678715 time_last_modified = mktime (& tm_last_modified .tm );
679716
680717 cfgname = time_fleet_config_filename (ctx , time_last_modified );
718+
681719 if (access (cfgname , F_OK ) == -1 && errno == ENOENT ) {
682720 cfgfp = fopen (cfgname , "w+" );
721+
683722 if (cfgfp == NULL ) {
684723 flb_plg_error (ctx -> ins , "unable to open configuration file: %s" , cfgname );
685724 goto http_error ;
686725 }
726+
687727 header = flb_sds_create_size (4096 );
728+
688729 if (ctx -> fleet_name == NULL ) {
689730 flb_sds_printf (& header ,
690731 "[CUSTOM]\n"
@@ -704,7 +745,8 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
704745 ctx -> ins -> host .port ,
705746 tls_setting_string (ctx -> ins -> use_tls )
706747 );
707- } else {
748+ }
749+ else {
708750 flb_sds_printf (& header ,
709751 "[CUSTOM]\n"
710752 " Name calyptia\n"
@@ -732,12 +774,14 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
732774 fclose (cfgfp );
733775
734776 cfgnewname = new_fleet_config_filename (ctx );
777+
735778 if (exists_new_fleet_config (ctx ) == FLB_TRUE ) {
736779 cfgoldname = old_fleet_config_filename (ctx );
737780 rename (cfgnewname , cfgoldname );
738781 unlink (cfgnewname );
739782 flb_sds_destroy (cfgoldname );
740783 }
784+
741785 link (cfgname , cfgnewname );
742786
743787 // FORCE THE RELOAD!!!
@@ -777,11 +821,14 @@ static void create_fleet_directory(struct flb_in_calyptia_fleet_config *ctx)
777821 }
778822
779823 myfleetdir = flb_sds_create_size (256 );
824+
780825 if (ctx -> fleet_name != NULL ) {
781826 flb_sds_printf (& myfleetdir , "%s" PATH_SEPARATOR "%s" , ctx -> config_dir , ctx -> fleet_name );
782- } else {
827+ }
828+ else {
783829 flb_sds_printf (& myfleetdir , "%s" PATH_SEPARATOR "%s" , ctx -> config_dir , ctx -> fleet_id );
784830 }
831+
785832 if (access (myfleetdir , F_OK )) {
786833 mkdir (myfleetdir , 0700 );
787834 }
@@ -818,13 +865,15 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
818865#endif
819866
820867 flb_plg_info (in , "initializing calyptia fleet input." );
868+
821869 if (in -> host .name == NULL ) {
822870 flb_plg_error (in , "no input 'Host' provided" );
823871 return -1 ;
824872 }
825873
826874 /* Allocate space for the configuration */
827875 ctx = flb_calloc (1 , sizeof (struct flb_in_calyptia_fleet_config ));
876+
828877 if (!ctx ) {
829878 flb_errno ();
830879 return -1 ;
@@ -834,6 +883,7 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
834883
835884 /* Load the config map */
836885 ret = flb_input_config_map_set (in , (void * )ctx );
886+
837887 if (ret == -1 ) {
838888 flb_free (ctx );
839889 flb_plg_error (in , "unable to load configuration" );
@@ -843,12 +893,14 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
843893#ifdef _WIN32
844894 if (ctx -> config_dir == NULL ) {
845895 tmpdir = getenv ("TEMP" );
896+
846897 if (tmpdir == NULL ) {
847898 flb_plg_error (in , "unable to find temporary directory (%%TEMP%%)." );
848899 return -1 ;
849900 }
850901
851902 ctx -> config_dir = flb_sds_create_size (4096 );
903+
852904 if (ctx -> config_dir == NULL ) {
853905 flb_plg_error (in , "unable to allocate config-dir." );
854906 return -1 ;
@@ -887,6 +939,7 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in,
887939 ctx -> interval_sec ,
888940 ctx -> interval_nsec ,
889941 config );
942+
890943 if (ret == -1 ) {
891944 flb_plg_error (ctx -> ins , "could not set collector for Health input plugin" );
892945 flb_free (ctx );
0 commit comments