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

[template]: fixes in file_owner template #13070

Merged
merged 12 commits into from
Mar 12, 2025

Conversation

alanmcanonical
Copy link
Contributor

Description:

Rationale:

  • We need to dynamically read uid from /etc/passwd to be more reliable

Copy link

openshift-ci bot commented Feb 17, 2025

Hi @alanmcanonical. Thanks for your PR.

I'm waiting for a ComplianceAsCode member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@openshift-ci openshift-ci bot added the needs-ok-to-test Used by openshift-ci bot. label Feb 17, 2025
@alanmcanonical alanmcanonical marked this pull request as draft February 17, 2025 21:04
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Feb 17, 2025
Copy link

github-actions bot commented Feb 17, 2025

This datastream diff is auto generated by the check Compare DS/Generate Diff

Click here to see the full diff
bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_owner_systemmap' differs.
--- xccdf_org.ssgproject.content_rule_file_owner_systemmap
+++ xccdf_org.ssgproject.content_rule_file_owner_systemmap
@@ -1,2 +1,2 @@
 
-find -L /boot/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended -regex '^.*System\.map.*$' -exec chown -L 0 {} \;
+find -L /boot/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended -regex '^.*System\.map.*$' -exec chown -L 0 {} \;

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_owner_systemmap' differs.
--- xccdf_org.ssgproject.content_rule_file_owner_systemmap
+++ xccdf_org.ssgproject.content_rule_file_owner_systemmap
@@ -1,6 +1,6 @@
 - name: Find /boot/ file(s) matching ^.*System\.map.*$
-  command: find -H /boot/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended -regex
-    "^.*System\.map.*$"
+  command: find -H /boot/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended
+    -regex "^.*System\.map.*$"
   register: files_found
   changed_when: false
   failed_when: false

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_groupowner_var_log_syslog' differs.
--- xccdf_org.ssgproject.content_rule_file_groupowner_var_log_syslog
+++ xccdf_org.ssgproject.content_rule_file_groupowner_var_log_syslog
@@ -1,7 +1,19 @@
+- name: Gather the package facts
+  package_facts:
+    manager: auto
+  tags:
+  - configure_strategy
+  - file_groupowner_var_log_syslog
+  - low_complexity
+  - low_disruption
+  - medium_severity
+  - no_reboot_needed
+
 - name: Test for existence /var/log/syslog
   stat:
     path: /var/log/syslog
   register: file_exists
+  when: '"rsyslog" in ansible_facts.packages'
   tags:
   - configure_strategy
   - file_groupowner_var_log_syslog
@@ -14,7 +26,9 @@
   file:
     path: /var/log/syslog
     group: '4'
-  when: file_exists.stat is defined and file_exists.stat.exists
+  when:
+  - '"rsyslog" in ansible_facts.packages'
+  - file_exists.stat is defined and file_exists.stat.exists
   tags:
   - configure_strategy
   - file_groupowner_var_log_syslog

Platform has been changed for rule 'xccdf_org.ssgproject.content_rule_file_groupowner_var_log_syslog'
--- xccdf_org.ssgproject.content_rule_file_groupowner_var_log_syslog
+++ xccdf_org.ssgproject.content_rule_file_groupowner_var_log_syslog
@@ -1 +1 @@
-
+oval:ssg-package_rsyslog:def:1

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_owner_var_log_syslog' differs.
--- xccdf_org.ssgproject.content_rule_file_owner_var_log_syslog
+++ xccdf_org.ssgproject.content_rule_file_owner_var_log_syslog
@@ -1,7 +1,6 @@
-- name: Test for existence /var/log/syslog
-  stat:
-    path: /var/log/syslog
-  register: file_exists
+- name: Gather the package facts
+  package_facts:
+    manager: auto
   tags:
   - configure_strategy
   - file_owner_var_log_syslog
@@ -10,11 +9,11 @@
   - medium_severity
   - no_reboot_needed
 
-- name: Ensure owner 0 on /var/log/syslog
-  file:
+- name: Test for existence /var/log/syslog
+  stat:
     path: /var/log/syslog
-    owner: '0'
-  when: file_exists.stat is defined and file_exists.stat.exists
+  register: file_exists
+  when: '"rsyslog" in ansible_facts.packages'
   tags:
   - configure_strategy
   - file_owner_var_log_syslog
