mirror of
https://github.com/falcosecurity/falco.git
synced 2025-07-04 10:26:40 +00:00
Falco application changes to support rule loading result struct
Update the load_rules_files and validate_rules_files actions to use the new falco_engine methods that return a rules result struct. The app action interface is the same, returning ::fatal on error, ok()/exit() otherwise. The difference is how any warnings/errors are obtained--from the struct instead of an exception. Signed-off-by: Mark Stemm <mark.stemm@gmail.com>
This commit is contained in:
parent
f7f6d72ac0
commit
550cdbd176
@ -73,7 +73,9 @@ watch_config_files: true
|
|||||||
# time zone, as governed by /etc/localtime.
|
# time zone, as governed by /etc/localtime.
|
||||||
time_format_iso_8601: false
|
time_format_iso_8601: false
|
||||||
|
|
||||||
# Whether to output events in json or text
|
# If "true", print falco alert messages and rules file
|
||||||
|
# loading/validation results as json, which allows for easier
|
||||||
|
# consumption by downstream programs. Default is "false".
|
||||||
json_output: false
|
json_output: false
|
||||||
|
|
||||||
# When using json output, whether or not to include the "output" property
|
# When using json output, whether or not to include the "output" property
|
||||||
|
@ -96,13 +96,24 @@ application::run_result application::load_rules_files()
|
|||||||
for (const auto& filename : m_state->config->m_loaded_rules_filenames)
|
for (const auto& filename : m_state->config->m_loaded_rules_filenames)
|
||||||
{
|
{
|
||||||
falco_logger::log(LOG_INFO, "Loading rules from file " + filename + "\n");
|
falco_logger::log(LOG_INFO, "Loading rules from file " + filename + "\n");
|
||||||
|
std::unique_ptr<falco::load_result> res;
|
||||||
|
|
||||||
try {
|
res = m_state->engine->load_rules_file(filename);
|
||||||
m_state->engine->load_rules_file(filename, m_options.verbose, m_options.all_events);
|
|
||||||
}
|
// Print the full output if verbose is true
|
||||||
catch(falco_exception &e)
|
if(m_options.verbose &&
|
||||||
|
(!res->successful() || res->has_warnings()))
|
||||||
{
|
{
|
||||||
return run_result::fatal(string("Could not load rules file ") + filename + ": " + e.what());
|
printf("%s\n",
|
||||||
|
(m_state->config->m_json_output ?
|
||||||
|
res->as_json().dump().c_str() :
|
||||||
|
res->as_string(true).c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!res->successful())
|
||||||
|
{
|
||||||
|
// Return the summary version as the error
|
||||||
|
return run_result::fatal(res->as_string(false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace falco::app;
|
using namespace falco::app;
|
||||||
|
|
||||||
@ -22,28 +23,65 @@ application::run_result application::validate_rules_files()
|
|||||||
{
|
{
|
||||||
if(m_options.validate_rules_filenames.size() > 0)
|
if(m_options.validate_rules_filenames.size() > 0)
|
||||||
{
|
{
|
||||||
|
bool successful = true;
|
||||||
|
std::string summary;
|
||||||
|
|
||||||
falco_logger::log(LOG_INFO, "Validating rules file(s):\n");
|
falco_logger::log(LOG_INFO, "Validating rules file(s):\n");
|
||||||
for(auto file : m_options.validate_rules_filenames)
|
for(auto file : m_options.validate_rules_filenames)
|
||||||
{
|
{
|
||||||
falco_logger::log(LOG_INFO, " " + file + "\n");
|
falco_logger::log(LOG_INFO, " " + file + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The json output encompasses all files so the
|
||||||
|
// validation result is a single json object.
|
||||||
|
nlohmann::json results = nlohmann::json::array();
|
||||||
|
|
||||||
for(auto file : m_options.validate_rules_filenames)
|
for(auto file : m_options.validate_rules_filenames)
|
||||||
{
|
{
|
||||||
// Only include the prefix if there is more than one file
|
std::unique_ptr<falco::load_result> res;
|
||||||
std::string prefix = (m_options.validate_rules_filenames.size() > 1 ? file + ": " : "");
|
|
||||||
try {
|
res = m_state->engine->load_rules_file(file);
|
||||||
m_state->engine->load_rules_file(file, m_options.verbose, m_options.all_events);
|
|
||||||
}
|
successful &= res->successful();
|
||||||
catch(falco_exception &e)
|
|
||||||
|
if(summary != "")
|
||||||
{
|
{
|
||||||
printf("%s%s", prefix.c_str(), e.what());
|
summary += "\n";
|
||||||
return run_result::fatal(prefix + e.what());
|
|
||||||
}
|
}
|
||||||
printf("%sOk\n", prefix.c_str());
|
summary += file + ": " + (res->successful() ? "Ok" : "Invalid");
|
||||||
|
|
||||||
|
if(m_state->config->m_json_output)
|
||||||
|
{
|
||||||
|
results.push_back(res->as_json());
|
||||||
}
|
}
|
||||||
falco_logger::log(LOG_INFO, "Ok\n");
|
else
|
||||||
|
{
|
||||||
|
// Print the full output when verbose is true
|
||||||
|
if(m_options.verbose &&
|
||||||
|
(!res->successful() || res->has_warnings()))
|
||||||
|
{
|
||||||
|
printf("%s\n", res->as_string(true).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(m_state->config->m_json_output)
|
||||||
|
{
|
||||||
|
nlohmann::json res;
|
||||||
|
res["falco_load_results"] = results;
|
||||||
|
printf("%s\n", res.dump().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(successful)
|
||||||
|
{
|
||||||
|
printf("%s\n", summary.c_str());
|
||||||
return run_result::exit();
|
return run_result::exit();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return run_result::fatal(summary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return run_result::ok();
|
return run_result::ok();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user