2021-05-20 16:52:29 +02:00
|
|
|
---
|
|
|
|
- hosts: all
|
|
|
|
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"
|
|
|
|
include_tasks: tasks/backup.yml
|
|
|
|
|
2021-06-10 21:46:06 +02:00
|
|
|
- name: Add configuration block to default configuration file
|
2021-05-20 16:52:29 +02:00
|
|
|
include_role:
|
|
|
|
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:
|
|
|
|
AcceptEnv: EDITOR
|
|
|
|
PasswordAuthentication: yes
|
|
|
|
Match:
|
|
|
|
Condition: user root
|
|
|
|
AllowAgentForwarding: no
|
|
|
|
|
2021-06-10 21:46:06 +02:00
|
|
|
- name: Add second configuration block to default configuration file
|
2021-05-20 16:52:29 +02:00
|
|
|
include_role:
|
|
|
|
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:
|
|
|
|
AcceptEnv: LS_COLORS
|
|
|
|
PasswordAuthentication: no
|
|
|
|
Match:
|
|
|
|
Condition: Address 127.0.0.1
|
|
|
|
AllowTcpForwarding: no
|
|
|
|
|
|
|
|
- name: Verify the options are correctly set
|
|
|
|
block:
|
|
|
|
- meta: flush_handlers
|
|
|
|
|
|
|
|
- name: Print current configuration file
|
|
|
|
slurp:
|
|
|
|
src: /etc/ssh/sshd_config
|
|
|
|
register: config
|
|
|
|
|
2021-05-21 18:45:14 +02:00
|
|
|
- name: List effective configuration using sshd -T (matching)
|
2021-06-11 11:27:30 +02:00
|
|
|
shell: |
|
|
|
|
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
|
|
|
|
|
2021-05-21 18:45:14 +02:00
|
|
|
- name: List effective configuration using sshd -T (non-matching)
|
|
|
|
command: sshd -T -Cuser=nobody,host=example.com,addr=127.0.0.2
|
|
|
|
register: nonmatching
|
|
|
|
|
2021-05-20 16:52:29 +02:00
|
|
|
- name: Check content of configuration file
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "'AcceptEnv EDITOR' in config.content | b64decode"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "config.content | b64decode | regex_search('Match all\\s*AcceptEnv EDITOR')"
|
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-05-20 16:52:29 +02:00
|
|
|
- "'AcceptEnv LS_COLORS' in config.content | b64decode"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "config.content | b64decode | regex_search('Match all\\s*AcceptEnv LS_COLORS')"
|
2021-05-20 16:52:29 +02:00
|
|
|
- "'PasswordAuthentication no' in config.content | b64decode"
|
|
|
|
- "'Match Address 127.0.0.1' in config.content | b64decode"
|
|
|
|
- "'AllowTcpForwarding no' in config.content | b64decode"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "config.content | b64decode | regex_search('Match Address 127.0.0.1\\s*AllowTcpForwarding no')"
|
2021-05-20 16:52:29 +02:00
|
|
|
|
|
|
|
- name: Check the configuration values are effective
|
|
|
|
# note, the options are in lower-case here
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "'acceptenv EDITOR' in runtime.stdout"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "'allowagentforwarding no' in runtime.stdout"
|
2021-05-20 16:52:29 +02:00
|
|
|
- "'acceptenv LS_COLORS' in runtime.stdout"
|
2021-05-21 18:45:14 +02:00
|
|
|
- "'allowtcpforwarding no' 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
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "'acceptenv EDITOR' in nonmatching.stdout"
|
|
|
|
- "'allowAgentforwarding no' not in nonmatching.stdout"
|
|
|
|
- "'acceptenv LS_COLORS' in nonmatching.stdout"
|
|
|
|
- "'allowtcpforwarding no' not in nonmatching.stdout"
|
|
|
|
- "'passwordauthentication yes' in nonmatching.stdout"
|
2021-05-20 16:52:29 +02:00
|
|
|
tags: tests::verify
|
|
|
|
|
|
|
|
- name: "Restore configuration files"
|
|
|
|
include_tasks: tasks/restore.yml
|