mirror of
https://github.com/falcosecurity/falco.git
synced 2025-06-28 15:47:25 +00:00
chore(unit_tests,userspace): use nlhomann json instead of jsoncpp.
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
parent
be927edfe8
commit
d1c715e7a8
@ -151,6 +151,7 @@ if (NOT EMSCRIPTEN)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
include(zlib)
|
include(zlib)
|
||||||
|
include(valijson)
|
||||||
if (NOT MINIMAL_BUILD)
|
if (NOT MINIMAL_BUILD)
|
||||||
if (NOT WIN32 AND NOT APPLE AND NOT EMSCRIPTEN)
|
if (NOT WIN32 AND NOT APPLE AND NOT EMSCRIPTEN)
|
||||||
include(cares)
|
include(cares)
|
||||||
|
@ -108,7 +108,7 @@ TEST(Configuration, schema_yaml_helper_validator)
|
|||||||
|
|
||||||
// We pass a string variable but not a schema
|
// We pass a string variable but not a schema
|
||||||
std::string validation;
|
std::string validation;
|
||||||
EXPECT_NO_THROW(conf.load_from_string(sample_yaml, Json::Value{}, &validation));
|
EXPECT_NO_THROW(conf.load_from_string(sample_yaml, nlohmann::json{}, &validation));
|
||||||
EXPECT_EQ(validation, yaml_helper::validation_none);
|
EXPECT_EQ(validation, yaml_helper::validation_none);
|
||||||
|
|
||||||
// We pass a schema but not a string storage for the validation; no validation takes place
|
// We pass a schema but not a string storage for the validation; no validation takes place
|
||||||
|
@ -651,10 +651,7 @@ falco_configuration::falco_configuration():
|
|||||||
m_metrics_convert_memory_to_mb(true),
|
m_metrics_convert_memory_to_mb(true),
|
||||||
m_metrics_include_empty_values(false)
|
m_metrics_include_empty_values(false)
|
||||||
{
|
{
|
||||||
if(!Json::Reader().parse(schema_json_string, m_config_schema) || m_config_schema.type() != Json::objectValue)
|
m_config_schema = nlohmann::json::parse(schema_json_string);
|
||||||
{
|
|
||||||
throw falco_exception("failed to parse config schema");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config_loaded_res falco_configuration::init_from_content(const std::string& config_content, const std::vector<std::string>& cmdline_options, const std::string& filename)
|
config_loaded_res falco_configuration::init_from_content(const std::string& config_content, const std::vector<std::string>& cmdline_options, const std::string& filename)
|
||||||
|
@ -31,7 +31,6 @@ limitations under the License.
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <json/json.h>
|
|
||||||
|
|
||||||
#include "config_falco.h"
|
#include "config_falco.h"
|
||||||
#include "yaml_helper.h"
|
#include "yaml_helper.h"
|
||||||
@ -197,7 +196,7 @@ public:
|
|||||||
|
|
||||||
// Needed by tests
|
// Needed by tests
|
||||||
yaml_helper m_config;
|
yaml_helper m_config;
|
||||||
Json::Value m_config_schema;
|
nlohmann::json m_config_schema;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void merge_config_files(const std::string& config_name, config_loaded_res &res);
|
void merge_config_files(const std::string& config_name, config_loaded_res &res);
|
||||||
|
@ -33,7 +33,8 @@ limitations under the License.
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
#include <valijson/adapters/jsoncpp_adapter.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <valijson/adapters/nlohmann_json_adapter.hpp>
|
||||||
#include <valijson/adapters/yaml_cpp_adapter.hpp>
|
#include <valijson/adapters/yaml_cpp_adapter.hpp>
|
||||||
#include <valijson/schema.hpp>
|
#include <valijson/schema.hpp>
|
||||||
#include <valijson/schema_parser.hpp>
|
#include <valijson/schema_parser.hpp>
|
||||||
@ -92,7 +93,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Load the YAML document represented by the input string.
|
* Load the YAML document represented by the input string.
|
||||||
*/
|
*/
|
||||||
void load_from_string(const std::string& input, const Json::Value& schema={}, std::string *validation=nullptr)
|
void load_from_string(const std::string& input, const nlohmann::json& schema={}, std::string *validation=nullptr)
|
||||||
{
|
{
|
||||||
m_root = YAML::Load(input);
|
m_root = YAML::Load(input);
|
||||||
pre_process_env_vars(m_root);
|
pre_process_env_vars(m_root);
|
||||||
@ -113,12 +114,12 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Load the YAML document from the given file path.
|
* Load the YAML document from the given file path.
|
||||||
*/
|
*/
|
||||||
void load_from_file(const std::string& path, const Json::Value& schema={}, std::string *validation=nullptr)
|
void load_from_file(const std::string& path, const nlohmann::json& schema={}, std::string *validation=nullptr)
|
||||||
{
|
{
|
||||||
m_root = load_from_file_int(path, schema, validation);
|
m_root = load_from_file_int(path, schema, validation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void include_config_file(const std::string& include_file_path, const Json::Value& schema={}, std::string *validation=nullptr)
|
void include_config_file(const std::string& include_file_path, const nlohmann::json& schema={}, std::string *validation=nullptr)
|
||||||
{
|
{
|
||||||
auto loaded_nodes = load_from_file_int(include_file_path, schema, validation);
|
auto loaded_nodes = load_from_file_int(include_file_path, schema, validation);
|
||||||
for(auto n : loaded_nodes)
|
for(auto n : loaded_nodes)
|
||||||
@ -206,7 +207,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
YAML::Node m_root;
|
YAML::Node m_root;
|
||||||
|
|
||||||
YAML::Node load_from_file_int(const std::string& path, const Json::Value& schema={}, std::string *validation=nullptr)
|
YAML::Node load_from_file_int(const std::string& path, const nlohmann::json& schema={}, std::string *validation=nullptr)
|
||||||
{
|
{
|
||||||
auto root = YAML::LoadFile(path);
|
auto root = YAML::LoadFile(path);
|
||||||
pre_process_env_vars(root);
|
pre_process_env_vars(root);
|
||||||
@ -225,7 +226,7 @@ private:
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string validate_node(const YAML::Node &node, const Json::Value& schema={})
|
std::string validate_node(const YAML::Node &node, const nlohmann::json& schema={})
|
||||||
{
|
{
|
||||||
// Validate the yaml against our json schema
|
// Validate the yaml against our json schema
|
||||||
valijson::Schema schemaDef;
|
valijson::Schema schemaDef;
|
||||||
@ -233,7 +234,7 @@ private:
|
|||||||
valijson::Validator validator(valijson::Validator::kWeakTypes);
|
valijson::Validator validator(valijson::Validator::kWeakTypes);
|
||||||
valijson::ValidationResults validationResults;
|
valijson::ValidationResults validationResults;
|
||||||
valijson::adapters::YamlCppAdapter configAdapter(node);
|
valijson::adapters::YamlCppAdapter configAdapter(node);
|
||||||
valijson::adapters::JsonCppAdapter schemaAdapter(schema);
|
valijson::adapters::NlohmannJsonAdapter schemaAdapter(schema);
|
||||||
schemaParser.populateSchema(schemaAdapter, schemaDef);
|
schemaParser.populateSchema(schemaAdapter, schemaDef);
|
||||||
|
|
||||||
if (!validator.validate(schemaDef, configAdapter, &validationResults))
|
if (!validator.validate(schemaDef, configAdapter, &validationResults))
|
||||||
|
Loading…
Reference in New Issue
Block a user