Skip to content

Commit 7889d6f

Browse files
committed
feat: Add logic to implement timeout and scheduling
The m_scanOnStart and m_scanInterval SCA class members are propagated to determine whether a scan should run at startup and to control the interval between scans.
1 parent dd563f4 commit 7889d6f

8 files changed

+46
-14
lines changed

src/modules/sca/include/sca_policy.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SCAPolicy
2424
};
2525

2626
/// @brief Constructor
27-
explicit SCAPolicy(Check requirements, std::vector<Check> checks);
27+
explicit SCAPolicy(Check requirements, std::vector<Check> checks, std::time_t scanInterval, bool scanOnStart);
2828

2929
SCAPolicy(SCAPolicy&& other) noexcept;
3030

@@ -42,4 +42,6 @@ class SCAPolicy
4242
std::vector<Check> m_checks;
4343
std::atomic<bool> m_keepRunning {true};
4444
std::function<int(Message)> m_pushMessage;
45+
std::time_t m_scanInterval;
46+
bool m_scanOnStart;
4547
};

src/modules/sca/include/sca_policy_loader.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class SCAPolicyLoader : public ISCAPolicyLoader
4646
/// maps:
4747
/// - modifiedPoliciesMap: maps policy ID to the JSON data of the created, modified or deleted policy
4848
/// - modifiedChecksMap: maps check ID to the JSON data of the created, modified or deleted check
49-
std::vector<SCAPolicy> GetPolicies(const CreateEventsFunc& createEvents) const;
49+
/// @param
50+
std::vector<SCAPolicy>
51+
GetPolicies(const CreateEventsFunc& createEvents, std::time_t scanInterval, bool scanOnStart) const;
5052

5153
/// @brief Saves SCA Policies into the database
5254
/// @param data All SCA policies and its checks

src/modules/sca/src/sca.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ void SecurityConfigurationAssessment::Setup(
7171
{
7272
const SCAEventHandler eventHandler(m_agentUUID, m_dBSync, m_pushMessage);
7373
eventHandler.CreateEvents(policyData, checksData);
74-
});
74+
},
75+
m_scanInterval,
76+
m_scanOnStart);
7577
}();
7678
}
7779

src/modules/sca/src/sca_policy.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
#include <sca_policy.hpp>
22

33
#include <check_condition_evaluator.hpp>
4+
#include <logger.hpp>
45

56
#include <boost/asio/steady_timer.hpp>
67
#include <boost/asio/this_coro.hpp>
78
#include <boost/asio/use_awaitable.hpp>
89

9-
SCAPolicy::SCAPolicy(Check requirements, std::vector<Check> checks)
10+
SCAPolicy::SCAPolicy(Check requirements, std::vector<Check> checks, std::time_t scanInterval, bool scanOnStart)
1011
: m_requirements(std::move(requirements))
1112
, m_checks(std::move(checks))
13+
, m_scanInterval(scanInterval)
14+
, m_scanOnStart(scanOnStart)
1215
{
1316
}
1417

1518
SCAPolicy::SCAPolicy(SCAPolicy&& other) noexcept
1619
: m_requirements(std::move(other.m_requirements))
1720
, m_checks(std::move(other.m_checks))
1821
, m_keepRunning(other.m_keepRunning.load())
22+
, m_scanInterval(other.m_scanInterval)
23+
, m_scanOnStart(other.m_scanOnStart)
1924
{
2025
}
2126

2227
boost::asio::awaitable<void> SCAPolicy::Run()
2328
{
29+
auto firstRun = true;
30+
2431
while (m_keepRunning)
2532
{
33+
if (!firstRun)
34+
{
35+
auto executor = co_await boost::asio::this_coro::executor;
36+
boost::asio::steady_timer timer(executor);
37+
timer.expires_after(std::chrono::milliseconds(m_scanInterval));
38+
co_await timer.async_wait(boost::asio::use_awaitable);
39+
}
40+
41+
if (firstRun && !m_scanOnStart)
42+
{
43+
firstRun = false;
44+
continue;
45+
}
46+
2647
auto requirementsOk = true;
2748

2849
if (!m_requirements.rules.empty())
@@ -54,10 +75,10 @@ boost::asio::awaitable<void> SCAPolicy::Run()
5475
}
5576
}
5677

