diff --git a/src/agent/configuration_parser/include/configuration_parser.hpp b/src/agent/configuration_parser/include/configuration_parser.hpp index 40b16f7651..a800517bd9 100644 --- a/src/agent/configuration_parser/include/configuration_parser.hpp +++ b/src/agent/configuration_parser/include/configuration_parser.hpp @@ -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 diff --git a/src/agent/configuration_parser/src/configuration_parser.cpp b/src/agent/configuration_parser/src/configuration_parser.cpp index e6a3271c3b..68b97b0c9a 100644 --- a/src/agent/configuration_parser/src/configuration_parser.cpp +++ b/src/agent/configuration_parser/src/configuration_parser.cpp @@ -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) { diff --git a/src/agent/include/agent.hpp b/src/agent/include/agent.hpp index eb08e6791b..a36cb2202c 100644 --- a/src/agent/include/agent.hpp +++ b/src/agent/include/agent.hpp @@ -17,7 +17,7 @@ class Agent { public: - Agent(const std::string& configPath, + Agent(const std::string& configFile, std::unique_ptr signalHandler = std::make_unique()); ~Agent(); diff --git a/src/agent/include/agent_registration.hpp b/src/agent/include/agent_registration.hpp index 9d130589f9..90d85eaf0f 100644 --- a/src/agent/include/agent_registration.hpp +++ b/src/agent/include/agent_registration.hpp @@ -21,7 +21,7 @@ namespace agent_registration std::string password, const std::string& key, const std::string& name, - std::optional configPath); + std::optional configFile); bool Register(http_client::IHttpClient& httpClient); private: diff --git a/src/agent/src/agent.cpp b/src/agent/src/agent.cpp index 1467ac7b9d..7a1449d9d2 100644 --- a/src/agent/src/agent.cpp +++ b/src/agent/src/agent.cpp @@ -11,11 +11,11 @@ #include #include -Agent::Agent(const std::string& configPath, std::unique_ptr signalHandler) +Agent::Agent(const std::string& configFile, std::unique_ptr signalHandler) : m_messageQueue(std::make_shared()) , 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(), m_agentInfo.GetUUID(), m_agentInfo.GetKey(), diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 7c6de032e6..e84d67984f 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -13,9 +13,9 @@ namespace agent_registration std::string password, const std::string& key, const std::string& name, - std::optional configPath) - : m_configurationParser(configPath.has_value() && !configPath->empty() - ? configuration::ConfigurationParser(std::filesystem::path(configPath.value())) + std::optional configFile) + : m_configurationParser(configFile.has_value() && !configFile->empty() + ? configuration::ConfigurationParser(std::filesystem::path(configFile.value())) : configuration::ConfigurationParser()) , m_managerIp(m_configurationParser.GetConfig("agent", "manager_ip")) , m_managerPort(m_configurationParser.GetConfig("agent", "server_mgmt_api_port")) diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index 975581efa8..b56aa266f7 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -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 { diff --git a/src/agent/src/process_options.cpp b/src/agent/src/process_options.cpp index 0b51f1a985..91b1b30aaa 100644 --- a/src/agent/src/process_options.cpp +++ b/src/agent/src/process_options.cpp @@ -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)) @@ -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); } } diff --git a/src/agent/src/process_options.hpp b/src/agent/src/process_options.hpp index 01ae006b97..bb6bc8219b 100644 --- a/src/agent/src/process_options.hpp +++ b/src/agent/src/process_options.hpp @@ -2,14 +2,30 @@ #include +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(); diff --git a/src/agent/src/process_options_unix.cpp b/src/agent/src/process_options_unix.cpp index b10cb81052..dd1e4ab874 100644 --- a/src/agent/src/process_options_unix.cpp +++ b/src/agent/src/process_options_unix.cpp @@ -11,16 +11,16 @@ #include #include -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"); @@ -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(); } @@ -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 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() diff --git a/src/agent/src/process_options_win.cpp b/src/agent/src/process_options_win.cpp index 7a34c67cd4..b2d8eb7232 100644 --- a/src/agent/src/process_options_win.cpp +++ b/src/agent/src/process_options_win.cpp @@ -9,14 +9,14 @@ #include #include -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(); } @@ -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() diff --git a/src/agent/src/windows_service.cpp b/src/agent/src/windows_service.cpp index 1e55f23f6f..f4207d099c 100644 --- a/src/agent/src/windows_service.cpp +++ b/src/agent/src/windows_service.cpp @@ -1,5 +1,6 @@ #include +#include "process_options.hpp" #include #include #include @@ -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)