Skip to content

Commit

Permalink
Log incorrect env vars
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Feb 24, 2025
1 parent 45d0c8a commit 2ce8ff9
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions src/config/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <limits.h>
// openFTLtoml()
#include "config/toml_helper.h"
// escape_json()
#include "webserver/http-common.h"
struct env_item
{
bool used;
Expand Down Expand Up @@ -174,6 +176,25 @@ void freeEnvVars(void)
}
}

/**
* @brief Marks an environment item as invalid and logs a warning message.
*
* @param envvar The value of the environment variable.
* @param conf_item A pointer to the configuration item structure.
* @param item A pointer to the environment item structure to be marked as invalid.
*/
static void invalid_item(const char *envvar, struct conf_item *conf_item, struct env_item *item)
{
item->error = "not an allowed option";
item->allowed = conf_item->h;
item->valid = false;

char *escaped_value = escape_json(envvar);
log_warn("ENV %s = \"%s\" is %s, allowed options are: %s",
conf_item->e, escaped_value, item->error, item->allowed);
free(escaped_value);
}

bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, struct config *newconf, cJSON *forced_vars, bool *reset)
{
// First check if a environmental variable with the given key exists by
Expand Down Expand Up @@ -386,11 +407,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -405,11 +422,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -424,11 +437,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -443,11 +452,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -462,11 +467,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -481,11 +482,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -500,11 +497,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -519,11 +512,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
else
{

item->error = "not an allowed option";
item->allowed = conf_item->h;
log_warn("ENV %s is %s, allowed options are: %s",
conf_item->e, item->error, item->allowed);
item->valid = false;
invalid_item(envvar, conf_item, item);
}
break;
}
Expand Down

0 comments on commit 2ce8ff9

Please sign in to comment.