Skip to content

Commit

Permalink
feat: changes in the configurationParser module and in the configurat…
Browse files Browse the repository at this point in the history
…ion to use yaml instead of toml
  • Loading branch information
Nicogp committed Nov 2, 2024
1 parent 25e74e2 commit 15f6f44
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 281 deletions.
21 changes: 21 additions & 0 deletions etc/config/wazuh-agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
agent:
server_url: https://localhost:27000
registration_url: https://localhost:55000
inventory:
enabled: true
interval: 3600
scan_on_start: true
hardware: true
os: true
network: true
packages: true
ports: true
ports_all: true
processes: true
hotfixes: true
logcollector:
enabled: true
localfiles:
- /var/log/auth.log
reload_interval: 60
file_wait: 500
22 changes: 0 additions & 22 deletions etc/config/wazuh.conf

This file was deleted.

4 changes: 2 additions & 2 deletions src/agent/configuration_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ set_common_settings()

include_directories(${CMAKE_SOURCE_DIR}/common/logger/include)

find_package(toml11 CONFIG REQUIRED)
find_package(yaml-cpp CONFIG REQUIRED)

add_library(ConfigurationParser src/configuration_parser.cpp)
target_include_directories(ConfigurationParser PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(ConfigurationParser PUBLIC toml11::toml11 Logger)
target_link_libraries(ConfigurationParser PUBLIC yaml-cpp::yaml-cpp Logger)

include(../../cmake/ConfigureTarget.cmake)
configure_target(ConfigurationParser)
Expand Down
28 changes: 20 additions & 8 deletions src/agent/configuration_parser/include/configuration_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <logger.hpp>

#include <toml.hpp>
#include <yaml-cpp/yaml.h>

#include <exception>
#include <filesystem>
Expand All @@ -14,22 +14,34 @@ namespace configuration
class ConfigurationParser
{
private:
toml::value tbl;
YAML::Node config;

public:
ConfigurationParser();
ConfigurationParser(const std::filesystem::path& configFile);
ConfigurationParser(std::string stringToParse);
ConfigurationParser(const std::string& stringToParse);

template<typename T, typename... Ks>
auto GetConfig(Ks... ks) const
template<typename T, typename... Keys>
T GetConfig(Keys... keys) const
{
try
{
auto config = toml::find<T>(tbl, ks...);
return config;
YAML::Node current = YAML::Clone(config);

(
[&current](const auto& key)
{
current = current[key];
if (!current.IsDefined())
{
throw YAML::Exception(YAML::Mark::null_mark(), "Key not found: " + std::string(key));
}
}(keys),
...);

return current.as<T>();
}
catch (const std::exception& e)
catch (const YAML::Exception& e)
{
LogError("The requested value could not be obtained: {}.", e.what());
throw;
Expand Down
61 changes: 30 additions & 31 deletions src/agent/configuration_parser/src/configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace
{
const std::string CONFIG_FILE_NAME = "/etc/wazuh-agent/wazuh.conf";
const std::string CONFIG_FILE_NAME = "/etc/wazuh-agent/wazuh-agent.yml";
}

namespace configuration
Expand All @@ -12,38 +12,37 @@ namespace configuration
{
try
{
tbl = toml::parse(configFile.string(), toml::spec::v(1, 0, 0));
config = YAML::LoadFile(configFile.string());
}
catch (const std::exception& e)
{
LogError("Using default values due to error parsing wazuh.conf file: {}", e.what());
LogError("Using default values due to error parsing wazuh-agent.yml file: {}", e.what());

tbl = toml::parse_str(
R"(
[agent]
server_url = "https://localhost:27000"
registration_url = "https://localhost:55000"
const std::string yamlStr = R"(
agent:
server_url: https://localhost:27000
registration_url: https://localhost:55000
inventory:
enabled: true
interval: 3600
scan_on_start: true
hardware: true
os: true
network: true
packages: true
ports: true
ports_all: true
processes: true
hotfixes: true
logcollector:
enabled: true
localfiles:
- /var/log/auth.log
reload_interval: 60
file_wait: 500
)";

[inventory]
enabled = true
interval = 3600
scan_on_start = true
hardware = true
os = true
network = true
packages = true
ports = true
ports_all = true
processes = true
hotfixes = true
[logcollector]
enabled = true
localfiles = [ "/var/log/auth.log" ]
reload_interval = 60
file_wait = 500
)",
toml::spec::v(1, 0, 0));
config = YAML::Load(yamlStr);
}
}

Expand All @@ -52,15 +51,15 @@ namespace configuration
{
}

ConfigurationParser::ConfigurationParser(std::string stringToParse)
ConfigurationParser::ConfigurationParser(const std::string& stringToParse)
{
try
{
tbl = toml::parse_str(std::move(stringToParse), toml::spec::v(1, 0, 0));
config = YAML::Load(stringToParse);
}
catch (const std::exception& e)
{
LogError("Error parsing wazuh.conf file: {}.", e.what());
LogError("Error parsing yaml string: {}.", e.what());
throw;
}
}
Expand Down
90 changes: 54 additions & 36 deletions src/agent/configuration_parser/tests/configuration_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ using namespace configuration;
TEST(ConfigurationParser, GetConfigString)
{
std::string strConfig = R"(
[agent]
server_url = "192.168.0.11"
string_conf = "string"
agent:
server_url: 192.168.0.11
string_conf: string
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::string>("agent", "server_url");
Expand All @@ -22,9 +22,11 @@ TEST(ConfigurationParser, GetConfigString)
TEST(ConfigurationParser, GetConfigArrayString)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
string_conf = "string"
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
string_conf: string
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::string>>("agent_array", "array_manager_ip");
Expand All @@ -35,9 +37,11 @@ TEST(ConfigurationParser, GetConfigArrayString)
TEST(ConfigurationParser, GetConfigInt)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
int_conf = 10
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
int_conf: 10
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<int>("agent_array", "int_conf");
Expand All @@ -47,9 +51,11 @@ TEST(ConfigurationParser, GetConfigInt)
TEST(ConfigurationParser, GetConfigFloat)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
float_conf = 12.34
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
float_conf: 12.34
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<float>("agent_array", "float_conf");
Expand All @@ -59,9 +65,11 @@ TEST(ConfigurationParser, GetConfigFloat)
TEST(ConfigurationParser, GetConfigNoKey)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
float_conf = 12.34
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
float_conf: 12.34
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
EXPECT_ANY_THROW(parserStr->GetConfig<float>("agent_array", "no_key"));
Expand All @@ -70,11 +78,13 @@ TEST(ConfigurationParser, GetConfigNoKey)
TEST(ConfigurationParser, GetConfigIntSubTable)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
int_conf = 10
[agent_array.sub_table]
int_conf = 1234
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
int_conf: 10
sub_table:
int_conf: 1234
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<int>("agent_array", "sub_table", "int_conf");
Expand All @@ -84,12 +94,14 @@ TEST(ConfigurationParser, GetConfigIntSubTable)
TEST(ConfigurationParser, GetConfigBoolSubTable)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
int_conf = 10
[agent_array.sub_table]
int_conf = 1234
bool_conf = true
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
int_conf: 10
sub_table:
int_conf: 1234
bool_conf: true
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<bool>("agent_array", "sub_table", "bool_conf");
Expand All @@ -99,10 +111,16 @@ TEST(ConfigurationParser, GetConfigBoolSubTable)
TEST(ConfigurationParser, GetConfigArrayMap)
{
std::string strConfig = R"(
[agent_array]
array_manager_ip = ["192.168.0.0", "192.168.0.1"]
string_conf = "string"
api_auth = [{org_name = "dummy1", api_token = "api_token1"}, {org_name = "dummy2", api_token = "api_token2"}]
agent_array:
array_manager_ip:
- 192.168.0.0
- 192.168.0.1
string_conf: string
api_auth:
- org_name: dummy1
api_token: api_token1
- org_name: dummy2
api_token: api_token2
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::vector<std::map<std::string, std::string>>>("agent_array", "api_auth");
Expand All @@ -115,9 +133,9 @@ TEST(ConfigurationParser, GetConfigArrayMap)
TEST(ConfigurationParser, GetConfigMap)
{
std::string strConfig = R"(
[map_string]
string_conf_1 = "string_1"
string_conf_2 = "string_2"
map_string:
string_conf_1: string_1
string_conf_2: string_2
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
const auto ret = parserStr->GetConfig<std::map<std::string, std::string>>("map_string");
Expand All @@ -128,9 +146,9 @@ TEST(ConfigurationParser, GetConfigMap)
TEST(ConfigurationParser, GetConfigBadCast)
{
std::string strConfig = R"(
[bad_cast_array]
string_conf_1 = "string_1"
int_conf = 10
bad_cast_array:
string_conf_1: string_1
int_conf: 10
)";
const auto parserStr = std::make_unique<configuration::ConfigurationParser>(strConfig);
EXPECT_ANY_THROW(parserStr->GetConfig<std::vector<std::string>>("bad_cast_array"));
Expand Down
Loading

0 comments on commit 15f6f44

Please sign in to comment.