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/14 add key to register and authentication #89

Merged
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
4 changes: 2 additions & 2 deletions .github/actions/compile/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ runs:
shell: bash

- name: Run Tests
continue-on-error: true
continue-on-error: false
jr0me marked this conversation as resolved.
Show resolved Hide resolved
run: |
set +e
cd build
ctest || echo "Tests failed, but continuing"
ctest
shell: bash
8 changes: 4 additions & 4 deletions src/agent/agent_info/include/agent_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class AgentInfo
{
public:
AgentInfo();
AgentInfo(const std::string& name, const std::string& ip, const std::string& uuid);
AgentInfo(const std::string& name, const std::string& key, const std::string& uuid);

std::string GetName() const;
std::string GetIP() const;
std::string GetKey() const;
std::string GetUUID() const;

void SetName(const std::string& name);
void SetIP(const std::string& ip);
void SetKey(const std::string& key);
void SetUUID(const std::string& uuid);

private:
std::string m_name;
std::string m_ip;
std::string m_key;
std::string m_uuid;
};
4 changes: 2 additions & 2 deletions src/agent/agent_info/include/agent_info_persistance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class AgentInfoPersistance
AgentInfoPersistance& operator=(AgentInfoPersistance&&) = delete;

std::string GetName() const;
std::string GetIP() const;
std::string GetKey() const;
std::string GetUUID() const;

void SetName(const std::string& name);
void SetIP(const std::string& ip);
void SetKey(const std::string& key);
void SetUUID(const std::string& uuid);

void ResetToDefault();
Expand Down
18 changes: 9 additions & 9 deletions src/agent/agent_info/src/agent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AgentInfo::AgentInfo()
{
AgentInfoPersistance agentInfoPersistance;
m_name = agentInfoPersistance.GetName();
m_ip = agentInfoPersistance.GetIP();
m_key = agentInfoPersistance.GetKey();
m_uuid = agentInfoPersistance.GetUUID();

if (m_uuid.empty())
Expand All @@ -20,24 +20,24 @@ AgentInfo::AgentInfo()
}
}

AgentInfo::AgentInfo(const std::string& name, const std::string& ip, const std::string& uuid)
AgentInfo::AgentInfo(const std::string& name, const std::string& key, const std::string& uuid)
: m_name(name)
, m_ip(ip)
, m_key(key)
, m_uuid(uuid)
{
AgentInfoPersistance agentInfoPersistance;
agentInfoPersistance.SetName(m_name);
agentInfoPersistance.SetIP(m_ip);
agentInfoPersistance.SetKey(m_key);
agentInfoPersistance.SetUUID(m_uuid);
}

std::string AgentInfo::GetName() const
{
return m_name;
}
std::string AgentInfo::GetIP() const
std::string AgentInfo::GetKey() const
{
return m_ip;
return m_key;
}
std::string AgentInfo::GetUUID() const
{
Expand All @@ -51,11 +51,11 @@ void AgentInfo::SetName(const std::string& name)
m_name = name;
}

void AgentInfo::SetIP(const std::string& ip)
void AgentInfo::SetKey(const std::string& key)
{
AgentInfoPersistance agentInfoPersistance;
agentInfoPersistance.SetIP(ip);
m_ip = ip;
agentInfoPersistance.SetKey(key);
m_key = key;
}

void AgentInfo::SetUUID(const std::string& uuid)
Expand Down
12 changes: 6 additions & 6 deletions src/agent/agent_info/src/agent_info_persistance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void AgentInfoPersistance::CreateAgentInfoTable()
{
m_db->exec("CREATE TABLE IF NOT EXISTS agent_info ("
"name TEXT, "
"ip TEXT, "
"key TEXT, "
"uuid TEXT"
");");
}
Expand All @@ -86,7 +86,7 @@ void AgentInfoPersistance::InsertDefaultAgentInfo()

if (count == 0)
{
SQLite::Statement insert(*m_db, "INSERT INTO agent_info (name, ip, uuid) VALUES (?, ?, ?);");
SQLite::Statement insert(*m_db, "INSERT INTO agent_info (name, key, uuid) VALUES (?, ?, ?);");
insert.exec();
}
}
Expand Down Expand Up @@ -133,9 +133,9 @@ std::string AgentInfoPersistance::GetName() const
return GetAgentInfoValue("name");
}

std::string AgentInfoPersistance::GetIP() const
std::string AgentInfoPersistance::GetKey() const
{
return GetAgentInfoValue("ip");
return GetAgentInfoValue("key");
}

std::string AgentInfoPersistance::GetUUID() const
Expand All @@ -148,9 +148,9 @@ void AgentInfoPersistance::SetName(const std::string& name)
SetAgentInfoValue("name", name);
}

