2021-05-20 16:52:29 +02:00
|
|
|
---
|
2023-04-10 22:19:29 +02:00
|
|
|
- name: Test config namespaces
|
|
|
|
hosts: all
|
2021-05-20 16:52:29 +02:00
|
|
|
vars:
|
|
|
|
__sshd_test_backup_files:
|
|
|
|
- /etc/ssh/sshd_config
|
2021-06-11 11:27:30 +02:00
|
|
|
- /etc/ssh/ssh_host_rsa_key
|
|
|
|
- /etc/ssh/ssh_host_rsa_key.pub
|
2021-05-20 16:52:29 +02:00
|
|
|
tasks:
|
|
|
|
- name: "Backup configuration files"
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.include_tasks: tasks/backup.yml
|
2021-05-20 16:52:29 +02:00
|
|
|
|
2021-06-10 21:46:06 +02:00
|
|
|
- name: Add configuration block to default configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.include_role:
|
2021-05-20 16:52:29 +02:00
|
|
|
name: ansible-sshd
|
|
|
|
vars:
|
|
|
|
sshd_config_file: /etc/ssh/sshd_config
|
2021-06-10 21:46:06 +02:00
|
|
|
sshd_config_namespace: nm1
|
2021-05-20 16:52:29 +02:00
|
|
|
sshd:
|
2023-04-10 22:19:29 +02:00
|
|
|
PasswordAuthentication: true
|
|
|
|
PermitRootLogin: true
|
2021-05-20 16:52:29 +02:00
|
|
|
Match:
|
|
|
|
Condition: user root
|
2023-04-10 22:19:29 +02:00
|
|
|
AllowAgentForwarding: false
|
2021-05-20 16:52:29 +02:00
|
|
|
|
2021-06-10 21:46:06 +02:00
|
|
|
- name: Add second configuration block to default configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.include_role:
|
2021-05-20 16:52:29 +02:00
|
|
|
name: ansible-sshd
|
|
|
|
vars:
|
|
|
|
sshd_config_file: /etc/ssh/sshd_config
|
2021-06-10 21:46:06 +02:00
|
|
|
sshd_config_namespace: nm2
|
2021-05-20 16:52:29 +02:00
|
|
|
sshd:
|
2023-04-10 22:19:29 +02:00
|
|
|
PasswordAuthentication: false
|
|
|
|
PermitRootLogin: false
|
2021-05-20 16:52:29 +02:00
|
|
|
Match:
|
|
|
|
Condition: Address 127.0.0.1
|
2023-01-12 21:10:28 +01:00
|
|
|
Banner: /etc/issue
|
2021-05-20 16:52:29 +02:00
|
|
|
|
|
|
|
- name: Verify the options are correctly set
|
2023-04-10 22:19:29 +02:00
|
|
|
tags: tests::verify
|
2021-05-20 16:52:29 +02:00
|
|
|
block:
|
2022-06-03 12:22:17 +02:00
|
|
|
- name: Flush handlers
|
|
|
|
ansible.builtin.meta: flush_handlers
|
2021-05-20 16:52:29 +02:00
|
|
|
|
|
|
|
- name: Print current configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.slurp:
|
2021-05-20 16:52:29 +02:00
|
|
|
src: /etc/ssh/sshd_config
|
|
|
|
register: config
|
|
|
|
|
2022-06-05 09:54:56 +02:00
|
|
|
- name: List effective configuration using sshd -T (matching)
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.shell: |
|
2021-06-11 11:27:30 +02:00
|
|
|
set -eu
|
|
|
|
if set -o | grep pipefail 2>&1 /dev/null ; then
|
|
|
|
set -o pipefail
|
|
|
|
fi
|
|
|
|
if test ! -f /etc/ssh/ssh_host_rsa_key; then
|
|
|
|
ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -C '' -N ''
|
|
|
|
fi
|
|
|
|
sshd -T -Cuser=root,host=localhost,addr=127.0.0.1
|
2021-05-20 16:52:29 +02:00
|
|
|
register: runtime
|
2022-06-05 09:54:56 +02:00
|
|
|
changed_when: false
|
2021-05-20 16:52:29 +02:00
|
|
|
|
2022-06-05 09:54:56 +02:00
|
|
|
- name: List effective configuration using sshd -T (non-matching)
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.command: sshd -T -Cuser=nobody,host=example.com,addr=127.0.0.2
|
2021-05-21 18:45:14 +02:00
|
|
|
register: nonmatching
|
2022-06-05 09:54:56 +02:00
|
|
|
changed_when: false
|
2021-05-21 18:45:14 +02:00
|
|
|
|
2021-08-06 20:52:42 +02:00
|
|
|
- name: Check content of configuration file (blocks)
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-08-06 20:52:42 +02:00
|
|
|
that:
|
2021-08-06 21:10:03 +02:00
|
|
|
- "config.content | b64decode | regex_search('Match all\\s*PasswordAuthentication yes')"
|
|
|
|
- "config.content | b64decode | regex_search('Match all\\s*PasswordAuthentication no')"
|
2021-08-06 20:52:42 +02:00
|
|
|
when:
|
|
|
|
- ansible_facts['os_family'] != 'RedHat' or ansible_facts['distribution_major_version'] != '6'
|
|
|
|
|
|
|
|
- name: Check content of configuration file (blocks for RHEL 6)
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-08-06 20:52:42 +02:00
|
|
|
that:
|
2021-08-06 21:10:03 +02:00
|
|
|
- "config.content | b64decode | regex_search('Match address \\*\\s*PasswordAuthentication yes')"
|
|
|
|
- "config.content | b64decode | regex_search('Match address \\*\\s*PasswordAuthentication no')"
|
2021-08-06 20:52:42 +02:00
|
|
|
when:
|
2022-04-12 11:40:35 +02:00
|
|
|
- ansible_facts['os_family'] == 'RedHat'
|
|
|
|
- ansible_facts['distribution_major_version'] == '6'
|
2021-08-06 20:52:42 +02:00
|
|
|
|
2021-05-20 16:52:29 +02:00
|
|
|
- name: Check content of configuration file
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-05-20 16:52:29 +02:00
|
|
|
that:
|
2021-08-06 21:10:03 +02:00
|
|
|
- "'PermitRootLogin yes' in config.content | b64decode"
|
2021-05-20 16:52:29 +02:00
|
|
|
- "'PasswordAuthentication yes' in config.content | b64decode"
|
|
|
|
- "'Match user root' in config.content | b64decode"
|
|
|
|
- "'AllowAgentForwarding no' in config.content | b64decode"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "config.content | b64decode | regex_search('Match user root\\s*AllowAgentForwarding no')"
|
2021-08-06 21:10:03 +02:00
|
|
|
- "'PermitRootLogin no' in config.content | b64decode"
|
2021-05-20 16:52:29 +02:00
|
|
|
- "'PasswordAuthentication no' in config.content | b64decode"
|
|
|
|
- "'Match Address 127.0.0.1' in config.content | b64decode"
|
2023-01-12 21:10:28 +01:00
|
|
|
- "'Banner /etc/issue' in config.content | b64decode"
|
|
|
|
- "config.content | b64decode | regex_search('Match Address 127.0.0.1\\s*Banner /etc/issue')"
|
2021-05-20 16:52:29 +02:00
|
|
|
|
|
|
|
- name: Check the configuration values are effective
|
|
|
|
# note, the options are in lower-case here
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-05-20 16:52:29 +02:00
|
|
|
that:
|
2021-08-06 21:10:03 +02:00
|
|
|
- "'permitrootlogin yes' in runtime.stdout"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "'allowagentforwarding no' in runtime.stdout"
|
2023-01-12 21:10:28 +01:00
|
|
|
- "'banner /etc/issue' in runtime.stdout"
|
2021-05-20 16:52:29 +02:00
|
|
|
- "'passwordauthentication yes' in runtime.stdout"
|
|
|
|
|
2021-05-21 18:45:14 +02:00
|
|
|
- name: Check the configuration values are not effective for non-matching connection
|
|
|
|
# note, the options are in lower-case here
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.assert:
|
2021-05-21 18:45:14 +02:00
|
|
|
that:
|
2021-08-06 21:10:03 +02:00
|
|
|
- "'permitrootlogin yes' in runtime.stdout"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "'allowAgentforwarding no' not in nonmatching.stdout"
|
2023-01-12 21:10:28 +01:00
|
|
|
- "'banner /etc/issue' not in nonmatching.stdout"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "'passwordauthentication yes' in nonmatching.stdout"
|
2021-05-20 16:52:29 +02:00
|
|
|
|
|
|
|
- name: "Restore configuration files"
|
2022-06-03 12:22:17 +02:00
|
|
|
ansible.builtin.include_tasks: tasks/restore.yml
|