Пример #1
0
def test_authenticator(plugin, config, temp_dir):
    """Tests plugin as an authenticator"""
    backup = os.path.join(temp_dir, "backup")
    shutil.copytree(config, backup, symlinks=True)

    achalls = _create_achalls(plugin)
    if achalls:
        try:
            responses = plugin.perform(achalls)
            for i in xrange(len(responses)):
                if not responses[i]:
                    raise errors.Error(
                        "Plugin returned 'None' or 'False' response to "
                        "challenge")
                elif isinstance(responses[i], challenges.DVSNIResponse):
                    if responses[i].simple_verify(achalls[i],
                                                  achalls[i].domain,
                                                  util.JWK.key.public_key(),
                                                  host="127.0.0.1",
                                                  port=plugin.https_port):
                        logger.info(
                            "Verification of DVSNI response for %s succeeded",
                            achalls[i].domain)
                    else:
                        raise errors.Error(
                            "Verification of DVSNI response for {0} "
                            "failed".format(achalls[i].domain))
        finally:
            plugin.cleanup(achalls)

    if _dirs_are_unequal(config, backup):
        raise errors.Error("Challenge cleanup failed")
    else:
        logger.info("Challenge cleanup succeeded")
Пример #2
0
    def load_config(self):
        """Loads the next configuration for the plugin to test"""
        if hasattr(self.le_config, "apache_init_script"):
            try:
                self.check_call_in_docker(
                    [self.le_config.apache_init_script, "stop"])
            except errors.Error:
                raise errors.Error(
                    "Failed to stop previous apache config from running")

        config = super(Proxy, self).load_config()
        self.modules = _get_modules(config)
        self.version = _get_version(config)
        self._all_names, self._test_names = _get_names(config)

        server_root = _get_server_root(config)
        with open(os.path.join(config, "config_file")) as f:
            config_file = os.path.join(server_root, f.readline().rstrip())
        self.test_conf = _create_test_conf(server_root, config_file)

        self.preprocess_config(server_root)
        self._prepare_configurator(server_root, config_file)

        try:
            self.check_call_in_docker(
                "apachectl -d {0} -f {1} -k start".format(
                    server_root, config_file))
        except errors.Error:
            raise errors.Error(
                "Apache failed to load {0} before tests started".format(
                    config))

        return config
Пример #3
0
    def preprocess_config(self, server_root):
        """Prepares the configuration for use in the Docker"""
        super(Proxy, self).preprocess_config(server_root)
        if self.version[1] != 4:
            raise errors.Error("Apache version not 2.4")

        with open(self.test_conf, "a") as f:
            for module in self.modules:
                if module not in STATIC_MODULES:
                    if module in SHARED_MODULES:
                        f.write(
                            "LoadModule {0}_module /usr/local/apache2/modules/"
                            "mod_{0}.so\n".format(module))
                    else:
                        raise errors.Error(
                            "Unsupported module {0}".format(module))
Пример #4
0
def _get_server_root(config):
    """Returns the server root directory in config"""
    subdirs = [
        name for name in os.listdir(config)
        if os.path.isdir(os.path.join(config, name))
    ]

    if len(subdirs) != 1:
        errors.Error("Malformed configuration directiory {0}".format(config))

    return os.path.join(config, subdirs[0].rstrip())
Пример #5
0
def extract_configs(configs, parent_dir):
    """Extracts configs to a new dir under parent_dir and returns it"""
    config_dir = os.path.join(parent_dir, "configs")

    if os.path.isdir(configs):
        shutil.copytree(configs, config_dir, symlinks=True)
    elif tarfile.is_tarfile(configs):
        with tarfile.open(configs, "r") as tar:
            tar.extractall(config_dir)
    else:
        raise errors.Error("Unknown configurations file type")

    return config_dir
Пример #6
0
def _get_version(config):
    """Return version of Apache Server.

    Version is returned as tuple. (ie. 2.4.7 = (2, 4, 7)). Code taken from
    the Apache plugin.

    """
    with open(os.path.join(config, "version")) as f:
        # Should be on first line of input
        matches = APACHE_VERSION_REGEX.findall(f.readline())

    if len(matches) != 1:
        raise errors.Error("Unable to find Apache version")

    return tuple([int(i) for i in matches[0].split(".")])
Пример #7
0
def test_installer(plugin, config, temp_dir):
    """Tests plugin as an installer"""
    backup = os.path.join(temp_dir, "backup")
    shutil.copytree(config, backup, symlinks=True)

    if plugin.get_all_names() != plugin.get_all_names_answer():
        raise errors.Error("get_all_names test failed")
    else:
        logging.info("get_all_names test succeeded")

    domains = list(plugin.get_testable_domain_names())
    cert = crypto_util.gen_ss_cert(util.KEY, domains)
    cert_path = os.path.join(temp_dir, "cert.pem")
    with open(cert_path, "w") as f:
        f.write(
            OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))

    for domain in domains:
        plugin.deploy_cert(domain, cert_path, util.KEY_PATH)
    plugin.save()
    plugin.restart()
Пример #8
0
def main():
    """Main test script execution."""
    args = get_args()
    setup_logging(args)

    if args.plugin not in PLUGINS:
        raise errors.Error("Unknown plugin {0}".format(args.plugin))

    temp_dir = tempfile.mkdtemp()
    plugin = PLUGINS[args.plugin](args)
    try:
        plugin.execute_in_docker("mkdir -p /var/log/apache2")
        while plugin.has_more_configs():
            try:
                config = plugin.load_config()
                logger.info("Loaded configuration: %s", config)
                if args.auth:
                    test_authenticator(plugin, config, temp_dir)
                #if args.install:
                #test_installer(plugin, temp_dir)
            except errors.Error as error:
                logger.warning("Test failed: %s", error)
    finally:
        plugin.cleanup_from_tests()
Пример #9
0
 def get_all_names_answer(self):
     """Returns the set of domain names that the plugin should find"""
     if self._all_names:
         return self._all_names
     else:
         raise errors.Error("No configuration file loaded")
Пример #10
0
 def get_testable_domain_names(self):
     """Returns the set of domain names that can be tested against"""
     if self._test_names:
         return self._test_names
     else:
         raise errors.Error("No configuration file loaded")