Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compliance/procedures pt1 #881

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open

Compliance/procedures pt1 #881

wants to merge 16 commits into from

Conversation

wupeka
Copy link
Contributor

@wupeka wupeka commented Feb 21, 2025

Description

First set of compliance procedures - testing procedures, ensureFilesystemOptions, ensureFilePermissions.
FilePermissions procedures are missing tests.

Checklist

  • I have read the contribution guidelines.
  • I added unit-tests to validate my changes. All unit tests are passing.
  • I have merged the latest dev branch prior to this PR submission.
  • I ran pre-commit on my changes prior to this PR submission.
  • I submitted this PR against the dev branch.

@wupeka wupeka requested a review from a team as a code owner February 21, 2025 08:08
Copy link

github-actions bot commented Feb 21, 2025

Test Results

 44 files   44 suites   40m 53s ⏱️
  4 tests   4 ✅  0 💤 0 ❌
176 runs  154 ✅ 22 💤 0 ❌

Results for commit 791ffa3.

♻️ This comment has been updated with latest results.

@wupeka wupeka force-pushed the compliance/procedures-pt1 branch 2 times, most recently from fa2fa36 to b1ace1a Compare February 21, 2025 09:02
@@ -16,6 +18,13 @@ add_library(compliancelib STATIC

set_property(TARGET compliancelib PROPERTY POSITION_INDEPENDENT_CODE ON)

include(FindPerl)
add_custom_command(
OUTPUT ProcedureMap.cpp ProcedureMap.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make clean will drop these files


namespace compliance {

AUDIT_FN(ensureFilePermissions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get rid of these macros? it reads as

Suggested change
AUDIT_FN(ensureFilePermissions)
Result<bool> Audit_ensureFilePermissions(std::map<std::string, std::string> args, std::ostringstream &logstream)


AUDIT_FN(ensureFilePermissions)
{
struct stat statbuf;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can skip the struct keyword here

#include <CommonUtils.h>

namespace compliance {
REMEDIATE_FN(remediationFailure)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the following define:

if (BUILD_TESTS)
    add_compile_options(-D TEST_CODE)
    add_subdirectory(tests)
endif()

Do you think it makes sense add these functions only when TEST_CODE is defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will be using those procedures for end-to-end tests etc, the costs of having them compiled in is neglible, so I'd keep them.

@@ -0,0 +1,99 @@
#!/usr/bin/env perl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just use bash for this? Bringing in Perl just for this seems like overkill

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Perl is currently in all of our build environments. (unfortunately, Python is not).
  2. We haven't decided finally yet, but we'll probably pre-generate the ProcedureMap and put it in the repo, so this script won't be launched at build time - only manually when someone adds/removes a procedure. For now, with the amount of churn, it's easier to just have it automated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh nice, i didn't realize the environments already contained Perl, i just validated that ubun14.04/debian-10 have it 👌


fstabMap[mnt->mnt_dir] = entry;
}
fclose(file);
Copy link
Contributor

@AhmedBM AhmedBM Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignoring fclose return value? Do we care if we cannot close file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's open for read only, so we don't care (and it should never fail in those circumstances).

struct passwd *pwd = getpwuid(statbuf.st_uid);
if (nullptr == pwd)
{
logstream << "No user with uid " << statbuf.st_uid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we not using the OSCONFIG_LOG_INFO/OSCONFIG_LOG_ERROR macros here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logstream is used to create a string with the result of the audit for the UI. It is then also logged to OSCONFIG_LOG.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say the UI, do you mean this is whats displayed in the non-compliance reason in the portal? In the end however, wouldn't this string be a very long concatenated string, im not seeing any newlines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately - yes, that's the only thing that we can (at least for now) present to the user.
It usually won't be that complex though.

@@ -5,6 +5,7 @@ project(compliancetests)

include(CTest)
find_package(GTest REQUIRED)
FILE(GLOB PROCEDURES procedures/*.cpp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im generally against CMake file GLOBS because CMake cannot track changes and re-run if there are new/removed files but i believe these procedures are generated dynamically? If so, i understand the need to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Procedures are not generated dynamically, but at this stage we expect to have a lot of churn both here and in src/modules/compliance/src/lib/procedures, and we want to parallelilze the work on different procedures in different files. Having a glob means we don't need to change CMakeLists.txt with every new file, so we won't have conflicts. In the end we'll just put a static list there. (The same rationale applies to generating ProcedureMap at build time)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, can you add a comment above to state that the GLOB is temporary until we stabilize?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@AhmedBM AhmedBM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Perl is being added as a build requirement, we would need to update every build container to include it. These are located in devops/docker

Makes me wonder though, can we not just do this in bash?

@wupeka wupeka force-pushed the compliance/procedures-pt1 branch 3 times, most recently from e369a13 to ca00f6e Compare February 25, 2025 13:40
wupeka and others added 10 commits February 28, 2025 12:49
Signed-off-by: Krzysztof Kanas <kkanas@microsoft.com>
Fix logs from:
    MmiGet(0x7ecdec017510, Compliance, auditEnsureAccessToEtcMotdIsConfigured, "{ allOf: [{ ensureFilePermissions: ensureFilePermissions for /etc/motdStat errorNo such file or directory } == FALSE] == FALSE }")
    BaselineMmiGet(Compliance, auditEnsureAccessToEtcMotdIsConfigured): '"{ allOf: [{ ensureFilePermissions: ensureFilePermissions for /etc/motdStat errorNo such file or directory } == FALSE] == FALSE }"' (130)
To:
    MmiGet(0x7cf35c02bf50, Compliance, auditEnsureAccessToEtcMotdIsConfigured, "{ allOf: [{ ensureFilePermissions: ensureFilePermissions for '/etc/motd' Stat error 'No such file or directory' } == FALSE] == FALSE }")
    BaselineMmiGet(Compliance, auditEnsureAccessToEtcMotdIsConfigured): '"{ allOf: [{ ensureFilePermissions: ensureFilePermissions for '/etc/motd' Stat error 'No such file or directory' } == FALSE] == FALSE }"' (136)

Signed-off-by: Krzysztof Kanas <kkanas@microsoft.com>
@wupeka wupeka force-pushed the compliance/procedures-pt1 branch from ae9d7f5 to 791ffa3 Compare February 28, 2025 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants