2021-06-03 16:56:28 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2021-06-01 14:52:03 +00:00
|
|
|
if node.has_bundle('pacman'):
|
|
|
|
package = 'pkg_pacman:nginx'
|
|
|
|
username = 'http'
|
|
|
|
else:
|
|
|
|
package = 'pkg_apt:nginx'
|
|
|
|
username = 'www-data'
|
|
|
|
|
2020-08-30 09:36:48 +00:00
|
|
|
directories = {
|
|
|
|
'/etc/nginx/sites': {
|
|
|
|
'purge': True,
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
},
|
2021-06-03 16:56:28 +00:00
|
|
|
'/etc/nginx/security.txt.d': {
|
|
|
|
'purge': True,
|
|
|
|
},
|
2021-04-25 07:09:23 +00:00
|
|
|
'/etc/nginx/ssl': {
|
|
|
|
'purge': True,
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
},
|
2021-06-05 13:53:02 +00:00
|
|
|
'/var/log/nginx-timing': {
|
|
|
|
'owner': username,
|
|
|
|
'needs': {
|
|
|
|
package,
|
|
|
|
},
|
|
|
|
},
|
2020-11-13 12:35:02 +00:00
|
|
|
'/var/www': {},
|
2020-08-30 09:36:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 07:52:26 +00:00
|
|
|
files = {
|
2021-06-05 13:53:02 +00:00
|
|
|
'/etc/logrotate.d/nginx': {
|
|
|
|
'source': 'logrotate.conf',
|
|
|
|
},
|
2020-04-13 07:52:26 +00:00
|
|
|
'/etc/nginx/nginx.conf': {
|
|
|
|
'content_type': 'mako',
|
2021-06-01 14:52:03 +00:00
|
|
|
'context': {
|
|
|
|
'username': username,
|
|
|
|
**node.metadata['nginx'],
|
|
|
|
},
|
2020-04-13 07:52:26 +00:00
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
},
|
2020-10-31 12:00:38 +00:00
|
|
|
'/etc/nginx/fastcgi.conf': {
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
},
|
2020-06-01 11:35:54 +00:00
|
|
|
'/etc/nginx/sites/stub_status': {
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
},
|
2021-04-25 07:20:16 +00:00
|
|
|
'/etc/nginx/sites/000-port80.conf': {
|
|
|
|
'source': 'port80.conf',
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
},
|
2020-11-10 09:57:04 +00:00
|
|
|
'/usr/local/share/icinga/plugins/check_nginx_status': {
|
|
|
|
'mode': '0755',
|
|
|
|
},
|
2020-04-13 07:52:26 +00:00
|
|
|
}
|
2021-06-01 14:52:03 +00:00
|
|
|
if node.has_bundle('pacman'):
|
|
|
|
files['/etc/systemd/system/nginx.service.d/bundlewrap.conf'] = {
|
|
|
|
'source': 'arch-override.conf',
|
|
|
|
'triggers': {
|
|
|
|
'action:systemd-reload',
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
}
|
2020-04-13 07:52:26 +00:00
|
|
|
|
2020-10-31 09:18:40 +00:00
|
|
|
actions = {
|
|
|
|
'nginx-generate-dhparam': {
|
|
|
|
'command': 'openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048',
|
|
|
|
'unless': 'test -f /etc/ssl/certs/dhparam.pem',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-04-13 07:52:26 +00:00
|
|
|
svc_systemd = {
|
|
|
|
'nginx': {
|
|
|
|
'needs': {
|
2020-10-31 09:18:40 +00:00
|
|
|
'action:nginx-generate-dhparam',
|
2021-06-05 13:53:02 +00:00
|
|
|
'directory:/var/log/nginx-timing',
|
2021-06-01 14:52:03 +00:00
|
|
|
package,
|
2020-04-13 07:52:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-06-01 08:52:52 +00:00
|
|
|
|
2021-06-03 16:56:28 +00:00
|
|
|
now = datetime.now()
|
|
|
|
in_three_months = now + timedelta(days=90)
|
2021-06-19 06:49:10 +00:00
|
|
|
default_security_expiry = in_three_months.strftime('%Y-%m') + '-01T00:00:00.000Z'
|
2021-06-03 16:56:28 +00:00
|
|
|
|
2021-02-18 17:12:25 +00:00
|
|
|
for vhost, config in node.metadata.get('nginx/vhosts', {}).items():
|
2020-10-31 09:30:07 +00:00
|
|
|
if not 'domain' in config:
|
|
|
|
config['domain'] = vhost
|
|
|
|
|
2021-06-03 16:56:28 +00:00
|
|
|
security_txt_enabled = False
|
|
|
|
if (
|
|
|
|
node.metadata.get('nginx/security.txt/enabled', True) and
|
|
|
|
config.get('security.txt', {}).get('enabled', True)
|
|
|
|
):
|
|
|
|
security_txt_enabled = True
|
|
|
|
|
|
|
|
files[f'/etc/nginx/security.txt.d/{vhost}'] = {
|
|
|
|
'source': 'security.txt',
|
|
|
|
'content_type': 'mako',
|
|
|
|
'context': {
|
|
|
|
'domain': config['domain'],
|
|
|
|
'expiry': default_security_expiry,
|
|
|
|
'proto': 'https' if config.get('ssl', 'letsencrypt') else 'http',
|
|
|
|
'vhost': config.get('security.txt', node.metadata.get('nginx/security.txt', {})),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-25 07:09:23 +00:00
|
|
|
files[f'/etc/nginx/sites/{vhost}'] = {
|
2020-06-01 08:52:52 +00:00
|
|
|
'source': 'site_template',
|
|
|
|
'content_type': 'mako',
|
2020-10-31 09:41:33 +00:00
|
|
|
'context': {
|
2021-06-05 13:53:02 +00:00
|
|
|
'create_access_log': config.get('access_log', node.metadata.get('nginx/access_log', False)),
|
2021-02-18 17:12:25 +00:00
|
|
|
'php_version': node.metadata.get('php/version', ''),
|
2021-06-03 16:56:28 +00:00
|
|
|
'security_txt': security_txt_enabled,
|
|
|
|
'vhost': vhost,
|
2020-10-31 09:41:33 +00:00
|
|
|
**config,
|
|
|
|
},
|
2020-09-20 12:36:43 +00:00
|
|
|
'needs': set(),
|
2021-04-25 07:09:23 +00:00
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:nginx',
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
2020-06-01 08:52:52 +00:00
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
}
|
2020-09-20 12:36:43 +00:00
|
|
|
|
2020-10-18 16:55:00 +00:00
|
|
|
if not 'webroot' in config:
|
2021-04-25 07:09:23 +00:00
|
|
|
directories[f'/var/www/{vhost}'] = {}
|
2020-11-12 21:59:46 +00:00
|
|
|
|
|
|
|
if node.has_bundle('zfs'):
|
2021-04-25 07:09:23 +00:00
|
|
|
directories[f'/var/www/{vhost}']['needs'] = {
|
2020-11-12 21:59:46 +00:00
|
|
|
'bundle:zfs',
|
|
|
|
}
|
|
|
|
|
2021-04-25 07:09:23 +00:00
|
|
|
directories[f'/var/www/{vhost}'].update(config.get('webroot_config', {}))
|
2020-10-18 16:55:00 +00:00
|
|
|
|
2021-02-20 13:25:27 +00:00
|
|
|
if config.get('ssl', 'letsencrypt') == 'letsencrypt':
|
2021-04-25 07:09:23 +00:00
|
|
|
files[f'/etc/nginx/sites/{vhost}']['needs'].add('action:letsencrypt_ensure-some-certificate_{}'.format(config['domain']))
|
|
|
|
files[f'/etc/nginx/sites/{vhost}']['needed_by'].add('action:letsencrypt_update_certificates')
|
|
|
|
|
|
|
|
elif config.get('ssl', 'letsencrypt'):
|
|
|
|
files[f'/etc/nginx/ssl/{vhost}.crt'] = {
|
|
|
|
'content_type': 'mako',
|
|
|
|
'source': 'ssl_template',
|
|
|
|
'context': {
|
|
|
|
'domain': config['ssl'],
|
|
|
|
},
|
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:nginx',
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:reload',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
files[f'/etc/nginx/ssl/{vhost}.key'] = {
|
|
|
|
'content': repo.vault.decrypt_file('ssl/{}.key.pem.vault'.format(config['ssl'])),
|
|
|
|
'mode': '0600',
|
|
|
|
'needed_by': {
|
|
|
|
'svc_systemd:nginx',
|
|
|
|
'svc_systemd:nginx:restart',
|
|
|
|
},
|
|
|
|
'triggers': {
|
|
|
|
'svc_systemd:nginx:reload',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
files[f'/etc/nginx/sites/{vhost}']['needs'].add(f'file:/etc/nginx/ssl/{vhost}.crt')
|
|
|
|
files[f'/etc/nginx/sites/{vhost}']['needs'].add(f'file:/etc/nginx/ssl/{vhost}.key')
|