Merge branch 'http01-nginx' into test-http01-nginx2

This commit is contained in:
Brad Warren 2018-01-11 14:18:56 -08:00
commit 0cc1e3d4bf
4 changed files with 25 additions and 22 deletions

View file

@ -368,7 +368,7 @@ class NginxConfigurator(common.Installer):
return sorted(matches, key=lambda x: x['rank'])
def choose_redirect_vhost(self, target_name, port):
def choose_redirect_vhost(self, target_name, port, create_if_no_match=False):
"""Chooses a single virtual host for redirect enhancement.
Chooses the vhost most closely matching target_name that is
@ -382,12 +382,28 @@ class NginxConfigurator(common.Installer):
:param str target_name: domain name
:param str port: port number
:param bool create_if_no_match: If we should create a new vhost from default
when there is no match found. If we can't choose a default, raise a
MisconfigurationError.
:returns: vhost associated with name
:rtype: :class:`~certbot_nginx.obj.VirtualHost`
"""
matches = self._get_redirect_ranked_matches(target_name, port)
return self._select_best_name_match(matches)
vhost = self._select_best_name_match(matches)
if not vhost:
if create_if_no_match:
vhost = self._vhost_from_duplicated_default(target_name)
else:
# No matches. Raise a misconfiguration error.
raise errors.MisconfigurationError(
("Cannot find a VirtualHost matching domain %s. "
"In order for Certbot to correctly perform the challenge "
"please add a corresponding server_name directive to your "
"nginx configuration: "
"https://nginx.org/en/docs/http/server_names.html") % (target_name))
return vhost
def _get_redirect_ranked_matches(self, target_name, port):
"""Gets a ranked list of plaintextish port-listening vhosts matching target_name

View file

@ -3,16 +3,10 @@
import logging
import os
import six
from acme import challenges
from certbot import errors
from certbot.plugins import common
from certbot_nginx import obj
from certbot_nginx import nginxparser
logger = logging.getLogger(__name__)
@ -37,9 +31,6 @@ class NginxHttp01(common.ChallengePerformer):
"""
def __init__(self, configurator):
super(NginxHttp01, self).__init__(configurator)
def perform(self):
"""Perform a challenge on Nginx.
@ -102,7 +93,8 @@ class NginxHttp01(common.ChallengePerformer):
:class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge`
"""
vhost = self.configurator.choose_vhost(achall.domain, create_if_no_match=True)
vhost = self.configurator.choose_redirect_vhost(achall.domain,
self.configurator.config.http01_port, create_if_no_match=True)
validation = achall.validation(achall.account_key)
validation_path = self._get_validation_path(achall)

View file

@ -290,8 +290,8 @@ class NginxConfiguratorTest(util.NginxTest):
]],
parsed_migration_conf[0])
@mock.patch("certbot_nginx.configurator.nginx_challenges.NginxTlsSni01.perform")
@mock.patch("certbot_nginx.configurator.nginx_challenges.NginxHttp01.perform")
@mock.patch("certbot_nginx.configurator.tls_sni_01.NginxTlsSni01.perform")
@mock.patch("certbot_nginx.configurator.http_01.NginxHttp01.perform")
@mock.patch("certbot_nginx.configurator.NginxConfigurator.restart")
@mock.patch("certbot_nginx.configurator.NginxConfigurator.revert_challenge_config")
def test_perform_and_cleanup(self, mock_revert, mock_restart, mock_http_perform,

View file

@ -8,12 +8,10 @@ import six
from acme import challenges
from certbot import achallenges
from certbot import errors
from certbot.plugins import common_test
from certbot.tests import acme_util
from certbot_nginx import obj
from certbot_nginx.tests import util
@ -96,13 +94,10 @@ class HttpPerformTest(util.NginxTest):
self.http01.configurator.parser.load()
http = self.http01.configurator.parser.parsed[
self.http01.configurator.parser.config_root][-1]
# vhosts = self.http01.configurator.parser.get_vhosts()
vhosts = self.http01.configurator.parser.get_vhosts()
for vhost in vhosts:
pass
# for vhost in vhosts:
# pass
# if the name matches
# check that the location block is in there and is correct