@@ -22,3 +21,18 @@
   - low_disruption
   - medium_severity
   - no_reboot_needed
+
+- name: Ensure owner syslog on /var/log/syslog
+  file:
+    path: /var/log/syslog
+    owner: syslog
+  when:
+  - '"rsyslog" in ansible_facts.packages'
+  - file_exists.stat is defined and file_exists.stat.exists
+  tags:
+  - configure_strategy
+  - file_owner_var_log_syslog
+  - low_complexity
+  - low_disruption
+  - medium_severity
+  - no_reboot_needed

Platform has been changed for rule 'xccdf_org.ssgproject.content_rule_file_owner_var_log_syslog'
--- xccdf_org.ssgproject.content_rule_file_owner_var_log_syslog
+++ xccdf_org.ssgproject.content_rule_file_owner_var_log_syslog
@@ -1 +1 @@
-
+oval:ssg-package_rsyslog:def:1

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_library_dirs' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_library_dirs
+++ xccdf_org.ssgproject.content_rule_file_ownership_library_dirs
@@ -1,8 +1,8 @@
 
-find  /lib/  -type f ! -uid 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
+find  /lib/  -type f ! -user 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
 
-find  /lib64/  -type f ! -uid 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
+find  /lib64/  -type f ! -user 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
 
-find  /usr/lib/  -type f ! -uid 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
+find  /usr/lib/  -type f ! -user 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
 
-find  /usr/lib64/  -type f ! -uid 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;
+find  /usr/lib64/  -type f ! -user 0 -regextype posix-extended -regex '^.*$' -exec chown -L 0 {} \;

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_library_dirs' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_library_dirs
+++ xccdf_org.ssgproject.content_rule_file_ownership_library_dirs
@@ -1,5 +1,5 @@
 - name: Find /lib/ file(s) matching ^.*$ recursively
-  command: find -H /lib/  -type f ! -uid 0 -regextype posix-extended -regex "^.*$"
+  command: find -H /lib/  -type f ! -user 0 -regextype posix-extended -regex "^.*$"
   register: files_found
   changed_when: false
   failed_when: false
@@ -40,7 +40,7 @@
   - no_reboot_needed
 
 - name: Find /lib64/ file(s) matching ^.*$ recursively
-  command: find -H /lib64/  -type f ! -uid 0 -regextype posix-extended -regex "^.*$"
+  command: find -H /lib64/  -type f ! -user 0 -regextype posix-extended -regex "^.*$"
   register: files_found
   changed_when: false
   failed_when: false
@@ -81,7 +81,7 @@
   - no_reboot_needed
 
 - name: Find /usr/lib/ file(s) matching ^.*$ recursively
-  command: find -H /usr/lib/  -type f ! -uid 0 -regextype posix-extended -regex "^.*$"
+  command: find -H /usr/lib/  -type f ! -user 0 -regextype posix-extended -regex "^.*$"
   register: files_found
   changed_when: false
   failed_when: false
@@ -122,7 +122,7 @@
   - no_reboot_needed
 
 - name: Find /usr/lib64/ file(s) matching ^.*$ recursively
-  command: find -H /usr/lib64/  -type f ! -uid 0 -regextype posix-extended -regex
+  command: find -H /usr/lib64/  -type f ! -user 0 -regextype posix-extended -regex
     "^.*$"
   register: files_found
   changed_when: false

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_cron_allow_exists' differs.
--- xccdf_org.ssgproject.content_rule_file_cron_allow_exists
+++ xccdf_org.ssgproject.content_rule_file_cron_allow_exists
@@ -2,7 +2,6 @@
 if rpm --quiet -q kernel; then
 
 touch /etc/cron.allow
-    chown 0 /etc/cron.allow
     chmod 0600 /etc/cron.allow
 
 else

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_cron_allow_exists' differs.
--- xccdf_org.ssgproject.content_rule_file_cron_allow_exists
+++ xccdf_org.ssgproject.content_rule_file_cron_allow_exists
@@ -14,7 +14,6 @@
   file:
     path: /etc/cron.allow
     state: touch
