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

Fix Ansible in rule ensure_redhat_gpgkey_installed #11413

Merged
merged 1 commit into from
Jan 5, 2024

Conversation

jan-cerny
Copy link
Collaborator

@jan-cerny jan-cerny commented Jan 3, 2024

We have discovered that in some Ansible Playbooks that we generate, for example in rhel8-playbook-anssi_bp28_high.yml, the remediation for rule ensure_redhat_gpgkey_installed doesn't ensure that Red Hat GPG key is installed.

Specifically, the Ansible Task Import RedHat GPG key is skipped during the Playbook execution because the condition (gpg_installed_fingerprints | difference(gpg_valid_fingerprints)) | length == 0 that is part of the when statement in that task is evaluated as false. The root cause is that the gpg_installed_fingerprints fact is a list but the gpg_valid_fingerprints is a tuple. Starting from Ansible 2.16, the difference filter changed behavior when its operands are each of a different type. Therefore a list of different items of a non-zero length is produced. An easy fix to this is to define both aforementioned facts as same data types, eg. lists.

Fixes: #11399, #11409

Review Hints:

  1. Get a machine running RHEL 8 (for example RHEL-8.10.0-20240101.1) that contains ansible-core-2.16.2-1.el8.x86_64
  2. Install the scap-security-guide built by Packit job rpm-build:centos-stream-8-x86_64 in this PR to that machine
  3. Run the contest test /hardening/ansible/anssi_bp28_high on that machine
  4. check that the Ansible Playbook that was run during the test finished completely and haven't terminated soon
  5. check that the rule ensure_redhat_gpgkey_installed is PASS in the after scan

We have discovered that in some Ansible Playbooks that we generate, for example
in `rhel8-playbook-anssi_bp28_high.yml`, the remediation for rule
`ensure_redhat_gpgkey_installed` doesn't ensure that Red Hat GPG key is
installed.

Specifically, the Ansible Task `Import RedHat GPG key`
is skipped during the Playbook execution because the condition
`(gpg_installed_fingerprints | difference(gpg_valid_fingerprints)) | length == 0`
that is part of the `when` statement in that task is evaluated as `false`.
The root cause is that the `gpg_installed_fingerprints` fact is a list
but the `gpg_valid_fingerprints` is a tuple. Starting from Ansible 2.16,
the `difference` filter changed behavior when its operands are each
of a different type. Therefore a list of different items of a non-zero
length is produced. An easy fix to this is to define both aforementioned facts
as same data types, eg. lists.

Fixes: ComplianceAsCode#11399, ComplianceAsCode#11409
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 3, 2024
Copy link

openshift-ci bot commented Jan 3, 2024

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 added the productization-issue Issue found in upstream stabilization process. label Jan 3, 2024
@jan-cerny jan-cerny added this to the 0.1.72 milestone Jan 3, 2024
Copy link

github-actions bot commented Jan 3, 2024

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

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

Fedora Testing Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

Copy link

github-actions bot commented Jan 3, 2024

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

Click here to see the full diff
ansible remediation for rule 'xccdf_org.ssgproject.content_rule_ensure_redhat_gpgkey_installed' differs.
--- xccdf_org.ssgproject.content_rule_ensure_redhat_gpgkey_installed
+++ xccdf_org.ssgproject.content_rule_ensure_redhat_gpgkey_installed
@@ -72,7 +72,9 @@
 
 - name: Set Fact - Valid fingerprints
   set_fact:
-    gpg_valid_fingerprints: ("567E347AD0044ADE55BA8A5F199E2F91FD431D51" "6A6AA7C97C8890AEC6AEBFE2F76F66C3D4082792")
+    gpg_valid_fingerprints:
+    - 567E347AD0044ADE55BA8A5F199E2F91FD431D51
+    - 6A6AA7C97C8890AEC6AEBFE2F76F66C3D4082792
   tags:
   - CCE-80795-8
   - CJIS-5.10.4.1

Copy link

codeclimate bot commented Jan 3, 2024

Code Climate has analyzed commit f0e38a3 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 58.5% (0.0% change).

View more on Code Climate.

@jan-cerny
Copy link
Collaborator Author

/packit retest-failed

1 similar comment
@jan-cerny
Copy link
Collaborator Author

/packit retest-failed

@jan-cerny
Copy link
Collaborator Author

Automatus job fails because we run the test scenarios against a non-RHEL back end which is usually a CentOS container and there there are no Red Hat GPG keys, these keys are specific to RHEL. If you execute the Automatus tests against a RHEL 8 virtual machine back end the tests pass.

jcerny@dhcp129-133:~/work/git/scap-security-guide (issue11399)$ python3 tests/automatus.py rule --libvirt qemu:///system ssgts_rhel8 --remediate-using ansible ensure_redhat_gpgkey_installed
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-2024-01-03-1646/test_suite.log
WARNING - Script fedora_key.fail.sh is not applicable on given platform
INFO - xccdf_org.ssgproject.content_rule_ensure_redhat_gpgkey_installed
INFO - Script key_installed.pass.sh using profile (all) OK
INFO - Script missing_key.fail.sh using profile (all) OK

@jan-cerny jan-cerny marked this pull request as ready for review January 3, 2024 15:51
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 3, 2024
@jan-cerny jan-cerny added the Ansible Ansible remediation update. label Jan 3, 2024
@vojtapolasek vojtapolasek self-assigned this Jan 5, 2024
Copy link
Collaborator

@vojtapolasek vojtapolasek left a comment

Choose a reason for hiding this comment

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

Thank you @jan-cerny for discovering this problem. I tested it and it works as expected.

@vojtapolasek vojtapolasek merged commit 9826daa into ComplianceAsCode:master Jan 5, 2024
33 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ansible Ansible remediation update. productization-issue Issue found in upstream stabilization process.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ansible Playbooks fail to install a package
2 participants