Skip to content

Commit 9263a97

Browse files
authored
Merge branch 'dev' into kkanas/fix-run-module-tests
2 parents b6eda91 + d3faa6a commit 9263a97

File tree

6 files changed

+111
-12
lines changed

6 files changed

+111
-12
lines changed

src/modules/complianceengine/src/lib/ProcedureMap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ const char* Bindings<PackageInstalledParams>::names[] = {"packageName", "minPack
8383
// SCE.h:18
8484
const char* Bindings<SCEParams>::names[] = {"scriptName", "ENVIRONMENT"};
8585

86-
// SystemdConfig.h:52
87-
const char* Bindings<SystemdParameterParams>::names[] = {"parameter", "valueRegex", "op", "value", "file", "block", "dir"};
86+
// SystemdConfig.h:55
87+
const char* Bindings<SystemdParameterParams>::names[] = {"parameter", "valueRegex", "op", "value", "file", "block", "dir", "passOnNotFound"};
8888

8989
// SystemdUnitState.h:28
9090
const char* Bindings<SystemdUnitStateParams>::names[] = {"unitName", "ActiveState", "LoadState", "UnitFileState", "Unit"};

src/modules/complianceengine/src/lib/ProcedureMap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ template <>
505505
struct Bindings<SystemdParameterParams>
506506
{
507507
using T = SystemdParameterParams;
508-
static constexpr size_t size = 7;
508+
static constexpr size_t size = 8;
509509
static const char* names[];
510-
static constexpr auto members = std::make_tuple(&T::parameter, &T::valueRegex, &T::op, &T::value, &T::file, &T::block, &T::dir);
510+
static constexpr auto members = std::make_tuple(&T::parameter, &T::valueRegex, &T::op, &T::value, &T::file, &T::block, &T::dir, &T::passOnNotFound);
511511
};
512512

513513
// Defines the bindings for the SystemdUnitStateParams structure.

src/modules/complianceengine/src/lib/procedures/SystemdConfig.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Result<bool> GetSystemdConfig(SystemdConfigMap_t& config, const std::string& fil
6767
std::string value = line.substr(eqSign + 1);
6868
config[std::make_pair(currentBlock, key)] = std::make_pair(value, currentConfig);
6969
}
70+
7071
return true;
7172
}
7273
} // namespace
@@ -160,7 +161,9 @@ Result<Status> AuditSystemdParameter(const SystemdParameterParams& params, Indic
160161
SystemdConfigMap_t::const_iterator paramIt = config.end();
161162
if (params.block.HasValue())
162163
{
163-
paramIt = config.find(std::make_pair(params.block.Value(), params.parameter));
164+
auto block = params.block.Value();
165+
block = "[" + block + "]";
166+
paramIt = config.find(std::make_pair(block, params.parameter));
164167
}
165168
else
166169
{
@@ -176,10 +179,19 @@ Result<Status> AuditSystemdParameter(const SystemdParameterParams& params, Indic
176179

177180
if (paramIt == config.end())
178181
{
182+
183+
assert(params.passOnNotFound.HasValue());
184+
if (params.passOnNotFound.Value())
185+
{
186+
OsConfigLogInfo(log, "Parameter '%s' not found but Compliant due to passOnNotFound==true", params.parameter.c_str());
187+
return indicators.Compliant("Parameter '" + params.parameter + "' not found but Compliant due to passOnNotFound==true");
188+
}
179189
if (params.block.HasValue())
180190
{
181-
OsConfigLogInfo(log, "Parameter '%s' not found in block '%s'", params.parameter.c_str(), params.block->c_str());
182-
return indicators.NonCompliant("Parameter '" + params.parameter + "' not found in block '" + params.block.Value() + "'");
191+
auto block = params.block.Value();
192+
block = "[" + block + "]";
193+
OsConfigLogInfo(log, "Parameter '%s' not found in block '%s'", params.parameter.c_str(), block.c_str());
194+
return indicators.NonCompliant("Parameter '" + params.parameter + "' not found in block '" + block + "'");
183195
}
184196
else
185197
{

src/modules/complianceengine/src/lib/procedures/SystemdConfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ struct SystemdParameterParams
4949

5050
/// Directory to search for config files
5151
Optional<std::string> dir;
52+
53+
/// If the value is not found return Compliant
54+
Optional<bool> passOnNotFound = false;
5255
};
5356

5457
Result<Status> AuditSystemdParameter(const SystemdParameterParams& params, IndicatorsTree& indicators, ContextInterface& context);

src/modules/complianceengine/src/lib/procedures/SystemdConfig.schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
"dir": {
4545
"type": "string",
4646
"description": "Directory to search for config files"
47+
},
48+
"passOnNotFound": {
49+
"type": "string",
50+
"description": "If the value is not found return Compliant"
4751
}
4852
}
4953
}

src/modules/complianceengine/tests/procedures/SystemdConfigTest.cpp

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ TEST_F(SystemdConfigTest, BlockParameterFoundInCorrectBlock)
643643
params.parameter = "Restart";
644644
params.valueRegex = regex("^always$");
645645
params.file = "test.conf";
646-
params.block = std::string("[Service]");
646+
params.block = std::string("Service");
647647

648648
auto result = AuditSystemdParameter(params, mIndicators, mContext);
649649
ASSERT_TRUE(result.HasValue());
@@ -666,7 +666,7 @@ TEST_F(SystemdConfigTest, BlockParameterNotFoundInWrongBlock)
666666
params.parameter = "Restart";
667667
params.valueRegex = regex("^always$");
668668
params.file = "test.conf";
669-
params.block = std::string("[Unit]");
669+
params.block = std::string("Unit");
670670

671671
auto result = AuditSystemdParameter(params, mIndicators, mContext);
672672
ASSERT_TRUE(result.HasValue());
@@ -686,7 +686,7 @@ TEST_F(SystemdConfigTest, BlockParameterNotFoundInNonexistentBlock)
686686
params.parameter = "ExecStart";
687687
params.valueRegex = regex(".*");
688688
params.file = "test.conf";
689-
params.block = std::string("[Install]");
689+
params.block = std::string("Install");
690690

691691
auto result = AuditSystemdParameter(params, mIndicators, mContext);
692692
ASSERT_TRUE(result.HasValue());
@@ -709,7 +709,7 @@ TEST_F(SystemdConfigTest, SameParameterInDifferentBlocksWithBlockFilter)
709709
params.op = SystemdParameterOperator::Equal;
710710
params.value = std::string("stream");
711711
params.file = "test.conf";
712-
params.block = std::string("[Socket]");
712+
params.block = std::string("Socket");
713713

714714
auto result = AuditSystemdParameter(params, mIndicators, mContext);
715715
ASSERT_TRUE(result.HasValue());
@@ -755,7 +755,7 @@ TEST_F(SystemdConfigTest, BlockWithOperatorComparison)
755755
params.op = SystemdParameterOperator::GreaterOrEqual;
756756
params.value = std::string("1024");
757757
params.file = "test.conf";
758-
params.block = std::string("[Service]");
758+
params.block = std::string("Service");
759759

760760
auto result = AuditSystemdParameter(params, mIndicators, mContext);
761761
ASSERT_TRUE(result.HasValue());
@@ -783,3 +783,83 @@ TEST_F(SystemdConfigTest, ParameterWithoutBlockHeaderFoundWithoutBlockFilter)
783783
ASSERT_TRUE(result.HasValue());
784784
ASSERT_EQ(result.Value(), Status::Compliant);
785785
}
786+
787+
TEST_F(SystemdConfigTest, JournalCompressUsesPassOnNotFoundWhenOutputHasNoJournalSection)
788+
{
789+
std::string systemdOutput = "# /etc/systemd/journald.conf\n";
790+
791+
EXPECT_CALL(mContext, ExecuteCommand(::testing::HasSubstr("/usr/bin/systemd-analyze cat-config journald.conf"))).WillOnce(Return(Result<std::string>(systemdOutput)));
792+
793+
SystemdParameterParams params;
794+
params.parameter = "Compress";
795+
params.valueRegex = regex("^yes$");
796+
params.file = "journald.conf";
797+
params.block = std::string("Journal");
798+
params.passOnNotFound = true;
799+
800+
auto result = AuditSystemdParameter(params, mIndicators, mContext);
801+
ASSERT_TRUE(result.HasValue());
802+
ASSERT_EQ(result.Value(), Status::Compliant);
803+
}
804+
805+
TEST_F(SystemdConfigTest, JournalCompressUsesPassOnNotFoundWhenJournalSectionHasNoCompressLine)
806+
{
807+
std::string systemdOutput =
808+
"# /etc/systemd/journald.conf\n"
809+
"[Journal]\n"
810+
"Storage=auto\n";
811+
812+
EXPECT_CALL(mContext, ExecuteCommand(::testing::HasSubstr("/usr/bin/systemd-analyze cat-config journald.conf"))).WillOnce(Return(Result<std::string>(systemdOutput)));
813+
814+
SystemdParameterParams params;
815+
params.parameter = "Compress";
816+
params.valueRegex = regex("^yes$");
817+
params.file = "journald.conf";
818+
params.block = std::string("Journal");
819+
params.passOnNotFound = true;
820+
821+
auto result = AuditSystemdParameter(params, mIndicators, mContext);
822+
ASSERT_TRUE(result.HasValue());
823+
ASSERT_EQ(result.Value(), Status::Compliant);
824+
}
825+
826+
TEST_F(SystemdConfigTest, JournalCompressWinsOverPassOnNotFound)
827+
{
828+
std::string systemdOutput =
829+
"# /etc/systemd/journald.conf\n"
830+
"[Journal]\n"
831+
"Compress=no\n";
832+
833+
EXPECT_CALL(mContext, ExecuteCommand(::testing::HasSubstr("/usr/bin/systemd-analyze cat-config journald.conf"))).WillOnce(Return(Result<std::string>(systemdOutput)));
834+
835+
SystemdParameterParams params;
836+
params.parameter = "Compress";
837+
params.valueRegex = regex("^yes$");
838+
params.file = "journald.conf";
839+
params.block = std::string("Journal");
840+
params.passOnNotFound = true;
841+
842+
auto result = AuditSystemdParameter(params, mIndicators, mContext);
843+
ASSERT_TRUE(result.HasValue());
844+
ASSERT_EQ(result.Value(), Status::NonCompliant);
845+
}
846+
847+
TEST_F(SystemdConfigTest, FileParameterNotFoundButPassOnNotFound)
848+
{
849+
std::string systemdOutput =
850+
"# /etc/systemd/test.conf\n"
851+
"OtherParam=value1\n"
852+
"AnotherParam=value2\n";
853+
854+
EXPECT_CALL(mContext, ExecuteCommand(::testing::HasSubstr("/usr/bin/systemd-analyze cat-config test.conf"))).WillOnce(Return(Result<std::string>(systemdOutput)));
855+
856+
SystemdParameterParams params;
857+
params.parameter = "TestParam";
858+
params.valueRegex = regex(".*");
859+
params.file = "test.conf";
860+
params.passOnNotFound = true;
861+
862+
auto result = AuditSystemdParameter(params, mIndicators, mContext);
863+
ASSERT_TRUE(result.HasValue());
864+
ASSERT_EQ(result.Value(), Status::Compliant);
865+
}

0 commit comments

Comments
 (0)