-    owner: '0'
     mode: '0600'
   when: '"kernel" in ansible_facts.packages'
   tags:

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_sshd_private_key' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_sshd_private_key
+++ xccdf_org.ssgproject.content_rule_file_ownership_sshd_private_key
@@ -1,7 +1,7 @@
 # Remediation is applicable only in certain platforms
 if rpm --quiet -q kernel; then
 
-find -L /etc/ssh/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended -regex '^.*_key$' -exec chown -L 0 {} \;
+find -L /etc/ssh/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended -regex '^.*_key$' -exec chown -L 0 {} \;
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_sshd_private_key' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_sshd_private_key
+++ xccdf_org.ssgproject.content_rule_file_ownership_sshd_private_key
@@ -11,7 +11,7 @@
   - no_reboot_needed
 
 - name: Find /etc/ssh/ file(s) matching ^.*_key$
-  command: find -H /etc/ssh/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended
+  command: find -H /etc/ssh/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended
     -regex "^.*_key$"
   register: files_found
   changed_when: false

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_sshd_pub_key' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_sshd_pub_key
+++ xccdf_org.ssgproject.content_rule_file_ownership_sshd_pub_key
@@ -1,7 +1,7 @@
 # Remediation is applicable only in certain platforms
 if rpm --quiet -q kernel; then
 
-find -L /etc/ssh/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended -regex '^.*\.pub$' -exec chown -L 0 {} \;
+find -L /etc/ssh/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended -regex '^.*\.pub$' -exec chown -L 0 {} \;
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_sshd_pub_key' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_sshd_pub_key
+++ xccdf_org.ssgproject.content_rule_file_ownership_sshd_pub_key
@@ -11,7 +11,7 @@
   - no_reboot_needed
 
 - name: Find /etc/ssh/ file(s) matching ^.*\.pub$
-  command: find -H /etc/ssh/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended
+  command: find -H /etc/ssh/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended
     -regex "^.*\.pub$"
   register: files_found
   changed_when: false

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration
+++ xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration
@@ -1,9 +1,9 @@
 # Remediation is applicable only in certain platforms
 if rpm --quiet -q audit && rpm --quiet -q kernel; then
 
-find -L /etc/audit/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended -regex '^.*audit(\.rules|d\.conf)$' -exec chown -L 0 {} \;
+find -L /etc/audit/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended -regex '^.*audit(\.rules|d\.conf)$' -exec chown -L 0 {} \;
 
-find -L /etc/audit/rules.d/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended -regex '^.*\.rules$' -exec chown -L 0 {} \;
+find -L /etc/audit/rules.d/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended -regex '^.*\.rules$' -exec chown -L 0 {} \;
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

ansible remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration' differs.
--- xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration
+++ xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration
@@ -11,7 +11,7 @@
   - no_reboot_needed
 
 - name: Find /etc/audit/ file(s) matching ^.*audit(\.rules|d\.conf)$
-  command: find -H /etc/audit/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended
+  command: find -H /etc/audit/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended
     -regex "^.*audit(\.rules|d\.conf)$"
   register: files_found
   changed_when: false
@@ -49,7 +49,7 @@
   - no_reboot_needed
 
 - name: Find /etc/audit/rules.d/ file(s) matching ^.*\.rules$
-  command: find -H /etc/audit/rules.d/ -maxdepth 1 -type f ! -uid 0 -regextype posix-extended
+  command: find -H /etc/audit/rules.d/ -maxdepth 1 -type f ! -user 0 -regextype posix-extended
     -regex "^.*\.rules$"
   register: files_found
   changed_when: false

Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

1 similar comment
Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

@dodys dodys self-assigned this Feb 18, 2025
Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

1 similar comment
Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

@alanmcanonical alanmcanonical marked this pull request as ready for review February 20, 2025 02:09
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Feb 20, 2025
@alanmcanonical
Copy link
Contributor Author

The audit pkg issue caused by rule file_ownership_audit_configuration # packages = audit, I checked ubi8 image which yum search audit indeed return no result. So probably rhel needs a package override.

Copy link
Contributor

@dodys dodys left a comment

Choose a reason for hiding this comment

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

please do a rebase on top of master

Copy link
Contributor

@dodys dodys left a comment

Choose a reason for hiding this comment

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

tests are failing a lot, please check logs

Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

@dodys dodys requested review from a team February 25, 2025 14:28
@dodys
Copy link
Contributor

