Skip to content
Draft
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
8 changes: 6 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ repos:
files: |
(?x)(
^src/modules/complianceengine/src/.*\.h$|
^src/modules/complianceengine/src/.*\.cpp$
^src/modules/complianceengine/src/.*\.cpp$|
^src/compliance-engine-assessor/.*\.hpp$|
^src/compliance-engine-assessor/.*\.cpp$
)
- repo: local
hooks:
Expand All @@ -67,7 +69,9 @@ repos:
files: |
(?x)(
^src/modules/complianceengine/.*\.h$|
^src/modules/complianceengine/.*\.cpp$
^src/modules/complianceengine/.*\.cpp$|
^src/compliance-engine-assessor/.*\.hpp$|
^src/compliance-engine-assessor/.*\.cpp$
)
exclude: |
(?x)(
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ option(BUILD_MODULETEST "Build the moduletest tool" ON)
option(BUILD_SAMPLES "Build samples" OFF)
option(COVERAGE "Enable code coverage" OFF)
option(BUILD_FUZZER "Build fuzzer" OFF)
option(BUILD_COMPLIANCE_ENGINE_ASSESSOR "Build the compliance engine assessor tool" ON)

add_compile_options("-Wno-psabi;-fPIC")
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
Expand Down
4 changes: 4 additions & 0 deletions src/modules/complianceengine/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

add_subdirectory(lib)
add_subdirectory(so)

if (BUILD_COMPLIANCE_ENGINE_ASSESSOR)
add_subdirectory(assessor)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <BenchmarkFormatter.hpp>
#include <cstdlib>
#include <iomanip>
#include <sstream>

namespace ComplianceEngine
{
namespace BenchmarkFormatters
{
using std::string;
using std::chrono::system_clock;

string BenchmarkFormatter::ToISODatetime(const system_clock::time_point& tp)
{
const auto time = system_clock::to_time_t(tp);
const auto tm = *std::gmtime(&time); // Convert to UTC time

char buffer[32];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &tm);
return buffer;
}

BenchmarkFormatter::BenchmarkFormatter()
{
mBegin = std::chrono::steady_clock::now();
}
} // namespace BenchmarkFormatters
} // namespace ComplianceEngine
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef COMPLIANCE_ENGINE_BENCHMARK_FORMATTER_HPP
#define COMPLIANCE_ENGINE_BENCHMARK_FORMATTER_HPP

#include <Evaluator.h>
#include <Mof.hpp>
#include <Optional.h>
#include <Result.h>
#include <chrono>
#include <string>

namespace ComplianceEngine
{
namespace BenchmarkFormatters
{
struct BenchmarkFormatter
{
static std::string ToISODatetime(const std::chrono::system_clock::time_point& tp);
std::chrono::time_point<std::chrono::steady_clock> mBegin;

BenchmarkFormatter();
virtual ~BenchmarkFormatter() = default;
BenchmarkFormatter(const BenchmarkFormatter&) = default;
BenchmarkFormatter& operator=(const BenchmarkFormatter&) = default;
BenchmarkFormatter(BenchmarkFormatter&&) = default;
BenchmarkFormatter& operator=(BenchmarkFormatter&&) = default;

virtual Optional<Error> Begin(Action action) = 0;
virtual Optional<Error> AddEntry(const MOF::Resource& entry, Status status, const std::string& payload) = 0;
virtual Result<std::string> Finish(Status status) = 0;
};
} // namespace BenchmarkFormatters
} // namespace ComplianceEngine
#endif // COMPLIANCE_ENGINE_BENCHMARK_FORMATTER_HPP
42 changes: 42 additions & 0 deletions src/modules/complianceengine/src/assessor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

add_compile_options("-Wall;-Wextra;-Wunused;-Werror;-Wformat;-Wformat-security;-Wno-unused-result")
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.4.7)
message(FATAL_ERROR "gcc-4.4.7 or newer is needed")
endif()

SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)

project(compliance-engine-assessor)
set(target_name compliance-engine-assessor)

set(SOURCES
Main.cpp
Mof.cpp
BenchmarkFormatter.cpp
CompactListFormatter.cpp
DebugFormatter.cpp
JsonFormatter.cpp
NestedListFormatter.cpp
)

add_executable(${target_name} ${SOURCES})

target_include_directories(${target_name} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${MODULES_INC_DIR}
)

target_link_libraries(${target_name}
${CMAKE_DL_LIBS}
logging
commonutils
parsonlib
complianceenginelib
)

include(GNUInstallDirs)
install(TARGETS ${target_name} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <CompactListFormatter.hpp>
#include <iomanip>
#include <sstream>
#include <version.h>

namespace ComplianceEngine
{
namespace BenchmarkFormatters
{
using std::string;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::steady_clock;
using std::chrono::system_clock;

Optional<Error> CompactListFormatter::Begin(const Action action)
{
mOutput << "Action: " << (action == Action::Audit ? "Audit" : "Remediation") << "\n";
mOutput << "OsConfig Version: " << OSCONFIG_VERSION << "\n";
mOutput << "Timestamp: " << ToISODatetime(system_clock::now()) << "\n";
mOutput << "Rules:\n";
return Optional<Error>();
}

Optional<Error> CompactListFormatter::AddEntry(const MOF::Resource& entry, const Status status, const string& payload)
{
mOutput << entry.resourceID << ":\n";
mOutput << payload;
mOutput << "Status: " << (status == Status::Compliant ? "Compliant" : "NonCompliant") << "\n";
return Optional<Error>();
}

Result<string> CompactListFormatter::Finish(const Status status)
{
mOutput << "Duration: " << std::chrono::duration_cast<milliseconds>(steady_clock::now() - mBegin).count() << " ms\n";
mOutput << "Status: " << (status == Status::Compliant ? "Compliant" : "NonCompliant") << "\n";
mOutput << "End of Report";
return mOutput.str();
}
} // namespace BenchmarkFormatters
} // namespace ComplianceEngine
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef COMPLIANCE_ENGINE_COMPACT_LIST_FORMATTER_HPP
#define COMPLIANCE_ENGINE_COMPACT_LIST_FORMATTER_HPP

#include <BenchmarkFormatter.hpp>
#include <sstream>

namespace ComplianceEngine
{
namespace BenchmarkFormatters
{
struct CompactListFormatter : public BenchmarkFormatter
{
Optional<Error> Begin(Action action) override;
Optional<Error> AddEntry(const MOF::Resource& entry, Status status, const std::string& payload) override;
Result<std::string> Finish(Status status) override;

private:
std::ostringstream mOutput;
};
} // namespace BenchmarkFormatters
} // namespace ComplianceEngine
#endif // COMPLIANCE_ENGINE_COMPACT_LIST_FORMATTER_HPP
45 changes: 45 additions & 0 deletions src/modules/complianceengine/src/assessor/DebugFormatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <DebugFormatter.hpp>
#include <version.h>

namespace ComplianceEngine
{
namespace BenchmarkFormatters
{
using ComplianceEngine::Action;
using ComplianceEngine::Error;
using ComplianceEngine::Evaluator;
using ComplianceEngine::Optional;
using ComplianceEngine::Result;
using ComplianceEngine::Status;
using std::string;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::steady_clock;
using std::chrono::system_clock;

Optional<Error> DebugFormatter::Begin(const Action action)
{
mOutput << "Action: " << (action == Action::Audit ? "Audit" : "Remediation") << "\n";
mOutput << "OsConfig Version: " << OSCONFIG_VERSION << "\n";
mOutput << "Timestamp: " << ToISODatetime(system_clock::now()) << "\n";
mOutput << "Rules:\n";
return Optional<Error>();
}

Optional<Error> DebugFormatter::AddEntry(const MOF::Resource& entry, const Status status, const string& payload)
{
mOutput << entry.resourceID << ":\n";
mOutput << payload << "\n";
mOutput << "Status: " << (status == Status::Compliant ? "Compliant" : "NonCompliant") << "\n";
return Optional<Error>();
}

Result<string> DebugFormatter::Finish(const Status status)
{
mOutput << "Duration: " << std::chrono::duration_cast<milliseconds>(steady_clock::now() - mBegin).count() << " ms\n";
mOutput << "Status: " << (status == Status::Compliant ? "Compliant" : "NonCompliant") << "\n";
mOutput << "End of Report";
return mOutput.str();
}
} // namespace BenchmarkFormatters
} // namespace ComplianceEngine
23 changes: 23 additions & 0 deletions src/modules/complianceengine/src/assessor/DebugFormatter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef COMPLIANCE_ENGINE_MMI_FORMATTER_HPP
#define COMPLIANCE_ENGINE_MMI_FORMATTER_HPP

#include <BenchmarkFormatter.hpp>
#include <sstream>

namespace ComplianceEngine
{
namespace BenchmarkFormatters
{
struct DebugFormatter : public BenchmarkFormatter
{
Optional<Error> Begin(Action action) override;
Optional<Error> AddEntry(const MOF::Resource& entry, Status status, const std::string& payload) override;
Result<std::string> Finish(Status status) override;

private:
std::ostringstream mOutput;
};
} // namespace BenchmarkFormatters
} // namespace ComplianceEngine

#endif // COMPLIANCE_ENGINE_MMI_FORMATTER_HPP
Loading
Loading