mirror of
https://github.com/falcosecurity/falco.git
synced 2025-09-24 19:38:52 +00:00
chore(unit_tests,userspace/falco): throw an exception when included config file is not present.
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
committed by
poiana
parent
de9efcbec7
commit
a8345327d4
@@ -32,12 +32,11 @@ falco::app::run_result falco::app::actions::load_config(const falco::app::state&
|
||||
// List of loaded conf files, ie: s.options.conf_filename
|
||||
// plus all the `configs_files` expanded list of configs.
|
||||
std::vector<std::string> loaded_conf_files;
|
||||
std::vector<std::string> loaded_conf_warnings;
|
||||
try
|
||||
{
|
||||
if (!s.options.conf_filename.empty())
|
||||
{
|
||||
s.config->init(s.options.conf_filename, loaded_conf_files, loaded_conf_warnings, s.options.cmdline_config_options);
|
||||
s.config->init(s.options.conf_filename, loaded_conf_files, s.options.cmdline_config_options);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -66,11 +65,6 @@ falco::app::run_result falco::app::actions::load_config(const falco::app::state&
|
||||
{
|
||||
falco_logger::log(falco_logger::level::INFO, std::string(" ") + path + "\n");
|
||||
}
|
||||
|
||||
for (const auto &warn : loaded_conf_warnings)
|
||||
{
|
||||
falco_logger::log(falco_logger::level::WARNING, warn + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
s.config->m_buffered_outputs = !s.options.unbuffered_outputs;
|
||||
|
@@ -91,11 +91,11 @@ void falco_configuration::init(const std::vector<std::string>& cmdline_options)
|
||||
}
|
||||
|
||||
void falco_configuration::init(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files,
|
||||
std::vector<std::string>& loaded_conf_warnings, const std::vector<std::string> &cmdline_options)
|
||||
const std::vector<std::string> &cmdline_options)
|
||||
{
|
||||
try
|
||||
{
|
||||
config.load_from_file(conf_filename, loaded_conf_files, loaded_conf_warnings);
|
||||
config.load_from_file(conf_filename, loaded_conf_files);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
|
@@ -86,7 +86,7 @@ public:
|
||||
falco_configuration();
|
||||
virtual ~falco_configuration() = default;
|
||||
|
||||
void init(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files, std::vector<std::string>& loaded_conf_warnings, const std::vector<std::string>& cmdline_options);
|
||||
void init(const std::string& conf_filename, std::vector<std::string>& loaded_conf_files, const std::vector<std::string>& cmdline_options);
|
||||
void init(const std::vector<std::string>& cmdline_options);
|
||||
|
||||
std::string dump();
|
||||
|
@@ -92,70 +92,49 @@ public:
|
||||
/**
|
||||
* Load the YAML document from the given file path.
|
||||
*/
|
||||
void load_from_file(const std::string& path, std::vector<std::string>& loaded_config_files, std::vector<std::string>& loaded_config_warnings)
|
||||
void load_from_file(const std::string& path, std::vector<std::string>& loaded_config_files)
|
||||
{
|
||||
loaded_config_files.clear();
|
||||
loaded_config_warnings.clear();
|
||||
|
||||
m_root = load_from_file_int(path, loaded_config_files);
|
||||
|
||||
const auto ppath = std::filesystem::path(path);
|
||||
const auto config_folder = ppath.parent_path();
|
||||
// Parse files to be included
|
||||
std::vector<std::string> include_files;
|
||||
get_sequence<std::vector<std::string>>(include_files, configs_key);
|
||||
for(const std::string& include_file : include_files)
|
||||
{
|
||||
// If user specifies a relative include file,
|
||||
// make it relative to main config file folder,
|
||||
// instead of cwd.
|
||||
auto include_file_path = std::filesystem::path(include_file);
|
||||
if (!include_file_path.is_absolute())
|
||||
{
|
||||
include_file_path = config_folder / include_file;
|
||||
}
|
||||
if (include_file_path == ppath)
|
||||
{
|
||||
throw std::runtime_error(
|
||||
"Config error: '" + configs_key + "' directive tried to recursively include main config file: " + path + ".");
|
||||
}
|
||||
if (std::filesystem::exists(include_file_path))
|
||||
if (!std::filesystem::exists(include_file_path))
|
||||
{
|
||||
if (std::filesystem::is_regular_file(include_file_path))
|
||||
{
|
||||
include_config_file(include_file_path.string(), loaded_config_files);
|
||||
}
|
||||
else if (std::filesystem::is_directory(include_file_path))
|
||||
{
|
||||
std::vector<std::string> v;
|
||||
const auto it_options = std::filesystem::directory_options::follow_directory_symlink
|
||||
| std::filesystem::directory_options::skip_permission_denied;
|
||||
for (auto const& dir_entry : std::filesystem::directory_iterator(include_file_path, it_options))
|
||||
{
|
||||
if (std::filesystem::is_regular_file(dir_entry.path()))
|
||||
{
|
||||
v.push_back(dir_entry.path().string());
|
||||
}
|
||||
// We don't support nested directories
|
||||
else
|
||||
{
|
||||
loaded_config_warnings.push_back("Included config file has wrong type: " + dir_entry.path().string());
|
||||
}
|
||||
}
|
||||
std::sort(v.begin(), v.end());
|
||||
for (const auto &f : v)
|
||||
{
|
||||
include_config_file(f, loaded_config_files);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
loaded_config_warnings.push_back("Included config entry has wrong type: " + include_file_path.string());
|
||||
}
|
||||
throw std::runtime_error("Included config entry not existent: " + include_file_path.string());
|
||||
}
|
||||
else
|
||||
if (std::filesystem::is_regular_file(include_file_path))
|
||||
{
|
||||
loaded_config_warnings.push_back("Included config entry unexistent: " + include_file_path.string());
|
||||
include_config_file(include_file_path.string(), loaded_config_files);
|
||||
}
|
||||
else if (std::filesystem::is_directory(include_file_path))
|
||||
{
|
||||
std::vector<std::string> v;
|
||||
const auto it_options = std::filesystem::directory_options::follow_directory_symlink
|
||||
| std::filesystem::directory_options::skip_permission_denied;
|
||||
for (auto const& dir_entry : std::filesystem::directory_iterator(include_file_path, it_options))
|
||||
{
|
||||
if (std::filesystem::is_regular_file(dir_entry.path()))
|
||||
{
|
||||
v.push_back(dir_entry.path().string());
|
||||
}
|
||||
}
|
||||
std::sort(v.begin(), v.end());
|
||||
for (const auto &f : v)
|
||||
{
|
||||
include_config_file(f, loaded_config_files);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user