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

Achieve consistent file and directory permissions for systemd journals #11974

Merged
merged 1 commit into from
May 10, 2024

Conversation

alanmcanonical
Copy link
Contributor

Description:

  • Set the appropriate permissions to the files and directories.

Rationale:

  • Configuring tmpfiles.d to manage permissions ensures that log files are maintained correctly after reboots.

…l and /run/log/journal

Set the appropriate permissions to the files and directories
@alanmcanonical alanmcanonical requested a review from a team as a code owner May 8, 2024 13:45
Copy link

openshift-ci bot commented May 8, 2024

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/test-infra repository.

@openshift-ci openshift-ci bot added the needs-ok-to-test Used by openshift-ci bot. label May 8, 2024
@alanmcanonical alanmcanonical changed the title Achieve consistent file and directory permissions for journal Achieve consistent file and directory permissions for systemd journals May 8, 2024
Copy link

github-actions bot commented May 8, 2024

Start a new ephemeral environment with changes proposed in this pull request:

ubuntu2204 (from CTF) Environment (using Fedora as testing environment)
Open in Gitpod

Fedora Testing Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

@dodys dodys self-assigned this May 8, 2024
@dodys dodys added the Ubuntu Ubuntu product related. label May 8, 2024
Copy link

github-actions bot commented May 8, 2024

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_dir_groupowner_system_journal' differs.
--- xccdf_org.ssgproject.content_rule_dir_groupowner_system_journal
+++ xccdf_org.ssgproject.content_rule_dir_groupowner_system_journal
@@ -1,8 +1,21 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-find -H /run/log/journal/  -type d -exec chgrp systemd-journal {} \;
-find -H /var/log/journal/  -type d -exec chgrp systemd-journal {} \;
+TMPFILES_CONF="/etc/tmpfiles.d/systemd.conf"
+
+if grep -qP "^.[+]*\s+\/var\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/var\/log\/journal\s\+[^ $]\+\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2systemd-journal/" "$TMPFILES_CONF"
+else
+    echo "Z /var/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+if grep -qP "^.[+]*\s+\/run\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/run\/log\/journal\s\+[^ $]\+\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2systemd-journal/" "$TMPFILES_CONF"
+else
+    echo "Z /run/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+systemd-tmpfiles --create
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

bash remediation for rule 'xccdf_org.ssgproject.content_rule_dir_owner_system_journal' differs.
--- xccdf_org.ssgproject.content_rule_dir_owner_system_journal
+++ xccdf_org.ssgproject.content_rule_dir_owner_system_journal
@@ -1,8 +1,21 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-find -H /run/log/journal/  -type d -exec chown 0 {} \;
-find -H /var/log/journal/  -type d -exec chown 0 {} \;
+TMPFILES_CONF="/etc/tmpfiles.d/systemd.conf"
+
+if grep -qP "^.[+]*\s+\/var\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/var\/log\/journal\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2root/" "$TMPFILES_CONF"
+else
+    echo "Z /var/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+if grep -qP "^.[+]*\s+\/run\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/run\/log\/journal\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2root/" "$TMPFILES_CONF"
+else
+    echo "Z /run/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+systemd-tmpfiles --create
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

bash remediation for rule 'xccdf_org.ssgproject.content_rule_dir_permissions_system_journal' differs.
--- xccdf_org.ssgproject.content_rule_dir_permissions_system_journal
+++ xccdf_org.ssgproject.content_rule_dir_permissions_system_journal
@@ -1,9 +1,21 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-find -H /run/log/journal/  -perm /u+s,g+w,o+xwrt -type d -exec chmod u-s,g-w,o-xwrt {} \;
+TMPFILES_CONF="/etc/tmpfiles.d/systemd.conf"
 
-find -H /var/log/journal/  -perm /u+s,g+w,o+xwrt -type d -exec chmod u-s,g-w,o-xwrt {} \;
+if grep -qP "^.[+]*\s+\/var\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/var\/log\/journal\s\+\)\([^ $]*\)/Z\2~2750/" "$TMPFILES_CONF"
+else
+    echo "Z /var/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+if grep -qP "^.[+]*\s+\/run\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/run\/log\/journal\s\+\)\([^ $]*\)/Z\2~2750/" "$TMPFILES_CONF"
+else
+    echo "Z /run/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+systemd-tmpfiles --create
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_groupowner_system_journal' differs.
--- xccdf_org.ssgproject.content_rule_file_groupowner_system_journal
+++ xccdf_org.ssgproject.content_rule_file_groupowner_system_journal
@@ -1,9 +1,21 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-find /run/log/journal/  -type f ! -group systemd-journal -regex '^.*$' -exec chgrp systemd-journal {} \;
+TMPFILES_CONF="/etc/tmpfiles.d/systemd.conf"
 
