Skip to content

Commit

Permalink
Merge pull request #45 from wazuh/enhancement/17-migrate-syscollector…
Browse files Browse the repository at this point in the history
…-to-inventory-at-new-agent

Migrate Syscollector to Inventory at new-agent repository
  • Loading branch information
cborla authored Oct 1, 2024
2 parents 5ad2356 + ac5c5e9 commit daf6f53
Show file tree
Hide file tree
Showing 349 changed files with 5,575 additions and 19,249 deletions.
3 changes: 3 additions & 0 deletions .github/actions/clang_format/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ runs:
shell: bash
run: |
# Print clanformat version used.
echo "Clang-format version: $(clang-format --version)"
# Don't apply changes, just check
arguments="--dry-run "
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Generate CMake project
run: |
mkdir -p build && cd build
cmake -DCMAKE_CXX_COMPILER=clang++-16 ../src -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=1 -DCOVERAGE=1 -G "Unix Makefiles"
cmake -DCMAKE_CXX_COMPILER=clang++-16 -DCMAKE_C_COMPILER=clang-16 ../src -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=1 -DCOVERAGE=1 -G "Unix Makefiles"
shell: bash

- name: Compile
Expand Down
11 changes: 9 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
cmake_minimum_required(VERSION 3.22)

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake")

get_filename_component(SRC_FOLDER ${CMAKE_SOURCE_DIR}/../ ABSOLUTE)
project(wazuh-agent)

include(cmake/CommonSettings.cmake)
set_common_settings()

add_subdirectory(agent)
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX-")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
add_compile_options(/w)
endif()

add_subdirectory(common)
add_subdirectory(modules)
add_subdirectory(agent)

add_executable(wazuh-agent agent/src/main.cpp)
target_link_libraries(wazuh-agent Agent Logger)
Expand Down
2 changes: 1 addition & 1 deletion src/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ set(SOURCES

add_library(Agent ${SOURCES})
target_include_directories(Agent PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(Agent PUBLIC ConfigurationParser Communicator AgentInfo CommandHandler MultiTypeQueue PRIVATE OpenSSL::SSL OpenSSL::Crypto Boost::asio Boost::beast Logger)
target_link_libraries(Agent PUBLIC ConfigurationParser Communicator AgentInfo CommandHandler MultiTypeQueue ModuleManager PRIVATE OpenSSL::SSL OpenSSL::Crypto Boost::asio Boost::beast)

include(../cmake/ConfigureTarget.cmake)
configure_target(Agent)
Expand Down
19 changes: 17 additions & 2 deletions src/agent/configuration_parser/src/configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,26 @@ namespace configuration
LogError("Using default values due to error parsing wazuh.conf file: {}", e.what());

tbl = toml::parse_str(
R"([agent]
R"(
[agent]
server_mgmt_api_port = "55000"
agent_comms_api_port = "27000"
manager_ip = "localhost"
https_enabled = "yes")",
https_enabled = "yes"
[inventory]
enabled = true
interval = 3600
scan_on_start = true
hardware = true
os = true
network = true
packages = true
ports = true
ports_all = true
processes = true
hotfixes = true
)",
toml::spec::v(1, 0, 0));
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/agent/include/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <communicator.hpp>
#include <configuration_parser.hpp>
#include <isignal_handler.hpp>
#include <moduleManager.hpp>
#include <multitype_queue.hpp>
#include <signal_handler.hpp>
#include <task_manager.hpp>
Expand All @@ -28,4 +29,5 @@ class Agent
configuration::ConfigurationParser m_configurationParser;
communicator::Communicator m_communicator;
command_handler::CommandHandler m_commandHandler;
ModuleManager m_moduleManager;
};
8 changes: 7 additions & 1 deletion src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <agent.hpp>
#include <inventory.hpp>

