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

Process files in tests/shared by Jinja #12867

Merged
merged 5 commits into from
Jan 23, 2025

Conversation

jan-cerny
Copy link
Collaborator

Description:

Resolve Jinja expressions in files in the tests/shared directory when running Automatus tests. The files will be processed with Jinja before they will be packed to a tarball and sent to the testing back end.

For more details, please read commit messages of every commit.

Rationale:

This change will fix some failing test scenarios in rules starting with audit_rules_unsuccessful_file_modification, specifically:

  • audit_rules_unsuccessful_file_modification_open_o_creat/o_creat_rules.pass
  • audit_rules_unsuccessful_file_modification_open_o_trunc_write/o_trunc_rules.pass
  • audit_rules_unsuccessful_file_modification_open_rule_order/ordered_by_arch.pass
  • audit_rules_unsuccessful_file_modification_open_rule_order/ordered_by_filter.pass
  • audit_rules_unsuccessful_file_modification_openat_o_creat/o_creat_last.pass
  • audit_rules_unsuccessful_file_modification_openat_o_creat/o_creat_rules.pass

These scenarios were erroring because they use files from tests/shared, these files contain Jinja expressions but the expansion of the Jinja expressions wasn't performed by automatus. A specific example is that the {{{ uid_min }}} expression wasn't expanded in tests/shared/audit_open_o_creat.rules and tests/shared/audit_open_o_creat.rules.

Review Hints:

python3 tests/automatus.py rule --libvirt qemu:///system ssgts_rhel10 audit_rules_unsuccessful_file_modification_open_o_creat audit_rules_unsuccessful_file_modification_open_o_trunc_write audit_rules_unsuccessful_file_modification_open_rule_order audit_rules_unsuccessful_file_modification_openat_o_creat 

After this change, the keys in other_content will be allowed to be not
only file names but also they can be relative file paths, including
directories. In other words, the other_content will be able to supply
content also in directories and subdirectories. The directory structure
will be preserved in the tarball.
The files located in `tests/shared` will not be copied directly
to the tarball to be uploaded. Instead, before copying them,
Jinja variables will be substituted in them. This change will allow us
to use conditions and product properties and other Jinja code in files
in the `tests/shared` directory.
The `is_rule_dir` function is a part of `ssg.rules` module, it doesn't need
to be transitively imported through `ssg_test_suite.common`, but it can be
imported directly.

Also, remove unused imports.
Ensure newline at the end of the file because
process_file_with_macros strips it off. This leads to unexpected
scenarios fails if files copied are concatenated together, for
example:
linux_os/guide/auditing/auditd_configure_rules/audit_file_modification/audit_rules_unsuccessful_file_modification_open_rule_order/tests/ordered_by_filter.pass.sh
@jan-cerny jan-cerny added Test Suite Update in Test Suite. RHEL10 Red Hat Enterprise Linux 10 product related. labels Jan 22, 2025
@jan-cerny jan-cerny added this to the 0.1.76 milestone Jan 22, 2025
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 22, 2025
Copy link

openshift-ci bot commented Jan 22, 2025

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@jan-cerny jan-cerny marked this pull request as ready for review January 22, 2025 09:44
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 22, 2025
@ggbecker
Copy link
Member

/packit retest-failed

@ggbecker ggbecker self-assigned this Jan 22, 2025
@ggbecker
Copy link
Member

ggbecker commented Jan 22, 2025

I've found out that there was a problem with the detection of the augenrules in /usr/lib/systemd/system/audit-rules.service from RHEL10 box, I'm not sure if that changed in newer composes of RHEL10, but the version I have here has the following content and the regex that detects expects it to be present in /sbin/augenrules instead, both binaries exist and they are the same (same checksum), but they don't seem to be a symlink as they usually are.

These binaries are owned by:

rpm -qf /usr/sbin/augenrules 
audit-rules-4.0.3-1.el10.x86_64

Excerpt of /usr/lib/systemd/system/audit-rules.service:

[Service]
Type=oneshot
ExecStart=/usr/sbin/augenrules --load

<ind:pattern operation="pattern match">^ExecStart=\/sbin\/augenrules.*$</ind:pattern>

The following change should fix the issue:

-    <ind:pattern operation="pattern match">^ExecStart=\/sbin\/augenrules.*$</ind:pattern>
+    <ind:pattern operation="pattern match">^ExecStart=(\/usr|)?\/sbin\/augenrules.*$</ind:pattern>

RHEL10 /usr/lib/systemd/system/audit-rules.service content now has the
following:

ExecStart=/usr/sbin/augenrules --load

So the regex had to be updated to accept /usr but also /sbin in case the
other binary is used which should be still acceptable.
@ggbecker
Copy link
Member

With 0337848 I now get the following from automatus:

