From 694389a4b088a3d0c3559fb3cb9cf5f6e2536be3 Mon Sep 17 00:00:00 2001 From: Santiago Vendramini Date: Fri, 22 Nov 2024 15:55:53 +0100 Subject: [PATCH] feat: Validate execution options Validation of execution options has been added. --- src/agent/include/cmd_ln_parser.hpp | 9 ++++++++- src/agent/src/main.cpp | 7 ++++--- src/agent/src/process_options.hpp | 15 +++++++++++++++ src/agent/tests/cmd_ln_parser_test.cpp | 5 +++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/agent/include/cmd_ln_parser.hpp b/src/agent/include/cmd_ln_parser.hpp index 09cf70252c..a1514d263b 100644 --- a/src/agent/include/cmd_ln_parser.hpp +++ b/src/agent/include/cmd_ln_parser.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -13,10 +14,16 @@ class CommandlineParser /// @brief Constructor /// @param argc The number of command line arguments /// @param argv The command line arguments - CommandlineParser(int argc, char** argv) + /// @param options The command line options available + CommandlineParser(int argc, char** argv, const std::vector& options) { for (int i = 1; i < argc; ++i) { + auto isValid = std::find(options.begin(), options.end(), argv[i]); + if (isValid == options.end() && argv[i][0] == '-') + { + throw std::invalid_argument("Invalid option: " + std::string(argv[i])); + } m_tokens.push_back(std::string(argv[i])); } } diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index 4baf2366b7..50a3d88140 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -7,12 +7,13 @@ int main(int argc, char* argv[]) { Logger logger; - CommandlineParser cmdParser(argc, argv); - - std::string configFile; try { + CommandlineParser cmdParser(argc, argv, validOptions); + + std::string configFile; + if (cmdParser.OptionExists(OPT_CONFIG_FILE)) { configFile = cmdParser.GetOptionValue(OPT_CONFIG_FILE); diff --git a/src/agent/src/process_options.hpp b/src/agent/src/process_options.hpp index 8bffb51e1e..ad5eb93e36 100644 --- a/src/agent/src/process_options.hpp +++ b/src/agent/src/process_options.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include /// Command-line options static const auto OPT_STATUS {"--status"}; @@ -17,6 +18,20 @@ static const auto OPT_KEY {"--key"}; static const auto OPT_NAME {"--name"}; static const auto OPT_HELP {"--help"}; +const std::vector validOptions = {{OPT_STATUS}, + {OPT_STOP}, + {OPT_RESTART}, + {OPT_CONFIG_FILE}, + {OPT_REGISTER_AGENT}, + {OPT_INSTALL_SERVICE}, + {OPT_REMOVE_SERVICE}, + {OPT_RUN_SERVICE}, + {OPT_USER}, + {OPT_PASSWORD}, + {OPT_KEY}, + {OPT_NAME}, + {OPT_HELP}}; + /// @brief Registers the agent with the given parameters. /// @param user The user to use for authentication with Server Management API. /// @param password The password to use for authentication with Server Management API. diff --git a/src/agent/tests/cmd_ln_parser_test.cpp b/src/agent/tests/cmd_ln_parser_test.cpp index 13d87ddc08..48a253b7bf 100644 --- a/src/agent/tests/cmd_ln_parser_test.cpp +++ b/src/agent/tests/cmd_ln_parser_test.cpp @@ -11,8 +11,9 @@ class CommandlineParserTest : public ::testing::Test std::string program = "program"; std::string option = "--option"; std::string value = "value"; + std::vector validOptions = {"--option"}; std::array args = {program.data(), option.data(), value.data()}; - CommandlineParser parser {static_cast(args.size()), args.data()}; + CommandlineParser parser {static_cast(args.size()), args.data(), validOptions}; }; TEST_F(CommandlineParserTest, GetOptionValue_OptionExists) @@ -38,7 +39,7 @@ TEST_F(CommandlineParserTest, OptionExists_OptionDoesNotExist) TEST_F(CommandlineParserTest, GetOptionValue_NoValueForOption) { std::array noValueArgs = {program.data(), option.data()}; - CommandlineParser noValueParser {static_cast(noValueArgs.size()), noValueArgs.data()}; + CommandlineParser noValueParser {static_cast(noValueArgs.size()), noValueArgs.data(), validOptions}; EXPECT_EQ(noValueParser.GetOptionValue(option), ""); }