#include <command.hpp>
#include <command_handler_utils.hpp>
Expand All @@ -19,6 +20,7 @@ Agent::Agent(std::unique_ptr<ISignalHandler> signalHandler)
m_agentInfo.GetKey(),
[this](std::string table, std::string key) -> std::string
{ return m_configurationParser.GetConfig<std::string>(std::move(table), std::move(key)); })
, m_moduleManager(m_messageQueue, m_configurationParser)
{
m_taskManager.Start(std::thread::hardware_concurrency());
}
Expand Down Expand Up @@ -50,7 +52,11 @@ void Agent::Run()
[this]() { return PopCommandFromQueue(m_messageQueue); },
[](command_store::Command& cmd) { return DispatchCommand(cmd); }));

m_moduleManager.AddModule(Inventory::Instance());
m_moduleManager.Setup();
m_taskManager.EnqueueTask([this]() { m_moduleManager.Start(); });

m_signalHandler->WaitForSignal();
m_moduleManager.Stop();
m_communicator.Stop();
m_commandHandler.Stop();
}
2 changes: 2 additions & 0 deletions src/agent/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ add_executable(agent_test agent_test.cpp)
configure_target(agent_test)
target_include_directories(agent_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../src)
target_link_libraries(agent_test PRIVATE Agent GTest::gtest GTest::gmock)
if(NOT WIN32)
add_test(NAME AgentTest COMMAND agent_test)
endif()

add_executable(task_manager_test task_manager_test.cpp)
configure_target(task_manager_test)
Expand Down
5 changes: 4 additions & 1 deletion src/agent/tests/agent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ TEST(AgentTests, AgentStopsWhenSignalReceived)
auto mockSignalHandler = std::make_unique<MockSignalHandler>();
MockSignalHandler* mockSignalHandlerPtr = mockSignalHandler.get();

EXPECT_CALL(*mockSignalHandlerPtr, WaitForSignal())
.Times(1)
.WillOnce([]() { std::this_thread::sleep_for(std::chrono::seconds(1)); });

Agent agent(std::move(mockSignalHandler));

EXPECT_CALL(*mockSignalHandlerPtr, WaitForSignal()).Times(1);
EXPECT_NO_THROW(agent.Run());
}

Expand Down
17 changes: 17 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
add_subdirectory(binaries_op)
add_subdirectory(bzip2_op)
add_subdirectory(data_provider)
add_subdirectory(dbsync)
add_subdirectory(error_messages)
add_subdirectory(file_op)
add_subdirectory(hashHelper)
add_subdirectory(logger)
add_subdirectory(mem_op)
add_subdirectory(networkHelper)
add_subdirectory(privsep_op)
add_subdirectory(pthreads_op)
add_subdirectory(randombytes)
add_subdirectory(regex_op)
add_subdirectory(sqliteWrapper)
add_subdirectory(time_op)
add_subdirectory(utils)
add_subdirectory(version_op)

