2015-05-02 03:01:44 -04:00
|
|
|
"""Plugin common functions."""
|
2015-06-02 09:55:16 -04:00
|
|
|
import os
|
2015-07-31 02:14:58 -04:00
|
|
|
import re
|
2015-06-02 09:55:16 -04:00
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
|
|
|
|
|
2015-10-04 16:13:00 -04:00
|
|
|
import OpenSSL
|
2016-02-07 15:58:35 -05:00
|
|
|
import pkg_resources
|
2015-05-02 03:01:44 -04:00
|
|
|
import zope.interface
|
|
|
|
|
|
2015-05-10 07:26:21 -04:00
|
|
|
from acme.jose import util as jose_util
|
2015-05-02 03:01:44 -04:00
|
|
|
|
2015-06-02 09:55:16 -04:00
|
|
|
from letsencrypt import constants
|
2015-05-10 08:25:29 -04:00
|
|
|
from letsencrypt import interfaces
|
2015-07-09 07:17:22 -04:00
|
|
|
from letsencrypt import le_util
|
2015-05-02 03:01:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def option_namespace(name):
|
|
|
|
|
"""ArgumentParser options namespace (prefix of all options)."""
|
2015-05-02 07:19:46 -04:00
|
|
|
return name + "-"
|
2015-05-02 03:01:44 -04:00
|
|
|
|
2015-09-06 05:20:11 -04:00
|
|
|
|
2015-05-02 03:01:44 -04:00
|
|
|
def dest_namespace(name):
|
|
|
|
|
"""ArgumentParser dest namespace (prefix of all destinations)."""
|
2015-08-17 15:50:36 -04:00
|
|
|
return name.replace("-", "_") + "_"
|
2015-05-02 03:01:44 -04:00
|
|
|
|
2015-09-30 15:09:38 -04:00
|
|
|
private_ips_regex = re.compile(
|
2015-07-31 02:14:58 -04:00
|
|
|
r"(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|"
|
|
|
|
|
r"(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)")
|
2015-09-30 15:09:38 -04:00
|
|
|
hostname_regex = re.compile(
|
2015-07-31 02:14:58 -04:00
|
|
|
r"^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*[a-z]+$", re.IGNORECASE)
|
|
|
|
|
|
2015-05-02 03:01:44 -04:00
|
|
|
|
2016-02-20 01:08:40 -05:00
|
|
|
@zope.interface.implementer(interfaces.IPlugin)
|
2015-05-02 03:01:44 -04:00
|
|
|
class Plugin(object):
|
|
|
|
|
"""Generic plugin."""
|
2016-02-20 01:08:40 -05:00
|
|
|
# provider is not inherited, subclasses must define it on their own
|
|
|
|
|
# @zope.interface.provider(interfaces.IPluginFactory)
|
2015-05-02 03:01:44 -04:00
|
|
|
|
|
|
|
|
def __init__(self, config, name):
|
|
|
|
|
self.config = config
|
|
|
|
|
self.name = name
|
|
|
|
|
|
2016-02-09 20:52:30 -05:00
|
|
|
@jose_util.abstractclassmethod
|
|
|
|
|
def add_parser_arguments(cls, add):
|
|
|
|
|
"""Add plugin arguments to the CLI argument parser.
|
|
|
|
|
|
2016-03-31 23:11:53 -04:00
|
|
|
NOTE: If some of your flags interact with others, you can
|
|
|
|
|
use cli.report_config_interaction to register this to ensure
|
|
|
|
|
values are correctly saved/overridable during renewal.
|
|
|
|
|
|
2016-02-09 20:52:30 -05:00
|
|
|
:param callable add: Function that proxies calls to
|
|
|
|
|
`argparse.ArgumentParser.add_argument` prepending options
|
|
|
|
|
with unique plugin name prefix.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def inject_parser_options(cls, parser, name):
|
|
|
|
|
"""Inject parser options.
|
|
|
|
|
|
|
|
|
|
See `~.IPlugin.inject_parser_options` for docs.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# dummy function, doesn't check if dest.startswith(self.dest_namespace)
|
|
|
|
|
def add(arg_name_no_prefix, *args, **kwargs):
|
|
|
|
|
# pylint: disable=missing-docstring
|
|
|
|
|
return parser.add_argument(
|
|
|
|
|
"--{0}{1}".format(option_namespace(name), arg_name_no_prefix),
|
|
|
|
|
*args, **kwargs)
|
|
|
|
|
return cls.add_parser_arguments(add)
|
|
|
|
|
|
2015-05-02 03:01:44 -04:00
|
|
|
@property
|
|
|
|
|
def option_namespace(self):
|
2015-05-02 07:19:46 -04:00
|
|
|
"""ArgumentParser options namespace (prefix of all options)."""
|
2015-05-02 03:01:44 -04:00
|
|
|
return option_namespace(self.name)
|
|
|
|
|
|
2015-09-06 10:03:21 -04:00
|
|
|
def option_name(self, name):
|
|
|
|
|
"""Option name (include plugin namespace)."""
|
|
|
|
|
return self.option_namespace + name
|
|
|
|
|
|
2015-05-02 03:01:44 -04:00
|
|
|
@property
|
|
|
|
|
def dest_namespace(self):
|
2015-05-02 07:19:46 -04:00
|
|
|
"""ArgumentParser dest namespace (prefix of all destinations)."""
|
2015-05-02 03:01:44 -04:00
|
|
|
return dest_namespace(self.name)
|
|
|
|
|
|
|
|
|
|
def dest(self, var):
|
|
|
|
|
"""Find a destination for given variable ``var``."""
|
|
|
|
|
# this should do exactly the same what ArgumentParser(arg),
|
|
|
|
|
# does to "arg" to compute "dest"
|
2015-05-02 07:19:46 -04:00
|
|
|
return self.dest_namespace + var.replace("-", "_")
|
2015-05-02 03:01:44 -04:00
|
|
|
|
|
|
|
|
def conf(self, var):
|
|
|
|
|
"""Find a configuration value for variable ``var``."""
|
|
|
|
|
return getattr(self.config, self.dest(var))
|
2015-06-02 09:55:16 -04:00
|
|
|
# other
|
|
|
|
|
|
2015-09-06 05:20:11 -04:00
|
|
|
|
2015-06-02 09:55:16 -04:00
|
|
|
class Addr(object):
|
|
|
|
|
r"""Represents an virtual host address.
|
|
|
|
|
|
|
|
|
|
:param str addr: addr part of vhost address
|
|
|
|
|
:param str port: port number or \*, or ""
|
|
|
|
|
|
|
|
|
|
"""
|
2016-03-20 12:09:43 -04:00
|
|
|
def __init__(self, tup, ipv6=False):
|
2015-06-02 09:55:16 -04:00
|
|
|
self.tup = tup
|
2016-03-20 12:09:43 -04:00
|
|
|
self.ipv6 = ipv6
|
2015-06-02 09:55:16 -04:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def fromstring(cls, str_addr):
|
|
|
|
|
"""Initialize Addr from string."""
|
2016-02-14 16:21:25 -05:00
|
|
|
if str_addr.startswith('['):
|
|
|
|
|
# ipv6 addresses starts with [
|
|
|
|
|
endIndex = str_addr.rfind(']')
|
|
|
|
|
host = str_addr[:endIndex + 1]
|
|
|
|
|
port = ''
|
2016-02-19 17:40:44 -05:00
|
|
|
if len(str_addr) > endIndex + 2 and str_addr[endIndex + 1] == ':':
|
|
|
|
|
port = str_addr[endIndex + 2:]
|
2016-03-25 11:48:13 -04:00
|
|
|
return cls((host, port), ipv6=True)
|
2016-02-14 16:21:25 -05:00
|
|
|
else:
|
|
|
|
|
tup = str_addr.partition(':')
|
|
|
|
|
return cls((tup[0], tup[2]))
|
2015-06-02 09:55:16 -04:00
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
if self.tup[1]:
|
|
|
|
|
return "%s:%s" % self.tup
|
|
|
|
|
return self.tup[0]
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
if isinstance(other, self.__class__):
|
2016-03-20 12:09:43 -04:00
|
|
|
if self.ipv6:
|
2016-03-22 04:23:14 -04:00
|
|
|
# compare normalized to take different
|
|
|
|
|
# styles of representation into account
|
2016-03-20 12:09:43 -04:00
|
|
|
return (other.ipv6 and
|
|
|
|
|
self._normalize_ipv6(self.tup[0]) ==
|
|
|
|
|
self._normalize_ipv6(other.tup[0]) and
|
|
|
|
|
self.tup[1] == other.tup[1])
|
|
|
|
|
else:
|
|
|
|
|
return self.tup == other.tup
|
|
|
|
|
|
2015-06-02 09:55:16 -04:00
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
def get_addr_obj(self, port):
|
|
|
|
|
"""Return new address object with same addr and new port."""
|
2016-03-20 12:09:43 -04:00
|
|
|
return self.__class__((self.tup[0], port), self.ipv6)
|
|
|
|
|
|
|
|
|
|
def _normalize_ipv6(self, addr):
|
|
|
|
|
"""Return IPv6 address in normalized form, helper function"""
|
|
|
|
|
addr = addr.lstrip("[")
|
|
|
|
|
addr = addr.rstrip("]")
|
|
|
|
|
return self._explode_ipv6(addr)
|
|
|
|
|
|
|
|
|
|
def get_ipv6_exploded(self):
|
|
|
|
|
"""Return IPv6 in normalized form"""
|
|
|
|
|
if self.ipv6:
|
|
|
|
|
return ":".join(self._normalize_ipv6(self.tup[0]))
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
def _explode_ipv6(self, addr):
|
|
|
|
|
"""Explode IPv6 address for comparison"""
|
|
|
|
|
result = ['0', '0', '0', '0', '0', '0', '0', '0']
|
|
|
|
|
addr_list = addr.split(":")
|
2016-03-25 11:48:13 -04:00
|
|
|
if len(addr_list) > len(result):
|
|
|
|
|
# too long, truncate
|
|
|
|
|
addr_list = addr_list[0:len(result)]
|
2016-03-20 12:09:43 -04:00
|
|
|
append_to_end = False
|
|
|
|
|
for i in range(0, len(addr_list)):
|
|
|
|
|
block = addr_list[i]
|
|
|
|
|
if len(block) == 0:
|
2016-03-22 04:23:14 -04:00
|
|
|
# encountered ::, so rest of the blocks should be
|
|
|
|
|
# appended to the end
|
2016-03-20 12:09:43 -04:00
|
|
|
append_to_end = True
|
|
|
|
|
continue
|
|
|
|
|
elif len(block) > 1:
|
2016-03-22 04:23:14 -04:00
|
|
|
# remove leading zeros
|
2016-03-20 12:09:43 -04:00
|
|
|
block = block.lstrip("0")
|
|
|
|
|
if not append_to_end:
|
|
|
|
|
result[i] = str(block)
|
|
|
|
|
else:
|
2016-03-22 04:23:14 -04:00
|
|
|
# count the location from the end using negative indices
|
2016-03-20 12:09:43 -04:00
|
|
|
result[i-len(addr_list)] = str(block)
|
|
|
|
|
return result
|
2015-06-02 09:55:16 -04:00
|
|
|
|
|
|
|
|
|
2015-11-07 13:10:56 -05:00
|
|
|
class TLSSNI01(object):
|
2015-11-25 12:44:17 -05:00
|
|
|
"""Abstract base for TLS-SNI-01 challenge performers"""
|
2015-06-02 09:55:16 -04:00
|
|
|
|
|
|
|
|
def __init__(self, configurator):
|
|
|
|
|
self.configurator = configurator
|
|
|
|
|
self.achalls = []
|
|
|
|
|
self.indices = []
|
|
|
|
|
self.challenge_conf = os.path.join(
|
2015-11-07 13:10:56 -05:00
|
|
|
configurator.config.config_dir, "le_tls_sni_01_cert_challenge.conf")
|
2015-06-02 09:55:16 -04:00
|
|
|
# self.completed = 0
|
|
|
|
|
|
|
|
|
|
def add_chall(self, achall, idx=None):
|
2015-11-07 13:10:56 -05:00
|
|
|
"""Add challenge to TLSSNI01 object to perform at once.
|
2015-06-02 09:55:16 -04:00
|
|
|
|
2015-11-07 13:10:56 -05:00
|
|
|
:param .KeyAuthorizationAnnotatedChallenge achall: Annotated
|
|
|
|
|
TLSSNI01 challenge.
|
2015-06-02 09:55:16 -04:00
|
|
|
|
|
|
|
|
:param int idx: index to challenge in a larger array
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
self.achalls.append(achall)
|
|
|
|
|
if idx is not None:
|
|
|
|
|
self.indices.append(idx)
|
|
|
|
|
|
2015-07-09 04:19:54 -04:00
|
|
|
def get_cert_path(self, achall):
|
2015-06-02 09:55:16 -04:00
|
|
|
"""Returns standardized name for challenge certificate.
|
|
|
|
|
|
2015-11-07 13:10:56 -05:00
|
|
|
:param .KeyAuthorizationAnnotatedChallenge achall: Annotated
|
|
|
|
|
tls-sni-01 challenge.
|
2015-06-02 09:55:16 -04:00
|
|
|
|
|
|
|
|
:returns: certificate file name
|
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
|
|
"""
|
2015-07-31 18:54:25 -04:00
|
|
|
return os.path.join(self.configurator.config.work_dir,
|
|
|
|
|
achall.chall.encode("token") + ".crt")
|
2015-06-02 09:55:16 -04:00
|
|
|
|
Rewrite acccounts and registration.
Save accounts to:
/etc/letsencrypt/accounts/www.letsencrypt-dmeo.org/acme/new-reg/ \
kuba.le.wtf@2015-07-04T14:04:10Z/ \
{regr.json,meta.json,private_key.json}
Account now represents a combination of private key, Registration
Resource and client account metadata. `Account.id` based on the
account metadata (creation host and datetime). UI interface
(`cli._determine_account`) based on the `id`, and not on email as
previously.
Add `AccountStorage` interface and `AccountFileStorage`,
`AccountMemoryStorage` implementations (latter, in-memory, useful for
testing).
Create Account only after Registration Resource is received
(`register()` returns `Account`).
Allow `client.Client(..., acme=acme, ...)`: API client might reuse
acme.client.Client as returned by `register()`.
Move report_new_account to letsencrypt.account, client.Client.register
into client.register.
Use Registration.from_data acme API.
achallenges.AChallenge.key is now the `acme.jose.JWK`, not
`le_util.Key`. Plugins have to export PEM/DER as necessary
(c.f. `letsencrypt.plugins.common.Dvsni.get_key_path`)
Add --agree-tos, save --agree-eula to "args.eula". Prompt for EULA as
soon as client is launched, add prompt for TOS.
Remove unnecessary letsencrypt.network. Remove, now irrelevant,
`IConfig.account_keys_dir`.
Based on the draft from
https://github.com/letsencrypt/letsencrypt/pull/362#issuecomment-97946817.
2015-07-04 02:46:36 -04:00
|
|
|
def get_key_path(self, achall):
|
|
|
|
|
"""Get standardized path to challenge key."""
|
2015-07-31 18:54:25 -04:00
|
|
|
return os.path.join(self.configurator.config.work_dir,
|
|
|
|
|
achall.chall.encode("token") + '.pem')
|
Rewrite acccounts and registration.
Save accounts to:
/etc/letsencrypt/accounts/www.letsencrypt-dmeo.org/acme/new-reg/ \
kuba.le.wtf@2015-07-04T14:04:10Z/ \
{regr.json,meta.json,private_key.json}
Account now represents a combination of private key, Registration
Resource and client account metadata. `Account.id` based on the
account metadata (creation host and datetime). UI interface
(`cli._determine_account`) based on the `id`, and not on email as
previously.
Add `AccountStorage` interface and `AccountFileStorage`,
`AccountMemoryStorage` implementations (latter, in-memory, useful for
testing).
Create Account only after Registration Resource is received
(`register()` returns `Account`).
Allow `client.Client(..., acme=acme, ...)`: API client might reuse
acme.client.Client as returned by `register()`.
Move report_new_account to letsencrypt.account, client.Client.register
into client.register.
Use Registration.from_data acme API.
achallenges.AChallenge.key is now the `acme.jose.JWK`, not
`le_util.Key`. Plugins have to export PEM/DER as necessary
(c.f. `letsencrypt.plugins.common.Dvsni.get_key_path`)
Add --agree-tos, save --agree-eula to "args.eula". Prompt for EULA as
soon as client is launched, add prompt for TOS.
Remove unnecessary letsencrypt.network. Remove, now irrelevant,
`IConfig.account_keys_dir`.
Based on the draft from
https://github.com/letsencrypt/letsencrypt/pull/362#issuecomment-97946817.
2015-07-04 02:46:36 -04:00
|
|
|
|
2015-11-07 13:10:56 -05:00
|
|
|
def _setup_challenge_cert(self, achall, cert_key=None):
|
2015-09-30 15:09:38 -04:00
|
|
|
|
2015-06-02 09:55:16 -04:00
|
|
|
"""Generate and write out challenge certificate."""
|
2015-07-09 04:19:54 -04:00
|
|
|
cert_path = self.get_cert_path(achall)
|
2015-07-10 03:25:29 -04:00
|
|
|
key_path = self.get_key_path(achall)
|
2015-06-02 09:55:16 -04:00
|
|
|
# Register the path before you write out the file
|
2015-07-10 03:25:29 -04:00
|
|
|
self.configurator.reverter.register_file_creation(True, key_path)
|
2015-06-02 09:55:16 -04:00
|
|
|
self.configurator.reverter.register_file_creation(True, cert_path)
|
|
|
|
|
|
2015-11-07 13:10:56 -05:00
|
|
|
response, (cert, key) = achall.response_and_validation(
|
|
|
|
|
cert_key=cert_key)
|
2015-10-04 16:13:00 -04:00
|
|
|
cert_pem = OpenSSL.crypto.dump_certificate(
|
|
|
|
|
OpenSSL.crypto.FILETYPE_PEM, cert)
|
|
|
|
|
key_pem = OpenSSL.crypto.dump_privatekey(
|
|
|
|
|
OpenSSL.crypto.FILETYPE_PEM, key)
|
2015-06-02 09:55:16 -04:00
|
|
|
|
2015-07-17 13:44:20 -04:00
|
|
|
# Write out challenge cert and key
|
2015-07-09 07:17:22 -04:00
|
|
|
with open(cert_path, "wb") as cert_chall_fd:
|
2015-06-02 09:55:16 -04:00
|
|
|
cert_chall_fd.write(cert_pem)
|
2015-07-09 07:17:22 -04:00
|
|
|
with le_util.safe_open(key_path, 'wb', chmod=0o400) as key_file:
|
Rewrite acccounts and registration.
Save accounts to:
/etc/letsencrypt/accounts/www.letsencrypt-dmeo.org/acme/new-reg/ \
kuba.le.wtf@2015-07-04T14:04:10Z/ \
{regr.json,meta.json,private_key.json}
Account now represents a combination of private key, Registration
Resource and client account metadata. `Account.id` based on the
account metadata (creation host and datetime). UI interface
(`cli._determine_account`) based on the `id`, and not on email as
previously.
Add `AccountStorage` interface and `AccountFileStorage`,
`AccountMemoryStorage` implementations (latter, in-memory, useful for
testing).
Create Account only after Registration Resource is received
(`register()` returns `Account`).
Allow `client.Client(..., acme=acme, ...)`: API client might reuse
acme.client.Client as returned by `register()`.
Move report_new_account to letsencrypt.account, client.Client.register
into client.register.
Use Registration.from_data acme API.
achallenges.AChallenge.key is now the `acme.jose.JWK`, not
`le_util.Key`. Plugins have to export PEM/DER as necessary
(c.f. `letsencrypt.plugins.common.Dvsni.get_key_path`)
Add --agree-tos, save --agree-eula to "args.eula". Prompt for EULA as
soon as client is launched, add prompt for TOS.
Remove unnecessary letsencrypt.network. Remove, now irrelevant,
`IConfig.account_keys_dir`.
Based on the draft from
https://github.com/letsencrypt/letsencrypt/pull/362#issuecomment-97946817.
2015-07-04 02:46:36 -04:00
|
|
|
key_file.write(key_pem)
|
|
|
|
|
|
2015-06-02 09:55:16 -04:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2015-06-28 05:38:03 -04:00
|
|
|
# test utils used by letsencrypt_apache/letsencrypt_nginx (hence
|
|
|
|
|
# "pragma: no cover") TODO: this might quickly lead to dead code (also
|
|
|
|
|
# c.f. #383)
|
2015-06-02 09:55:16 -04:00
|
|
|
|
2015-06-28 05:38:03 -04:00
|
|
|
def setup_ssl_options(config_dir, src, dest): # pragma: no cover
|
2015-06-02 09:55:16 -04:00
|
|
|
"""Move the ssl_options into position and return the path."""
|
2015-06-20 16:04:58 -04:00
|
|
|
option_path = os.path.join(config_dir, dest)
|
|
|
|
|
shutil.copyfile(src, option_path)
|
2015-06-02 09:55:16 -04:00
|
|
|
return option_path
|
|
|
|
|
|
|
|
|
|
|
2015-06-28 05:38:03 -04:00
|
|
|
def dir_setup(test_dir, pkg): # pragma: no cover
|
2015-06-02 09:55:16 -04:00
|
|
|
"""Setup the directories necessary for the configurator."""
|
|
|
|
|
temp_dir = tempfile.mkdtemp("temp")
|
|
|
|
|
config_dir = tempfile.mkdtemp("config")
|
|
|
|
|
work_dir = tempfile.mkdtemp("work")
|
|
|
|
|
|
|
|
|
|
os.chmod(temp_dir, constants.CONFIG_DIRS_MODE)
|
|
|
|
|
os.chmod(config_dir, constants.CONFIG_DIRS_MODE)
|
|
|
|
|
os.chmod(work_dir, constants.CONFIG_DIRS_MODE)
|
|
|
|
|
|
|
|
|
|
test_configs = pkg_resources.resource_filename(
|
|
|
|
|
pkg, os.path.join("testdata", test_dir))
|
|
|
|
|
|
|
|
|
|
shutil.copytree(
|
|
|
|
|
test_configs, os.path.join(temp_dir, test_dir), symlinks=True)
|
|
|
|
|
|
|
|
|
|
return temp_dir, config_dir, work_dir
|