Files
@ 17cf34f73ca6
Branch filter:
Location: majic-ansible-roles/roles/mail_server/tests/test_optional.py
17cf34f73ca6
10.6 KiB
text/x-python
MAR-28: Implemented additional tests for mail_server role:
- Deploy a number of tools on clients in order to test SMTP, IMAP, and Sieve
services.
- Added one more user to LDAP directory for testing group restrictions.
- Deploy CA certificate on all testing machines for TLS validation purposes.
- Use different custom-configured cipher for mail server ciphers.
- Fixed invalid postmaster address for parameters-optional host.
- Deploy configuration files for use with Imap-CLI on client test machines.
- Updated testing of SMTP server to include checks for users that do not belong
to mail group.
- Extended some SMTP-related tests to cover both test servers.
- Some small fixes in SMTP-related tests for expected output from commands.
- Implemented tests covering Dovecot (IMAP + Sieve) functionality.
- Implemented tests for running/enabled services.
- Implemented tests for ClamAV.
- Implemented tests for firewall and connectivity.
- Implemented tests for Postfix TLS configuration.
- TODO: Tests for Sieve TLS configuration have not been written yet due to
limitation of available tools.
- Deploy a number of tools on clients in order to test SMTP, IMAP, and Sieve
services.
- Added one more user to LDAP directory for testing group restrictions.
- Deploy CA certificate on all testing machines for TLS validation purposes.
- Use different custom-configured cipher for mail server ciphers.
- Fixed invalid postmaster address for parameters-optional host.
- Deploy configuration files for use with Imap-CLI on client test machines.
- Updated testing of SMTP server to include checks for users that do not belong
to mail group.
- Extended some SMTP-related tests to cover both test servers.
- Some small fixes in SMTP-related tests for expected output from commands.
- Implemented tests covering Dovecot (IMAP + Sieve) functionality.
- Implemented tests for running/enabled services.
- Implemented tests for ClamAV.
- Implemented tests for firewall and connectivity.
- Implemented tests for Postfix TLS configuration.
- TODO: Tests for Sieve TLS configuration have not been written yet due to
limitation of available tools.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | import re
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
'.molecule/ansible_inventory').get_hosts('parameters-optional')
def test_smtp_tls_files(File, Sudo):
"""
Tests if SMTP TLS private key has been deployed correctly.
"""
with Sudo():
tls_file = File('/etc/ssl/private/parameters-optional_smtp.key')
assert tls_file.is_file
assert tls_file.user == 'root'
assert tls_file.group == 'root'
assert tls_file.mode == 0o640
assert tls_file.content == open("tests/data/x509/parameters-optional_smtp.key.pem", "r").read().rstrip()
tls_file = File('/etc/ssl/certs/parameters-optional_smtp.pem')
assert tls_file.is_file
assert tls_file.user == 'root'
assert tls_file.group == 'root'
assert tls_file.mode == 0o644
assert tls_file.content == open("tests/data/x509/parameters-optional_smtp.cert.pem", "r").read().rstrip()
tls_file = File('/etc/ssl/private/parameters-optional_imap.key')
assert tls_file.is_file
assert tls_file.user == 'root'
assert tls_file.group == 'root'
assert tls_file.mode == 0o640
assert tls_file.content == open("tests/data/x509/parameters-optional_imap.key.pem", "r").read().rstrip()
tls_file = File('/etc/ssl/certs/parameters-optional_imap.pem')
assert tls_file.is_file
assert tls_file.user == 'root'
assert tls_file.group == 'root'
assert tls_file.mode == 0o644
assert tls_file.content == open("tests/data/x509/parameters-optional_imap.cert.pem", "r").read().rstrip()
def test_certificate_validity_check_configuration(File):
"""
Tests if certificate validity check configuration file has been deployed
correctly.
"""
config = File('/etc/check_certificate/parameters-optional_smtp.conf')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
assert config.content == "/etc/ssl/certs/parameters-optional_smtp.pem"
config = File('/etc/check_certificate/parameters-optional_imap.conf')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
assert config.content == "/etc/ssl/certs/parameters-optional_imap.pem"
def test_mailname_file_content(File):
"""
Tests the system mail name file content.
"""
mailname = File('/etc/mailname')
assert mailname.content == "parameters-optional"
def test_postfix_main_cf_file_content(File):
"""
Tests if the Postfix main configuration file content is correct.
"""
config = File('/etc/postfix/main.cf')
config_lines = config.content.split("\n")
assert "myhostname = parameters-optional" in config_lines
assert "mydestination = parameters-optional, parameters-optional, localhost.localdomain, localhost" in config_lines
assert "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 10.31.127.20" in config_lines
assert "smtpd_tls_cert_file = /etc/ssl/certs/parameters-optional_smtp.pem" in config_lines
assert "smtpd_tls_key_file = /etc/ssl/private/parameters-optional_smtp.key" in config_lines
assert " reject_rbl bl.spamcop.net" in config_lines
assert " reject_rbl zen.spamhaus.org" in config_lines
def test_local_aliases(Command, File, Sudo):
"""
Tests if local aliases are configured correctly.
"""
send = Command('swaks --suppress-data --to root@localhost')
assert send.rc == 0
message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
with Sudo():
mail_log = File('/var/log/mail.log')
pattern = "dovecot: lda\(john.doe@domain1\): msgid=<[^.]*.%s@[^>]*>: saved mail to INBOX" % message_id
assert re.search(pattern, mail_log.content) is not None
def test_dovecot_mailbox_directories(File, Sudo):
"""
Tests if mailbox directories are created correctly.
"""
with Sudo():
for directory_path in ["/var/virtmail/domain1",
"/var/virtmail/domain1/john.doe",
"/var/virtmail/domain1/john.doe/Maildir",
"/var/virtmail/domain2",
"/var/virtmail/domain2/jane.doe",
"/var/virtmail/domain1/john.doe/Maildir"]:
directory = File(directory_path)
assert directory.is_directory
assert directory.user == "virtmail"
assert directory.group == "virtmail"
assert directory.mode == 0o700
def test_mail_owner(Group, User):
"""
Tests creation of mail owner group and user.
"""
group = Group("virtmail")
assert group.exists
assert group.gid == 5000
user = User("virtmail")
assert user.exists
assert user.uid == 5000
assert user.home == "/var/virtmail"
assert user.group == "virtmail"
assert user.groups == ["virtmail"]
def test_imap_tls_configuration(Command):
"""
Tests TLS configuration for IMAP in Dovecot.
"""
# Test plain connectivity first.
starttls = Command('echo "a0001 LOGOUT" | openssl s_client -quiet -starttls imap -connect parameters-optional:143')
assert starttls.rc == 0
assert '* BYE Logging out' in starttls.stdout
tls = Command('echo "a0001 LOGOUT" | openssl s_client -quiet -connect parameters-optional:993')
assert tls.rc == 0
assert '* BYE Logging out' in starttls.stdout
# Test TLS protocol versions.
starttls = Command('echo "a0001 LOGOUT" | openssl s_client -quiet -starttls imap -no_tls1_2 -connect parameters-optional:143')
assert starttls.rc == 0
assert '* BYE Logging out' in starttls.stdout
tls = Command('echo "a0001 LOGOUT" | openssl s_client -quiet -no_tls1_2 -connect parameters-optional:993')
assert tls.rc == 0
assert '* BYE Logging out' in starttls.stdout
starttls = Command("echo 'a0001 LOGOUT' | openssl s_client -quiet -starttls imap -no_tls1_1 -no_tls1_2 -connect parameters-optional:143")
assert starttls.rc != 0
assert "write:errno=104" in starttls.stderr
tls = Command("echo 'a0001 LOGOUT' | openssl s_client -quiet -no_tls1_1 -no_tls1_2 -connect parameters-optional:993")
assert tls.rc != 0
assert "write:errno=104" in tls.stderr
# Test at least one strong TLS cipher.
starttls_cipher = Command("echo 'a0001 LOGOUT' | openssl s_client -starttls imap -cipher ECDHE-RSA-AES128-SHA256 -connect parameters-optional:143")
assert starttls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA256" in starttls_cipher.stdout
tls_cipher = Command("echo 'a0001 LOGOUT' | openssl s_client -cipher ECDHE-RSA-AES128-SHA256 -connect parameters-optional:993")
assert tls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA256" in tls_cipher.stdout
# Test weaker TLS cipher that was explicitly configured
starttls_cipher = Command("echo 'a0001 LOGOUT' | openssl s_client -starttls imap -cipher ECDHE-RSA-AES128-SHA -connect parameters-optional:143")
assert starttls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA" in starttls_cipher.stdout
tls_cipher = Command("echo 'a0001 LOGOUT' | openssl s_client -cipher ECDHE-RSA-AES128-SHA -connect parameters-optional:993")
assert tls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA" in tls_cipher.stdout
def test_dovecot_postmaster(Command, Sudo):
"""
Tests if Dovecot postmaster has been correctly configured.
"""
with Sudo():
config = Command("doveadm config")
assert config.rc == 0
assert " postmaster_address = webmaster@parameters-optional" in config.stdout
def test_imap_max_user_connections_per_ip(Command, Sudo):
"""
Tests if Dovecot per-user connection limit has been set-up correctly.
"""
with Sudo():
config = Command("doveadm config")
assert config.rc == 0
assert " mail_max_userip_connections = 2" in config.stdout
def test_postfix_tls_configuration(Command):
"""
Tests TLS configuration for SMTP in Postfix.
"""
# Test TLS protocol versions for default port (all should be enabled).
starttls = Command("echo 'QUIT' | openssl s_client -quiet -starttls smtp -no_tls1 -no_tls1_1 -connect parameters-optional:25")
assert starttls.rc == 0
assert '221 2.0.0 Bye' in starttls.stdout
starttls = Command("echo 'QUIT' | openssl s_client -quiet -starttls smtp -no_tls1_2 -connect parameters-optional:25")
assert starttls.rc == 0
assert '221 2.0.0 Bye' in starttls.stdout
starttls = Command("echo 'QUIT' | openssl s_client -quiet -starttls smtp -no_tls1_2 -no_tls1_1 -connect parameters-optional:25")
assert starttls.rc == 0
assert '221 2.0.0 Bye' in starttls.stdout
# Test TLS protocol versions for submission port (only TLS 1.1 and TLS 1.2 should be enabled).
starttls = Command("echo 'QUIT' | openssl s_client -quiet -starttls smtp -connect parameters-optional:587")
assert starttls.rc == 0
assert '221 2.0.0 Bye' in starttls.stdout
starttls = Command("echo 'QUIT' | openssl s_client -quiet -starttls smtp -no_tls1_2 -connect parameters-optional:587")
assert starttls.rc == 0
assert '221 2.0.0 Bye' in starttls.stdout
starttls = Command("echo 'QUIT' | openssl s_client -quiet -starttls smtp -no_tls1_1 -no_tls1_2 -connect parameters-optional:587")
assert starttls.rc != 0
assert 'write:errno=104' in starttls.stderr
# Test ciphers for default port (less restrictive).
starttls_cipher = Command("echo 'QUIT' | openssl s_client -starttls smtp -cipher ECDHE-RSA-AES128-SHA256 -connect parameters-optional:25")
assert starttls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA256" in starttls_cipher.stdout
starttls_cipher = Command("echo 'QUIT' | openssl s_client -starttls smtp -cipher ECDHE-RSA-AES128-SHA -connect parameters-optional:25")
assert starttls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA" in starttls_cipher.stdout
# Test ciphers for submission port (at least one weak cipher was configured).
starttls_cipher = Command("echo 'QUIT' | openssl s_client -starttls smtp -cipher ECDHE-RSA-AES128-SHA256 -connect parameters-optional:587")
assert starttls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA256" in starttls_cipher.stdout
starttls_cipher = Command("echo 'QUIT' | openssl s_client -starttls smtp -cipher ECDHE-RSA-AES128-SHA -connect parameters-optional:587")
assert starttls_cipher.rc == 0
assert "ECDHE-RSA-AES128-SHA" in starttls_cipher.stdout
def test_sieve_tls_configuration(Command):
"""
Tests TLS configuration for SIEVE in Dovecot
"""
# @TODO: Currently not possible to test since openssl s_client does not
# support STARTTLS for Sieve.
pass
|