57-
auto executor = co_await boost::asio::this_coro::executor;
58-
boost::asio::steady_timer timer(executor);
59-
timer.expires_after(std::chrono::seconds(5)); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
60-
co_await timer.async_wait(boost::asio::use_awaitable);
78+
// To do: add policy id to class members and log
79+
LogDebug("Policy checks completed for policy {}", m_requirements.title);
80+
81+
firstRun = false;
6182
}
6283
co_return;
6384
}

src/modules/sca/src/sca_policy_loader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ SCAPolicyLoader::SCAPolicyLoader(std::shared_ptr<IFileSystemWrapper> fileSystemW
3939
m_disabledPoliciesPaths = loadPoliciesPathsFromConfig("policies_disabled");
4040
}
4141

42-
std::vector<SCAPolicy> SCAPolicyLoader::GetPolicies(const CreateEventsFunc& createEvents) const
42+
std::vector<SCAPolicy>
43+
SCAPolicyLoader::GetPolicies(const CreateEventsFunc& createEvents, std::time_t scanInterval, bool scanOnStart) const
4344
{
4445
std::vector<std::filesystem::path> allPolicyPaths;
4546

@@ -71,7 +72,7 @@ std::vector<SCAPolicy> SCAPolicyLoader::GetPolicies(const CreateEventsFunc& crea
7172
};
7273

7374
const PolicyParser parser(path, loadFunc);
74-
auto policy = parser.ParsePolicy(policiesAndChecks);
75+
auto policy = parser.ParsePolicy(policiesAndChecks, scanInterval, scanOnStart);
7576
if (policy)
7677
{
7778
policies.emplace_back(std::move(policy.value()));

src/modules/sca/src/sca_policy_parser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ bool PolicyParser::isValidYamlFile(const std::filesystem::path& filename) const
115115
}
116116
}
117117

118-
std::optional<SCAPolicy> PolicyParser::ParsePolicy(nlohmann::json& policiesAndChecks) const
118+
std::optional<SCAPolicy>
119+
PolicyParser::ParsePolicy(nlohmann::json& policiesAndChecks, std::time_t scanInterval, bool scanOnStart) const
119120
{
120121
std::vector<SCAPolicy::Check> checks;
121122
SCAPolicy::Check requirements;
@@ -219,7 +220,7 @@ std::optional<SCAPolicy> PolicyParser::ParsePolicy(nlohmann::json& policiesAndCh
219220
return std::nullopt;
220221
}
221222

222-
return SCAPolicy(std::move(requirements), std::move(checks));
223+
return SCAPolicy(std::move(requirements), std::move(checks), scanInterval, scanOnStart);
223224
}
224225

225226
// NOLINTNEXTLINE(misc-no-recursion)

src/modules/sca/src/sca_policy_parser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class PolicyParser
5656
///
5757
/// @param policiesAndChecks JSON object to be filled with extracted data.
5858
/// @return A populated SCAPolicy object.
59-
std::optional<SCAPolicy> ParsePolicy(nlohmann::json& policiesAndChecks) const;
59+
std::optional<SCAPolicy>
60+
ParsePolicy(nlohmann::json& policiesAndChecks, std::time_t scanInterval, bool scanOnStart) const;
6061

6162
private:
6263
/// @brief Recursively replaces variables in the YAML node with their values.

src/modules/sca/tests/sca_policy_loader_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ TEST(ScaPolicyLoaderTest, NoPolicies)
2424
auto configurationParser = std::make_shared<configuration::ConfigurationParser>(std::string(R"()"));
2525
auto dbSync = std::make_shared<MockDBSync>();
2626

27+
const std::time_t scanInterval {3600};
28+
2729
const SCAPolicyLoader loader(fsMock, configurationParser, dbSync);
28-
ASSERT_EQ(loader.GetPolicies([](auto, auto) { return; }).size(), 0);
30+
ASSERT_EQ(loader.GetPolicies([](auto, auto) { return; }, scanInterval, true).size(), 0);
2931
}

0 commit comments

Comments
 (0)