Skip to content

Commit

Permalink
feat: add functions to create and validate key
Browse files Browse the repository at this point in the history
Two functions were added to create and validate a key during the agent
registration process. Additionally, the --key argument is now optional.
  • Loading branch information
sdvendramini committed Oct 28, 2024
1 parent 5217108 commit a325035
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/agent/agent_info/include/agent_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class AgentInfo
void SetUUID(const std::string& uuid);
void SetGroups(const std::vector<std::string>& groupList);

std::string CreateKey();
bool ValidateKey(const std::string& key);

private:
std::string m_name;
std::string m_key;
Expand Down
33 changes: 33 additions & 0 deletions src/agent/agent_info/src/agent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <agent_info_persistance.hpp>

#include <boost/random.hpp>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
Expand Down Expand Up @@ -80,3 +83,33 @@ void AgentInfo::SetGroups(const std::vector<std::string>& 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<size_t> 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;
}
19 changes: 18 additions & 1 deletion src/agent/src/agent_registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <boost/beast/http.hpp>
#include <nlohmann/json.hpp>

#include <iostream>

namespace http = boost::beast::http;

namespace agent_registration
Expand All @@ -23,7 +25,22 @@ namespace agent_registration
, m_password(std::move(password))
, m_useHttps(!(m_configurationParser.GetConfig<std::string>("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);
Expand Down
4 changes: 2 additions & 2 deletions src/agent/src/process_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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");
}
}

0 comments on commit a325035

Please sign in to comment.