From 022f9317a59be32d0db16c2ec709183b9b4c4820 Mon Sep 17 00:00:00 2001 From: Erica Portnoy Date: Wed, 4 Oct 2017 12:19:59 -0700 Subject: [PATCH] address multiple default_server problem --- certbot-nginx/certbot_nginx/configurator.py | 11 +++++++++-- .../certbot_nginx/tests/configurator_test.py | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/certbot-nginx/certbot_nginx/configurator.py b/certbot-nginx/certbot_nginx/configurator.py index 1e3335194..fc1985654 100644 --- a/certbot-nginx/certbot_nginx/configurator.py +++ b/certbot-nginx/certbot_nginx/configurator.py @@ -271,12 +271,19 @@ class NginxConfigurator(common.Installer): def _get_default_vhost(self): vhost_list = self.parser.get_vhosts() # if one has default_server set, return that one + default_vhosts = [] for vhost in vhost_list: for addr in vhost.addrs: if addr.default: - return vhost + default_vhosts.append(vhost) - raise errors.MisconfigurationError("No server blocks found in Nginx configuration.") + if len(default_vhosts) == 1: + return default_vhosts[0] + + # TODO: present a list of vhosts for user to choose from + + raise errors.MisconfigurationError("Could not automatically find a matching server" + " block. Set the `server_name` directive to use the Nginx installer.") def _get_ranked_matches(self, target_name): """Returns a ranked list of vhosts that match target_name. diff --git a/certbot-nginx/certbot_nginx/tests/configurator_test.py b/certbot-nginx/certbot_nginx/tests/configurator_test.py index dc0875430..96991d07c 100644 --- a/certbot-nginx/certbot_nginx/tests/configurator_test.py +++ b/certbot-nginx/certbot_nginx/tests/configurator_test.py @@ -604,6 +604,12 @@ class NginxConfiguratorTest(util.NginxTest): "www.nomatch.com", "example/cert.pem", "example/key.pem", "example/chain.pem", "example/fullchain.pem") + def test_deploy_no_match_fail_multiple_defaults(self): + self.config.version = (1, 3, 1) + self.assertRaises(errors.MisconfigurationError, self.config.deploy_cert, + "www.nomatch.com", "example/cert.pem", "example/key.pem", + "example/chain.pem", "example/fullchain.pem") + def test_deploy_no_match_add_redirect(self): default_conf = self.config.parser.abs_path('sites-enabled/default') foo_conf = self.config.parser.abs_path('foo.conf')