Files
@ b21d839465c4
Branch filter:
Location: majic-ansible-roles/roles/php_website/molecule/default/tests/test_parameters_mandatory.py - annotation
b21d839465c4
6.8 KiB
text/x-python
MAR-170: Fixed tests for HTTPS enforcment in web_server role:
- Make sure to use local machine's hostname when performing the test.
- Make sure to use local machine's hostname when performing the test.
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 | 14eb78a4f466 3dd7f39302f8 37cc1e381720 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 d62b3adec462 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 92011aae7258 57e103ba33ee 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 d752715bb533 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 57e103ba33ee 92011aae7258 92011aae7258 3dd7f39302f8 ff510f233909 3dd7f39302f8 37cc1e381720 37cc1e381720 37cc1e381720 92011aae7258 92011aae7258 3dd7f39302f8 3dd7f39302f8 6c1d08d39449 d752715bb533 3dd7f39302f8 3dd7f39302f8 ff510f233909 d752715bb533 3dd7f39302f8 3dd7f39302f8 ff510f233909 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 398dcbce5d84 ff510f233909 92011aae7258 3dd7f39302f8 ff510f233909 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 7dd7757be724 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 7dd7757be724 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 d752715bb533 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 92011aae7258 3dd7f39302f8 3dd7f39302f8 3dd7f39302f8 | import os
import re
import time
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-mandatory')
def test_website_group(host):
"""
Tests if website group has been created correctly.
"""
group = host.group('web-parameters-mandatory')
assert group.exists
assert group.gid == 1003
def test_website_admin_user(host):
"""
Tests if website administrator user has been created correctly.
"""
user = host.user('admin-parameters-mandatory')
assert user.exists
assert user.uid == 1003
assert user.group == 'web-parameters-mandatory'
assert user.groups == ['web-parameters-mandatory']
assert user.shell == '/bin/bash'
assert user.home == '/var/www/parameters-mandatory'
def test_website_admin_home(host):
"""
Tests if permissions on website admin home directory are correct.
"""
home = host.file('/var/www/parameters-mandatory')
assert home.is_directory
assert home.user == 'admin-parameters-mandatory'
assert home.group == 'web-parameters-mandatory'
assert home.mode == 0o750
def test_home_profile_directory(host):
"""
Tests if profile directory has been set-up correctly for the website
administrator/application user.
"""
with host.sudo():
directory = host.file('/var/www/parameters-mandatory/.profile.d')
assert directory.is_directory
assert directory.user == 'admin-parameters-mandatory'
assert directory.group == 'web-parameters-mandatory'
assert directory.mode == 0o750
def test_website_application_user(host):
"""
Tests if website application user has been created correctly.
"""
user = host.user('web-parameters-mandatory')
assert user.exists
assert user.uid == 999
assert user.group == 'web-parameters-mandatory'
assert user.groups == ['web-parameters-mandatory']
assert user.shell == '/bin/sh'
assert user.home == '/var/www/parameters-mandatory'
with host.sudo():
umask = host.run("su -l web-parameters-mandatory -c 'bash -c umask'")
assert umask.stdout == '0007\n'
def test_nginx_user(host):
"""
Tests if web server user has been added to website group.
"""
user = host.user('www-data')
assert 'web-parameters-mandatory' in user.groups
def test_forward_file(host):
"""
Tests if the forward file has correct permissions and content.
"""
with host.sudo():
config = host.file('/var/www/parameters-mandatory/.forward')
assert config.is_file
assert config.user == 'root'
assert config.group == 'web-parameters-mandatory'
assert config.mode == 0o640
assert config.content_string == "root\n"
def test_mail_forwarding(host):
"""
Tests if mail forwarding works as expected.
"""
hostname = host.run('hostname').stdout.strip()
send = host.run('swaks --suppress-data --to web-parameters-mandatory@localhost')
assert send.rc == 0
original_queue_id = re.search('Ok: queued as (.*)', send.stdout).group(1)
# Sleep for a couple of seconds so the mail can get delivered.
time.sleep(5)
with host.sudo():
mail_log = host.file('/var/log/mail.log')
# First extract message ID of forwarded mail.
pattern = r"%s: to=<web-parameters-mandatory@localhost>.*status=sent \(forwarded as ([^)]*)\)" % original_queue_id
forward_queue_id = re.search(pattern, mail_log.content_string).group(1)
# Now try to determine where the forward ended-up at.
pattern = "%s: to=<vagrant@%s>, orig_to=<web-parameters-mandatory@localhost>.*status=sent" % (forward_queue_id, hostname)
assert re.search(pattern, mail_log.content_string) is not None
def test_php_fpm_configuration_file(host):
"""
Tests if PHP FPM configuration file has been correctly deployed.
"""
config_file_path = '/etc/php/7.0/fpm/pool.d/parameters-mandatory.conf'
with host.sudo():
config = host.file(config_file_path)
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o640
def test_nginx_tls_files(host):
"""
Tests if TLS private key and certificate have been deployed correctly.
"""
with host.sudo():
tls_file = host.file('/etc/ssl/private/parameters-mandatory_https.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_string == open("tests/data/x509/server/parameters-mandatory_https.key.pem", "r").read().rstrip()
tls_file = host.file('/etc/ssl/certs/parameters-mandatory_https.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_string == open("tests/data/x509/server/parameters-mandatory_https.cert.pem", "r").read().rstrip()
def test_certificate_validity_check_configuration(host):
"""
Tests if certificate validity check configuration file has been deployed
correctly.
"""
config = host.file('/etc/check_certificate/parameters-mandatory_https.conf')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o644
assert config.content_string == "/etc/ssl/certs/parameters-mandatory_https.pem"
def test_vhost_file(host):
"""
Tests permissions of vhost configuration file.
"""
config = host.file('/etc/nginx/sites-available/parameters-mandatory')
assert config.is_file
assert config.user == 'root'
assert config.group == 'root'
assert config.mode == 0o640
def test_website_enabled(host):
"""
Tests if website has been enabled.
"""
config = host.file('/etc/nginx/sites-enabled/parameters-mandatory')
assert config.is_symlink
assert config.linked_to == '/etc/nginx/sites-available/parameters-mandatory'
def test_https_enforcement(host):
"""
Tests if HTTPS is being enforced.
"""
https_enforcement = host.run('curl -I http://parameters-mandatory/')
assert https_enforcement.rc == 0
assert 'HTTP/1.1 301 Moved Permanently' in https_enforcement.stdout
assert 'Location: https://parameters-mandatory/' in https_enforcement.stdout
https_enforcement = host.run('curl -I https://parameters-mandatory/')
assert https_enforcement.rc == 0
assert 'Strict-Transport-Security: max-age=31536000; includeSubDomains' in https_enforcement.stdout
def test_index_page(host):
"""
Tests if index page is served correctly.
"""
page = host.run('curl https://parameters-mandatory/')
assert page.rc == 0
assert page.stdout == "This is the index page for parameters-mandatory."
|