void AgentInfoPersistance::SetIP(const std::string& ip)
void AgentInfoPersistance::SetKey(const std::string& key)
{
SetAgentInfoValue("ip", ip);
SetAgentInfoValue("key", key);
}

void AgentInfoPersistance::SetUUID(const std::string& uuid)
Expand Down
12 changes: 6 additions & 6 deletions src/agent/agent_info/tests/agent_info_persistance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEST_F(AgentInfoPersistanceTest, TestConstruction)
TEST_F(AgentInfoPersistanceTest, TestDefaultValues)
{
EXPECT_EQ(persistance->GetName(), "");
EXPECT_EQ(persistance->GetIP(), "");
EXPECT_EQ(persistance->GetKey(), "");
EXPECT_EQ(persistance->GetUUID(), "");
}

Expand All @@ -36,11 +36,11 @@ TEST_F(AgentInfoPersistanceTest, TestSetName)
EXPECT_EQ(persistance->GetName(), newName);
}

TEST_F(AgentInfoPersistanceTest, TestSetIP)
TEST_F(AgentInfoPersistanceTest, TestSetKey)
{
const std::string newIP = "192.168.1.1";
persistance->SetIP(newIP);
EXPECT_EQ(persistance->GetIP(), newIP);
const std::string newKey = "new_key";
persistance->SetKey(newKey);
EXPECT_EQ(persistance->GetKey(), newKey);
}

TEST_F(AgentInfoPersistanceTest, TestSetUUID)
Expand All @@ -58,7 +58,7 @@ TEST_F(AgentInfoPersistanceTest, TestResetToDefault)

persistance->ResetToDefault();
EXPECT_EQ(persistance->GetName(), "");
EXPECT_EQ(persistance->GetIP(), "");
EXPECT_EQ(persistance->GetKey(), "");
EXPECT_EQ(persistance->GetUUID(), "");
}

Expand Down
22 changes: 11 additions & 11 deletions src/agent/agent_info/tests/agent_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ TEST_F(AgentInfoTest, TestDefaultConstructorDefaultValues)
{
const AgentInfo agentInfo;
EXPECT_EQ(agentInfo.GetName(), "");
EXPECT_EQ(agentInfo.GetIP(), "");
EXPECT_EQ(agentInfo.GetKey(), "");
EXPECT_NE(agentInfo.GetUUID(), "");
}

TEST_F(AgentInfoTest, TestParameterizedConstructor)
{
const std::string name = "new_name";
const std::string ip = "192.168.1.1";
const std::string key = "new_key";
const std::string uuid = "new_uuid";

const AgentInfo agentInfo(name, ip, uuid);
const AgentInfo agentInfo(name, key, uuid);
EXPECT_EQ(agentInfo.GetName(), name);
EXPECT_EQ(agentInfo.GetIP(), ip);
EXPECT_EQ(agentInfo.GetKey(), key);
EXPECT_EQ(agentInfo.GetUUID(), uuid);
}

TEST_F(AgentInfoTest, TestPersistedValues)
{
const AgentInfo agentInfo("test_name", "test_ip", "test_uuid");
const AgentInfo agentInfo("test_name", "test_key", "test_uuid");
const AgentInfo agentInfoReloaded;
EXPECT_EQ(agentInfoReloaded.GetName(), "test_name");
EXPECT_EQ(agentInfoReloaded.GetIP(), "test_ip");
EXPECT_EQ(agentInfoReloaded.GetKey(), "test_key");
EXPECT_EQ(agentInfoReloaded.GetUUID(), "test_uuid");
}

Expand All @@ -60,16 +60,16 @@ TEST_F(AgentInfoTest, TestSetName)
EXPECT_EQ(agentInfoReloaded.GetName(), newName);
}

TEST_F(AgentInfoTest, TestSetIP)
TEST_F(AgentInfoTest, TestSetKey)
{
AgentInfo agentInfo;
const std::string newIP = "192.168.1.1";
const std::string newKey = "new_key";

agentInfo.SetIP(newIP);
EXPECT_EQ(agentInfo.GetIP(), newIP);
agentInfo.SetKey(newKey);
EXPECT_EQ(agentInfo.GetKey(), newKey);

const AgentInfo agentInfoReloaded;
EXPECT_EQ(agentInfoReloaded.GetIP(), newIP);
EXPECT_EQ(agentInfoReloaded.GetKey(), newKey);
}

