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

Add whitelist option for yum repository files #487

Merged
merged 1 commit into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions molecule/os_hardening/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
os_security_suid_sgid_whitelist: ['/usr/bin/rlogin']
os_filesystem_whitelist: []
os_ctrlaltdel_disabled: true
os_yum_repo_file_whitelist: ['foo.repo']
sysctl_config:
net.ipv4.ip_forward: 0
net.ipv6.conf.all.forwarding: 0
Expand Down
4 changes: 4 additions & 0 deletions molecule/os_hardening/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@
- name: create recursing symlink to test minimize access
shell: "rm -f /usr/bin/zzz && ln -s /usr/bin /usr/bin/zzz"
changed_when: false

- name: include YUM prepare tasks
include: prepare_yum.yml
when: ansible_facts.os_family == 'RedHat'
16 changes: 16 additions & 0 deletions molecule/os_hardening/prepare_yum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: create 'foo' repository
yum_repository:
name: foo
description: mandatory description
baseurl: file:///mandatory-url
enabled: false
gpgcheck: false

- name: create 'bar' repository
yum_repository:
name: bar
description: mandatory description
baseurl: file:///mandatory-url
enabled: false
gpgcheck: false
4 changes: 4 additions & 0 deletions molecule/os_hardening/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
include: verify_pam.yml
when: ansible_facts.distribution in ['Debian', 'Ubuntu'] or ansible_facts.os_family == 'RedHat'

- name: include YUM tests
include: verify_yum.yml
when: ansible_facts.os_family == 'RedHat'

- name: download cinc-auditor
get_url:
url: https://omnitruck.cinc.sh/install.sh
Expand Down
8 changes: 8 additions & 0 deletions molecule/os_hardening/verify_yum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: verify 'gpgcheck' was not enabled for 'foo' repository (in whitelist)
command: grep -e 'gpgcheck\s*=\s*0' /etc/yum.repos.d/foo.repo
changed_when: false

- name: verify 'gpgcheck' was enabled for 'bar' repository (not in whitelist)
command: grep -e 'gpgcheck\s*=\s*1' /etc/yum.repos.d/bar.repo
changed_when: false
3 changes: 3 additions & 0 deletions roles/os_hardening/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ We know that this is the case on Raspberry Pi.
- `os_yum_enabled`
- Default: `true`
- Description: Set to false to disable installing and configuring yum.
- `os_yum_repo_file_whitelist`
- Default: `[]`
- Description: List of yum repository files under /etc/yum.repos.d/ which should not be altered.
- `os_apt_enabled`
- Default: `true`
- Description: Set to false to disable installing and configuring apt.
Expand Down
3 changes: 3 additions & 0 deletions roles/os_hardening/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ os_rhosts_enabled: true
# Set to false to disable installing and configuring yum.
os_yum_enabled: true

# List of yum repository files under /etc/yum.repos.d/ which should not be altered.
os_yum_repo_file_whitelist: []

# Set to false to disable installing and configuring apt.
os_apt_enabled: true

Expand Down
8 changes: 4 additions & 4 deletions roles/os_hardening/tasks/yum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- 'CentOS-Vault'
when: os_security_packages_clean | bool

- name: Get yum-repository-files
- name: Get yum repository files
find:
paths: '/etc/yum.repos.d'
patterns: '*.repo'
Expand All @@ -18,14 +18,14 @@
# for the 'default([])' see here:
# https://github.com/dev-sec/ansible-os-hardening/issues/99 and
# https://stackoverflow.com/questions/37067827/ansible-deprecation-warning-for-undefined-variable-despite-when-clause
- name: Activate gpg-check for yum-repository-files
- name: Activate gpg-check for yum repository files
replace:
path: '{{ item.path }}'
path: '{{ item }}'
regexp: '^\s*gpgcheck.*'
replace: 'gpgcheck=1'
mode: '0644'
with_items:
- '{{ yum_repos.files | default([]) }}'
- "{{ yum_repos.files | default([]) | map(attribute='path') | difference(os_yum_repo_file_whitelist | map('regex_replace', '^', '/etc/yum.repos.d/') | list) }}"

# failed_when is needed because by default replace module will fail if the file doesn't exists.
# status.rc is only defined if an error accrued and only error code (rc) 257 will be ignored.
Expand Down