diff --git a/certbot-apache/certbot_apache/http_01.py b/certbot-apache/certbot_apache/http_01.py index 2b2b8e796..1dfb0b01a 100644 --- a/certbot-apache/certbot_apache/http_01.py +++ b/certbot-apache/certbot_apache/http_01.py @@ -2,6 +2,8 @@ import logging import os +from certbot import errors + from certbot.plugins import common logger = logging.getLogger(__name__) @@ -79,9 +81,8 @@ class ApacheHttp01(common.TLSSNI01): if vh: self._set_up_include_directive(vh) else: - for vh in self.configurator.vhosts: - if not vh.ssl: - self._set_up_include_directive(vh) + for vhost in self._relevant_vhosts(): + self._set_up_include_directive(vh) self.configurator.reverter.register_file_creation( True, self.challenge_conf) @@ -97,6 +98,19 @@ class ApacheHttp01(common.TLSSNI01): with open(self.challenge_conf, "w") as new_conf: new_conf.write(config_text) + def _relevant_vhosts(self): + http01_port = self.configurator.config.http01_port + relevant_vhosts = [] + for vhost in self.vhosts: + if any(a.is_wildcard() or a.get_port() == http01_port for a in vhost.addrs): + if not vhost.ssl: + relevant_vhosts.append(vhost) + if not relevant_vhosts: + raise errors.PluginError( + "Unable to find a virtual host listening on port {0}." + " Please add one.".format(http01_port)) + + return relevant_vhosts def _set_up_challenges(self): if not os.path.isdir(self.challenge_dir): diff --git a/certbot-apache/certbot_apache/tests/http_01_test.py b/certbot-apache/certbot_apache/tests/http_01_test.py index 12f571354..1afaef0c5 100644 --- a/certbot-apache/certbot_apache/tests/http_01_test.py +++ b/certbot-apache/certbot_apache/tests/http_01_test.py @@ -6,6 +6,7 @@ import unittest from acme import challenges from certbot import achallenges +from certbot import errors from certbot.tests import acme_util @@ -139,6 +140,10 @@ class ApacheHttp01Test(util.ApacheTest): domain="something.nonexistent", account_key=self.account_key)] self.common_perform_test(achalls, vhosts) + def test_no_vhost(self): + self.config.config.http01_port = 12345 + self.assertRaises(errors.PluginError, self.http.perform, self.achalls) + def common_perform_test(self, achalls, vhosts): """Tests perform with the given achalls.""" challenge_dir = self.http.challenge_dir