mirror of
https://github.com/openappsec/openappsec.git
synced 2025-11-19 18:44:24 +03:00
Jun 9th update
This commit is contained in:
@@ -175,7 +175,7 @@ copyFile(const string &src, const string &dest, bool overide_if_exists, mode_t p
|
||||
struct stat stat_buf;
|
||||
int source_fd = open(src.c_str(), O_RDONLY);
|
||||
fstat(source_fd, &stat_buf);
|
||||
int dest_fd = open(dest.c_str(), O_WRONLY | O_CREAT, permission);
|
||||
int dest_fd = open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC, permission);
|
||||
int bytes_copied = 1;
|
||||
while (bytes_copied > 0) {
|
||||
static const int buf_size = 4096*1000;
|
||||
|
||||
@@ -37,8 +37,11 @@ TEST_F(AgentCoreUtilUT, filesTest)
|
||||
EXPECT_FALSE(NGEN::Filesystem::exists("/i/am/not/a/real/path"));
|
||||
|
||||
const vector<string> lines{"i am a line in the text file", "i am iron man"};
|
||||
const vector<string> lines_b{"i am a line 2 in the text file", "i am iron man 2", "hello again"};
|
||||
CPTestTempfile test_file(lines);
|
||||
CPTestTempfile test_file_b(lines_b);
|
||||
ASSERT_TRUE(NGEN::Filesystem::exists(test_file.fname));
|
||||
ASSERT_TRUE(NGEN::Filesystem::exists(test_file_b.fname));
|
||||
|
||||
string output_orig = test_file.readFile();
|
||||
string new_path = test_file.fname + ".new";
|
||||
@@ -46,6 +49,7 @@ TEST_F(AgentCoreUtilUT, filesTest)
|
||||
ASSERT_TRUE(NGEN::Filesystem::exists(new_path));
|
||||
ASSERT_FALSE(NGEN::Filesystem::copyFile(test_file.fname, new_path, false));
|
||||
ASSERT_TRUE(NGEN::Filesystem::copyFile(test_file.fname, new_path, true));
|
||||
ASSERT_TRUE(NGEN::Filesystem::copyFile(test_file.fname, test_file_b.fname, true));
|
||||
string output_new;
|
||||
{
|
||||
ifstream new_file_stream(new_path);
|
||||
@@ -55,11 +59,20 @@ TEST_F(AgentCoreUtilUT, filesTest)
|
||||
output_new = buffer.str();
|
||||
}
|
||||
|
||||
string output_test_b;
|
||||
ifstream new_file_stream(test_file_b.fname);
|
||||
ASSERT_TRUE(new_file_stream.good());
|
||||
stringstream buffer;
|
||||
buffer << new_file_stream.rdbuf();
|
||||
output_test_b = buffer.str();
|
||||
|
||||
EXPECT_EQ(output_orig, output_new);
|
||||
EXPECT_EQ(output_orig, output_test_b);
|
||||
EXPECT_THAT(output_new, HasSubstr("i am a line in the text file"));
|
||||
EXPECT_THAT(output_new, HasSubstr("i am iron man"));
|
||||
EXPECT_TRUE(NGEN::Filesystem::deleteFile(test_file.fname));
|
||||
EXPECT_TRUE(NGEN::Filesystem::deleteFile(new_path));
|
||||
EXPECT_TRUE(NGEN::Filesystem::deleteFile(test_file_b.fname));
|
||||
EXPECT_FALSE(NGEN::Filesystem::exists(test_file.fname));
|
||||
EXPECT_FALSE(NGEN::Filesystem::exists(new_path));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "i_time_get.h"
|
||||
#include "i_encryptor.h"
|
||||
#include "i_shell_cmd.h"
|
||||
#include "i_rest_api.h"
|
||||
#include "i_instance_awareness.h"
|
||||
|
||||
#include "config.h"
|
||||
@@ -41,6 +42,7 @@ class Messaging
|
||||
Singleton::Consume<I_TimeGet>,
|
||||
Singleton::Consume<I_ShellCmd>,
|
||||
Singleton::Consume<I_MainLoop>,
|
||||
Singleton::Consume<I_RestApi>,
|
||||
Singleton::Consume<I_InstanceAwareness>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -142,6 +142,7 @@ DEFINE_FLAG(D_COMPONENT, D_ALL)
|
||||
DEFINE_FLAG(D_LOCAL_POLICY, D_ORCHESTRATOR)
|
||||
DEFINE_FLAG(D_NGINX_POLICY, D_ORCHESTRATOR)
|
||||
DEFINE_FLAG(D_SERVICE_CONTROLLER, D_ORCHESTRATOR)
|
||||
DEFINE_FLAG(D_UPDATES_PROCESS_REPORTER, D_ORCHESTRATOR)
|
||||
|
||||
DEFINE_FLAG(D_GRADUAL_DEPLOYMENT, D_COMPONENT)
|
||||
DEFINE_FLAG(D_SDWAN, D_COMPONENT)
|
||||
|
||||
@@ -89,11 +89,4 @@ private:
|
||||
std::map<std::string, std::string> extended_status = {};
|
||||
};
|
||||
|
||||
class HealthCheckStatusEvent : public Event<HealthCheckStatusEvent, HealthCheckStatusReply>
|
||||
{
|
||||
public:
|
||||
HealthCheckStatusEvent() {}
|
||||
~HealthCheckStatusEvent() {}
|
||||
};
|
||||
|
||||
#endif // __HEALTH_CHECK_STATUS_H__
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "agent_core_utilities.h"
|
||||
#include "connection_comp.h"
|
||||
#include "rest.h"
|
||||
#include "debug.h"
|
||||
#include "messaging_buffer.h"
|
||||
|
||||
@@ -25,6 +26,40 @@ using namespace std;
|
||||
|
||||
USE_DEBUG_FLAG(D_MESSAGING);
|
||||
|
||||
class FogConnectionChecker : public ServerRest
|
||||
{
|
||||
public:
|
||||
void
|
||||
doCall() override
|
||||
{
|
||||
dbgTrace(D_MESSAGING) << "Checking connection to the FOG";
|
||||
auto response = Singleton::Consume<I_Messaging>::from<Messaging>()->sendSyncMessage(
|
||||
HTTPMethod::GET,
|
||||
"/access-manager/health/live",
|
||||
string("")
|
||||
);
|
||||
if (!response.ok()) {
|
||||
dbgTrace(D_MESSAGING) << "Failed to check connection to the FOG";
|
||||
connected_to_fog = false;
|
||||
error = response.getErr().toString();
|
||||
return;
|
||||
}
|
||||
if (response.unpack().getHTTPStatusCode() == HTTPStatusCode::HTTP_OK) {
|
||||
dbgTrace(D_MESSAGING) << "Connected to the FOG";
|
||||
connected_to_fog = true;
|
||||
error = "";
|
||||
} else {
|
||||
dbgTrace(D_MESSAGING) << "No connection to the FOG";
|
||||
connected_to_fog = false;
|
||||
error = response.unpack().toString();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
S2C_PARAM(bool, connected_to_fog);
|
||||
S2C_PARAM(string, error);
|
||||
};
|
||||
|
||||
void
|
||||
MessagingComp::init()
|
||||
{
|
||||
@@ -42,6 +77,13 @@ MessagingComp::init()
|
||||
"message",
|
||||
"Buffer Failed Requests"
|
||||
);
|
||||
|
||||
if (Singleton::exists<I_RestApi>()) {
|
||||
Singleton::Consume<I_RestApi>::by<Messaging>()->addRestCall<FogConnectionChecker>(
|
||||
RestAction::SHOW,
|
||||
"check-fog-connection"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include "mocks/mock_messaging_connection.h"
|
||||
#include "rest.h"
|
||||
#include "rest_server.h"
|
||||
#include "mock/mock_messaging.h"
|
||||
#include "mock/mock_rest_api.h"
|
||||
#include "dummy_socket.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -26,12 +28,6 @@ operator<<(ostream &os, const Maybe<BufferedMessage> &)
|
||||
return os;
|
||||
}
|
||||
|
||||
static std::ostream &
|
||||
operator<<(std::ostream &os, const HTTPResponse &)
|
||||
{
|
||||
return os;
|
||||
}
|
||||
|
||||
static std::ostream &
|
||||
operator<<(std::ostream &os, const HTTPStatusCode &)
|
||||
{
|
||||
@@ -51,6 +47,9 @@ public:
|
||||
{
|
||||
Debug::setUnitTestFlag(D_MESSAGING, Debug::DebugLevel::TRACE);
|
||||
EXPECT_CALL(mock_time_get, getMonotonicTime()).WillRepeatedly(Return(chrono::microseconds(0)));
|
||||
EXPECT_CALL(mock_rest, mockRestCall(RestAction::SHOW, "check-fog-connection", _))
|
||||
.WillOnce(WithArg<2>(Invoke(this, &TestMessagingComp::showFogConnection))
|
||||
);
|
||||
|
||||
ON_CALL(mock_agent_details, getFogDomain()).WillByDefault(Return(Maybe<string>(fog_addr)));
|
||||
ON_CALL(mock_agent_details, getFogPort()).WillByDefault(Return(Maybe<uint16_t>(fog_port)));
|
||||
@@ -73,6 +72,13 @@ public:
|
||||
EXPECT_CALL(mock_proxy_conf, getProxyAuthentication(_)).WillRepeatedly(Return(string("cred")));
|
||||
}
|
||||
|
||||
bool
|
||||
showFogConnection(const unique_ptr<RestInit> &p)
|
||||
{
|
||||
show_fog_connection = p->getRest();
|
||||
return true;
|
||||
}
|
||||
|
||||
const string fog_addr = "127.0.0.1";
|
||||
int fog_port = 8080;
|
||||
CPTestTempfile agent_details_file;
|
||||
@@ -85,16 +91,37 @@ public:
|
||||
NiceMock<MockTimeGet> mock_time_get;
|
||||
NiceMock<MockAgentDetails> mock_agent_details;
|
||||
NiceMock<MockProxyConfiguration> mock_proxy_conf;
|
||||
NiceMock<MockRestApi> mock_rest;
|
||||
NiceMock<MockMessaging> mock_message;
|
||||
unique_ptr<ServerRest> show_fog_connection;
|
||||
DummySocket dummy_socket;
|
||||
};
|
||||
|
||||
TEST_F(TestMessagingComp, testInitComp)
|
||||
TEST_F(TestMessagingComp, testCheckFogConnectivity)
|
||||
{
|
||||
EXPECT_CALL(
|
||||
mock_mainloop, addRecurringRoutine(I_MainLoop::RoutineType::Timer, _, _, "Delete expired cache entries", _)
|
||||
)
|
||||
.WillOnce(Return(0));
|
||||
messaging_comp.init();
|
||||
setAgentDetails();
|
||||
HTTPResponse res(
|
||||
HTTPStatusCode::HTTP_OK,
|
||||
string(
|
||||
"{"
|
||||
" \"up\": true,"
|
||||
" \"timestamp\":\"\""
|
||||
"}"
|
||||
)
|
||||
);
|
||||
|
||||
EXPECT_CALL(mock_message, sendSyncMessage(HTTPMethod::GET, "/access-manager/health/live", "", _, _))
|
||||
.WillOnce(Return(res));
|
||||
|
||||
stringstream ss("{}");
|
||||
Maybe<string> maybe_res = show_fog_connection->performRestCall(ss);
|
||||
EXPECT_TRUE(maybe_res.ok());
|
||||
EXPECT_EQ(maybe_res.unpack(),
|
||||
"{\n"
|
||||
" \"connected_to_fog\": true,\n"
|
||||
" \"error\": \"\"\n"
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
TEST_F(TestMessagingComp, testSendSyncMessage)
|
||||
|
||||
Reference in New Issue
Block a user