python3 tests/automatus.py rule --libvirt qemu:///system rhel10 --datastream build/ssg-rhel10-ds.xml audit_rules_unsuccessful_file_modification_open_o_creat audit_rules_unsuccessful_file_modification_open_o_trunc_write audit_rules_unsuccessful_file_modification_open_rule_order audit_rules_unsuccessful_file_modification_openat_o_creat
Setting console output to log level INFO
INFO - The base image option has not been specified, choosing libvirt-based test environment.
INFO - Logging into /home/ggasparb/workspace/github/content/logs/rule-custom-2025-01-22-1522/test_suite.log
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_open_o_creat
INFO - Script empty.fail.sh using profile (all) OK
INFO - Script o_trunc_write_rules.fail.sh using profile (all) OK
INFO - Script open_rules.fail.sh using profile (all) OK
INFO - Script rules-amis.fail.sh using profile (all) OK
INFO - Script o_creat_rules.pass.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_open_o_trunc_write
INFO - Script empty.fail.sh using profile (all) OK
INFO - Script o_creat_rules.fail.sh using profile (all) OK
INFO - Script o_trunc_rules.pass.sh using profile (all) OK
INFO - Script open_rules.fail.sh using profile (all) OK
INFO - Script rules-amis.fail.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_open_rule_order
INFO - Script one_rule_missing.fail.sh using profile (all) OK
INFO - Script ordered_by_arch.pass.sh using profile (all) OK
INFO - Script ordered_by_filter.pass.sh using profile (all) OK
INFO - Script sorted_rules.fail.sh using profile (all) OK
INFO - Script unordered.fail.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_openat_o_creat
INFO - Script empty.fail.sh using profile (all) OK
INFO - Script o_creat_last.pass.sh using profile (all) OK
INFO - Script o_creat_rules.pass.sh using profile (all) OK
INFO - Script o_trunc_write.fail.sh using profile (all) OK
INFO - Script open_rules.fail.sh using profile (all) OK
INFO - Script rules-amis.fail.sh using profile (all) OK

Copy link

openshift-ci bot commented Jan 22, 2025

@jan-cerny: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/4.13-images 0337848 link true /test 4.13-images

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@jan-cerny
Copy link
Collaborator Author

Interesting. I have a newer one in my automatus machine:

[root@localhost ~]# rpm -qf /usr/sbin/augenrules 
audit-rules-4.0-10.el10.x86_64


Copy link

codeclimate bot commented Jan 22, 2025

Code Climate has analyzed commit 0337848 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 61.9% (0.0% change).

View more on Code Climate.

@jan-cerny
Copy link
Collaborator Author

With my audit-rules version and with your changes it passes as well:

jcerny@fedora:~/work/git/scap-security-guide (audit_shared)$ python3 tests/automatus.py rule --libvirt qemu:///system ssgts_rhel10 audit_rules_unsuccessful_file_modification_open_o_creat audit_rules_unsuccessful_file_modification_open_o_trunc_write audit_rules_unsuccessful_file_modification_open_rule_order audit_rules_unsuccessful_file_modification_openat_o_creat 
Setting console output to log level INFO
INFO - The base image option has not been specified, choosing libvirt-based test environment.
INFO - Logging into /home/jcerny/work/git/scap-security-guide/logs/rule-custom-2025-01-22-1555/test_suite.log
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_open_o_creat
INFO - Script empty.fail.sh using profile (all) OK
INFO - Script o_creat_rules.pass.sh using profile (all) OK
INFO - Script o_trunc_write_rules.fail.sh using profile (all) OK
INFO - Script open_rules.fail.sh using profile (all) OK
INFO - Script rules-amis.fail.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_open_o_trunc_write
INFO - Script empty.fail.sh using profile (all) OK
INFO - Script o_creat_rules.fail.sh using profile (all) OK
INFO - Script o_trunc_rules.pass.sh using profile (all) OK
INFO - Script open_rules.fail.sh using profile (all) OK
INFO - Script rules-amis.fail.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_open_rule_order
INFO - Script one_rule_missing.fail.sh using profile (all) OK
INFO - Script ordered_by_arch.pass.sh using profile (all) OK
INFO - Script ordered_by_filter.pass.sh using profile (all) OK
INFO - Script sorted_rules.fail.sh using profile (all) OK
INFO - Script unordered.fail.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_openat_o_creat
INFO - Script empty.fail.sh using profile (all) OK
INFO - Script o_creat_last.pass.sh using profile (all) OK
INFO - Script o_creat_rules.pass.sh using profile (all) OK
INFO - Script o_trunc_write.fail.sh using profile (all) OK
INFO - Script open_rules.fail.sh using profile (all) OK
INFO - Script rules-amis.fail.sh using profile (all) OK

@ggbecker
Copy link
Member

Interesting. I have a newer one in my automatus machine:

[root@localhost ~]# rpm -qf /usr/sbin/augenrules 
audit-rules-4.0-10.el10.x86_64

And what are the contents of: /usr/lib/systemd/system/audit-rules.service?

Because if the addition of /usr is a bug, we might not need to have this improved regex.

@jan-cerny
Copy link
Collaborator Author

Sorry for confusion, 4.0.3-1 is newer than 4.0-10.

@jan-cerny
Copy link
Collaborator Author

4.0-10 has ExecStart=/sbin/augenrules --load
4.0.3-1 has ExecStart=/usr/sbin/augenrules --load

That means we need to keep the improved regex in the PR.

@ggbecker ggbecker merged commit 04e847f into ComplianceAsCode:master Jan 23, 2025
108 of 109 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RHEL10 Red Hat Enterprise Linux 10 product related. Test Suite Update in Test Suite.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants