From f3126e77a714ae8acd04e49c0cf4b6e74463cf35 Mon Sep 17 00:00:00 2001 From: yan Date: Fri, 17 Apr 2015 17:57:04 -0700 Subject: [PATCH] Fix duplicate code lint errors --- .../client/plugins/nginx/configurator.py | 4 +- letsencrypt/client/plugins/nginx/dvsni.py | 45 +++++-------------- letsencrypt/client/plugins/nginx/obj.py | 17 ++----- letsencrypt/client/plugins/nginx/parser.py | 8 ++-- .../plugins/nginx/tests/configurator_test.py | 16 +++---- .../client/plugins/nginx/tests/dvsni_test.py | 12 ++--- .../client/plugins/nginx/tests/util.py | 13 ++---- 7 files changed, 40 insertions(+), 75 deletions(-) diff --git a/letsencrypt/client/plugins/nginx/configurator.py b/letsencrypt/client/plugins/nginx/configurator.py index 2caec77dc..d799432f3 100644 --- a/letsencrypt/client/plugins/nginx/configurator.py +++ b/letsencrypt/client/plugins/nginx/configurator.py @@ -338,13 +338,13 @@ class NginxConfigurator(object): Make sure that files/directories are setup with appropriate permissions Aim for defensive coding... make sure all input files - have permissions of root + have permissions of root. """ uid = os.geteuid() - le_util.make_or_verify_dir(self.config.config_dir, 0o755, uid) le_util.make_or_verify_dir(self.config.work_dir, 0o755, uid) le_util.make_or_verify_dir(self.config.backup_dir, 0o755, uid) + le_util.make_or_verify_dir(self.config.config_dir, 0o755, uid) def get_version(self): """Return version of Nginx Server. diff --git a/letsencrypt/client/plugins/nginx/dvsni.py b/letsencrypt/client/plugins/nginx/dvsni.py index 450dcf800..9535a90c7 100644 --- a/letsencrypt/client/plugins/nginx/dvsni.py +++ b/letsencrypt/client/plugins/nginx/dvsni.py @@ -1,9 +1,10 @@ """NginxDVSNI""" import logging -import os + +from letsencrypt.client.plugins.apache.dvsni import ApacheDvsni -class NginxDvsni(object): +class NginxDvsni(ApacheDvsni): """Class performs DVSNI challenges within the Nginx configurator. .. todo:: This is basically copied-and-pasted from the Apache equivalent. @@ -38,51 +39,29 @@ class NginxDvsni(object): """ - def __init__(self, configurator): - self.configurator = configurator - self.achalls = [] - self.indices = [] - self.challenge_conf = os.path.join( - configurator.config.config_dir, "le_dvsni_cert_challenge.conf") - # self.completed = 0 - - def add_chall(self, achall, idx=None): - """Add challenge to DVSNI object to perform at once. - - :param achall: Annotated DVSNI challenge. - :type achall: :class:`letsencrypt.client.achallenges.DVSNI` - - :param int idx: index to challenge in a larger array - - """ - self.achalls.append(achall) - if idx is not None: - self.indices.append(idx) - def perform(self): - """Peform a DVSNI challenge.""" + """Perform a DVSNI challenge on Nginx.""" if not self.achalls: return [] - # Save any changes to the configuration as a precaution - # About to make temporary changes to the config + self.configurator.save() addresses = [] - default_addr = "*:443" + # default_addr = "*:443" for achall in self.achalls: vhost = self.configurator.choose_vhost(achall.domain) if vhost is None: logging.error( - "No vhost exists with servername or alias of: %s", + "No nginx vhost exists with servername or alias of: %s", achall.domain) - logging.error("No _default_:443 vhost exists") + logging.error("No default 443 nginx vhost exists") logging.error("Please specify servernames in the Nginx config") return None - for addr in vhost.addrs: - if "_default_" == addr.get_addr(): - addresses.append([default_addr]) - break + # for addr in vhost.addrs: + # if "_default_" == addr.get_addr(): + # addresses.append([default_addr]) + # break else: addresses.append(list(vhost.addrs)) diff --git a/letsencrypt/client/plugins/nginx/obj.py b/letsencrypt/client/plugins/nginx/obj.py index 3509c16f9..acaacb3b0 100644 --- a/letsencrypt/client/plugins/nginx/obj.py +++ b/letsencrypt/client/plugins/nginx/obj.py @@ -1,8 +1,10 @@ """Module contains classes used by the Nginx Configurator.""" import re +from letsencrypt.client.plugins.apache.obj import Addr as ApacheAddr -class Addr(object): + +class Addr(ApacheAddr): """Represents an Nginx address, i.e. what comes after the 'listen' directive. @@ -24,7 +26,7 @@ class Addr(object): """ def __init__(self, host, port, ssl, default): - self.tup = (host, port) + super(Addr, self).__init__((host, port)) self.ssl = ssl self.default = default @@ -79,17 +81,6 @@ class Addr(object): self.default == other.default) return False - def __hash__(self): - return hash(self.tup) - - def get_addr(self): - """Return addr part of Addr object.""" - return self.tup[0] - - def get_port(self): - """Return port.""" - return self.tup[1] - class VirtualHost(object): # pylint: disable=too-few-public-methods """Represents an Nginx Virtualhost. diff --git a/letsencrypt/client/plugins/nginx/parser.py b/letsencrypt/client/plugins/nginx/parser.py index dca022022..55a0b01e8 100644 --- a/letsencrypt/client/plugins/nginx/parser.py +++ b/letsencrypt/client/plugins/nginx/parser.py @@ -178,10 +178,10 @@ class NginxParser(object): root = self._find_config_root() default = root - temp = os.path.join(self.root, "ports.conf") - if os.path.isfile(temp): - listen = temp - name = temp + nginx_temp = os.path.join(self.root, "nginx_ports.conf") + if os.path.isfile(nginx_temp): + listen = nginx_temp + name = nginx_temp else: listen = default name = default diff --git a/letsencrypt/client/plugins/nginx/tests/configurator_test.py b/letsencrypt/client/plugins/nginx/tests/configurator_test.py index 35c2573ef..225ab1610 100644 --- a/letsencrypt/client/plugins/nginx/tests/configurator_test.py +++ b/letsencrypt/client/plugins/nginx/tests/configurator_test.py @@ -167,18 +167,18 @@ class NginxConfiguratorTest(util.NginxTest): auth_key = le_util.Key(self.rsa256_file, self.rsa256_pem) achall1 = achallenges.DVSNI( chall=challenges.DVSNI( - r="jIq_Xy1mXGN37tb4L6Xj_es58fW571ZNyXekdZzhh7Q", - nonce="37bc5eb75d3e00a19b4f6355845e5a18"), - domain="encryption-example.demo", key=auth_key) + r="foo", + nonce="bar"), + domain="localhost", key=auth_key) achall2 = achallenges.DVSNI( chall=challenges.DVSNI( - r="uqnaPzxtrndteOqtrXb0Asl5gOJfWAnnx6QJyvcmlDU", - nonce="59ed014cac95f77057b1d7a1b2c596ba"), - domain="letsencrypt.demo", key=auth_key) + r="abc", + nonce="def"), + domain="example.com", key=auth_key) dvsni_ret_val = [ - challenges.DVSNIResponse(s="randomS1"), - challenges.DVSNIResponse(s="randomS2"), + challenges.DVSNIResponse(s="irrelevant"), + challenges.DVSNIResponse(s="arbitrary"), ] mock_dvsni_perform.return_value = dvsni_ret_val diff --git a/letsencrypt/client/plugins/nginx/tests/dvsni_test.py b/letsencrypt/client/plugins/nginx/tests/dvsni_test.py index 98fefebe1..a6dfac2e2 100644 --- a/letsencrypt/client/plugins/nginx/tests/dvsni_test.py +++ b/letsencrypt/client/plugins/nginx/tests/dvsni_test.py @@ -23,21 +23,21 @@ class DvsniPerformTest(util.NginxTest): self.config_path, self.config_dir, self.work_dir, self.ssl_options) - from letsencrypt.client.plugins.nginx import dvsni - self.sni = dvsni.NginxDvsni(config) - rsa256_file = pkg_resources.resource_filename( "letsencrypt.client.tests", "testdata/rsa256_key.pem") rsa256_pem = pkg_resources.resource_string( "letsencrypt.client.tests", "testdata/rsa256_key.pem") auth_key = le_util.Key(rsa256_file, rsa256_pem) + + from letsencrypt.client.plugins.nginx import dvsni + self.sni = dvsni.NginxDvsni(config) + self.achalls = [ achallenges.DVSNI( chall=challenges.DVSNI( - r="\x8c\x8a\xbf_-f\\cw\xee\xd6\xf8/\xa5\xe3\xfd\xeb9\xf1" - "\xf5\xb9\xefVM\xc9w\xa4u\x9c\xe1\x87\xb4", - nonce="7\xbc^\xb7]>\x00\xa1\x9bOcU\x84^Z\x18", + r="foo", + nonce="bar", ), domain="www.example.com", key=auth_key), achallenges.DVSNI( chall=challenges.DVSNI( diff --git a/letsencrypt/client/plugins/nginx/tests/util.py b/letsencrypt/client/plugins/nginx/tests/util.py index 4a4502379..4570f2de2 100644 --- a/letsencrypt/client/plugins/nginx/tests/util.py +++ b/letsencrypt/client/plugins/nginx/tests/util.py @@ -66,16 +66,11 @@ def get_nginx_configurator( config = configurator.NginxConfigurator( mock.MagicMock( - nginx_server_root=config_path, - nginx_mod_ssl_conf=ssl_options, - le_vhost_ext="-le-ssl.conf", - backup_dir=backups, - config_dir=config_dir, + nginx_server_root=config_path, nginx_mod_ssl_conf=ssl_options, + le_vhost_ext="-le-ssl.conf", backup_dir=backups, + config_dir=config_dir, work_dir=work_dir, temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"), - in_progress_dir=os.path.join(backups, "IN_PROGRESS"), - work_dir=work_dir), + in_progress_dir=os.path.join(backups, "IN_PROGRESS")), version) - config.prepare() - return config