Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print values of env vars if they are invalid #2273

Merged
merged 4 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 61 additions & 74 deletions src/config/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
#include <limits.h>
// openFTLtoml()
#include "config/toml_helper.h"
// escape_json()
#include "webserver/http-common.h"
struct env_item
{
bool used;
bool valid;
bool used :1;
bool valid :1;
bool error_allocated :1;
char *key;
char *value;
const char *error;
const char *allowed;
char *error;
struct env_item *next;
};

Expand Down Expand Up @@ -77,7 +79,6 @@ void getEnvVars(void)
new_item->key = strdup(key);
new_item->value = strdup(value);
new_item->error = NULL;
new_item->allowed = NULL;
new_item->next = env_list;
env_list = new_item;

Expand Down Expand Up @@ -120,15 +121,15 @@ void printFTLenv(void)
log_info(" %s %s is used", cli_tick(), item->key);
else
{
if(item->error != NULL && item->allowed == NULL)
log_err(" %s %s is invalid (%s)",
cli_cross(), item->key, item->error);
else if(item->error != NULL && item->allowed != NULL)
log_err(" %s %s is invalid (%s, allowed options are: %s)",
cli_cross(), item->key, item->error, item->allowed);
if(item->error != NULL)
log_err(" %s %s %s",
cli_cross(), item->key, item->error);
else
log_err(" %s %s is invalid",
cli_cross(), item->key);

if(item->error_allocated)
free(item->error);
}

continue;
Expand Down Expand Up @@ -174,6 +175,35 @@ 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_enum_item(const char *envvar, struct conf_item *conf_item, struct env_item *item)
{
item->valid = false;

cJSON *allowed_items = cJSON_CreateArray();
cJSON *it = NULL;
cJSON_ArrayForEach(it, conf_item->a)
{
cJSON *sub_item = cJSON_GetObjectItem(it, "item");
cJSON_AddItemToArray(allowed_items, cJSON_Duplicate(sub_item, true));
}
char *allowed_values = cJSON_PrintUnformatted(allowed_items);
char *escaped_value = escape_json(envvar);

// Calculate the size of the error message
asprintf(&item->error, "= %s is invalid, allowed options are: %s",
escaped_value, allowed_values);

free(escaped_value);
free(allowed_values);
}

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 @@ -244,8 +274,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type bool";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not a boolean";
item->valid = false;
}
break;
Expand All @@ -264,8 +293,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type bool";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not a boolean";
item->valid = false;
}
break;
Expand All @@ -280,8 +308,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type integer";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not an integer";
item->valid = false;
}
break;
Expand All @@ -296,8 +323,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type unsigned integer";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not an unsigned integer";
item->valid = false;
}
break;
Expand All @@ -312,8 +338,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type unsigned integer (16 bit";
log_warn("ENV %s is %s)", conf_item->e, item->error);
item->error = (char *)"is not an unsigned integer (16 bit)";
item->valid = false;
}
break;
Expand All @@ -328,8 +353,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type long";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not a long integer";
item->valid = false;
}
break;
Expand All @@ -344,8 +368,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type unsigned long";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not an unsigned long integer";
item->valid = false;
}
break;
Expand All @@ -360,8 +383,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type double";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not a double";
item->valid = false;
}
break;
Expand All @@ -386,11 +408,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -405,11 +423,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -424,11 +438,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -443,11 +453,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -462,11 +468,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -481,11 +483,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -500,11 +498,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -519,11 +513,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_enum_item(envvar, conf_item, item);
}
break;
}
Expand All @@ -537,8 +527,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type integer or outside allowed bounds";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not an integer or outside allowed bounds";
item->valid = false;
}
break;
Expand All @@ -558,8 +547,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type IPv4 address";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not an IPv4 address";
item->valid = false;
}
break;
Expand All @@ -579,8 +567,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
}
else
{
item->error = "not of type IPv6 address";
log_warn("ENV %s is %s", conf_item->e, item->error);
item->error = (char *)"is not an IPv6 address";
item->valid = false;
}
break;
Expand Down Expand Up @@ -617,7 +604,7 @@ bool __attribute__((nonnull(1,2,3))) readEnvValue(struct conf_item *conf_item, s
{
if(!set_and_check_password(conf_item, envvar))
{
log_warn("ENV %s is invalid", conf_item->e);
item->error = (char *)"is not a valid password";
item->valid = false;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions test/test_suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@
@test "No ERROR messages in FTL.log (besides known/intended error)" {
run bash -c 'grep "ERROR: " /var/log/pihole/FTL.log'
printf "%s\n" "${lines[@]}"
run bash -c 'grep "ERROR: " /var/log/pihole/FTL.log | grep -c -v -E "(index\.html)|(Failed to create shared memory object)|(FTLCONF_debug_api is invalid)|(Failed to set|adjust time during NTP sync: Insufficient permissions)"'
run bash -c 'grep "ERROR: " /var/log/pihole/FTL.log | grep -c -v -E "(index\.html)|(Failed to create shared memory object)|(FTLCONF_debug_api is not a boolean)|(Failed to set|adjust time during NTP sync: Insufficient permissions)"'
printf "count: %s\n" "${lines[@]}"
[[ ${lines[0]} == "0" ]]
}
Expand Down Expand Up @@ -1642,7 +1642,7 @@
}

@test "Invalid environmental variable is logged" {
run bash -c 'grep -q "FTLCONF_debug_api is invalid" /var/log/pihole/FTL.log'
run bash -c 'grep -q "FTLCONF_debug_api is not a boolean" /var/log/pihole/FTL.log'
printf "%s\n" "${lines[@]}"
[[ $status == 0 ]]
}
Expand Down
Loading