certbot/letsencrypt/auth_handler.py

505 lines
17 KiB
Python
Raw Normal View History

"""ACME AuthHandler."""
2015-04-07 17:36:59 -04:00
import itertools
import logging
2015-04-11 02:02:01 -04:00
import time
import zope.component
2015-05-10 07:26:21 -04:00
from acme import challenges
2015-06-11 11:00:18 -04:00
from acme import messages
2015-02-01 05:07:36 -05:00
2015-05-10 08:25:29 -04:00
from letsencrypt import achallenges
from letsencrypt import errors
2015-09-23 18:02:20 -04:00
from letsencrypt import error_handler
from letsencrypt import interfaces
2015-04-27 16:14:39 -04:00
2015-06-11 09:45:41 -04:00
logger = logging.getLogger(__name__)
2015-04-23 02:17:53 -04:00
class AuthHandler(object):
"""ACME Authorization Handler for a client.
:ivar auth: Authenticator capable of solving
:class:`~acme.challenges.Challenge` types
:type auth: :class:`letsencrypt.interfaces.IAuthenticator`
:ivar acme.client.Client acme: ACME client API.
2015-04-17 06:40:22 -04:00
:ivar account: Client's Account
2015-05-10 08:25:29 -04:00
:type account: :class:`letsencrypt.account.Account`
2015-04-13 20:33:11 -04:00
2015-04-19 23:10:40 -04:00
:ivar dict authzr: ACME Authorization Resource dict where keys are domains
2015-06-11 11:00:18 -04:00
and values are :class:`acme.messages.AuthorizationResource`
:ivar list achalls: DV challenges in the form of
2015-05-10 08:25:29 -04:00
:class:`letsencrypt.achallenges.AnnotatedChallenge`
"""
def __init__(self, auth, acme, account):
self.auth = auth
self.acme = acme
2015-04-17 06:40:22 -04:00
self.account = account
2015-04-06 20:03:07 -04:00
self.authzr = dict()
2015-04-13 20:33:11 -04:00
# List must be used to keep responses straight.
self.achalls = []
2015-04-19 23:10:40 -04:00
def get_authorizations(self, domains, best_effort=False):
2015-04-06 20:03:07 -04:00
"""Retrieve all authorizations for challenges.
:param list domains: Domains for authorization
:param bool best_effort: Whether or not all authorizations are
required (this is useful in renewal)
2016-03-21 17:43:38 -04:00
:returns: List of authorization resources
:rtype: list
2015-06-12 10:45:28 -04:00
:raises .AuthorizationError: If unable to retrieve all
authorizations
"""
2015-04-11 02:02:01 -04:00
for domain in domains:
self.authzr[domain] = self.acme.request_domain_challenges(
domain, self.account.regr.new_authzr_uri)
2015-04-22 19:27:54 -04:00
2015-04-07 17:36:59 -04:00
self._choose_challenges(domains)
2015-04-13 20:33:11 -04:00
# While there are still challenges remaining...
while self.achalls:
resp = self._solve_challenges()
2015-06-11 09:45:41 -04:00
logger.info("Waiting for verification...")
2015-04-13 20:33:11 -04:00
# Send all Responses - this modifies achalls
self._respond(resp, best_effort)
2015-04-22 19:27:54 -04:00
# Just make sure all decisions are complete.
self.verify_authzr_complete()
2015-04-22 19:27:54 -04:00
# Only return valid authorizations
retVal = [authzr for authzr in self.authzr.values()
2016-03-20 19:12:59 -04:00
if authzr.body.status == messages.STATUS_VALID]
2016-03-21 17:43:38 -04:00
if not retVal:
2016-03-20 19:12:59 -04:00
raise errors.AuthorizationError(
"Challenges failed for all domains")
return retVal
2015-04-11 02:02:01 -04:00
2015-04-07 17:36:59 -04:00
def _choose_challenges(self, domains):
2015-04-19 23:10:40 -04:00
"""Retrieve necessary challenges to satisfy server."""
2015-06-11 09:45:41 -04:00
logger.info("Performing the following challenges:")
2015-04-07 17:36:59 -04:00
for dom in domains:
path = gen_challenge_path(
2015-04-11 02:02:01 -04:00
self.authzr[dom].body.challenges,
2015-04-07 17:36:59 -04:00
self._get_chall_pref(dom),
2015-04-11 02:02:01 -04:00
self.authzr[dom].body.combinations)
dom_achalls = self._challenge_factory(
2015-04-07 17:36:59 -04:00
dom, path)
self.achalls.extend(dom_achalls)
2015-04-13 20:33:11 -04:00
def _solve_challenges(self):
2015-04-07 17:36:59 -04:00
"""Get Responses for challenges from authenticators."""
resp = []
2015-09-23 18:02:20 -04:00
with error_handler.ErrorHandler(self._cleanup_challenges):
try:
if self.achalls:
resp = self.auth.perform(self.achalls)
except errors.AuthorizationError:
logger.critical("Failure in setting up challenges.")
logger.info("Attempting to clean up outstanding challenges...")
raise
2015-04-07 17:36:59 -04:00
assert len(resp) == len(self.achalls)
2015-04-07 17:36:59 -04:00
return resp
def _respond(self, resp, best_effort):
2015-04-11 02:02:01 -04:00
"""Send/Receive confirmation of all challenges.
2015-01-13 04:22:24 -05:00
2015-04-06 20:03:07 -04:00
.. note:: This method also cleans up the auth_handler state.
2015-01-13 04:22:24 -05:00
"""
2015-04-13 20:33:11 -04:00
# TODO: chall_update is a dirty hack to get around acme-spec #105
2015-04-11 02:02:01 -04:00
chall_update = dict()
active_achalls = self._send_responses(self.achalls,
resp, chall_update)
2015-04-11 02:02:01 -04:00
2015-04-13 20:33:11 -04:00
# Check for updated status...
2015-06-18 21:02:51 -04:00
try:
self._poll_challenges(chall_update, best_effort)
2015-06-18 21:02:51 -04:00
finally:
# This removes challenges from self.achalls
2015-06-18 21:02:51 -04:00
self._cleanup_challenges(active_achalls)
2015-04-11 02:02:01 -04:00
def _send_responses(self, achalls, resps, chall_update):
2015-04-13 20:33:11 -04:00
"""Send responses and make sure errors are handled.
:param dict chall_update: parameter that is updated to hold
authzr -> list of outstanding solved annotated challenges
"""
2015-04-22 19:27:54 -04:00
active_achalls = []
2015-04-07 17:36:59 -04:00
for achall, resp in itertools.izip(achalls, resps):
2015-10-19 22:13:48 -04:00
# This line needs to be outside of the if block below to
# ensure failed challenges are cleaned up correctly
active_achalls.append(achall)
2015-04-13 20:33:11 -04:00
# Don't send challenges for None and False authenticator responses
2015-06-27 15:38:00 -04:00
if resp is not None and resp:
self.acme.answer_challenge(achall.challb, resp)
# TODO: answer_challenge returns challr, with URI,
# that can be used in _find_updated_challr
# comparisons...
2015-04-13 20:33:11 -04:00
if achall.domain in chall_update:
chall_update[achall.domain].append(achall)
else:
chall_update[achall.domain] = [achall]
2015-04-22 19:27:54 -04:00
return active_achalls
def _poll_challenges(
self, chall_update, best_effort, min_sleep=3, max_rounds=15):
2015-04-13 20:33:11 -04:00
"""Wait for all challenge results to be determined."""
dom_to_check = set(chall_update.keys())
comp_domains = set()
rounds = 0
2015-04-13 20:33:11 -04:00
while dom_to_check and rounds < max_rounds:
2015-04-13 20:33:11 -04:00
# TODO: Use retry-after...
time.sleep(min_sleep)
all_failed_achalls = set()
2015-04-13 20:33:11 -04:00
for domain in dom_to_check:
comp_achalls, failed_achalls = self._handle_check(
2015-04-13 20:33:11 -04:00
domain, chall_update[domain])
if len(comp_achalls) == len(chall_update[domain]):
2015-04-13 20:33:11 -04:00
comp_domains.add(domain)
elif not failed_achalls:
for achall, _ in comp_achalls:
chall_update[domain].remove(achall)
2015-04-13 20:33:11 -04:00
# We failed some challenges... damage control
else:
if best_effort:
comp_domains.add(domain)
logger.warning(
"Challenge failed for domain %s",
domain)
2015-04-13 20:33:11 -04:00
else:
all_failed_achalls.update(
updated for _, updated in failed_achalls)
if all_failed_achalls:
_report_failed_challs(all_failed_achalls)
raise errors.FailedChallenges(all_failed_achalls)
2015-04-13 20:33:11 -04:00
dom_to_check -= comp_domains
2015-04-13 20:33:11 -04:00
comp_domains.clear()
rounds += 1
2015-04-13 20:33:11 -04:00
def _handle_check(self, domain, achalls):
"""Returns tuple of ('completed', 'failed')."""
completed = []
failed = []
self.authzr[domain], _ = self.acme.poll(self.authzr[domain])
2015-06-11 11:00:18 -04:00
if self.authzr[domain].body.status == messages.STATUS_VALID:
2015-04-13 20:33:11 -04:00
return achalls, []
# Note: if the whole authorization is invalid, the individual failed
# challenges will be determined here...
for achall in achalls:
updated_achall = achall.update(challb=self._find_updated_challb(
self.authzr[domain], achall))
2015-04-22 19:27:54 -04:00
2015-04-13 20:33:11 -04:00
# This does nothing for challenges that have yet to be decided yet.
if updated_achall.status == messages.STATUS_VALID:
completed.append((achall, updated_achall))
elif updated_achall.status == messages.STATUS_INVALID:
failed.append((achall, updated_achall))
2015-04-13 20:33:11 -04:00
return completed, failed
def _find_updated_challb(self, authzr, achall): # pylint: disable=no-self-use
"""Find updated challenge body within Authorization Resource.
2015-04-13 20:33:11 -04:00
.. warning:: This assumes only one instance of type of challenge in
each challenge resource.
:param .AuthorizationResource authzr: Authorization Resource
:param .AnnotatedChallenge achall: Annotated challenge for which
to get status
2015-04-23 02:17:53 -04:00
2015-04-13 20:33:11 -04:00
"""
2015-04-22 19:27:54 -04:00
for authzr_challb in authzr.body.challenges:
2015-09-06 05:20:11 -04:00
if type(authzr_challb.chall) is type(achall.challb.chall): # noqa
return authzr_challb
2015-04-15 19:53:39 -04:00
raise errors.AuthorizationError(
"Target challenge not found in authorization resource")
2015-01-12 08:44:06 -05:00
def _get_chall_pref(self, domain):
2015-01-13 04:22:24 -05:00
"""Return list of challenge preferences.
:param str domain: domain for which you are requesting preferences
2015-01-13 19:48:18 -05:00
2015-01-13 04:22:24 -05:00
"""
2015-04-22 19:27:54 -04:00
# Make sure to make a copy...
chall_prefs = []
chall_prefs.extend(self.auth.get_chall_pref(domain))
return chall_prefs
2015-04-13 20:33:11 -04:00
def _cleanup_challenges(self, achall_list=None):
"""Cleanup challenges.
If achall_list is not provided, cleanup all achallenges.
2015-04-13 20:33:11 -04:00
"""
2015-06-11 09:45:41 -04:00
logger.info("Cleaning up challenges")
2015-04-13 20:33:11 -04:00
if achall_list is None:
achalls = self.achalls
2015-04-13 20:33:11 -04:00
else:
achalls = achall_list
2015-04-13 20:33:11 -04:00
if achalls:
self.auth.cleanup(achalls)
for achall in achalls:
self.achalls.remove(achall)
def verify_authzr_complete(self):
"""Verifies that all authorizations have been decided.
:returns: Whether all authzr are complete
:rtype: bool
"""
2015-04-22 19:27:54 -04:00
for authzr in self.authzr.values():
2015-06-11 11:00:18 -04:00
if (authzr.body.status != messages.STATUS_VALID and
authzr.body.status != messages.STATUS_INVALID):
2015-04-22 19:27:54 -04:00
raise errors.AuthorizationError("Incomplete authorizations")
def _challenge_factory(self, domain, path):
"""Construct Namedtuple Challenges
:param str domain: domain of the enrollee
:param list path: List of indices from `challenges`.
:returns: achalls, list of challenge type
2015-05-10 08:25:29 -04:00
:class:`letsencrypt.achallenges.Indexed`
2016-02-26 16:01:58 -05:00
:rtype: list
2015-06-12 10:45:28 -04:00
:raises .errors.Error: if challenge type is not recognized
"""
achalls = []
2015-01-13 20:10:39 -05:00
for index in path:
challb = self.authzr[domain].body.challenges[index]
achalls.append(challb_to_achall(challb, self.account.key, domain))
return achalls
def challb_to_achall(challb, account_key, domain):
"""Converts a ChallengeBody object to an AnnotatedChallenge.
:param .ChallengeBody challb: ChallengeBody
:param .JWK account_key: Authorized Account Key
:param str domain: Domain of the challb
:returns: Appropriate AnnotatedChallenge
2015-05-10 08:25:29 -04:00
:rtype: :class:`letsencrypt.achallenges.AnnotatedChallenge`
"""
chall = challb.chall
2015-06-11 09:45:41 -04:00
logger.info("%s challenge for %s", chall.typ, domain)
2015-11-07 13:17:24 -05:00
if isinstance(chall, challenges.KeyAuthorizationChallenge):
return achallenges.KeyAuthorizationAnnotatedChallenge(
challb=challb, domain=domain, account_key=account_key)
elif isinstance(chall, challenges.DNS):
return achallenges.DNS(challb=challb, domain=domain)
else:
raise errors.Error(
2015-06-12 10:45:28 -04:00
"Received unsupported challenge of type: %s", chall.typ)
def gen_challenge_path(challbs, preferences, combinations):
"""Generate a plan to get authority over the identity.
2015-03-28 00:08:14 -04:00
.. todo:: This can be possibly be rewritten to use resolved_combinations.
:param tuple challbs: A tuple of challenges
2015-06-11 11:00:18 -04:00
(:class:`acme.messages.Challenge`) from
:class:`acme.messages.AuthorizationResource` to be
fulfilled by the client in order to prove possession of the
2015-02-13 17:37:45 -05:00
identifier.
:param list preferences: List of challenge preferences for domain
2015-05-10 07:26:21 -04:00
(:class:`acme.challenges.Challenge` subclasses)
2015-03-28 00:08:14 -04:00
:param tuple combinations: A collection of sets of challenges from
2015-05-10 07:26:21 -04:00
:class:`acme.messages.Challenge`, each of which would
be sufficient to prove possession of the identifier.
2015-03-28 00:08:14 -04:00
:returns: tuple of indices from ``challenges``.
:rtype: tuple
2015-05-10 08:25:29 -04:00
:raises letsencrypt.errors.AuthorizationError: If a
2015-03-28 00:36:06 -04:00
path cannot be created that satisfies the CA given the preferences and
combinations.
"""
2015-02-13 17:37:45 -05:00
if combinations:
return _find_smart_path(challbs, preferences, combinations)
else:
return _find_dumb_path(challbs, preferences)
def _find_smart_path(challbs, preferences, combinations):
"""Find challenge path with server hints.
Can be called if combinations is included. Function uses a simple
ranking system to choose the combo with the lowest cost.
"""
chall_cost = {}
2015-03-28 00:08:14 -04:00
max_cost = 1
2015-02-13 17:37:45 -05:00
for i, chall_cls in enumerate(preferences):
chall_cost[chall_cls] = i
max_cost += i
2015-03-28 00:08:14 -04:00
# max_cost is now equal to sum(indices) + 1
best_combo = []
# Set above completing all of the available challenges
2015-03-28 00:08:14 -04:00
best_combo_cost = max_cost
combo_total = 0
2015-02-13 17:37:45 -05:00
for combo in combinations:
for challenge_index in combo:
combo_total += chall_cost.get(challbs[
challenge_index].chall.__class__, max_cost)
2015-03-28 00:08:14 -04:00
if combo_total < best_combo_cost:
best_combo = combo
best_combo_cost = combo_total
2015-03-28 00:08:14 -04:00
combo_total = 0
if not best_combo:
2016-02-26 18:32:11 -05:00
_report_no_chall_path()
return best_combo
2015-01-13 19:48:18 -05:00
def _find_dumb_path(challbs, preferences):
"""Find challenge path without server hints.
Should be called if the combinations hint is not included by the
2016-02-26 18:32:11 -05:00
server. This function either returns a path containing all
challenges provided by the CA or raises an exception.
"""
2015-02-13 17:37:45 -05:00
path = []
2016-02-26 18:32:11 -05:00
for i, challb in enumerate(challbs):
# supported is set to True if the challenge type is supported
supported = next((True for pref_c in preferences
if isinstance(challb.chall, pref_c)), False)
if supported:
path.append(i)
else:
_report_no_chall_path()
2015-02-13 17:37:45 -05:00
2016-02-26 18:32:11 -05:00
return path
2015-01-13 19:48:18 -05:00
2016-02-26 18:32:11 -05:00
def _report_no_chall_path():
"""Logs and raises an error that no satisfiable chall path exists."""
msg = ("Client with the currently selected authenticator does not support "
"any combination of challenges that will satisfy the CA.")
logger.fatal(msg)
raise errors.AuthorizationError(msg)
2016-02-08 16:35:54 -05:00
_ACME_PREFIX = "urn:acme:error:"
_ERROR_HELP_COMMON = (
2015-06-24 21:24:54 -04:00
"To fix these errors, please make sure that your domain name was entered "
"correctly and the DNS A record(s) for that domain contain(s) the "
2015-06-24 21:24:54 -04:00
"right IP address.")
_ERROR_HELP = {
2015-09-06 04:21:29 -04:00
"connection":
2015-06-24 21:24:54 -04:00
_ERROR_HELP_COMMON + " Additionally, please check that your computer "
"has a publicly routable IP address and that no firewalls are preventing "
2016-02-08 17:06:34 -05:00
"the server from communicating with the client. If you're using the "
"webroot plugin, you should also verify that you are serving files "
"from the webroot path you provided.",
2015-09-06 04:21:29 -04:00
"dnssec":
2015-06-24 21:24:54 -04:00
_ERROR_HELP_COMMON + " Additionally, if you have DNSSEC enabled for "
"your domain, please ensure that the signature is valid.",
2015-09-06 04:21:29 -04:00
"malformed":
2015-06-24 21:24:54 -04:00
"To fix these errors, please make sure that you did not provide any "
"invalid information to the client, and try running Let's Encrypt "
2015-06-24 21:24:54 -04:00
"again.",
2015-09-06 04:21:29 -04:00
"serverInternal":
2015-06-24 21:24:54 -04:00
"Unfortunately, an error on the ACME server prevented you from completing "
"authorization. Please try again later.",
2015-09-06 04:21:29 -04:00
"tls":
_ERROR_HELP_COMMON + " Additionally, please check that you have an "
"up-to-date TLS configuration that allows the server to communicate "
"with the Let's Encrypt client.",
2015-09-06 04:21:29 -04:00
"unauthorized": _ERROR_HELP_COMMON,
2015-09-06 05:20:11 -04:00
"unknownHost": _ERROR_HELP_COMMON,
}
def _report_failed_challs(failed_achalls):
"""Notifies the user about failed challenges.
:param set failed_achalls: A set of failed
:class:`letsencrypt.achallenges.AnnotatedChallenge`.
"""
problems = dict()
for achall in failed_achalls:
if achall.error:
problems.setdefault(achall.error.typ, []).append(achall)
reporter = zope.component.getUtility(interfaces.IReporter)
for achalls in problems.itervalues():
2015-06-24 14:51:50 -04:00
reporter.add_message(
_generate_failed_chall_msg(achalls), reporter.MEDIUM_PRIORITY)
def _generate_failed_chall_msg(failed_achalls):
"""Creates a user friendly error message about failed challenges.
:param list failed_achalls: A list of failed
:class:`letsencrypt.achallenges.AnnotatedChallenge` with the same error
type.
:returns: A formatted error message for the client.
:rtype: str
"""
typ = failed_achalls[0].error.typ
2016-02-08 16:35:54 -05:00
if typ.startswith(_ACME_PREFIX):
typ = typ[len(_ACME_PREFIX):]
msg = ["The following errors were reported by the server:"]
for achall in failed_achalls:
msg.append("\n\nDomain: %s\nType: %s\nDetail: %s" % (
2016-02-08 16:35:54 -05:00
achall.domain, typ, achall.error.detail))
if typ in _ERROR_HELP:
2015-06-24 21:24:54 -04:00
msg.append("\n\n")
msg.append(_ERROR_HELP[typ])
2015-06-24 21:24:54 -04:00
return "".join(msg)