certbot/certbot-nginx/certbot_nginx/tests/util.py

136 lines
4.7 KiB
Python
Raw Permalink Normal View History

"""Common utilities for certbot_nginx."""
2016-06-18 17:52:07 -04:00
import copy
2019-04-02 16:48:22 -04:00
import shutil
import tempfile
2015-03-23 13:53:44 -04:00
import unittest
import warnings
2015-03-23 13:53:44 -04:00
import josepy as jose
2015-03-23 13:53:44 -04:00
import mock
2019-04-02 16:48:22 -04:00
import pkg_resources
import zope.component
2015-03-23 13:53:44 -04:00
from certbot import configuration
2019-04-12 16:32:52 -04:00
from certbot.compat import os
from certbot.plugins import common
2019-04-02 16:48:22 -04:00
from certbot.tests import util as test_util
from certbot_nginx import configurator
2016-07-14 21:15:01 -04:00
from certbot_nginx import nginxparser
2015-03-23 13:53:44 -04:00
class NginxTest(unittest.TestCase): # pylint: disable=too-few-public-methods
def setUp(self):
super(NginxTest, self).setUp()
self.temp_dir, self.config_dir, self.work_dir = common.dir_setup(
"etc_nginx", "certbot_nginx.tests")
self.logs_dir = tempfile.mkdtemp('logs')
2015-03-23 13:53:44 -04:00
self.config_path = os.path.join(self.temp_dir, "etc_nginx")
2015-03-23 13:53:44 -04:00
2015-08-05 18:39:31 -04:00
self.rsa512jwk = jose.JWKRSA.load(test_util.load_vector(
"rsa512_key.pem"))
def tearDown(self):
# On Windows we have various files which are not correctly closed at the time of tearDown.
# For know, we log them until a proper file close handling is written.
# Useful for development only, so no warning when we are on a CI process.
def onerror_handler(_, path, excinfo):
"""On error handler"""
if not os.environ.get('APPVEYOR'): # pragma: no cover
message = ('Following error occurred when deleting path {0}'
'during tearDown process: {1}'.format(path, str(excinfo)))
warnings.warn(message)
shutil.rmtree(self.temp_dir, onerror=onerror_handler)
shutil.rmtree(self.config_dir, onerror=onerror_handler)
shutil.rmtree(self.work_dir, onerror=onerror_handler)
shutil.rmtree(self.logs_dir, onerror=onerror_handler)
def get_data_filename(filename):
2015-04-17 20:05:00 -04:00
"""Gets the filename of a test data file."""
return pkg_resources.resource_filename(
"certbot_nginx.tests", os.path.join(
"testdata", "etc_nginx", filename))
2015-03-23 13:53:44 -04:00
def get_nginx_configurator(
config_path, config_dir, work_dir, logs_dir, version=(1, 6, 2)):
2015-03-23 13:53:44 -04:00
"""Create an Nginx Configurator with the specified options."""
backups = os.path.join(work_dir, "backups")
with mock.patch("certbot_nginx.configurator.NginxConfigurator."
"config_test"):
2016-05-26 16:51:56 -04:00
with mock.patch("certbot_nginx.configurator.util."
"exe_exists") as mock_exe_exists:
mock_exe_exists.return_value = True
config = configurator.NginxConfigurator(
config=mock.MagicMock(
nginx_server_root=config_path,
le_vhost_ext="-le-ssl.conf",
config_dir=config_dir,
work_dir=work_dir,
logs_dir=logs_dir,
backup_dir=backups,
temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"),
in_progress_dir=os.path.join(backups, "IN_PROGRESS"),
server="https://acme-server.org:443/new",
http01_port=80,
Remove tls-sni related flags in cli. Add a deprecation warning instead. (#6853) This PR is a part of the tls-sni-01 removal plan described in #6849. This PR removes --tls-sni-01-port, --tls-sni-01-address and tls-sni-01/tls-sni options from --preferred-challenges. They are replace by deprecation warning, indicating that these options will be removed soon. This deprecation, instead of complete removal, is done to avoid certbot instances to hard fail if some automated scripts still use these flags for some users. Once this PR lands, we can remove completely theses flags in one or two release. * Remove tls-sni related flags in cli. Add a deprecation warning instead. * Adapt tests to cli and renewal towards tls-sni flags deprecation * Add https_port option. Make tls_sni_01_port show a deprecation warning, but silently modify https_port if set * Migrate last items * Fix lint * Update certbot/cli.py Co-Authored-By: adferrand <adferrand@users.noreply.github.com> * Ensure to remove all occurences of tls-sni-01 * Remove unused parameter * Revert modifications on cli-help.txt * Use logger.warning instead of sys.stderr * Update the logger warning message * Remove standalone_supported_challenges option. * Fix order of preferred-challenges * Remove supported_challenges property * Fix some tests * Fix lint * Fix tests * Add a changelog * Clean code, fix test * Update CI * Reload * No hard date for tls-sni removal * Remove useless cast to list * Update certbot/tests/renewal_test.py Co-Authored-By: adferrand <adferrand@users.noreply.github.com> * Add entry to the changelog * Add entry to the changelog
2019-03-26 20:46:32 -04:00
https_port=5001,
),
name="nginx",
version=version)
config.prepare()
2015-09-16 21:52:11 -04:00
# Provide general config utility.
nsconfig = configuration.NamespaceConfig(config.config)
zope.component.provideUtility(nsconfig)
2015-09-16 21:52:11 -04:00
2015-03-23 13:53:44 -04:00
return config
def filter_comments(tree):
"""Filter comment nodes from parsed configurations."""
def traverse(tree):
"""Generator dropping comment nodes"""
2016-06-18 17:52:07 -04:00
for entry in tree:
2016-07-14 21:15:01 -04:00
# key, values = entry
spaceless = [e for e in entry if not nginxparser.spacey(e)]
if spaceless:
key = spaceless[0]
values = spaceless[1] if len(spaceless) > 1 else None
else:
key = values = ""
if isinstance(key, list):
2016-06-18 17:52:07 -04:00
new = copy.deepcopy(entry)
new[1] = filter_comments(values)
yield new
else:
2016-07-14 21:15:01 -04:00
if key != '#' and spaceless:
yield spaceless
return list(traverse(tree))
2015-10-11 14:28:39 -04:00
def contains_at_depth(haystack, needle, n):
2015-10-11 14:28:39 -04:00
"""Is the needle in haystack at depth n?
Return true if the needle is present in one of the sub-iterables in haystack
at depth n. Haystack must be an iterable.
"""
2015-10-11 15:19:39 -04:00
# Specifically use hasattr rather than isinstance(..., collections.Iterable)
# because we want to include lists but reject strings.
if not hasattr(haystack, '__iter__') or hasattr(haystack, 'strip'):
return False
if n == 0:
return needle in haystack
for item in haystack:
if contains_at_depth(item, needle, n - 1):
return True
return False