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 <phil@calyptia.com>
  • Loading branch information
pwhelan committed Nov 3, 2023
1 parent 5e7ba78 commit 0fa3f79
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 @@ -224,6 +224,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 @@ -255,11 +259,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 @@ -275,12 +287,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 Down Expand Up @@ -326,6 +345,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 @@ -356,6 +379,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 @@ -373,6 +400,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 @@ -386,6 +418,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 @@ -399,6 +436,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 @@ -409,6 +451,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 @@ -429,6 +475,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 @@ -478,7 +527,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 @@ -514,7 +563,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 @@ -529,7 +578,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 @@ -642,6 +693,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 @@ -695,6 +750,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 @@ -750,6 +809,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 @@ -785,6 +848,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 @@ -832,6 +899,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 @@ -1131,6 +1202,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 @@ -1139,7 +1213,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 @@ -1180,6 +1257,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 @@ -1216,6 +1297,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 0fa3f79

Please sign in to comment.