TEST_F(AgentInfoTest, TestSetUUID)
Expand Down
2 changes: 2 additions & 0 deletions src/agent/communicator/include/communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace communicator
{
public:
Communicator(const std::string& uuid,
const std::string& key,
const std::function<std::string(std::string, std::string)> GetStringConfigValue);

boost::asio::awaitable<void> WaitForTokenExpirationAndAuthenticate();
Expand All @@ -37,6 +38,7 @@ namespace communicator
std::string m_managerIp;
std::string m_port;
std::string m_uuid;
std::string m_key;
std::string m_token;
long long m_tokenExpTimeInSeconds = 0;
std::unique_ptr<boost::asio::steady_timer> m_tokenExpTimer;
Expand Down
17 changes: 11 additions & 6 deletions src/agent/communicator/include/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ namespace http_client

boost::beast::http::response<boost::beast::http::dynamic_body> PerformHttpRequest(const HttpRequestParams& params);

std::optional<std::string>
AuthenticateWithUuid(const std::string& host, const std::string& port, const std::string& uuid);
std::optional<std::string> AuthenticateWithUserPassword(const std::string& host,
const std::string& port,
const std::string& user,
const std::string& password);
std::optional<std::string> AuthenticateWithUuidAndKey(const std::string& host,
const std::string& port,
const std::string& uuid,
const std::string& key);

using AuthenticateFunctionType = std::optional<std::string>(const std::string& host,
const std::string& port,
const std::string& user,
const std::string& password);

AuthenticateFunctionType AuthenticateWithUserPassword;
} // namespace http_client
4 changes: 3 additions & 1 deletion src/agent/communicator/src/communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace communicator
constexpr int TokenPreExpirySecs = 2;

Communicator::Communicator(const std::string& uuid,
const std::string& key,
const std::function<std::string(std::string, std::string)> GetStringConfigValue)
: m_uuid(uuid)
, m_key(key)
{
if (GetStringConfigValue != nullptr)
{
Expand All @@ -30,7 +32,7 @@ namespace communicator

boost::beast::http::status Communicator::SendAuthenticationRequest()
{
const auto token = http_client::AuthenticateWithUuid(m_managerIp, m_port, m_uuid);
const auto token = http_client::AuthenticateWithUuidAndKey(m_managerIp, m_port, m_uuid, m_key);

if (token.has_value())
{
Expand Down
12 changes: 7 additions & 5 deletions src/agent/communicator/src/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,14 @@ namespace http_client
return res;
}

std::optional<std::string>
AuthenticateWithUuid(const std::string& host, const std::string& port, const std::string& uuid)
std::optional<std::string> AuthenticateWithUuidAndKey(const std::string& host,
const std::string& port,
const std::string& uuid,
const std::string& key)
{
const std::string uuidKeyValue = "{\"uuid\":\"" + uuid + "\"}";
const auto reqParams = http_client::HttpRequestParams(
boost::beast::http::verb::post, host, port, "/authentication", "", "", uuidKeyValue);
const std::string body = "{\"uuid\":\"" + uuid + "\", \"key\":\"" + key + "\"}";
const auto reqParams =
http_client::HttpRequestParams(boost::beast::http::verb::post, host, port, "/authentication", "", "", body);

return GetTokenFromResponse(reqParams);
}
Expand Down
2 changes: 1 addition & 1 deletion src/agent/communicator/tests/communicator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

TEST(CommunicatorTest, CommunicatorConstructor)
{
EXPECT_NO_THROW(communicator::Communicator communicator("uuid", nullptr));
EXPECT_NO_THROW(communicator::Communicator communicator("uuid", "key", nullptr));
}

int main(int argc, char** argv)
Expand Down
18 changes: 17 additions & 1 deletion src/agent/include/register.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <http_client.hpp>
jr0me marked this conversation as resolved.
Show resolved Hide resolved

#include <boost/beast/http.hpp>
#include <functional>
#include <optional>
#include <string>

Expand All @@ -11,5 +15,17 @@ namespace registration
std::string password;
};

bool RegisterAgent(const UserCredentials& userCredentials);
using RegisterFunctionType = boost::beast::http::status(const std::string& host,
const std::string& port,
const std::string& token,
const std::string& uuid,
const std::string& key,
const std::optional<std::string>& name);

RegisterFunctionType SendRegistrationRequest;

bool RegisterAgent(const UserCredentials& userCredentials,
std::function<http_client::AuthenticateFunctionType> AuthenticateFunction,
std::function<RegisterFunctionType> RegisterFunction);

} // namespace registration
1 change: 1 addition & 0 deletions src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Agent::Agent()
: m_communicator(m_agentInfo.GetUUID(),
m_agentInfo.GetKey(),
[this](std::string table, std::string key) -> std::string
{ return m_configurationParser.GetConfig<std::string>(table, key); })
{
Expand Down
Loading
Loading