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

Enhancement/244 change the configuration file option #247

Merged
merged 3 commits into from
Oct 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace configuration

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

template<typename T, typename... Ks>
Expand Down
6 changes: 3 additions & 3 deletions src/agent/configuration_parser/src/configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

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

namespace configuration
{
ConfigurationParser::ConfigurationParser(const std::filesystem::path& configPath)
ConfigurationParser::ConfigurationParser(const std::filesystem::path& configFile)
{
try
{
tbl = toml::parse(configPath.string(), toml::spec::v(1, 0, 0));
tbl = toml::parse(configFile.string(), toml::spec::v(1, 0, 0));
}
catch (const std::exception& e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/agent/include/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class Agent
{
public:
Agent(const std::string& configPath,
Agent(const std::string& configFile,
std::unique_ptr<ISignalHandler> signalHandler = std::make_unique<SignalHandler>());
~Agent();

Expand Down
2 changes: 1 addition & 1 deletion src/agent/include/agent_registration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace agent_registration
std::string password,
const std::string& key,
const std::string& name,
std::optional<std::string> configPath);
std::optional<std::string> configFile);
bool Register(http_client::IHttpClient& httpClient);

private:
Expand Down
6 changes: 3 additions & 3 deletions src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include <memory>
#include <thread>

Agent::Agent(const std::string& configPath, std::unique_ptr<ISignalHandler> signalHandler)
Agent::Agent(const std::string& configFile, std::unique_ptr<ISignalHandler> signalHandler)
: m_messageQueue(std::make_shared<MultiTypeQueue>())
, m_signalHandler(std::move(signalHandler))
, m_configurationParser(configPath.empty() ? configuration::ConfigurationParser()
: configuration::ConfigurationParser(std::filesystem::path(configPath)))
, m_configurationParser(configFile.empty() ? configuration::ConfigurationParser()
: configuration::ConfigurationParser(std::filesystem::path(configFile)))
, m_communicator(std::make_unique<http_client::HttpClient>(),
m_agentInfo.GetUUID(),
m_agentInfo.GetKey(),
Expand Down
6 changes: 3 additions & 3 deletions src/agent/src/agent_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace agent_registration
std::string password,
const std::string& key,
const std::string& name,
std::optional<std::string> configPath)
: m_configurationParser(configPath.has_value() && !configPath->empty()
? configuration::ConfigurationParser(std::filesystem::path(configPath.value()))
std::optional<std::string> configFile)
: m_configurationParser(configFile.has_value() && !configFile->empty()
? configuration::ConfigurationParser(std::filesystem::path(configFile.value()))
: configuration::ConfigurationParser())
, m_managerIp(m_configurationParser.GetConfig<std::string>("agent", "manager_ip"))
, m_managerPort(m_configurationParser.GetConfig<std::string>("agent", "server_mgmt_api_port"))
Expand Down
36 changes: 18 additions & 18 deletions src/agent/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,52 @@ int main(int argc, char* argv[])
Logger logger;
CommandlineParser cmdParser(argc, argv);

std::string configPath;
std::string configFile;

try
{
if (cmdParser.OptionExists("--config-path"))
if (cmdParser.OptionExists(OPT_CONFIG_FILE))
{
configPath = cmdParser.GetOptionValue("--config-path");
configFile = cmdParser.GetOptionValue(OPT_CONFIG_FILE);
}

if (cmdParser.OptionExists("--register-agent"))
if (cmdParser.OptionExists(OPT_REGISTER_AGENT))
{
RegisterAgent(cmdParser.GetOptionValue("--user"),
cmdParser.GetOptionValue("--password"),
cmdParser.GetOptionValue("--key"),
cmdParser.GetOptionValue("--name"),
configPath);
RegisterAgent(cmdParser.GetOptionValue(OPT_USER),
cmdParser.GetOptionValue(OPT_PASSWORD),
cmdParser.GetOptionValue(OPT_KEY),
cmdParser.GetOptionValue(OPT_NAME),
configFile);
}
else if (cmdParser.OptionExists("--restart"))
else if (cmdParser.OptionExists(OPT_RESTART))
{
RestartAgent(configPath);
RestartAgent(configFile);
}
else if (cmdParser.OptionExists("--status"))
else if (cmdParser.OptionExists(OPT_STATUS))
{
StatusAgent();
}
else if (cmdParser.OptionExists("--stop"))
else if (cmdParser.OptionExists(OPT_STOP))
{
StopAgent();
}
else if (cmdParser.OptionExists("--install-service"))
else if (cmdParser.OptionExists(OPT_INSTALL_SERVICE))
{
if (!InstallService())
return 1;
}
else if (cmdParser.OptionExists("--remove-service"))
else if (cmdParser.OptionExists(OPT_REMOVE_SERVICE))
{
if (!RemoveService())
return 1;
}
else if (cmdParser.OptionExists("--run-service"))
else if (cmdParser.OptionExists(OPT_RUN_SERVICE))
{
SetDispatcherThread();
}
else if (cmdParser.OptionExists("--run") || cmdParser.OptionExists("--start"))
else if (cmdParser.OptionExists(OPT_RUN) || cmdParser.OptionExists(OPT_START))
{
StartAgent(configPath);
StartAgent(configFile);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions src/agent/src/process_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ void RegisterAgent(const std::string& user,
const std::string& password,
const std::string& key,
const std::string& name,
const std::string& configPath)
const std::string& configFile)
{
if (!user.empty() && !password.empty() && !key.empty())
{
agent_registration::AgentRegistration reg(user, password, key, name, configPath);
agent_registration::AgentRegistration reg(user, password, key, name, configFile);

http_client::HttpClient httpClient;
if (reg.Register(httpClient))
Expand All @@ -28,6 +28,6 @@ void RegisterAgent(const std::string& user,
}
else
{
LogError("--user, --password and --key args are mandatory");
LogError("{}, {}, and {} args are mandatory", OPT_USER, OPT_PASSWORD, OPT_KEY);
}
}
24 changes: 20 additions & 4 deletions src/agent/src/process_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@

#include <string>

static const auto OPT_RUN {"--run"};
static const auto OPT_START {"--start"};
static const auto OPT_STATUS {"--status"};
static const auto OPT_STOP {"--stop"};
static const auto OPT_RESTART {"--restart"};
static const auto OPT_CONFIG_FILE {"--config-file"};
static const auto OPT_REGISTER_AGENT {"--register-agent"};
static const auto OPT_INSTALL_SERVICE {"--install-service"};
static const auto OPT_REMOVE_SERVICE {"--remove-service"};
static const auto OPT_RUN_SERVICE {"--run-service"};
static const auto OPT_USER {"--user"};
static const auto OPT_PASSWORD {"--password"};
static const auto OPT_KEY {"--key"};
static const auto OPT_NAME {"--name"};
static const auto OPT_HELP {"--help"};

void RegisterAgent(const std::string& user,
const std::string& password,
const std::string& key,
const std::string& name,
const std::string& configPath);
void RestartAgent([[maybe_unused]] const std::string& configPath);
void StartAgent([[maybe_unused]] const std::string& configPath);
void StartAgentDaemon([[maybe_unused]] const std::string& configPath);
const std::string& configFile);
void RestartAgent([[maybe_unused]] const std::string& configFile);
void StartAgent([[maybe_unused]] const std::string& configFile);
void StartAgentDaemon([[maybe_unused]] const std::string& configFile);
void StatusAgent();
void StopAgent();
void PrintHelp();
Expand Down
27 changes: 14 additions & 13 deletions src/agent/src/process_options_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
#include <thread>
#include <vector>

void RestartAgent(const std::string& configPath)
void RestartAgent(const std::string& configFile)
{
StopAgent();

std::this_thread::sleep_for(std::chrono::seconds(1)); // NOLINT

StartAgent(configPath);
StartAgent(configFile);
}

void StartAgent([[maybe_unused]] const std::string& configPath)
void StartAgent([[maybe_unused]] const std::string& configFile)
{
LogInfo("Starting wazuh-agent");

Expand All @@ -32,7 +32,7 @@ void StartAgent([[maybe_unused]] const std::string& configPath)
}

unix_daemon::PIDFileHandler handler = unix_daemon::GeneratePIDFile();
Agent agent(configPath);
Agent agent(configFile);
agent.Run();
}

Expand Down Expand Up @@ -63,15 +63,16 @@ void PrintHelp()
std::cout << "Usage: wazuh-agent [options]\n";
std::cout << "\n";
std::cout << "Options:\n";
std::cout << " --run Start wazuh-agent\n";
std::cout << " --start Start wazuh-agent daemon\n";
std::cout << " --status Get wazuh-agent daemon status\n";
std::cout << " --stop Stop wazuh-agent daemon\n";
std::cout << " --restart Restart wazuh-agent daemon\n";
std::cout << " --register-agent Register wazuh-agent\n";
std::cout << " --config-path <path> Specify configuration file path (optional).\n";
std::cout << " Used with --start, --restart, or --register-agent.\n";
std::cout << " --help This help message\n";
std::cout << " " << OPT_RUN << " Start wazuh-agent\n";
std::cout << " " << OPT_START << " Start wazuh-agent daemon\n";
std::cout << " " << OPT_STATUS << " Get wazuh-agent daemon status\n";
std::cout << " " << OPT_STOP << " Stop wazuh-agent daemon\n";
std::cout << " " << OPT_RESTART << " Restart wazuh-agent daemon\n";
std::cout << " " << OPT_REGISTER_AGENT << " Register wazuh-agent\n";
std::cout << " " << OPT_CONFIG_FILE << " Specify the full path of the configuration file (optional).\n";
std::cout << " Used with " << OPT_RUN << ", " << OPT_RESTART << ", or "
<< OPT_REGISTER_AGENT << ".\n";
std::cout << " " << OPT_HELP << " This help message\n";
}

bool InstallService()
Expand Down
24 changes: 12 additions & 12 deletions src/agent/src/process_options_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#include <iostream>
#include <vector>

void RestartAgent([[maybe_unused]] const std::string& configPath)
void RestartAgent([[maybe_unused]] const std::string& configFile)
{
WindowsService::ServiceRestart();
}

void StartAgentDaemon([[maybe_unused]] const std::string& configPath) {}
void StartAgentDaemon([[maybe_unused]] const std::string& configFile) {}

void StartAgent([[maybe_unused]] const std::string& configPath)
void StartAgent([[maybe_unused]] const std::string& configFile)
{
WindowsService::ServiceStart();
}
Expand All @@ -39,15 +39,15 @@ void PrintHelp()
std::cout << "Usage: wazuh-agent [options]\n";
std::cout << "\n";
std::cout << "Options:\n";
std::cout << " --start Start wazuh-agent daemon\n";
std::cout << " --status Get wazuh-agent daemon status\n";
std::cout << " --stop Stop wazuh-agent daemon\n";
std::cout << " --restart Restart wazuh-agent daemon\n";
std::cout << " --register-agent Register wazuh-agent\n";
std::cout << " --install-service Install Windows Service\n";
std::cout << " --remove-service Remove Windows Service\n";
std::cout << " --run-service Used by Windows SCM to run as Service\n";
std::cout << " --help This help message\n";
std::cout << " " << OPT_START << " Start wazuh-agent daemon\n";
std::cout << " " << OPT_STATUS << " Get wazuh-agent daemon status\n";
std::cout << " " << OPT_STOP << " Stop wazuh-agent daemon\n";
std::cout << " " << OPT_RESTART << " Restart wazuh-agent daemon\n";
std::cout << " " << OPT_REGISTER_AGENT << " Register wazuh-agent\n";
std::cout << " " << OPT_INSTALL_SERVICE << " Install Windows Service\n";
std::cout << " " << OPT_REMOVE_SERVICE << " Remove Windows Service\n";
std::cout << " " << OPT_RUN_SERVICE << " Used by Windows SCM to run as Service\n";
std::cout << " " << OPT_HELP << " This help message\n";
}

bool InstallService()
Expand Down
3 changes: 2 additions & 1 deletion src/agent/src/windows_service.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <windows_service.hpp>

#include "process_options.hpp"
#include <agent.hpp>
#include <logger.hpp>
#include <signal_handler.hpp>
Expand Down Expand Up @@ -124,7 +125,7 @@ namespace WindowsService
{
bool InstallService()
{
const std::string exePath = GetExecutablePath() + " --run-service";
const std::string exePath = GetExecutablePath() + " " + OPT_RUN_SERVICE;

SC_HANDLE schSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
if (!schSCManager)
Expand Down
Loading