2019-11-25 17:30:24 -05:00
|
|
|
"""Test for certbot_nginx._internal.configurator."""
|
2015-03-23 13:53:44 -04:00
|
|
|
import unittest
|
|
|
|
|
|
2020-04-15 14:39:44 -04:00
|
|
|
try:
|
|
|
|
|
import mock
|
|
|
|
|
except ImportError: # pragma: no cover
|
|
|
|
|
from unittest import mock # type: ignore
|
2019-12-09 15:50:20 -05:00
|
|
|
import OpenSSL
|
|
|
|
|
|
2015-05-10 07:26:21 -04:00
|
|
|
from acme import challenges
|
2015-06-11 11:00:18 -04:00
|
|
|
from acme import messages
|
2016-04-13 19:45:54 -04:00
|
|
|
from certbot import achallenges
|
2017-05-23 16:18:50 -04:00
|
|
|
from certbot import crypto_util
|
2016-04-13 19:45:54 -04:00
|
|
|
from certbot import errors
|
2019-04-12 16:32:52 -04:00
|
|
|
from certbot.compat import os
|
2017-05-01 17:49:12 -04:00
|
|
|
from certbot.tests import util as certbot_test_util
|
2019-11-25 17:30:24 -05:00
|
|
|
from certbot_nginx._internal import obj
|
|
|
|
|
from certbot_nginx._internal import parser
|
|
|
|
|
from certbot_nginx._internal.configurator import _redirect_block_for_domain
|
|
|
|
|
from certbot_nginx._internal.nginxparser import UnspacedList
|
2019-11-26 20:45:18 -05:00
|
|
|
import test_util as util
|
2015-03-23 13:53:44 -04:00
|
|
|
|
|
|
|
|
|
2015-04-14 19:43:40 -04:00
|
|
|
class NginxConfiguratorTest(util.NginxTest):
|
|
|
|
|
"""Test a semi complex vhost configuration."""
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2016-12-06 23:39:16 -05:00
|
|
|
|
2015-03-23 13:53:44 -04:00
|
|
|
def setUp(self):
|
2015-04-14 19:43:40 -04:00
|
|
|
super(NginxConfiguratorTest, self).setUp()
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2019-11-13 13:19:27 -05:00
|
|
|
self.config = self.get_nginx_configurator(
|
2017-03-17 16:10:02 -04:00
|
|
|
self.config_path, self.config_dir, self.work_dir, self.logs_dir)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.util.exe_exists")
|
2015-11-10 19:25:41 -05:00
|
|
|
def test_prepare_no_install(self, mock_exe_exists):
|
|
|
|
|
mock_exe_exists.return_value = False
|
|
|
|
|
self.assertRaises(
|
|
|
|
|
errors.NoInstallationError, self.config.prepare)
|
|
|
|
|
|
2015-04-14 19:43:40 -04:00
|
|
|
def test_prepare(self):
|
2016-07-19 13:12:47 -04:00
|
|
|
self.assertEqual((1, 6, 2), self.config.version)
|
2021-02-26 16:43:22 -05:00
|
|
|
self.assertEqual(13, len(self.config.parser.parsed))
|
2015-04-14 19:43:40 -04:00
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.util.exe_exists")
|
|
|
|
|
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
|
2016-01-05 15:15:29 -05:00
|
|
|
def test_prepare_initializes_version(self, mock_popen, mock_exe_exists):
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", "\n".join(["nginx version: nginx/1.6.2",
|
|
|
|
|
"built by clang 6.0 (clang-600.0.56)"
|
|
|
|
|
" (based on LLVM 3.5svn)",
|
|
|
|
|
"TLS SNI support enabled",
|
|
|
|
|
"configure arguments: --prefix=/usr/local/Cellar/"
|
|
|
|
|
"nginx/1.6.2 --with-http_ssl_module"]))
|
|
|
|
|
|
|
|
|
|
mock_exe_exists.return_value = True
|
|
|
|
|
|
|
|
|
|
self.config.version = None
|
2016-01-13 14:05:22 -05:00
|
|
|
self.config.config_test = mock.Mock()
|
2016-01-05 15:15:29 -05:00
|
|
|
self.config.prepare()
|
2016-07-19 13:12:47 -04:00
|
|
|
self.assertEqual((1, 6, 2), self.config.version)
|
2016-01-05 15:15:29 -05:00
|
|
|
|
2017-05-01 17:49:12 -04:00
|
|
|
def test_prepare_locked(self):
|
|
|
|
|
server_root = self.config.conf("server-root")
|
2019-02-20 19:20:16 -05:00
|
|
|
|
|
|
|
|
from certbot import util as certbot_util
|
|
|
|
|
certbot_util._LOCKS[server_root].release() # pylint: disable=protected-access
|
|
|
|
|
|
2017-05-01 17:49:12 -04:00
|
|
|
self.config.config_test = mock.Mock()
|
|
|
|
|
certbot_test_util.lock_and_call(self._test_prepare_locked, server_root)
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.util.exe_exists")
|
2017-05-01 17:49:12 -04:00
|
|
|
def _test_prepare_locked(self, unused_exe_exists):
|
|
|
|
|
try:
|
|
|
|
|
self.config.prepare()
|
|
|
|
|
except errors.PluginError as err:
|
|
|
|
|
err_msg = str(err)
|
|
|
|
|
self.assertTrue("lock" in err_msg)
|
|
|
|
|
self.assertTrue(self.config.conf("server-root") in err_msg)
|
|
|
|
|
else: # pragma: no cover
|
|
|
|
|
self.fail("Exception wasn't raised!")
|
|
|
|
|
|
2020-02-06 15:10:41 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.socket.gethostname")
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.socket.gethostbyaddr")
|
2020-02-06 15:10:41 -05:00
|
|
|
def test_get_all_names(self, mock_gethostbyaddr, mock_gethostname):
|
2015-07-15 11:57:51 -04:00
|
|
|
mock_gethostbyaddr.return_value = ('155.225.50.69.nephoscale.net', [], [])
|
2020-02-06 15:10:41 -05:00
|
|
|
mock_gethostname.return_value = ('example.net')
|
2015-03-23 13:53:44 -04:00
|
|
|
names = self.config.get_all_names()
|
2019-02-20 19:20:16 -05:00
|
|
|
self.assertEqual(names, {
|
|
|
|
|
"155.225.50.69.nephoscale.net", "www.example.org", "another.alias",
|
2016-12-05 22:17:04 -05:00
|
|
|
"migration.com", "summer.com", "geese.com", "sslon.com",
|
2018-06-04 20:44:51 -04:00
|
|
|
"globalssl.com", "globalsslsetssl.com", "ipv6.com", "ipv6ssl.com",
|
2021-02-26 16:43:22 -05:00
|
|
|
"headers.com", "example.net", "ssl.both.com"})
|
2015-04-14 19:43:40 -04:00
|
|
|
|
|
|
|
|
def test_supported_enhancements(self):
|
2018-03-16 18:27:39 -04:00
|
|
|
self.assertEqual(['redirect', 'ensure-http-header', 'staple-ocsp'],
|
2016-09-21 18:48:24 -04:00
|
|
|
self.config.supported_enhancements())
|
2015-04-14 19:43:40 -04:00
|
|
|
|
|
|
|
|
def test_enhance(self):
|
2015-06-12 10:45:28 -04:00
|
|
|
self.assertRaises(
|
2015-11-05 13:20:39 -05:00
|
|
|
errors.PluginError, self.config.enhance, 'myhost', 'unknown_enhancement')
|
2015-04-14 19:43:40 -04:00
|
|
|
|
|
|
|
|
def test_get_chall_pref(self):
|
2019-10-31 13:17:29 -04:00
|
|
|
self.assertEqual([challenges.HTTP01],
|
2015-04-14 19:43:40 -04:00
|
|
|
self.config.get_chall_pref('myhost'))
|
|
|
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
|
filep = self.config.parser.abs_path('sites-enabled/example.com')
|
2016-09-26 16:13:29 -04:00
|
|
|
mock_vhost = obj.VirtualHost(filep,
|
|
|
|
|
None, None, None,
|
2020-04-13 13:41:39 -04:00
|
|
|
{'.example.com', 'example.*'},
|
2016-09-26 16:13:29 -04:00
|
|
|
None, [0])
|
2015-04-14 19:43:40 -04:00
|
|
|
self.config.parser.add_server_directives(
|
2016-09-26 16:13:29 -04:00
|
|
|
mock_vhost,
|
2018-03-23 19:30:13 -04:00
|
|
|
[['listen', ' ', '5001', ' ', 'ssl']])
|
2015-04-14 19:43:40 -04:00
|
|
|
self.config.save()
|
2015-04-06 14:24:03 -04:00
|
|
|
|
2015-04-14 19:43:40 -04:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
parsed = self.config.parser._parse_files(filep, override=True)
|
2016-08-17 00:04:28 -04:00
|
|
|
self.assertEqual([[['server'],
|
|
|
|
|
[['listen', '69.50.225.155:9000'],
|
|
|
|
|
['listen', '127.0.0.1'],
|
|
|
|
|
['server_name', '.example.com'],
|
|
|
|
|
['server_name', 'example.*'],
|
2017-03-24 22:45:53 -04:00
|
|
|
['listen', '5001', 'ssl'],
|
2016-08-17 00:04:28 -04:00
|
|
|
['#', parser.COMMENT]]]],
|
2015-04-14 19:43:40 -04:00
|
|
|
parsed[0])
|
|
|
|
|
|
2018-10-19 22:16:54 -04:00
|
|
|
def test_choose_vhosts_alias(self):
|
|
|
|
|
self._test_choose_vhosts_common('alias', 'server_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_example_com(self):
|
|
|
|
|
self._test_choose_vhosts_common('example.com', 'example_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_localhost(self):
|
|
|
|
|
self._test_choose_vhosts_common('localhost', 'localhost_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_example_com_uk_test(self):
|
|
|
|
|
self._test_choose_vhosts_common('example.com.uk.test', 'example_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_www_example_com(self):
|
|
|
|
|
self._test_choose_vhosts_common('www.example.com', 'example_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_test_www_example_com(self):
|
|
|
|
|
self._test_choose_vhosts_common('test.www.example.com', 'foo_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_abc_www_foo_com(self):
|
|
|
|
|
self._test_choose_vhosts_common('abc.www.foo.com', 'foo_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_www_bar_co_uk(self):
|
|
|
|
|
self._test_choose_vhosts_common('www.bar.co.uk', 'localhost_conf')
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_ipv6_com(self):
|
|
|
|
|
self._test_choose_vhosts_common('ipv6.com', 'ipv6_conf')
|
|
|
|
|
|
|
|
|
|
def _test_choose_vhosts_common(self, name, conf):
|
2020-04-13 13:41:39 -04:00
|
|
|
conf_names = {'localhost_conf': {'localhost', r'~^(www\.)?(example|bar)\.'},
|
|
|
|
|
'server_conf': {'somename', 'another.alias', 'alias'},
|
|
|
|
|
'example_conf': {'.example.com', 'example.*'},
|
|
|
|
|
'foo_conf': {'*.www.foo.com', '*.www.example.com'},
|
|
|
|
|
'ipv6_conf': {'ipv6.com'}}
|
2016-01-04 22:48:01 -05:00
|
|
|
|
|
|
|
|
conf_path = {'localhost': "etc_nginx/nginx.conf",
|
|
|
|
|
'alias': "etc_nginx/nginx.conf",
|
|
|
|
|
'example.com': "etc_nginx/sites-enabled/example.com",
|
|
|
|
|
'example.com.uk.test': "etc_nginx/sites-enabled/example.com",
|
|
|
|
|
'www.example.com': "etc_nginx/sites-enabled/example.com",
|
|
|
|
|
'test.www.example.com': "etc_nginx/foo.conf",
|
|
|
|
|
'abc.www.foo.com': "etc_nginx/foo.conf",
|
2017-12-06 20:45:20 -05:00
|
|
|
'www.bar.co.uk': "etc_nginx/nginx.conf",
|
|
|
|
|
'ipv6.com': "etc_nginx/sites-enabled/ipv6.com"}
|
2019-02-20 19:20:16 -05:00
|
|
|
conf_path = {key: os.path.normpath(value) for key, value in conf_path.items()}
|
2016-01-04 22:48:01 -05:00
|
|
|
|
2018-10-19 22:16:54 -04:00
|
|
|
vhost = self.config.choose_vhosts(name)[0]
|
|
|
|
|
path = os.path.relpath(vhost.filep, self.temp_dir)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(conf_names[conf], vhost.names)
|
|
|
|
|
self.assertEqual(conf_path[name], path)
|
|
|
|
|
# IPv6 specific checks
|
|
|
|
|
if name == "ipv6.com":
|
|
|
|
|
self.assertTrue(vhost.ipv6_enabled())
|
|
|
|
|
# Make sure that we have SSL enabled also for IPv6 addr
|
|
|
|
|
self.assertTrue(
|
2020-04-13 13:41:39 -04:00
|
|
|
any(True for x in vhost.addrs if x.ssl and x.ipv6))
|
2018-10-19 22:16:54 -04:00
|
|
|
|
|
|
|
|
def test_choose_vhosts_bad(self):
|
2015-04-16 16:39:24 -04:00
|
|
|
bad_results = ['www.foo.com', 'example', 't.www.bar.co',
|
|
|
|
|
'69.255.225.155']
|
2015-04-14 19:43:40 -04:00
|
|
|
|
|
|
|
|
for name in bad_results:
|
2016-09-26 16:13:29 -04:00
|
|
|
self.assertRaises(errors.MisconfigurationError,
|
2018-03-01 17:05:50 -05:00
|
|
|
self.config.choose_vhosts, name)
|
2015-04-14 19:43:40 -04:00
|
|
|
|
2017-12-06 20:45:20 -05:00
|
|
|
def test_ipv6only(self):
|
|
|
|
|
# ipv6_info: (ipv6_active, ipv6only_present)
|
2018-07-11 20:33:04 -04:00
|
|
|
self.assertEqual((True, False), self.config.ipv6_info("80"))
|
2017-12-06 20:45:20 -05:00
|
|
|
# Port 443 has ipv6only=on because of ipv6ssl.com vhost
|
2018-07-11 20:33:04 -04:00
|
|
|
self.assertEqual((True, True), self.config.ipv6_info("443"))
|
2017-12-06 20:45:20 -05:00
|
|
|
|
2018-03-01 18:08:53 -05:00
|
|
|
def test_ipv6only_detection(self):
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"ipv6.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
|
|
|
|
|
for addr in self.config.choose_vhosts("ipv6.com")[0].addrs:
|
|
|
|
|
self.assertFalse(addr.ipv6only)
|
2017-12-06 20:45:20 -05:00
|
|
|
|
2015-04-14 19:43:40 -04:00
|
|
|
def test_more_info(self):
|
|
|
|
|
self.assertTrue('nginx.conf' in self.config.more_info())
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2016-01-04 15:02:15 -05:00
|
|
|
def test_deploy_cert_requires_fullchain_path(self):
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.deploy_cert,
|
|
|
|
|
"www.example.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
None)
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch('certbot_nginx._internal.parser.NginxParser.update_or_add_server_directives')
|
2018-03-23 19:30:13 -04:00
|
|
|
def test_deploy_cert_raise_on_add_error(self, mock_update_or_add_server_directives):
|
|
|
|
|
mock_update_or_add_server_directives.side_effect = errors.MisconfigurationError()
|
2017-01-10 20:27:09 -05:00
|
|
|
self.assertRaises(
|
|
|
|
|
errors.PluginError,
|
|
|
|
|
self.config.deploy_cert,
|
|
|
|
|
"migration.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
|
2015-03-23 13:53:44 -04:00
|
|
|
def test_deploy_cert(self):
|
2015-04-16 02:11:35 -04:00
|
|
|
server_conf = self.config.parser.abs_path('server.conf')
|
|
|
|
|
nginx_conf = self.config.parser.abs_path('nginx.conf')
|
|
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
2015-10-11 13:20:08 -04:00
|
|
|
self.config.version = (1, 3, 1)
|
2015-04-16 02:11:35 -04:00
|
|
|
|
2015-06-27 04:37:29 -04:00
|
|
|
# Get the default SSL vhost
|
2015-04-16 02:11:35 -04:00
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"www.example.com",
|
2015-10-11 13:20:08 -04:00
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
2015-04-16 02:11:35 -04:00
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"another.alias",
|
2015-10-11 13:20:08 -04:00
|
|
|
"/etc/nginx/cert.pem",
|
|
|
|
|
"/etc/nginx/key.pem",
|
|
|
|
|
"/etc/nginx/chain.pem",
|
|
|
|
|
"/etc/nginx/fullchain.pem")
|
2015-04-16 02:11:35 -04:00
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
|
|
self.config.parser.load()
|
|
|
|
|
|
2015-07-11 03:15:59 -04:00
|
|
|
parsed_example_conf = util.filter_comments(self.config.parser.parsed[example_conf])
|
|
|
|
|
parsed_server_conf = util.filter_comments(self.config.parser.parsed[server_conf])
|
|
|
|
|
parsed_nginx_conf = util.filter_comments(self.config.parser.parsed[nginx_conf])
|
|
|
|
|
|
2015-04-16 02:11:35 -04:00
|
|
|
self.assertEqual([[['server'],
|
2016-01-01 19:35:57 -05:00
|
|
|
[
|
2015-10-11 13:20:08 -04:00
|
|
|
['listen', '69.50.225.155:9000'],
|
2015-04-16 02:11:35 -04:00
|
|
|
['listen', '127.0.0.1'],
|
|
|
|
|
['server_name', '.example.com'],
|
2016-01-01 19:35:57 -05:00
|
|
|
['server_name', 'example.*'],
|
|
|
|
|
|
2017-03-24 22:45:53 -04:00
|
|
|
['listen', '5001', 'ssl'],
|
2016-01-01 19:35:57 -05:00
|
|
|
['ssl_certificate', 'example/fullchain.pem'],
|
2017-05-02 20:56:56 -04:00
|
|
|
['ssl_certificate_key', 'example/key.pem'],
|
2017-09-01 10:57:30 -04:00
|
|
|
['include', self.config.mod_ssl_conf],
|
|
|
|
|
['ssl_dhparam', self.config.ssl_dhparams],
|
|
|
|
|
]]],
|
2015-07-11 03:15:59 -04:00
|
|
|
parsed_example_conf)
|
2017-03-24 22:45:53 -04:00
|
|
|
self.assertEqual([['server_name', 'somename', 'alias', 'another.alias']],
|
2015-07-11 03:15:59 -04:00
|
|
|
parsed_server_conf)
|
2016-01-01 19:35:57 -05:00
|
|
|
self.assertTrue(util.contains_at_depth(
|
|
|
|
|
parsed_nginx_conf,
|
|
|
|
|
[['server'],
|
|
|
|
|
[
|
|
|
|
|
['listen', '8000'],
|
|
|
|
|
['listen', 'somename:8080'],
|
|
|
|
|
['include', 'server.conf'],
|
|
|
|
|
[['location', '/'],
|
|
|
|
|
[['root', 'html'],
|
2017-03-24 22:45:53 -04:00
|
|
|
['index', 'index.html', 'index.htm']]],
|
|
|
|
|
['listen', '5001', 'ssl'],
|
2016-01-01 19:35:57 -05:00
|
|
|
['ssl_certificate', '/etc/nginx/fullchain.pem'],
|
2017-05-02 20:56:56 -04:00
|
|
|
['ssl_certificate_key', '/etc/nginx/key.pem'],
|
2017-09-01 10:57:30 -04:00
|
|
|
['include', self.config.mod_ssl_conf],
|
|
|
|
|
['ssl_dhparam', self.config.ssl_dhparams],
|
|
|
|
|
]],
|
2016-01-01 19:35:57 -05:00
|
|
|
2))
|
2015-04-16 02:11:35 -04:00
|
|
|
|
2016-09-29 19:16:07 -04:00
|
|
|
def test_deploy_cert_add_explicit_listen(self):
|
|
|
|
|
migration_conf = self.config.parser.abs_path('sites-enabled/migration.com')
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"summer.com",
|
|
|
|
|
"summer/cert.pem",
|
|
|
|
|
"summer/key.pem",
|
|
|
|
|
"summer/chain.pem",
|
|
|
|
|
"summer/fullchain.pem")
|
|
|
|
|
self.config.save()
|
|
|
|
|
self.config.parser.load()
|
|
|
|
|
parsed_migration_conf = util.filter_comments(self.config.parser.parsed[migration_conf])
|
|
|
|
|
self.assertEqual([['server'],
|
|
|
|
|
[
|
|
|
|
|
['server_name', 'migration.com'],
|
|
|
|
|
['server_name', 'summer.com'],
|
|
|
|
|
|
|
|
|
|
['listen', '80'],
|
2017-03-24 22:45:53 -04:00
|
|
|
['listen', '5001', 'ssl'],
|
2016-09-29 19:16:07 -04:00
|
|
|
['ssl_certificate', 'summer/fullchain.pem'],
|
2017-05-02 20:56:56 -04:00
|
|
|
['ssl_certificate_key', 'summer/key.pem'],
|
2017-09-01 10:57:30 -04:00
|
|
|
['include', self.config.mod_ssl_conf],
|
|
|
|
|
['ssl_dhparam', self.config.ssl_dhparams],
|
|
|
|
|
]],
|
2016-09-29 19:16:07 -04:00
|
|
|
parsed_migration_conf[0])
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.http_01.NginxHttp01.perform")
|
|
|
|
|
@mock.patch("certbot_nginx._internal.configurator.NginxConfigurator.restart")
|
|
|
|
|
@mock.patch("certbot_nginx._internal.configurator.NginxConfigurator.revert_challenge_config")
|
2019-03-18 13:22:19 -04:00
|
|
|
def test_perform_and_cleanup(self, mock_revert, mock_restart, mock_http_perform):
|
2015-03-23 13:53:44 -04:00
|
|
|
# Only tests functionality specific to configurator.perform
|
|
|
|
|
# Note: As more challenges are offered this will have to be expanded
|
2019-03-18 13:22:19 -04:00
|
|
|
achall = achallenges.KeyAuthorizationAnnotatedChallenge(
|
2015-06-11 11:00:18 -04:00
|
|
|
challb=messages.ChallengeBody(
|
2018-01-11 20:06:23 -05:00
|
|
|
chall=challenges.HTTP01(token=b"m8TdO1qik4JVFtgPPurJmg"),
|
2015-04-23 16:57:35 -04:00
|
|
|
uri="https://ca.org/chall1_uri",
|
2015-06-11 11:00:18 -04:00
|
|
|
status=messages.Status("pending"),
|
2015-08-11 16:22:03 -04:00
|
|
|
), domain="example.com", account_key=self.rsa512jwk)
|
2015-04-16 02:11:35 -04:00
|
|
|
|
2015-11-19 12:12:25 -05:00
|
|
|
expected = [
|
2019-03-18 13:22:19 -04:00
|
|
|
achall.response(self.rsa512jwk),
|
2015-04-16 02:11:35 -04:00
|
|
|
]
|
|
|
|
|
|
2019-03-18 13:22:19 -04:00
|
|
|
mock_http_perform.return_value = expected[:]
|
|
|
|
|
responses = self.config.perform([achall])
|
2015-04-16 02:11:35 -04:00
|
|
|
|
2018-01-11 20:06:23 -05:00
|
|
|
self.assertEqual(mock_http_perform.call_count, 1)
|
2015-11-19 12:12:25 -05:00
|
|
|
self.assertEqual(responses, expected)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2019-03-18 13:22:19 -04:00
|
|
|
self.config.cleanup([achall])
|
2016-06-24 19:48:22 -04:00
|
|
|
self.assertEqual(0, self.config._chall_out) # pylint: disable=protected-access
|
|
|
|
|
self.assertEqual(mock_revert.call_count, 1)
|
|
|
|
|
self.assertEqual(mock_restart.call_count, 2)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
|
2015-03-23 13:53:44 -04:00
|
|
|
def test_get_version(self, mock_popen):
|
|
|
|
|
mock_popen().communicate.return_value = (
|
2015-04-14 19:43:40 -04:00
|
|
|
"", "\n".join(["nginx version: nginx/1.4.2",
|
|
|
|
|
"built by clang 6.0 (clang-600.0.56)"
|
|
|
|
|
" (based on LLVM 3.5svn)",
|
|
|
|
|
"TLS SNI support enabled",
|
|
|
|
|
"configure arguments: --prefix=/usr/local/Cellar/"
|
|
|
|
|
"nginx/1.6.2 --with-http_ssl_module"]))
|
|
|
|
|
self.assertEqual(self.config.get_version(), (1, 4, 2))
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2015-04-20 13:58:02 -04:00
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", "\n".join(["nginx version: nginx/0.9",
|
|
|
|
|
"built by clang 6.0 (clang-600.0.56)"
|
|
|
|
|
" (based on LLVM 3.5svn)",
|
|
|
|
|
"TLS SNI support enabled",
|
|
|
|
|
"configure arguments: --with-http_ssl_module"]))
|
|
|
|
|
self.assertEqual(self.config.get_version(), (0, 9))
|
|
|
|
|
|
2015-03-23 13:53:44 -04:00
|
|
|
mock_popen().communicate.return_value = (
|
2015-04-14 19:43:40 -04:00
|
|
|
"", "\n".join(["blah 0.0.1",
|
2015-04-20 13:58:02 -04:00
|
|
|
"built by clang 6.0 (clang-600.0.56)"
|
|
|
|
|
" (based on LLVM 3.5svn)",
|
|
|
|
|
"TLS SNI support enabled",
|
|
|
|
|
"configure arguments: --with-http_ssl_module"]))
|
2015-06-26 12:29:40 -04:00
|
|
|
self.assertRaises(errors.PluginError, self.config.get_version)
|
2015-04-20 13:58:02 -04:00
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", "\n".join(["nginx version: nginx/1.4.2",
|
2015-04-14 19:43:40 -04:00
|
|
|
"TLS SNI support enabled"]))
|
2015-06-26 12:29:40 -04:00
|
|
|
self.assertRaises(errors.PluginError, self.config.get_version)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
2015-04-14 19:43:40 -04:00
|
|
|
"", "\n".join(["nginx version: nginx/1.4.2",
|
2015-04-20 13:58:02 -04:00
|
|
|
"built by clang 6.0 (clang-600.0.56)"
|
|
|
|
|
" (based on LLVM 3.5svn)",
|
|
|
|
|
"configure arguments: --with-http_ssl_module"]))
|
2015-06-26 12:29:40 -04:00
|
|
|
self.assertRaises(errors.PluginError, self.config.get_version)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
2015-04-14 19:43:40 -04:00
|
|
|
"", "\n".join(["nginx version: nginx/0.8.1",
|
2015-04-20 13:58:02 -04:00
|
|
|
"built by clang 6.0 (clang-600.0.56)"
|
|
|
|
|
" (based on LLVM 3.5svn)",
|
|
|
|
|
"TLS SNI support enabled",
|
|
|
|
|
"configure arguments: --with-http_ssl_module"]))
|
2015-07-31 13:44:21 -04:00
|
|
|
self.assertRaises(errors.NotSupportedError, self.config.get_version)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
|
|
|
|
mock_popen.side_effect = OSError("Can't find program")
|
2015-06-26 12:29:40 -04:00
|
|
|
self.assertRaises(errors.PluginError, self.config.get_version)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
|
2019-09-05 16:51:56 -04:00
|
|
|
def test_get_openssl_version(self, mock_popen):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", """
|
|
|
|
|
nginx version: nginx/1.15.5
|
|
|
|
|
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
|
|
|
|
|
built with OpenSSL 1.0.2g 1 Mar 2016
|
|
|
|
|
TLS SNI support enabled
|
|
|
|
|
configure arguments:
|
|
|
|
|
""")
|
|
|
|
|
self.assertEqual(self.config._get_openssl_version(), "1.0.2g")
|
|
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", """
|
|
|
|
|
nginx version: nginx/1.15.5
|
|
|
|
|
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
|
|
|
|
|
built with OpenSSL 1.0.2-beta1 1 Mar 2016
|
|
|
|
|
TLS SNI support enabled
|
|
|
|
|
configure arguments:
|
|
|
|
|
""")
|
|
|
|
|
self.assertEqual(self.config._get_openssl_version(), "1.0.2-beta1")
|
|
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", """
|
|
|
|
|
nginx version: nginx/1.15.5
|
|
|
|
|
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
|
|
|
|
|
built with OpenSSL 1.0.2 1 Mar 2016
|
|
|
|
|
TLS SNI support enabled
|
|
|
|
|
configure arguments:
|
|
|
|
|
""")
|
|
|
|
|
self.assertEqual(self.config._get_openssl_version(), "1.0.2")
|
|
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", """
|
|
|
|
|
nginx version: nginx/1.15.5
|
|
|
|
|
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
|
|
|
|
|
built with OpenSSL 1.0.2g 1 Mar 2016 (running with OpenSSL 1.0.2a 1 Mar 2016)
|
|
|
|
|
TLS SNI support enabled
|
|
|
|
|
configure arguments:
|
|
|
|
|
""")
|
|
|
|
|
self.assertEqual(self.config._get_openssl_version(), "1.0.2a")
|
|
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", """
|
|
|
|
|
nginx version: nginx/1.15.5
|
|
|
|
|
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
|
|
|
|
|
built with LibreSSL 2.2.2
|
|
|
|
|
TLS SNI support enabled
|
|
|
|
|
configure arguments:
|
|
|
|
|
""")
|
|
|
|
|
self.assertEqual(self.config._get_openssl_version(), "")
|
|
|
|
|
|
|
|
|
|
mock_popen().communicate.return_value = (
|
|
|
|
|
"", """
|
|
|
|
|
nginx version: nginx/1.15.5
|
|
|
|
|
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
|
|
|
|
|
TLS SNI support enabled
|
|
|
|
|
configure arguments:
|
|
|
|
|
""")
|
|
|
|
|
self.assertEqual(self.config._get_openssl_version(), "")
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
|
2020-07-27 15:52:12 -04:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.time")
|
|
|
|
|
def test_nginx_restart(self, mock_time, mock_popen):
|
2015-04-17 20:05:00 -04:00
|
|
|
mocked = mock_popen()
|
|
|
|
|
mocked.communicate.return_value = ('', '')
|
|
|
|
|
mocked.returncode = 0
|
2016-01-01 19:35:57 -05:00
|
|
|
self.config.restart()
|
2020-09-16 15:22:15 -04:00
|
|
|
self.assertEqual(mocked.communicate.call_count, 1)
|
2020-07-27 15:52:12 -04:00
|
|
|
mock_time.sleep.assert_called_once_with(0.1234)
|
2015-04-14 19:43:40 -04:00
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
|
2020-09-16 15:22:15 -04:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.logger.debug")
|
|
|
|
|
def test_nginx_restart_fail(self, mock_log_debug, mock_popen):
|
2015-05-08 15:34:48 -04:00
|
|
|
mocked = mock_popen()
|
|
|
|
|
mocked.communicate.return_value = ('', '')
|
|
|
|
|
mocked.returncode = 1
|
2016-01-01 19:35:57 -05:00
|
|
|
self.assertRaises(errors.MisconfigurationError, self.config.restart)
|
2020-09-16 15:22:15 -04:00
|
|
|
self.assertEqual(mocked.communicate.call_count, 2)
|
|
|
|
|
mock_log_debug.assert_called_once_with("nginx reload failed:\n%s", "")
|
2015-05-08 15:34:48 -04:00
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.configurator.subprocess.Popen")
|
2015-05-08 15:34:48 -04:00
|
|
|
def test_no_nginx_start(self, mock_popen):
|
|
|
|
|
mock_popen.side_effect = OSError("Can't find program")
|
2016-01-01 19:35:57 -05:00
|
|
|
self.assertRaises(errors.MisconfigurationError, self.config.restart)
|
2015-05-08 15:34:48 -04:00
|
|
|
|
2016-05-26 16:51:56 -04:00
|
|
|
@mock.patch("certbot.util.run_script")
|
2016-01-13 14:05:22 -05:00
|
|
|
def test_config_test_bad_process(self, mock_run_script):
|
|
|
|
|
mock_run_script.side_effect = errors.SubprocessError
|
|
|
|
|
self.assertRaises(errors.MisconfigurationError, self.config.config_test)
|
2015-03-23 13:53:44 -04:00
|
|
|
|
2016-12-08 23:29:59 -05:00
|
|
|
@mock.patch("certbot.util.run_script")
|
|
|
|
|
def test_config_test(self, _):
|
|
|
|
|
self.config.config_test()
|
|
|
|
|
|
2016-04-13 19:45:54 -04:00
|
|
|
@mock.patch("certbot.reverter.Reverter.recovery_routine")
|
2016-01-26 12:29:49 -05:00
|
|
|
def test_recovery_routine_throws_error_from_reverter(self, mock_recovery_routine):
|
|
|
|
|
mock_recovery_routine.side_effect = errors.ReverterError("foo")
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.recovery_routine)
|
|
|
|
|
|
2016-04-13 19:45:54 -04:00
|
|
|
@mock.patch("certbot.reverter.Reverter.rollback_checkpoints")
|
2016-01-26 12:29:49 -05:00
|
|
|
def test_rollback_checkpoints_throws_error_from_reverter(self, mock_rollback_checkpoints):
|
|
|
|
|
mock_rollback_checkpoints.side_effect = errors.ReverterError("foo")
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.rollback_checkpoints)
|
|
|
|
|
|
2016-04-13 19:45:54 -04:00
|
|
|
@mock.patch("certbot.reverter.Reverter.revert_temporary_config")
|
2016-01-26 12:29:49 -05:00
|
|
|
def test_revert_challenge_config_throws_error_from_reverter(self, mock_revert_temporary_config):
|
|
|
|
|
mock_revert_temporary_config.side_effect = errors.ReverterError("foo")
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.revert_challenge_config)
|
|
|
|
|
|
2016-04-13 19:45:54 -04:00
|
|
|
@mock.patch("certbot.reverter.Reverter.add_to_checkpoint")
|
2016-01-26 12:29:49 -05:00
|
|
|
def test_save_throws_error_from_reverter(self, mock_add_to_checkpoint):
|
|
|
|
|
mock_add_to_checkpoint.side_effect = errors.ReverterError("foo")
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.save)
|
|
|
|
|
|
2015-06-27 04:19:16 -04:00
|
|
|
def test_get_snakeoil_paths(self):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
cert, key = self.config._get_snakeoil_paths()
|
|
|
|
|
self.assertTrue(os.path.exists(cert))
|
|
|
|
|
self.assertTrue(os.path.exists(key))
|
|
|
|
|
with open(cert) as cert_file:
|
|
|
|
|
OpenSSL.crypto.load_certificate(
|
|
|
|
|
OpenSSL.crypto.FILETYPE_PEM, cert_file.read())
|
|
|
|
|
with open(key) as key_file:
|
|
|
|
|
OpenSSL.crypto.load_privatekey(
|
|
|
|
|
OpenSSL.crypto.FILETYPE_PEM, key_file.read())
|
|
|
|
|
|
2016-01-04 23:04:18 -05:00
|
|
|
def test_redirect_enhance(self):
|
2016-09-29 19:16:07 -04:00
|
|
|
# Test that we successfully add a redirect when there is
|
|
|
|
|
# a listen directive
|
2018-01-25 01:19:32 -05:00
|
|
|
expected = UnspacedList(_redirect_block_for_domain("www.example.com"))[0]
|
2016-01-04 23:04:18 -05:00
|
|
|
|
|
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
|
|
|
|
self.config.enhance("www.example.com", "redirect")
|
|
|
|
|
|
|
|
|
|
generated_conf = self.config.parser.parsed[example_conf]
|
|
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
|
2016-01-25 14:22:03 -05:00
|
|
|
|
2016-09-29 19:16:07 -04:00
|
|
|
# Test that we successfully add a redirect when there is
|
|
|
|
|
# no listen directive
|
|
|
|
|
migration_conf = self.config.parser.abs_path('sites-enabled/migration.com')
|
|
|
|
|
self.config.enhance("migration.com", "redirect")
|
|
|
|
|
|
2018-01-25 01:19:32 -05:00
|
|
|
expected = UnspacedList(_redirect_block_for_domain("migration.com"))[0]
|
|
|
|
|
|
2016-09-29 19:16:07 -04:00
|
|
|
generated_conf = self.config.parser.parsed[migration_conf]
|
|
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
|
|
|
|
|
|
2017-12-07 12:48:54 -05:00
|
|
|
def test_split_for_redirect(self):
|
|
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"example.org",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
self.config.enhance("www.example.com", "redirect")
|
|
|
|
|
generated_conf = self.config.parser.parsed[example_conf]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
[[['server'], [
|
|
|
|
|
['server_name', '.example.com'],
|
|
|
|
|
['server_name', 'example.*'], [],
|
|
|
|
|
['listen', '5001', 'ssl'], ['#', ' managed by Certbot'],
|
|
|
|
|
['ssl_certificate', 'example/fullchain.pem'], ['#', ' managed by Certbot'],
|
|
|
|
|
['ssl_certificate_key', 'example/key.pem'], ['#', ' managed by Certbot'],
|
|
|
|
|
['include', self.config.mod_ssl_conf], ['#', ' managed by Certbot'],
|
|
|
|
|
['ssl_dhparam', self.config.ssl_dhparams], ['#', ' managed by Certbot'],
|
|
|
|
|
[], []]],
|
|
|
|
|
[['server'], [
|
2018-01-25 01:19:32 -05:00
|
|
|
[['if', '($host', '=', 'www.example.com)'], [
|
|
|
|
|
['return', '301', 'https://$host$request_uri']]],
|
|
|
|
|
['#', ' managed by Certbot'], [],
|
2017-12-07 12:48:54 -05:00
|
|
|
['listen', '69.50.225.155:9000'],
|
|
|
|
|
['listen', '127.0.0.1'],
|
|
|
|
|
['server_name', '.example.com'],
|
|
|
|
|
['server_name', 'example.*'],
|
2018-01-25 01:19:32 -05:00
|
|
|
['return', '404'], ['#', ' managed by Certbot'], [], [], []]]],
|
2017-12-07 12:48:54 -05:00
|
|
|
generated_conf)
|
|
|
|
|
|
2018-03-16 18:27:39 -04:00
|
|
|
def test_split_for_headers(self):
|
|
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"example.org",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
self.config.enhance("www.example.com", "ensure-http-header", "Strict-Transport-Security")
|
|
|
|
|
generated_conf = self.config.parser.parsed[example_conf]
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
[[['server'], [
|
|
|
|
|
['server_name', '.example.com'],
|
|
|
|
|
['server_name', 'example.*'], [],
|
|
|
|
|
['listen', '5001', 'ssl'], ['#', ' managed by Certbot'],
|
|
|
|
|
['ssl_certificate', 'example/fullchain.pem'], ['#', ' managed by Certbot'],
|
|
|
|
|
['ssl_certificate_key', 'example/key.pem'], ['#', ' managed by Certbot'],
|
|
|
|
|
['include', self.config.mod_ssl_conf], ['#', ' managed by Certbot'],
|
|
|
|
|
['ssl_dhparam', self.config.ssl_dhparams], ['#', ' managed by Certbot'],
|
|
|
|
|
[], [],
|
|
|
|
|
['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'],
|
|
|
|
|
['#', ' managed by Certbot'],
|
|
|
|
|
[], []]],
|
|
|
|
|
[['server'], [
|
|
|
|
|
['listen', '69.50.225.155:9000'],
|
|
|
|
|
['listen', '127.0.0.1'],
|
|
|
|
|
['server_name', '.example.com'],
|
|
|
|
|
['server_name', 'example.*'],
|
|
|
|
|
[], [], []]]],
|
|
|
|
|
generated_conf)
|
|
|
|
|
|
|
|
|
|
def test_http_header_hsts(self):
|
|
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
|
|
|
|
self.config.enhance("www.example.com", "ensure-http-header",
|
|
|
|
|
"Strict-Transport-Security")
|
|
|
|
|
expected = ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always']
|
|
|
|
|
generated_conf = self.config.parser.parsed[example_conf]
|
|
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
|
|
|
|
|
|
2018-06-04 20:44:51 -04:00
|
|
|
def test_multiple_headers_hsts(self):
|
|
|
|
|
headers_conf = self.config.parser.abs_path('sites-enabled/headers.com')
|
|
|
|
|
self.config.enhance("headers.com", "ensure-http-header",
|
|
|
|
|
"Strict-Transport-Security")
|
|
|
|
|
expected = ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always']
|
|
|
|
|
generated_conf = self.config.parser.parsed[headers_conf]
|
|
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
|
|
|
|
|
|
2018-03-16 18:27:39 -04:00
|
|
|
def test_http_header_hsts_twice(self):
|
|
|
|
|
self.config.enhance("www.example.com", "ensure-http-header",
|
|
|
|
|
"Strict-Transport-Security")
|
|
|
|
|
self.assertRaises(
|
|
|
|
|
errors.PluginEnhancementAlreadyPresent,
|
|
|
|
|
self.config.enhance, "www.example.com",
|
|
|
|
|
"ensure-http-header", "Strict-Transport-Security")
|
|
|
|
|
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch('certbot_nginx._internal.obj.VirtualHost.contains_list')
|
2018-01-25 01:19:32 -05:00
|
|
|
def test_certbot_redirect_exists(self, mock_contains_list):
|
2016-11-07 18:48:46 -05:00
|
|
|
# Test that we add no redirect statement if there is already a
|
|
|
|
|
# redirect in the block that is managed by certbot
|
|
|
|
|
# Has a certbot redirect
|
|
|
|
|
mock_contains_list.return_value = True
|
2019-11-25 17:30:24 -05:00
|
|
|
with mock.patch("certbot_nginx._internal.configurator.logger") as mock_logger:
|
2016-11-07 18:48:46 -05:00
|
|
|
self.config.enhance("www.example.com", "redirect")
|
|
|
|
|
self.assertEqual(mock_logger.info.call_args[0][0],
|
|
|
|
|
"Traffic on port %s already redirecting to ssl in %s")
|
|
|
|
|
|
2016-09-29 19:16:07 -04:00
|
|
|
def test_redirect_dont_enhance(self):
|
|
|
|
|
# Test that we don't accidentally add redirect to ssl-only block
|
2019-11-25 17:30:24 -05:00
|
|
|
with mock.patch("certbot_nginx._internal.configurator.logger") as mock_logger:
|
2016-09-29 19:16:07 -04:00
|
|
|
self.config.enhance("geese.com", "redirect")
|
|
|
|
|
self.assertEqual(mock_logger.info.call_args[0][0],
|
|
|
|
|
'No matching insecure server blocks listening on port %s found.')
|
|
|
|
|
|
2018-01-25 01:19:32 -05:00
|
|
|
def test_double_redirect(self):
|
|
|
|
|
# Test that we add one redirect for each domain
|
2017-10-19 17:56:53 -04:00
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
|
|
|
|
self.config.enhance("example.com", "redirect")
|
|
|
|
|
self.config.enhance("example.org", "redirect")
|
|
|
|
|
|
2018-01-25 01:19:32 -05:00
|
|
|
expected1 = UnspacedList(_redirect_block_for_domain("example.com"))[0]
|
|
|
|
|
expected2 = UnspacedList(_redirect_block_for_domain("example.org"))[0]
|
|
|
|
|
|
2017-10-19 17:56:53 -04:00
|
|
|
generated_conf = self.config.parser.parsed[example_conf]
|
2018-01-25 01:19:32 -05:00
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected1, 2))
|
|
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected2, 2))
|
2017-10-19 17:56:53 -04:00
|
|
|
|
2016-09-21 18:38:37 -04:00
|
|
|
def test_staple_ocsp_bad_version(self):
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.enhance,
|
|
|
|
|
"www.example.com", "staple-ocsp", "chain_path")
|
|
|
|
|
|
|
|
|
|
def test_staple_ocsp_no_chain_path(self):
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.enhance,
|
|
|
|
|
"www.example.com", "staple-ocsp", None)
|
|
|
|
|
|
|
|
|
|
def test_staple_ocsp_internal_error(self):
|
|
|
|
|
self.config.enhance("www.example.com", "staple-ocsp", "chain_path")
|
|
|
|
|
# error is raised because the server block has conflicting directives
|
|
|
|
|
self.assertRaises(errors.PluginError, self.config.enhance,
|
|
|
|
|
"www.example.com", "staple-ocsp", "different_path")
|
|
|
|
|
|
|
|
|
|
def test_staple_ocsp(self):
|
|
|
|
|
chain_path = "example/chain.pem"
|
|
|
|
|
self.config.enhance("www.example.com", "staple-ocsp", chain_path)
|
|
|
|
|
|
|
|
|
|
example_conf = self.config.parser.abs_path('sites-enabled/example.com')
|
|
|
|
|
generated_conf = self.config.parser.parsed[example_conf]
|
|
|
|
|
|
|
|
|
|
self.assertTrue(util.contains_at_depth(
|
|
|
|
|
generated_conf,
|
|
|
|
|
['ssl_trusted_certificate', 'example/chain.pem'], 2))
|
|
|
|
|
self.assertTrue(util.contains_at_depth(
|
|
|
|
|
generated_conf, ['ssl_stapling', 'on'], 2))
|
|
|
|
|
self.assertTrue(util.contains_at_depth(
|
|
|
|
|
generated_conf, ['ssl_stapling_verify', 'on'], 2))
|
|
|
|
|
|
2017-12-06 20:45:20 -05:00
|
|
|
def test_deploy_no_match_default_set(self):
|
|
|
|
|
default_conf = self.config.parser.abs_path('sites-enabled/default')
|
|
|
|
|
foo_conf = self.config.parser.abs_path('foo.conf')
|
|
|
|
|
del self.config.parser.parsed[foo_conf][2][1][0][1][0] # remove default_server
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"www.nomatch.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
|
|
self.config.parser.load()
|
|
|
|
|
|
|
|
|
|
parsed_default_conf = util.filter_comments(self.config.parser.parsed[default_conf])
|
|
|
|
|
|
|
|
|
|
self.assertEqual([[['server'],
|
|
|
|
|
[['listen', 'myhost', 'default_server'],
|
|
|
|
|
['listen', 'otherhost', 'default_server'],
|
2018-04-03 15:14:23 -04:00
|
|
|
['server_name', '"www.example.org"'],
|
2017-12-06 20:45:20 -05:00
|
|
|
[['location', '/'],
|
|
|
|
|
[['root', 'html'],
|
|
|
|
|
['index', 'index.html', 'index.htm']]]]],
|
|
|
|
|
[['server'],
|
|
|
|
|
[['listen', 'myhost'],
|
|
|
|
|
['listen', 'otherhost'],
|
|
|
|
|
['server_name', 'www.nomatch.com'],
|
|
|
|
|
[['location', '/'],
|
|
|
|
|
[['root', 'html'],
|
|
|
|
|
['index', 'index.html', 'index.htm']]],
|
|
|
|
|
['listen', '5001', 'ssl'],
|
|
|
|
|
['ssl_certificate', 'example/fullchain.pem'],
|
|
|
|
|
['ssl_certificate_key', 'example/key.pem'],
|
|
|
|
|
['include', self.config.mod_ssl_conf],
|
|
|
|
|
['ssl_dhparam', self.config.ssl_dhparams]]]],
|
|
|
|
|
parsed_default_conf)
|
|
|
|
|
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"nomatch.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
|
|
self.config.parser.load()
|
|
|
|
|
|
|
|
|
|
parsed_default_conf = util.filter_comments(self.config.parser.parsed[default_conf])
|
|
|
|
|
|
|
|
|
|
self.assertTrue(util.contains_at_depth(parsed_default_conf, "nomatch.com", 3))
|
|
|
|
|
|
|
|
|
|
def test_deploy_no_match_default_set_multi_level_path(self):
|
|
|
|
|
default_conf = self.config.parser.abs_path('sites-enabled/default')
|
|
|
|
|
foo_conf = self.config.parser.abs_path('foo.conf')
|
|
|
|
|
del self.config.parser.parsed[default_conf][0][1][0]
|
|
|
|
|
del self.config.parser.parsed[default_conf][0][1][0]
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"www.nomatch.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
|
|
self.config.parser.load()
|
|
|
|
|
|
|
|
|
|
parsed_foo_conf = util.filter_comments(self.config.parser.parsed[foo_conf])
|
|
|
|
|
|
|
|
|
|
self.assertEqual([['server'],
|
|
|
|
|
[['listen', '*:80', 'ssl'],
|
|
|
|
|
['server_name', 'www.nomatch.com'],
|
|
|
|
|
['root', '/home/ubuntu/sites/foo/'],
|
|
|
|
|
[['location', '/status'], [[['types'], [['image/jpeg', 'jpg']]]]],
|
|
|
|
|
[['location', '~', 'case_sensitive\\.php$'], [['index', 'index.php'],
|
|
|
|
|
['root', '/var/root']]],
|
|
|
|
|
[['location', '~*', 'case_insensitive\\.php$'], []],
|
|
|
|
|
[['location', '=', 'exact_match\\.php$'], []],
|
|
|
|
|
[['location', '^~', 'ignore_regex\\.php$'], []],
|
|
|
|
|
['ssl_certificate', 'example/fullchain.pem'],
|
|
|
|
|
['ssl_certificate_key', 'example/key.pem']]],
|
|
|
|
|
parsed_foo_conf[1][1][1])
|
|
|
|
|
|
|
|
|
|
def test_deploy_no_match_no_default_set(self):
|
|
|
|
|
default_conf = self.config.parser.abs_path('sites-enabled/default')
|
|
|
|
|
foo_conf = self.config.parser.abs_path('foo.conf')
|
|
|
|
|
del self.config.parser.parsed[default_conf][0][1][0]
|
|
|
|
|
del self.config.parser.parsed[default_conf][0][1][0]
|
|
|
|
|
del self.config.parser.parsed[foo_conf][2][1][0][1][0]
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(errors.MisconfigurationError, self.config.deploy_cert,
|
|
|
|
|
"www.nomatch.com", "example/cert.pem", "example/key.pem",
|
|
|
|
|
"example/chain.pem", "example/fullchain.pem")
|
|
|
|
|
|
|
|
|
|
def test_deploy_no_match_fail_multiple_defaults(self):
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
self.assertRaises(errors.MisconfigurationError, self.config.deploy_cert,
|
|
|
|
|
"www.nomatch.com", "example/cert.pem", "example/key.pem",
|
|
|
|
|
"example/chain.pem", "example/fullchain.pem")
|
|
|
|
|
|
2018-06-05 16:40:48 -04:00
|
|
|
def test_deploy_no_match_multiple_defaults_ok(self):
|
|
|
|
|
foo_conf = self.config.parser.abs_path('foo.conf')
|
|
|
|
|
self.config.parser.parsed[foo_conf][2][1][0][1][0][1] = '*:5001'
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
self.config.deploy_cert("www.nomatch.com", "example/cert.pem", "example/key.pem",
|
|
|
|
|
"example/chain.pem", "example/fullchain.pem")
|
|
|
|
|
|
2017-12-06 20:45:20 -05:00
|
|
|
def test_deploy_no_match_add_redirect(self):
|
|
|
|
|
default_conf = self.config.parser.abs_path('sites-enabled/default')
|
|
|
|
|
foo_conf = self.config.parser.abs_path('foo.conf')
|
|
|
|
|
del self.config.parser.parsed[foo_conf][2][1][0][1][0] # remove default_server
|
|
|
|
|
self.config.version = (1, 3, 1)
|
|
|
|
|
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"www.nomatch.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
|
|
|
|
|
self.config.deploy_cert(
|
|
|
|
|
"nomatch.com",
|
|
|
|
|
"example/cert.pem",
|
|
|
|
|
"example/key.pem",
|
|
|
|
|
"example/chain.pem",
|
|
|
|
|
"example/fullchain.pem")
|
|
|
|
|
|
|
|
|
|
self.config.enhance("www.nomatch.com", "redirect")
|
|
|
|
|
|
|
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
|
|
self.config.parser.load()
|
|
|
|
|
|
2018-01-25 01:19:32 -05:00
|
|
|
expected = UnspacedList(_redirect_block_for_domain("www.nomatch.com"))[0]
|
2017-12-06 20:45:20 -05:00
|
|
|
|
|
|
|
|
generated_conf = self.config.parser.parsed[default_conf]
|
|
|
|
|
self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
|
|
|
|
|
|
2017-12-07 12:48:54 -05:00
|
|
|
@mock.patch('certbot.reverter.logger')
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch('certbot_nginx._internal.parser.NginxParser.load')
|
2017-12-07 12:48:54 -05:00
|
|
|
def test_parser_reload_after_config_changes(self, mock_parser_load, unused_mock_logger):
|
|
|
|
|
self.config.recovery_routine()
|
|
|
|
|
self.config.revert_challenge_config()
|
|
|
|
|
self.config.rollback_checkpoints()
|
2020-11-12 17:33:02 -05:00
|
|
|
self.assertEqual(mock_parser_load.call_count, 3)
|
2017-12-06 20:45:20 -05:00
|
|
|
|
2018-03-01 17:05:50 -05:00
|
|
|
def test_choose_vhosts_wildcard(self):
|
|
|
|
|
# pylint: disable=protected-access
|
2019-11-25 17:30:24 -05:00
|
|
|
mock_path = "certbot_nginx._internal.display_ops.select_vhost_multiple"
|
2018-03-01 17:05:50 -05:00
|
|
|
with mock.patch(mock_path) as mock_select_vhs:
|
|
|
|
|
vhost = [x for x in self.config.parser.get_vhosts()
|
|
|
|
|
if 'summer.com' in x.names][0]
|
|
|
|
|
mock_select_vhs.return_value = [vhost]
|
|
|
|
|
vhs = self.config._choose_vhosts_wildcard("*.com",
|
|
|
|
|
prefer_ssl=True)
|
|
|
|
|
# Check that the dialog was called with migration.com
|
|
|
|
|
self.assertTrue(vhost in mock_select_vhs.call_args[0][0])
|
|
|
|
|
|
|
|
|
|
# And the actual returned values
|
2018-07-11 20:33:04 -04:00
|
|
|
self.assertEqual(len(vhs), 1)
|
2018-03-01 17:05:50 -05:00
|
|
|
self.assertEqual(vhs[0], vhost)
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_wildcard_redirect(self):
|
|
|
|
|
# pylint: disable=protected-access
|
2019-11-25 17:30:24 -05:00
|
|
|
mock_path = "certbot_nginx._internal.display_ops.select_vhost_multiple"
|
2018-03-01 17:05:50 -05:00
|
|
|
with mock.patch(mock_path) as mock_select_vhs:
|
|
|
|
|
vhost = [x for x in self.config.parser.get_vhosts()
|
|
|
|
|
if 'summer.com' in x.names][0]
|
|
|
|
|
mock_select_vhs.return_value = [vhost]
|
|
|
|
|
vhs = self.config._choose_vhosts_wildcard("*.com",
|
|
|
|
|
prefer_ssl=False)
|
|
|
|
|
# Check that the dialog was called with migration.com
|
|
|
|
|
self.assertTrue(vhost in mock_select_vhs.call_args[0][0])
|
|
|
|
|
|
|
|
|
|
# And the actual returned values
|
2018-07-11 20:33:04 -04:00
|
|
|
self.assertEqual(len(vhs), 1)
|
2018-03-01 17:05:50 -05:00
|
|
|
self.assertEqual(vhs[0], vhost)
|
|
|
|
|
|
|
|
|
|
def test_deploy_cert_wildcard(self):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
mock_choose_vhosts = mock.MagicMock()
|
|
|
|
|
vhost = [x for x in self.config.parser.get_vhosts()
|
|
|
|
|
if 'geese.com' in x.names][0]
|
|
|
|
|
mock_choose_vhosts.return_value = [vhost]
|
|
|
|
|
self.config._choose_vhosts_wildcard = mock_choose_vhosts
|
2019-11-25 17:30:24 -05:00
|
|
|
mock_d = "certbot_nginx._internal.configurator.NginxConfigurator._deploy_cert"
|
2018-03-01 17:05:50 -05:00
|
|
|
with mock.patch(mock_d) as mock_dep:
|
|
|
|
|
self.config.deploy_cert("*.com", "/tmp/path",
|
|
|
|
|
"/tmp/path", "/tmp/path", "/tmp/path")
|
|
|
|
|
self.assertTrue(mock_dep.called)
|
2018-07-11 20:33:04 -04:00
|
|
|
self.assertEqual(len(mock_dep.call_args_list), 1)
|
2018-03-01 17:05:50 -05:00
|
|
|
self.assertEqual(vhost, mock_dep.call_args_list[0][0][0])
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.display_ops.select_vhost_multiple")
|
2018-03-01 17:05:50 -05:00
|
|
|
def test_deploy_cert_wildcard_no_vhosts(self, mock_dialog):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
mock_dialog.return_value = []
|
|
|
|
|
self.assertRaises(errors.PluginError,
|
|
|
|
|
self.config.deploy_cert,
|
|
|
|
|
"*.wild.cat", "/tmp/path", "/tmp/path",
|
|
|
|
|
"/tmp/path", "/tmp/path")
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.display_ops.select_vhost_multiple")
|
2018-03-01 17:05:50 -05:00
|
|
|
def test_enhance_wildcard_ocsp_after_install(self, mock_dialog):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
vhost = [x for x in self.config.parser.get_vhosts()
|
|
|
|
|
if 'geese.com' in x.names][0]
|
|
|
|
|
self.config._wildcard_vhosts["*.com"] = [vhost]
|
|
|
|
|
self.config.enhance("*.com", "staple-ocsp", "example/chain.pem")
|
|
|
|
|
self.assertFalse(mock_dialog.called)
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.display_ops.select_vhost_multiple")
|
2018-03-01 17:05:50 -05:00
|
|
|
def test_enhance_wildcard_redirect_or_ocsp_no_install(self, mock_dialog):
|
|
|
|
|
vhost = [x for x in self.config.parser.get_vhosts()
|
|
|
|
|
if 'summer.com' in x.names][0]
|
|
|
|
|
mock_dialog.return_value = [vhost]
|
|
|
|
|
self.config.enhance("*.com", "staple-ocsp", "example/chain.pem")
|
|
|
|
|
self.assertTrue(mock_dialog.called)
|
|
|
|
|
|
2019-11-25 17:30:24 -05:00
|
|
|
@mock.patch("certbot_nginx._internal.display_ops.select_vhost_multiple")
|
2018-03-01 17:05:50 -05:00
|
|
|
def test_enhance_wildcard_double_redirect(self, mock_dialog):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
vhost = [x for x in self.config.parser.get_vhosts()
|
|
|
|
|
if 'summer.com' in x.names][0]
|
|
|
|
|
self.config._wildcard_redirect_vhosts["*.com"] = [vhost]
|
|
|
|
|
self.config.enhance("*.com", "redirect")
|
|
|
|
|
self.assertFalse(mock_dialog.called)
|
|
|
|
|
|
|
|
|
|
def test_choose_vhosts_wildcard_no_ssl_filter_port(self):
|
|
|
|
|
# pylint: disable=protected-access
|
2019-11-25 17:30:24 -05:00
|
|
|
mock_path = "certbot_nginx._internal.display_ops.select_vhost_multiple"
|
2018-03-01 17:05:50 -05:00
|
|
|
with mock.patch(mock_path) as mock_select_vhs:
|
|
|
|
|
mock_select_vhs.return_value = []
|
|
|
|
|
self.config._choose_vhosts_wildcard("*.com",
|
|
|
|
|
prefer_ssl=False,
|
|
|
|
|
no_ssl_filter_port='80')
|
|
|
|
|
# Check that the dialog was called with only port 80 vhosts
|
2021-02-26 16:43:22 -05:00
|
|
|
self.assertEqual(len(mock_select_vhs.call_args[0][0]), 8)
|
|
|
|
|
|
|
|
|
|
def test_choose_auth_vhosts(self):
|
|
|
|
|
"""choose_auth_vhosts correctly selects duplicative and HTTP/HTTPS vhosts"""
|
|
|
|
|
http, https = self.config.choose_auth_vhosts('ssl.both.com')
|
|
|
|
|
self.assertEqual(len(http), 4)
|
|
|
|
|
self.assertEqual(len(https), 2)
|
|
|
|
|
self.assertEqual(http[0].names, {'ssl.both.com'})
|
|
|
|
|
self.assertEqual(http[1].names, {'ssl.both.com'})
|
|
|
|
|
self.assertEqual(http[2].names, {'ssl.both.com'})
|
|
|
|
|
self.assertEqual(http[3].names, {'*.both.com'})
|
|
|
|
|
self.assertEqual(https[0].names, {'ssl.both.com'})
|
|
|
|
|
self.assertEqual(https[1].names, {'*.both.com'})
|
2018-03-01 17:05:50 -05:00
|
|
|
|
|
|
|
|
|
2017-05-23 16:18:50 -04:00
|
|
|
class InstallSslOptionsConfTest(util.NginxTest):
|
|
|
|
|
"""Test that the options-ssl-nginx.conf file is installed and updated properly."""
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super(InstallSslOptionsConfTest, self).setUp()
|
|
|
|
|
|
2019-11-13 13:19:27 -05:00
|
|
|
self.config = self.get_nginx_configurator(
|
2017-05-23 16:18:50 -04:00
|
|
|
self.config_path, self.config_dir, self.work_dir, self.logs_dir)
|
|
|
|
|
|
|
|
|
|
def _call(self):
|
2019-06-14 16:44:50 -04:00
|
|
|
self.config.install_ssl_options_conf(self.config.mod_ssl_conf,
|
|
|
|
|
self.config.updated_mod_ssl_conf_digest)
|
2017-05-23 16:18:50 -04:00
|
|
|
|
2017-06-01 12:04:48 -04:00
|
|
|
def _current_ssl_options_hash(self):
|
2019-06-14 16:44:50 -04:00
|
|
|
return crypto_util.sha256sum(self.config.mod_ssl_conf_src)
|
2017-06-01 12:04:48 -04:00
|
|
|
|
2017-05-23 16:18:50 -04:00
|
|
|
def _assert_current_file(self):
|
|
|
|
|
self.assertTrue(os.path.isfile(self.config.mod_ssl_conf))
|
2017-06-01 12:04:48 -04:00
|
|
|
self.assertEqual(crypto_util.sha256sum(self.config.mod_ssl_conf),
|
|
|
|
|
self._current_ssl_options_hash())
|
2017-05-23 16:18:50 -04:00
|
|
|
|
|
|
|
|
def test_no_file(self):
|
|
|
|
|
# prepare should have placed a file there
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
os.remove(self.config.mod_ssl_conf)
|
|
|
|
|
self.assertFalse(os.path.isfile(self.config.mod_ssl_conf))
|
|
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
|
|
|
|
|
def test_current_file(self):
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
|
2019-06-14 16:44:50 -04:00
|
|
|
def _mock_hash_except_ssl_conf_src(self, fake_hash):
|
|
|
|
|
# Write a bad file in place so that update tests fail if no update occurs.
|
|
|
|
|
# We're going to pretend this file (the currently installed conf file)
|
|
|
|
|
# actually hashes to `fake_hash` for the update tests.
|
|
|
|
|
with open(self.config.mod_ssl_conf, "w") as f:
|
|
|
|
|
f.write("bogus")
|
|
|
|
|
sha256 = crypto_util.sha256sum
|
|
|
|
|
def _hash(filename):
|
|
|
|
|
return sha256(filename) if filename == self.config.mod_ssl_conf_src else fake_hash
|
|
|
|
|
return _hash
|
|
|
|
|
|
2017-05-23 16:18:50 -04:00
|
|
|
def test_prev_file_updates_to_current(self):
|
2019-11-25 17:30:24 -05:00
|
|
|
from certbot_nginx._internal.constants import ALL_SSL_OPTIONS_HASHES
|
2019-06-14 16:44:50 -04:00
|
|
|
with mock.patch('certbot.crypto_util.sha256sum',
|
|
|
|
|
new=self._mock_hash_except_ssl_conf_src(ALL_SSL_OPTIONS_HASHES[0])):
|
|
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
|
|
|
|
|
def test_prev_file_updates_to_current_old_nginx(self):
|
2019-11-25 17:30:24 -05:00
|
|
|
from certbot_nginx._internal.constants import ALL_SSL_OPTIONS_HASHES
|
2019-06-14 16:44:50 -04:00
|
|
|
self.config.version = (1, 5, 8)
|
|
|
|
|
with mock.patch('certbot.crypto_util.sha256sum',
|
|
|
|
|
new=self._mock_hash_except_ssl_conf_src(ALL_SSL_OPTIONS_HASHES[0])):
|
2017-05-23 16:18:50 -04:00
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
|
|
|
|
|
def test_manually_modified_current_file_does_not_update(self):
|
|
|
|
|
with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf:
|
|
|
|
|
mod_ssl_conf.write("a new line for the wrong hash\n")
|
2017-06-01 12:04:48 -04:00
|
|
|
with mock.patch("certbot.plugins.common.logger") as mock_logger:
|
2017-05-23 16:18:50 -04:00
|
|
|
self._call()
|
|
|
|
|
self.assertFalse(mock_logger.warning.called)
|
|
|
|
|
self.assertTrue(os.path.isfile(self.config.mod_ssl_conf))
|
2019-06-14 16:44:50 -04:00
|
|
|
self.assertEqual(crypto_util.sha256sum(self.config.mod_ssl_conf_src),
|
2017-06-01 12:04:48 -04:00
|
|
|
self._current_ssl_options_hash())
|
2017-05-23 16:18:50 -04:00
|
|
|
self.assertNotEqual(crypto_util.sha256sum(self.config.mod_ssl_conf),
|
2017-06-01 12:04:48 -04:00
|
|
|
self._current_ssl_options_hash())
|
2017-05-23 16:18:50 -04:00
|
|
|
|
|
|
|
|
def test_manually_modified_past_file_warns(self):
|
|
|
|
|
with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf:
|
|
|
|
|
mod_ssl_conf.write("a new line for the wrong hash\n")
|
|
|
|
|
with open(self.config.updated_mod_ssl_conf_digest, "w") as f:
|
|
|
|
|
f.write("hashofanoldversion")
|
2017-06-01 12:04:48 -04:00
|
|
|
with mock.patch("certbot.plugins.common.logger") as mock_logger:
|
2017-05-23 16:18:50 -04:00
|
|
|
self._call()
|
|
|
|
|
self.assertEqual(mock_logger.warning.call_args[0][0],
|
2017-09-01 10:57:30 -04:00
|
|
|
"%s has been manually modified; updated file "
|
2017-05-23 16:18:50 -04:00
|
|
|
"saved to %s. We recommend updating %s for security purposes.")
|
2019-06-14 16:44:50 -04:00
|
|
|
self.assertEqual(crypto_util.sha256sum(self.config.mod_ssl_conf_src),
|
2017-06-01 12:04:48 -04:00
|
|
|
self._current_ssl_options_hash())
|
2017-05-23 16:18:50 -04:00
|
|
|
# only print warning once
|
2017-06-01 12:04:48 -04:00
|
|
|
with mock.patch("certbot.plugins.common.logger") as mock_logger:
|
2017-05-23 16:18:50 -04:00
|
|
|
self._call()
|
|
|
|
|
self.assertFalse(mock_logger.warning.called)
|
|
|
|
|
|
2017-06-01 12:04:48 -04:00
|
|
|
def test_current_file_hash_in_all_hashes(self):
|
2019-11-25 17:30:24 -05:00
|
|
|
from certbot_nginx._internal.constants import ALL_SSL_OPTIONS_HASHES
|
2017-06-01 12:04:48 -04:00
|
|
|
self.assertTrue(self._current_ssl_options_hash() in ALL_SSL_OPTIONS_HASHES,
|
|
|
|
|
"Constants.ALL_SSL_OPTIONS_HASHES must be appended"
|
|
|
|
|
" with the sha256 hash of self.config.mod_ssl_conf when it is updated.")
|
|
|
|
|
|
2019-08-02 15:25:40 -04:00
|
|
|
def test_ssl_config_files_hash_in_all_hashes(self):
|
|
|
|
|
"""
|
|
|
|
|
It is really critical that all TLS Nginx config files have their SHA256 hash registered in
|
|
|
|
|
constants.ALL_SSL_OPTIONS_HASHES. Otherwise Certbot will mistakenly assume that the config
|
|
|
|
|
file has been manually edited by the user, and will refuse to update it.
|
|
|
|
|
This test ensures that all necessary hashes are present.
|
|
|
|
|
"""
|
2019-11-25 17:30:24 -05:00
|
|
|
from certbot_nginx._internal.constants import ALL_SSL_OPTIONS_HASHES
|
2019-08-02 15:25:40 -04:00
|
|
|
import pkg_resources
|
2019-08-05 18:45:08 -04:00
|
|
|
all_files = [
|
2019-11-25 17:30:24 -05:00
|
|
|
pkg_resources.resource_filename("certbot_nginx",
|
|
|
|
|
os.path.join("_internal", "tls_configs", x))
|
2019-08-05 18:45:08 -04:00
|
|
|
for x in ("options-ssl-nginx.conf",
|
|
|
|
|
"options-ssl-nginx-old.conf",
|
|
|
|
|
"options-ssl-nginx-tls12-only.conf")
|
|
|
|
|
]
|
2019-08-02 15:25:40 -04:00
|
|
|
self.assertTrue(all_files)
|
|
|
|
|
for one_file in all_files:
|
|
|
|
|
file_hash = crypto_util.sha256sum(one_file)
|
|
|
|
|
self.assertTrue(file_hash in ALL_SSL_OPTIONS_HASHES,
|
|
|
|
|
"Constants.ALL_SSL_OPTIONS_HASHES must be appended with the sha256 "
|
|
|
|
|
"hash of {0} when it is updated.".format(one_file))
|
|
|
|
|
|
|
|
|
|
def test_nginx_version_uses_correct_config(self):
|
2019-06-14 16:44:50 -04:00
|
|
|
self.config.version = (1, 5, 8)
|
2019-09-05 16:51:56 -04:00
|
|
|
self.config.openssl_version = "1.0.2g" # shouldn't matter
|
2019-06-14 16:44:50 -04:00
|
|
|
self.assertEqual(os.path.basename(self.config.mod_ssl_conf_src),
|
|
|
|
|
"options-ssl-nginx-old.conf")
|
|
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
self.config.version = (1, 5, 9)
|
2019-09-05 16:51:56 -04:00
|
|
|
self.config.openssl_version = "1.0.2l"
|
2019-08-02 15:25:40 -04:00
|
|
|
self.assertEqual(os.path.basename(self.config.mod_ssl_conf_src),
|
|
|
|
|
"options-ssl-nginx-tls12-only.conf")
|
|
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
self.config.version = (1, 13, 0)
|
2019-06-14 16:44:50 -04:00
|
|
|
self.assertEqual(os.path.basename(self.config.mod_ssl_conf_src),
|
|
|
|
|
"options-ssl-nginx.conf")
|
2019-09-05 16:51:56 -04:00
|
|
|
self._call()
|
|
|
|
|
self._assert_current_file()
|
|
|
|
|
self.config.version = (1, 13, 0)
|
|
|
|
|
self.config.openssl_version = "1.0.2k"
|
|
|
|
|
self.assertEqual(os.path.basename(self.config.mod_ssl_conf_src),
|
|
|
|
|
"options-ssl-nginx-tls13-session-tix-on.conf")
|
2019-06-14 16:44:50 -04:00
|
|
|
|
2016-09-21 18:38:37 -04:00
|
|
|
|
2018-06-25 21:09:30 -04:00
|
|
|
class DetermineDefaultServerRootTest(certbot_test_util.ConfigTestCase):
|
2019-11-25 17:30:24 -05:00
|
|
|
"""Tests for certbot_nginx._internal.configurator._determine_default_server_root."""
|
2018-06-25 21:09:30 -04:00
|
|
|
|
|
|
|
|
def _call(self):
|
2019-11-25 17:30:24 -05:00
|
|
|
from certbot_nginx._internal.configurator import _determine_default_server_root
|
2018-06-25 21:09:30 -04:00
|
|
|
return _determine_default_server_root()
|
|
|
|
|
|
|
|
|
|
@mock.patch.dict(os.environ, {"CERTBOT_DOCS": "1"})
|
|
|
|
|
def test_docs_value(self):
|
|
|
|
|
self._test(expect_both_values=True)
|
|
|
|
|
|
|
|
|
|
@mock.patch.dict(os.environ, {})
|
|
|
|
|
def test_real_values(self):
|
|
|
|
|
self._test(expect_both_values=False)
|
|
|
|
|
|
|
|
|
|
def _test(self, expect_both_values):
|
|
|
|
|
server_root = self._call()
|
|
|
|
|
|
|
|
|
|
if expect_both_values:
|
|
|
|
|
self.assertIn("/usr/local/etc/nginx", server_root)
|
|
|
|
|
self.assertIn("/etc/nginx", server_root)
|
|
|
|
|
else:
|
Lint certbot code on Python 3, and update Pylint to the latest version (#7551)
Part of #7550
This PR makes appropriate corrections to run pylint on Python 3.
Why not keeping the dependencies unchanged and just run pylint on Python 3?
Because the old version of pylint breaks horribly on Python 3 because of unsupported version of astroid.
Why updating pylint + astroid to the latest version ?
Because this version only fixes some internal errors occuring during the lint of Certbot code, and is also ready to run gracefully on Python 3.8.
Why upgrading mypy ?
Because the old version does not support the new version of astroid required to run pylint correctly.
Why not upgrading mypy to its latest version ?
Because this latest version includes a new typshed version, that adds a lot of new type definitions, and brings dozens of new errors on the Certbot codebase. I would like to fix that in a future PR.
That said so, the work has been to find the correct set of new dependency versions, then configure pylint for sane configuration errors in our situation, disable irrelevant lintings errors, then fixing (or ignoring for good reason) the remaining mypy errors.
I also made PyLint and MyPy checks run correctly on Windows.
* Start configuration
* Reconfigure travis
* Suspend a check specific to python 3. Start fixing code.
* Repair call_args
* Fix return + elif lints
* Reconfigure development to run mainly on python3
* Remove incompatible Python 3.4 jobs
* Suspend pylint in some assertions
* Remove pylint in dev
* Take first mypy that supports typed-ast>=1.4.0 to limit the migration path
* Various return + else lint errors
* Find a set of deps that is working with current mypy version
* Update local oldest requirements
* Remove all current pylint errors
* Rebuild letsencrypt-auto
* Update mypy to fix pylint with new astroid version, and fix mypy issues
* Explain type: ignore
* Reconfigure tox, fix none path
* Simplify pinning
* Remove useless directive
* Remove debugging code
* Remove continue
* Update requirements
* Disable unsubscriptable-object check
* Disable one check, enabling two more
* Plug certbot dev version for oldest requirements
* Remove useless disable directives
* Remove useless no-member disable
* Remove no-else-* checks. Use elif in symetric branches.
* Add back assertion
* Add new line
* Remove unused pylint disable
* Remove other pylint disable
2019-12-10 17:12:50 -05:00
|
|
|
self.assertTrue(server_root in ("/etc/nginx", "/usr/local/etc/nginx"))
|
2018-06-25 21:09:30 -04:00
|
|
|
|
|
|
|
|
|
2015-04-06 14:24:03 -04:00
|
|
|
if __name__ == "__main__":
|
2015-05-10 13:34:25 -04:00
|
|
|
unittest.main() # pragma: no cover
|