dodys commented Feb 25, 2025

@ComplianceAsCode/red-hatters @ComplianceAsCode/suse-maintainers @ComplianceAsCode/oracle-maintainers
could you take a look if we are not breaking anything for you?

Copy link
Contributor

@Xeicker Xeicker left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@teacup-on-rockingchair teacup-on-rockingchair left a comment

Choose a reason for hiding this comment

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

Good stuff 👍 Besides the tests fixes everything else looks great to me 🙇

Copy link
Contributor

@dodys dodys left a comment

Choose a reason for hiding this comment

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

@alanmcanonical tests are still failing.

Copy link

github-actions bot commented Mar 5, 2025

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

@dodys
Copy link
Contributor

dodys commented Mar 5, 2025

/packit build

change parameter fileuid of file_owner template to uid_or_name
explain its behavior
change the parameter name from filgeuid to uid_or_name
additionally, use the -user parameter instead of -uid parameter in find command invocation
the -user parameter accepts both uid and user names, where the -uid parameter accepts only uid
establish a new variable which carries information if the parameter is a uid or a user name
decide if to user ID or user name based on the variable declared in the preprocessing function
change parameter name from file_uid to uid_or_name
Copy link

github-actions bot commented Mar 6, 2025

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

@alanmcanonical
Copy link
Contributor Author

alanmcanonical commented Mar 9, 2025

The file_owner_var_log_syslog-incorrect_owner.fail.sh-remediation.verbose.log shows:

TASK [Gather the package facts] ************************************************
[skip some log for clearness]
...
...
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="root"' -o ConnectTimeout=10 -o Port=35135 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o 'ControlPath="/home/runner/.ansible/cp/72dc686b8e"' -tt localhost '/bin/sh -c '"'"'/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1741253229.205663-61782-134313576207627/AnsiballZ_package_facts.py && sleep 0'"'"''
(1, b'Traceback (most recent call last):\r\n  File \x1b[35m"<string>"\x1b[0m, line \x1b[35m1\x1b[0m, in \x1b[35m<module>\x1b[0m\r\n    import rpm\r\n\x1b[1;35mModuleNotFoundError\x1b[0m: \x1b[35mNo module named \'rpm\'\x1b

Seems like this anisble snippet from build/rhel8/fixes/ansible/file_owner_var_log_syslog.yml is problematic

  package_facts:
    manager: auto

more log:

<localhost> (1, b'Traceback (most recent call last):\r\n  File \x1b[35m"<string>"\x1b[0m, line \x1b[35m1\x1b[0m, in \x1b[35m<module>\x1b[0m\r\n    import rpm\r\n\x1b[1;35mModuleNotFoundError\x1b[0m: \x1b[35mNo module named \'rpm\'\x1b[0m\r\n\r\n{"failed": true, "msg": "Could not detect a supported package manager from the following list: [\'rpm\', \'pacman\', \'pkg_info\', \'pkg\', \'apt\', \'apk\', \'portage\'], or the required Python library is not installed. Check warnings for details.", "invocation": {"module_args": {"manager": ["auto"], "strategy": "first"}}, "warnings": ["Found \\"rpm\\" but Failed to import the required Python library (rpm) on c96b3ba27c3d\'s Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"]}\r\n', b'Shared connection to localhost closed.\r\n')

@alanmcanonical
Copy link
Contributor Author

Updates:

according to https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_facts_module.html#requirements

here is the requirement:

  • "rpm": For RPM based distros, requires RPM Python bindings, not installed by default on Suse (python3-rpm)

Copy link

github-actions bot commented Mar 9, 2025

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

1 similar comment
Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

Copy link

Change in Ansible shell module found.

Please consider using more suitable Ansible module than shell if possible.

Copy link

codeclimate bot commented Mar 10, 2025

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

Here's the issue category breakdown:

Category Count
Duplication 2

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 62.0% (0.0% change).

View more on Code Climate.

@alanmcanonical
Copy link
Contributor Author

Make all the automatus tests passed except those on Debian 12 and UBI8 which are package installation errors.

Copy link
Contributor

@dodys dodys left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

@dodys dodys merged commit 02f241a into ComplianceAsCode:master Mar 12, 2025
98 of 102 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ok-to-test Used by openshift-ci bot.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants