2015-05-10 06:47:58 -04:00
|
|
|
"""Common utilities for letsencrypt_apache."""
|
2014-12-19 18:49:29 -05:00
|
|
|
import os
|
|
|
|
|
import pkg_resources
|
|
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
2015-01-24 08:12:45 -05:00
|
|
|
import unittest
|
2014-12-19 18:49:29 -05:00
|
|
|
|
|
|
|
|
import mock
|
|
|
|
|
|
2015-05-12 17:06:17 -04:00
|
|
|
from letsencrypt import constants as core_constants
|
|
|
|
|
|
2015-05-10 06:47:58 -04:00
|
|
|
from letsencrypt_apache import configurator
|
|
|
|
|
from letsencrypt_apache import constants
|
|
|
|
|
from letsencrypt_apache import obj
|
2014-12-19 18:49:29 -05:00
|
|
|
|
|
|
|
|
|
2015-01-28 08:02:14 -05:00
|
|
|
class ApacheTest(unittest.TestCase): # pylint: disable=too-few-public-methods
|
2015-01-24 08:12:45 -05:00
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super(ApacheTest, self).setUp()
|
|
|
|
|
|
|
|
|
|
self.temp_dir, self.config_dir, self.work_dir = dir_setup(
|
|
|
|
|
"debian_apache_2_4/two_vhost_80")
|
|
|
|
|
|
2015-05-04 11:33:53 -04:00
|
|
|
self.ssl_options = setup_ssl_options(self.config_dir)
|
2015-01-24 08:12:45 -05:00
|
|
|
|
|
|
|
|
self.config_path = os.path.join(
|
2015-02-02 16:11:59 -05:00
|
|
|
self.temp_dir, "debian_apache_2_4/two_vhost_80/apache2")
|
2015-01-24 08:12:45 -05:00
|
|
|
|
|
|
|
|
self.rsa256_file = pkg_resources.resource_filename(
|
2015-05-10 07:26:21 -04:00
|
|
|
"acme.jose", "testdata/rsa256_key.pem")
|
2015-01-24 08:12:45 -05:00
|
|
|
self.rsa256_pem = pkg_resources.resource_string(
|
2015-05-10 07:26:21 -04:00
|
|
|
"acme.jose", "testdata/rsa256_key.pem")
|
2015-01-24 08:12:45 -05:00
|
|
|
|
|
|
|
|
|
2015-05-04 11:33:53 -04:00
|
|
|
def dir_setup(test_dir="debian_apache_2_4/two_vhost_80",
|
2015-05-12 17:06:17 -04:00
|
|
|
pkg="letsencrypt_apache.tests"):
|
2014-12-19 18:49:29 -05:00
|
|
|
"""Setup the directories necessary for the configurator."""
|
|
|
|
|
temp_dir = tempfile.mkdtemp("temp")
|
|
|
|
|
config_dir = tempfile.mkdtemp("config")
|
|
|
|
|
work_dir = tempfile.mkdtemp("work")
|
|
|
|
|
|
2015-05-12 17:06:17 -04:00
|
|
|
os.chmod(temp_dir, core_constants.CONFIG_DIRS_MODE)
|
|
|
|
|
os.chmod(config_dir, core_constants.CONFIG_DIRS_MODE)
|
|
|
|
|
os.chmod(work_dir, core_constants.CONFIG_DIRS_MODE)
|
Fix test dirs chmods errors.
Stragely, when run on digitalocean jessie x64 droplet (as root), the
following error were produced before this fix:
======================================================================
ERROR: test_add_name_vhost (letsencrypt.client.plugins.apache.tests.configurator_test.TwoVhost80Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/root/lets-encrypt-preview/letsencrypt/client/plugins/apache/tests/configurator_test.py", line 35, in setUp
self.ssl_options)
File "/root/lets-encrypt-preview/letsencrypt/client/plugins/apache/tests/util.py", line 76, in get_apache_configurator
version)
File "/root/lets-encrypt-preview/letsencrypt/client/plugins/apache/configurator.py", line 96, in __init__
self.verify_setup()
File "/root/lets-encrypt-preview/letsencrypt/client/plugins/apache/configurator.py", line 938, in verify_setup
self.config.config_dir, constants.CONFIG_DIRS_MODE, uid)
File "/root/lets-encrypt-preview/letsencrypt/client/le_util.py", line 37, in make_or_verify_dir
"permissions or owner" % directory)
LetsEncryptClientError: /tmp/tmp1wYWIMconfig exists, but does not have the proper permissions or owner
2015-05-04 09:48:18 -04:00
|
|
|
|
2014-12-19 18:49:29 -05:00
|
|
|
test_configs = pkg_resources.resource_filename(
|
2015-05-04 11:33:53 -04:00
|
|
|
pkg, os.path.join("testdata", test_dir))
|
2014-12-19 18:49:29 -05:00
|
|
|
|
|
|
|
|
shutil.copytree(
|
|
|
|
|
test_configs, os.path.join(temp_dir, test_dir), symlinks=True)
|
|
|
|
|
|
|
|
|
|
return temp_dir, config_dir, work_dir
|
|
|
|
|
|
|
|
|
|
|
2015-05-04 11:33:53 -04:00
|
|
|
def setup_ssl_options(
|
2015-05-12 17:06:17 -04:00
|
|
|
config_dir, mod_ssl_conf=constants.MOD_SSL_CONF):
|
2014-12-19 18:49:29 -05:00
|
|
|
"""Move the ssl_options into position and return the path."""
|
|
|
|
|
option_path = os.path.join(config_dir, "options-ssl.conf")
|
2015-05-04 11:33:53 -04:00
|
|
|
shutil.copyfile(mod_ssl_conf, option_path)
|
2014-12-19 18:49:29 -05:00
|
|
|
return option_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_apache_configurator(
|
|
|
|
|
config_path, config_dir, work_dir, ssl_options, version=(2, 4, 7)):
|
|
|
|
|
"""Create an Apache Configurator with the specified options."""
|
|
|
|
|
|
|
|
|
|
backups = os.path.join(work_dir, "backups")
|
|
|
|
|
|
2015-05-10 06:47:58 -04:00
|
|
|
with mock.patch("letsencrypt_apache.configurator."
|
2014-12-19 18:49:29 -05:00
|
|
|
"subprocess.Popen") as mock_popen:
|
|
|
|
|
# This just states that the ssl module is already loaded
|
|
|
|
|
mock_popen().communicate.return_value = ("ssl_module", "")
|
|
|
|
|
config = configurator.ApacheConfigurator(
|
2015-05-02 03:01:44 -04:00
|
|
|
config=mock.MagicMock(
|
2015-02-02 09:14:32 -05:00
|
|
|
apache_server_root=config_path,
|
|
|
|
|
apache_mod_ssl_conf=ssl_options,
|
|
|
|
|
le_vhost_ext="-le-ssl.conf",
|
|
|
|
|
backup_dir=backups,
|
|
|
|
|
config_dir=config_dir,
|
|
|
|
|
temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"),
|
|
|
|
|
in_progress_dir=os.path.join(backups, "IN_PROGRESS"),
|
|
|
|
|
work_dir=work_dir),
|
2015-05-02 03:01:44 -04:00
|
|
|
name="apache",
|
|
|
|
|
version=version)
|
2014-12-19 18:49:29 -05:00
|
|
|
|
2015-02-23 07:26:43 -05:00
|
|
|
config.prepare()
|
|
|
|
|
|
2014-12-19 18:49:29 -05:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_vh_truth(temp_dir, config_name):
|
|
|
|
|
"""Return the ground truth for the specified directory."""
|
|
|
|
|
if config_name == "debian_apache_2_4/two_vhost_80":
|
|
|
|
|
prefix = os.path.join(
|
|
|
|
|
temp_dir, config_name, "apache2/sites-available")
|
|
|
|
|
aug_pre = "/files" + prefix
|
|
|
|
|
vh_truth = [
|
|
|
|
|
obj.VirtualHost(
|
|
|
|
|
os.path.join(prefix, "encryption-example.conf"),
|
|
|
|
|
os.path.join(aug_pre, "encryption-example.conf/VirtualHost"),
|
|
|
|
|
set([obj.Addr.fromstring("*:80")]),
|
|
|
|
|
False, True, set(["encryption-example.demo"])),
|
|
|
|
|
obj.VirtualHost(
|
|
|
|
|
os.path.join(prefix, "default-ssl.conf"),
|
|
|
|
|
os.path.join(aug_pre, "default-ssl.conf/IfModule/VirtualHost"),
|
|
|
|
|
set([obj.Addr.fromstring("_default_:443")]), True, False),
|
|
|
|
|
obj.VirtualHost(
|
|
|
|
|
os.path.join(prefix, "000-default.conf"),
|
|
|
|
|
os.path.join(aug_pre, "000-default.conf/VirtualHost"),
|
|
|
|
|
set([obj.Addr.fromstring("*:80")]), False, True,
|
|
|
|
|
set(["ip-172-30-0-17"])),
|
|
|
|
|
obj.VirtualHost(
|
|
|
|
|
os.path.join(prefix, "letsencrypt.conf"),
|
|
|
|
|
os.path.join(aug_pre, "letsencrypt.conf/VirtualHost"),
|
|
|
|
|
set([obj.Addr.fromstring("*:80")]), False, True,
|
|
|
|
|
set(["letsencrypt.demo"])),
|
|
|
|
|
]
|
|
|
|
|
return vh_truth
|
|
|
|
|
|
|
|
|
|
return None
|