if(BUILD_TESTS)
enable_testing()
Expand Down
24 changes: 12 additions & 12 deletions src/common/audit_op/src/audit_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int audit_get_rule_list(int fd) {

int rc = audit_send(fd, AUDIT_LIST_RULES, NULL, 0);
if (rc < 0 && rc != -EINVAL) {
merror("Error sending rule list data request (%s)",strerror(-rc));
LogError("Error sending rule list data request (%s)",strerror(-rc));
return -1;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ int audit_print_reply(struct audit_reply *rep) {
}
}
if (path && key) {
mdebug2("Audit rule loaded: -w %s -p %s -k %s",path, perms, key);
LogDebug("Audit rule loaded: -w %s -p %s -k %s",path, perms, key);
if (audit_rules_list) {
w_audit_rule *rule;
os_calloc(1, sizeof(w_audit_rule), rule);
Expand Down Expand Up @@ -218,19 +218,19 @@ int audit_restart(void) {
char *service_path = NULL;

if (get_binary_path("service", &service_path) < 0) {
mdebug1("Binary '%s' not found in default paths, the full path will not be used.", service_path);
LogDebug("Binary '%s' not found in default paths, the full path will not be used.", service_path);
}
char * command[] = { service_path, "auditd", "restart", NULL };

if (wfd = wpopenv(*command, command, W_BIND_STDERR), !wfd) {
merror("Could not launch command to restart Auditd: %s (%d)", strerror(errno), errno);
LogError("Could not launch command to restart Auditd: %s (%d)", strerror(errno), errno);
os_free(service_path);
return -1;
}

// Print stderr
while (fgets(buffer, sizeof(buffer), wfd->file_out)) {
mdebug1("auditd: %s", buffer);
LogDebug("auditd: %s", buffer);
}

switch (status = wpclose(wfd), WEXITSTATUS(status)) {
Expand All @@ -239,11 +239,11 @@ int audit_restart(void) {
return 0;
case 127:
// exec error
merror("Could not launch command to restart Auditd.");
LogError("Could not launch command to restart Auditd.");
os_free(service_path);
return -1;
default:
merror("Could not restart Auditd service.");
LogError("Could not restart Auditd service.");
os_free(service_path);
return -1;
}
Expand All @@ -270,23 +270,23 @@ int audit_manage_rules(int action, const char *path, int permissions, const char
type = AUDIT_DIR;

if (stat(path, &buf) != 0) {
mdebug2(FIM_STAT_FAILED, path, errno, strerror(errno));
LogDebug(FIM_STAT_FAILED, path, errno, strerror(errno));
retval = -1;
goto end;
}

// Set watcher
output = audit_add_watch_dir(type, &myrule, path);
if (output) {
mdebug2("audit_add_watch_dir = (%d) %s", output, audit_errno_to_name(abs(output)));
LogDebug("audit_add_watch_dir = (%d) %s", output, audit_errno_to_name(abs(output)));
retval = -1;
goto end;
}

// Set permissions
output = audit_update_watch_perms(myrule, permissions);
if (output) {
mdebug2("audit_update_watch_perms = (%d) %s", output, audit_errno_to_name(abs(output)));
LogDebug("audit_update_watch_perms = (%d) %s", output, audit_errno_to_name(abs(output)));
retval = -1;
goto end;
}
Expand All @@ -311,7 +311,7 @@ int audit_manage_rules(int action, const char *path, int permissions, const char
} else {
output = audit_rule_fieldpair_data(&myrule, cmd, flags);
if (output) {
mdebug2("audit_rule_fieldpair_data = (%d) %s", output, audit_errno_to_name(abs(output)));
LogDebug("audit_rule_fieldpair_data = (%d) %s", output, audit_errno_to_name(abs(output)));
free(cmd);
retval = -1;
goto end;
Expand All @@ -330,7 +330,7 @@ int audit_manage_rules(int action, const char *path, int permissions, const char
}

if (retval <= 0 && retval != -EEXIST) {
mdebug2("Can't add or delete a rule (%d) = %s", retval, audit_errno_to_name(abs(retval)));
LogDebug("Can't add or delete a rule (%d) = %s", retval, audit_errno_to_name(abs(retval)));
}

end:
Expand Down
8 changes: 8 additions & 0 deletions src/common/binaries_op/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(binaries_op STATIC src/binaries_op.c)

target_include_directories(binaries_op PUBLIC include)

target_link_libraries(binaries_op
utils
Logger
time_op)
8 changes: 8 additions & 0 deletions src/common/bzip2_op/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(bzip2_op STATIC src/bzip2_op.c)

target_include_directories(bzip2_op PUBLIC include ../logger/include)

target_link_libraries(bzip2_op
utils
Logger
time_op)
40 changes: 40 additions & 0 deletions src/common/bzip2_op/include/bzip2_op.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2015, Wazuh Inc.
* April, 2020.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/

#ifndef BZIP2_OP_H
#define BZIP2_OP_H

#include <bzlib.h>

#define BZIP2_BUFFER_SIZE 4096

/**
* @brief bzpi2 library, function to compress
*
* @param file File path to compress
* @param filebz2 File name compressed
*
* @retval 0 on success
* @retval -1 on error
*/
int bzip2_compress(const char *file, const char *filebz2);

/**
* @brief bzpi2 library, function to uncompress
*
* @param filebz2 File path to uncompress
* @param file File name uncompressed
*
* @retval 0 on success
* @retval -1 on error
*/
int bzip2_uncompress(const char *filebz2, const char *file);

#endif /* BZIP2_OP_H */
Loading

0 comments on commit daf6f53

Please sign in to comment.