Fix duplicate code lint errors

This commit is contained in:
yan 2015-04-17 17:57:04 -07:00
parent 995b5622f8
commit f3126e77a7
7 changed files with 40 additions and 75 deletions

View file

@ -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.

View file

@ -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))

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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(

View file

@ -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