diff --git a/src/agent/agent_info/include/agent_info.hpp b/src/agent/agent_info/include/agent_info.hpp index 2d9dcbd524..dad7020ab9 100644 --- a/src/agent/agent_info/include/agent_info.hpp +++ b/src/agent/agent_info/include/agent_info.hpp @@ -19,6 +19,9 @@ class AgentInfo void SetUUID(const std::string& uuid); void SetGroups(const std::vector& groupList); + std::string CreateKey(); + bool ValidateKey(const std::string& key); + private: std::string m_name; std::string m_key; diff --git a/src/agent/agent_info/src/agent_info.cpp b/src/agent/agent_info/src/agent_info.cpp index ae852e00f0..5c445ba2d7 100644 --- a/src/agent/agent_info/src/agent_info.cpp +++ b/src/agent/agent_info/src/agent_info.cpp @@ -2,6 +2,9 @@ #include +#include +#include +#include #include #include #include @@ -80,3 +83,33 @@ void AgentInfo::SetGroups(const std::vector& groupList) agentInfoPersistance.SetGroups(groupList); m_groups = groupList; } + +std::string AgentInfo::CreateKey() +{ + static constexpr size_t key_length = 32; + constexpr std::string_view charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + boost::random::mt19937 generator {boost::random::random_device {}()}; + boost::random::uniform_int_distribution distribution(0, charset.size() - 1); + + std::string key; + for (size_t i = 0; i < key_length; ++i) + { + key += charset[distribution(generator)]; + } + + return key; +} + +bool AgentInfo::ValidateKey(const std::string& key) +{ + static constexpr size_t key_length = 32; + if (key.length() != key_length) + { + return false; + } + + return std::ranges::all_of(key, ::isalnum); + + return true; +} diff --git a/src/agent/src/agent_registration.cpp b/src/agent/src/agent_registration.cpp index 7c6de032e6..cc51b6e7b4 100644 --- a/src/agent/src/agent_registration.cpp +++ b/src/agent/src/agent_registration.cpp @@ -5,6 +5,8 @@ #include #include +#include + namespace http = boost::beast::http; namespace agent_registration @@ -23,7 +25,22 @@ namespace agent_registration , m_password(std::move(password)) , m_useHttps(!(m_configurationParser.GetConfig("agent", "https_enabled") == "no")) { - m_agentInfo.SetKey(key); + if (!key.empty()) + { + if (m_agentInfo.ValidateKey(key)) + { + m_agentInfo.SetKey(key); + } + else + { + std::cout << "--key argument must be alphanumeric and 32 characters in lengt\n"; + } + } + else + { + m_agentInfo.SetKey(m_agentInfo.CreateKey()); + } + if (!name.empty()) { m_agentInfo.SetName(name); diff --git a/src/agent/src/process_options.cpp b/src/agent/src/process_options.cpp index 0b51f1a985..1dbd02691b 100644 --- a/src/agent/src/process_options.cpp +++ b/src/agent/src/process_options.cpp @@ -12,7 +12,7 @@ void RegisterAgent(const std::string& user, const std::string& name, const std::string& configPath) { - if (!user.empty() && !password.empty() && !key.empty()) + if (!user.empty() && !password.empty()) { agent_registration::AgentRegistration reg(user, password, key, name, configPath); @@ -28,6 +28,6 @@ void RegisterAgent(const std::string& user, } else { - LogError("--user, --password and --key args are mandatory"); + LogError("--user, --password args are mandatory"); } }