Skip to content

Commit

Permalink
in_calyptia_fleet: improve return value and parameter checking.
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan committed Oct 26, 2023
1 parent 2182ddf commit 083b2b0
Showing 1 changed file with 89 additions and 4 deletions.
93 changes: 89 additions & 4 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,
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;
}
Expand Down Expand Up @@ -257,11 +261,19 @@ static int is_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct
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;
Expand All @@ -277,12 +289,19 @@ static int is_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct
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;
Expand All @@ -299,6 +318,10 @@ static int is_timestamped_fleet_config(struct flb_in_calyptia_fleet_config *ctx,
char *end;
long val;

if (cfg == NULL) {
return FLB_FALSE;
}

if (cfg->conf_path_file == NULL) {
return FLB_FALSE;
}
Expand Down Expand Up @@ -329,6 +352,10 @@ static int is_timestamped_fleet_config(struct flb_in_calyptia_fleet_config *ctx,

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;
}
Expand All @@ -345,6 +372,11 @@ static int exists_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx)


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);
Expand All @@ -358,6 +390,11 @@ static int exists_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx)


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);
Expand All @@ -371,6 +408,11 @@ static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx)


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);
Expand All @@ -381,6 +423,10 @@ 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;
Expand All @@ -401,6 +447,9 @@ static int test_config_is_valid(flb_sds_t cfgpath)
struct flb_cf *conf;
int ret = FLB_FALSE;

if (cfgpath == NULL) {
return FLB_FALSE;
}

config = flb_config_init();

Expand Down Expand Up @@ -450,7 +499,7 @@ static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx,
char *fname;
ssize_t len;

if (config_timestamp == NULL || cfgpath == NULL) {
if (ctx == NULL || config_timestamp == NULL || cfgpath == NULL) {
return FLB_FALSE;
}

Expand Down Expand Up @@ -486,7 +535,7 @@ static int parse_config_timestamp(struct flb_in_calyptia_fleet_config *ctx,
{
flb_ctx_t *flb_ctx = flb_context_get();

if (config_timestamp == NULL) {
if (ctx == NULL || config_timestamp == NULL) {
return FLB_FALSE;
}

Expand All @@ -501,7 +550,9 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf
flb_ctx_t *flb = flb_context_get();


parse_config_name_timestamp(ctx, cfgpath, &ctx->config_timestamp);
if (parse_config_name_timestamp(ctx, cfgpath, &ctx->config_timestamp) != FLB_TRUE) {
return FLB_FALSE;
}

if (ctx->collect_fd > 0) {
flb_input_collector_pause(ctx->collect_fd, ctx->ins);
Expand Down Expand Up @@ -614,6 +665,10 @@ static flb_sds_t parse_api_key_json(struct flb_in_calyptia_fleet_config *ctx,
msgpack_object *projectID;
flb_sds_t project_id = NULL;

if (ctx == NULL || payload == NULL) {
return NULL;
}

/* Initialize packer */
flb_pack_state_init(&pack_state);

Expand Down Expand Up @@ -667,6 +722,10 @@ static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx,
msgpack_object *map;
msgpack_object *fleet;

if (ctx == NULL || payload == NULL) {
return -1;
}

/* Initialize packer */
flb_pack_state_init(&pack_state);

Expand Down Expand Up @@ -722,6 +781,10 @@ static flb_sds_t get_project_id_from_api_key(struct flb_in_calyptia_fleet_config
size_t elen;
int ret;

if (ctx == NULL) {
return NULL;
}

api_token_sep = strchr(ctx->api_key, '.');

if (api_token_sep == NULL) {
Expand Down Expand Up @@ -757,6 +820,10 @@ static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config
size_t b_sent;
int ret = -1;

if (ctx == NULL || u_conn == NULL || url == NULL) {
return NULL;
}

client = flb_http_client(u_conn, FLB_HTTP_GET, url, NULL, 0,
ctx->ins->host.name, ctx->ins->host.port, NULL, 0);

Expand Down Expand Up @@ -804,6 +871,10 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
flb_sds_t url;
flb_sds_t project_id;

if (ctx == NULL || u_conn == NULL || config == NULL) {
return -1;
}

project_id = get_project_id_from_api_key(ctx);

if (project_id == NULL) {
Expand Down Expand Up @@ -1103,6 +1174,9 @@ static int calyptia_config_add(struct flb_in_calyptia_fleet_config *ctx,
flb_sds_t cfgoldname;

cfgnewname = new_fleet_config_filename(ctx);
if (cfgnewname == NULL) {
return -1;
}

if (exists_new_fleet_config(ctx) == FLB_TRUE) {
cfgoldname = old_fleet_config_filename(ctx);
Expand All @@ -1111,7 +1185,10 @@ static int calyptia_config_add(struct flb_in_calyptia_fleet_config *ctx,
flb_sds_destroy(cfgoldname);
}

symlink(cfgname, cfgnewname);
if (symlink(cfgname, cfgnewname)) {
flb_sds_destroy(cfgnewname);
return -1;
}
flb_sds_destroy(cfgnewname);

return 0;
Expand Down Expand Up @@ -1152,6 +1229,10 @@ static int calyptia_config_delete_old_dir(const char *cfgpath)
struct cfl_array *files;
int idx;

if (cfgpath == NULL) {
return FLB_FALSE;
}

ext = strrchr(cfgpath, '.');
if (ext == NULL) {
return FLB_FALSE;
Expand Down Expand Up @@ -1188,6 +1269,10 @@ static int calyptia_config_delete_old(struct flb_in_calyptia_fleet_config *ctx)
flb_sds_t glob_ini = NULL;
int idx;

if (ctx == NULL) {
return -1;
}

if (generate_base_fleet_directory(ctx, &glob_ini) == NULL) {
flb_sds_destroy(glob_ini);
return -1;
Expand Down

0 comments on commit 083b2b0

Please sign in to comment.