-find /var/log/journal/  -type f ! -group systemd-journal -regex '^.*$' -exec chgrp systemd-journal {} \;
+if grep -qP "^.[+]*\s+\/var\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/var\/log\/journal\s\+[^ $]\+\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2systemd-journal/" "$TMPFILES_CONF"
+else
+    echo "Z /var/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+if grep -qP "^.[+]*\s+\/run\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/run\/log\/journal\s\+[^ $]\+\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2systemd-journal/" "$TMPFILES_CONF"
+else
+    echo "Z /run/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+systemd-tmpfiles --create
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_owner_system_journal' differs.
--- xccdf_org.ssgproject.content_rule_file_owner_system_journal
+++ xccdf_org.ssgproject.content_rule_file_owner_system_journal
@@ -1,9 +1,21 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-find /run/log/journal/  -type f ! -uid 0 -regex '^.*$' -exec chown 0 {} \;
+TMPFILES_CONF="/etc/tmpfiles.d/systemd.conf"
 
-find /var/log/journal/  -type f ! -uid 0 -regex '^.*$' -exec chown 0 {} \;
+if grep -qP "^.[+]*\s+\/var\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/var\/log\/journal\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2root/" "$TMPFILES_CONF"
+else
+    echo "Z /var/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+if grep -qP "^.[+]*\s+\/run\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/run\/log\/journal\s\+[^ $]\+\s\+\)\([^ $]\+\)/Z\2root/" "$TMPFILES_CONF"
+else
+    echo "Z /run/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+systemd-tmpfiles --create
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_system_journal' differs.
--- xccdf_org.ssgproject.content_rule_file_permissions_system_journal
+++ xccdf_org.ssgproject.content_rule_file_permissions_system_journal
@@ -1,9 +1,21 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-find -H /run/log/journal/  -perm /u+xs,g+xws,o+xwrt  -type f -regex '^.*$' -exec chmod u-xs,g-xws,o-xwrt {} \;
+TMPFILES_CONF="/etc/tmpfiles.d/systemd.conf"
 
-find -H /var/log/journal/  -perm /u+xs,g+xws,o+xwrt  -type f -regex '^.*$' -exec chmod u-xs,g-xws,o-xwrt {} \;
+if grep -qP "^.[+]*\s+\/var\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/var\/log\/journal\s\+\)\([^ $]*\)/Z\2~2750/" "$TMPFILES_CONF"
+else
+    echo "Z /var/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+if grep -qP "^.[+]*\s+\/run\/log\/journal\s+" "$TMPFILES_CONF"; then
+    sed -i --follow-symlinks "s/\(^.[+]*\)\(\s\+\/run\/log\/journal\s\+\)\([^ $]*\)/Z\2~2750/" "$TMPFILES_CONF"
+else
+    echo "Z /run/log/journal ~2750 root systemd-journal - -" >> "$TMPFILES_CONF"
+fi
+
+systemd-tmpfiles --create
 
 else
     >&2 echo 'Remediation is not applicable, nothing was done'

Copy link

github-actions bot commented May 8, 2024

🤖 A k8s content image for this PR is available at:
ghcr.io/complianceascode/k8scontent:11974
This image was built from commit: 0b205de

Click here to see how to deploy it

If you alread have Compliance Operator deployed:
utils/build_ds_container.py -i ghcr.io/complianceascode/k8scontent:11974

Otherwise deploy the content and operator together by checking out ComplianceAsCode/compliance-operator and:
CONTENT_IMAGE=ghcr.io/complianceascode/k8scontent:11974 make deploy-local

Copy link

codeclimate bot commented May 8, 2024

Code Climate has analyzed commit 0b205de 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 59.4% (0.0% change).

View more on Code Climate.

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 6a88b94 into ComplianceAsCode:master May 10, 2024
105 of 108 checks passed
@Mab879 Mab879 added this to the 0.1.74 milestone May 10, 2024
@Mab879 Mab879 added the Bash Bash remediation update. label May 10, 2024
@alanmcanonical alanmcanonical deleted the fix_perm_reset branch May 30, 2024 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bash Bash remediation update. needs-ok-to-test Used by openshift-ci bot. Ubuntu Ubuntu product related.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants