Пример #1
0
 def test_is_acme_error(self):
     from acme.messages import is_acme_error, Error
     self.assertTrue(is_acme_error(self.error))
     self.assertFalse(is_acme_error(self.error_custom))
     self.assertFalse(is_acme_error(Error()))
     self.assertFalse(is_acme_error(self.empty_error))
     self.assertFalse(is_acme_error("must pet all the {dogs|rabbits}"))
Пример #2
0
def post_arg_parse_except_hook(exc_type, exc_value, trace, debug, log_path):
    """Logs fatal exceptions and reports them to the user.

    If debug is True, the full exception and traceback is shown to the
    user, otherwise, it is suppressed. sys.exit is always called with a
    nonzero status.

    :param type exc_type: type of the raised exception
    :param BaseException exc_value: raised exception
    :param traceback trace: traceback of where the exception was raised
    :param bool debug: True if the traceback should be shown to the user
    :param str log_path: path to file or directory containing the log

    """
    exc_info = (exc_type, exc_value, trace)
    # constants.QUIET_LOGGING_LEVEL or higher should be used to
    # display message the user, otherwise, a lower level like
    # logger.DEBUG should be used
    if debug or not issubclass(exc_type, Exception):
        assert constants.QUIET_LOGGING_LEVEL <= logging.ERROR
        logger.error('Exiting abnormally:', exc_info=exc_info)
    else:
        logger.debug('Exiting abnormally:', exc_info=exc_info)
        if issubclass(exc_type, errors.Error):
            sys.exit(exc_value)
        logger.error('An unexpected error occurred:')
        if messages.is_acme_error(exc_value):
            # Remove the ACME error prefix from the exception
            _, _, exc_str = str(exc_value).partition(':: ')
            logger.error(exc_str)
        else:
            traceback.print_exception(exc_type, exc_value, None)
    exit_with_log_path(log_path)
Пример #3
0
 def test_unicode_error(self):
     from acme.messages import Error, is_acme_error
     arabic_error = Error.with_code(
         'malformed',
         detail=u'\u0639\u062f\u0627\u0644\u0629',
         title='title')
     self.assertTrue(is_acme_error(arabic_error))
Пример #4
0
def post_arg_parse_except_hook(exc_type, exc_value, trace, debug, log_path):
    """Logs fatal exceptions and reports them to the user.

    If debug is True, the full exception and traceback is shown to the
    user, otherwise, it is suppressed. sys.exit is always called with a
    nonzero status.

    :param type exc_type: type of the raised exception
    :param BaseException exc_value: raised exception
    :param traceback trace: traceback of where the exception was raised
    :param bool debug: True if the traceback should be shown to the user
    :param str log_path: path to file or directory containing the log

    """
    exc_info = (exc_type, exc_value, trace)
    # constants.QUIET_LOGGING_LEVEL or higher should be used to
    # display message the user, otherwise, a lower level like
    # logger.DEBUG should be used
    if debug or not issubclass(exc_type, Exception):
        assert constants.QUIET_LOGGING_LEVEL <= logging.ERROR
        logger.error('Exiting abnormally:', exc_info=exc_info)
    else:
        logger.debug('Exiting abnormally:', exc_info=exc_info)
        if issubclass(exc_type, errors.Error):
            sys.exit(exc_value)
        print('An unexpected error occurred:', file=sys.stderr)
        if messages.is_acme_error(exc_value):
            # Remove the ACME error prefix from the exception
            _, _, exc_str = str(exc_value).partition(':: ')
            print(exc_str, file=sys.stderr)
        else:
            traceback.print_exception(exc_type, exc_value, None)
    exit_with_log_path(log_path)
Пример #5
0
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:`certbot.achallenges.AnnotatedChallenge` with the same error
        type.

    :returns: A formatted error message for the client.
    :rtype: str

    """
    error = failed_achalls[0].error
    typ = error.typ
    if messages.is_acme_error(error):
        typ = error.code
    msg = ["The following errors were reported by the server:"]

    for achall in failed_achalls:
        msg.append("\n\nDomain: %s\nType:   %s\nDetail: %s" % (
            achall.domain, typ, achall.error.detail))

    if typ in _ERROR_HELP:
        msg.append("\n\n")
        msg.append(_ERROR_HELP[typ])

    return "".join(msg)
Пример #6
0
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:`certbot.achallenges.AnnotatedChallenge` with the same error
        type.

    :returns: A formatted error message for the client.
    :rtype: str

    """
    error = failed_achalls[0].error
    typ = error.typ
    if messages.is_acme_error(error):
        typ = error.code
    msg = ["The following errors were reported by the server:"]

    for achall in failed_achalls:
        msg.append("\n\nDomain: %s\nType:   %s\nDetail: %s" % (
            achall.domain, typ, achall.error.detail))

    if typ in _ERROR_HELP:
        msg.append("\n\n")
        msg.append(_ERROR_HELP[typ])

    return "".join(msg)
Пример #7
0
def _handle_exception(exc_type, exc_value, trace, config):
    """Logs exceptions and reports them to the user.

    Config is used to determine how to display exceptions to the user. In
    general, if config.debug is True, then the full exception and traceback is
    shown to the user, otherwise it is suppressed. If config itself is None,
    then the traceback and exception is attempted to be written to a logfile.
    If this is successful, the traceback is suppressed, otherwise it is shown
    to the user. sys.exit is always called with a nonzero status.

    """
    tb_str = "".join(traceback.format_exception(exc_type, exc_value, trace))
    logger.debug("Exiting abnormally:%s%s", os.linesep, tb_str)

    if issubclass(exc_type, Exception) and (config is None
                                            or not config.debug):
        if config is None:
            logfile = "certbot.log"
            try:
                with open(logfile, "w") as logfd:
                    traceback.print_exception(exc_type,
                                              exc_value,
                                              trace,
                                              file=logfd)
                assert "--debug" not in sys.argv  # config is None if this explodes
            except:  # pylint: disable=bare-except
                sys.exit(tb_str)
            if "--debug" in sys.argv:
                sys.exit(tb_str)

        if issubclass(exc_type, errors.Error):
            sys.exit(exc_value)
        else:
            # Here we're passing a client or ACME error out to the client at the shell
            # Tell the user a bit about what happened, without overwhelming
            # them with a full traceback
            err = traceback.format_exception_only(exc_type, exc_value)[0]
            # Typical error from the ACME module:
            # acme.messages.Error: urn:ietf:params:acme:error:malformed :: The
            # request message was malformed :: Error creating new registration
            # :: Validation of contact mailto:[email protected] failed:
            # Server failure at resolver
            if (messages.is_acme_error(err) and ":: " in err and
                    config.verbose_count <= cli.flag_default("verbose_count")):
                # prune ACME error code, we have a human description
                _code, _sep, err = err.partition(":: ")
            msg = "An unexpected error occurred:\n" + err + "Please see the "
            if config is None:
                msg += "logfile '{0}' for more details.".format(logfile)
            else:
                msg += "logfiles in {0} for more details.".format(
                    config.logs_dir)
            sys.exit(msg)
    else:
        sys.exit(tb_str)
Пример #8
0
def _handle_exception(exc_type, exc_value, trace, config):
    """Logs exceptions and reports them to the user.

    Config is used to determine how to display exceptions to the user. In
    general, if config.debug is True, then the full exception and traceback is
    shown to the user, otherwise it is suppressed. If config itself is None,
    then the traceback and exception is attempted to be written to a logfile.
    If this is successful, the traceback is suppressed, otherwise it is shown
    to the user. sys.exit is always called with a nonzero status.

    """
    logger.debug(
        "Exiting abnormally:%s%s",
        os.linesep,
        "".join(traceback.format_exception(exc_type, exc_value, trace)))

    if issubclass(exc_type, Exception) and (config is None or not config.debug):
        if config is None:
            logfile = "certbot.log"
            try:
                with open(logfile, "w") as logfd:
                    traceback.print_exception(
                        exc_type, exc_value, trace, file=logfd)
            except:  # pylint: disable=bare-except
                sys.exit("".join(
                    traceback.format_exception(exc_type, exc_value, trace)))

        if issubclass(exc_type, errors.Error):
            sys.exit(exc_value)
        else:
            # Here we're passing a client or ACME error out to the client at the shell
            # Tell the user a bit about what happened, without overwhelming
            # them with a full traceback
            if issubclass(exc_type, dialog.error):
                err = exc_value.complete_message()
            else:
                err = traceback.format_exception_only(exc_type, exc_value)[0]
            # Typical error from the ACME module:
            # acme.messages.Error: urn:ietf:params:acme:error:malformed :: The
            # request message was malformed :: Error creating new registration
            # :: Validation of contact mailto:[email protected] failed:
            # Server failure at resolver
            if (messages.is_acme_error(err) and ":: " in err and
                 config.verbose_count <= cli.flag_default("verbose_count")):
                # prune ACME error code, we have a human description
                _code, _sep, err = err.partition(":: ")
            msg = "An unexpected error occurred:\n" + err + "Please see the "
            if config is None:
                msg += "logfile '{0}' for more details.".format(logfile)
            else:
                msg += "logfiles in {0} for more details.".format(config.logs_dir)
            sys.exit(msg)
    else:
        sys.exit("".join(
            traceback.format_exception(exc_type, exc_value, trace)))
Пример #9
0
def post_arg_parse_except_hook(exc_type: Type[BaseException],
                               exc_value: BaseException, trace: TracebackType,
                               debug: bool, quiet: bool,
                               log_path: str) -> None:
    """Logs fatal exceptions and reports them to the user.

    If debug is True, the full exception and traceback is shown to the
    user, otherwise, it is suppressed. sys.exit is always called with a
    nonzero status.

    :param type exc_type: type of the raised exception
    :param BaseException exc_value: raised exception
    :param traceback trace: traceback of where the exception was raised
    :param bool debug: True if the traceback should be shown to the user
    :param bool quiet: True if Certbot is running in quiet mode
    :param str log_path: path to file or directory containing the log

    """
    exc_info = (exc_type, exc_value, trace)
    # Only print human advice if not running under --quiet
    exit_func = lambda: sys.exit(1) if quiet else exit_with_advice(log_path)
    # constants.QUIET_LOGGING_LEVEL or higher should be used to
    # display message the user, otherwise, a lower level like
    # logger.DEBUG should be used
    if debug or not issubclass(exc_type, Exception):
        assert constants.QUIET_LOGGING_LEVEL <= logging.ERROR
        if exc_type is KeyboardInterrupt:
            logger.error('Exiting due to user request.')
            sys.exit(1)
        logger.error('Exiting abnormally:', exc_info=exc_info)
    else:
        logger.debug('Exiting abnormally:', exc_info=exc_info)
        # Use logger to print the error message to take advantage of
        # our logger printing warnings and errors in red text.
        if issubclass(exc_type, errors.Error):
            logger.error(str(exc_value))
            exit_func()
        logger.error('An unexpected error occurred:')
        if messages.is_acme_error(exc_value):
            # Remove the ACME error prefix from the exception
            _, _, exc_str = str(exc_value).partition(':: ')
            logger.error(exc_str)
        else:
            output = traceback.format_exception_only(exc_type, exc_value)
            # format_exception_only returns a list of strings each
            # terminated by a newline. We combine them into one string
            # and remove the final newline before passing it to
            # logger.error.
            logger.error(''.join(output).rstrip())
    exit_func()
Пример #10
0
def _generate_failed_chall_msg(failed_achalls: List[achallenges.AnnotatedChallenge]) -> str:
    """Creates a user friendly error message about failed challenges.

    :param list failed_achalls: A list of failed
        :class:`certbot.achallenges.AnnotatedChallenge` with the same error
        type.
    :returns: A formatted error message for the client.
    :rtype: str

    """
    error = failed_achalls[0].error
    typ = error.typ
    if messages.is_acme_error(error):
        typ = error.code
    msg = []

    for achall in failed_achalls:
        msg.append("\n  Domain: %s\n  Type:   %s\n  Detail: %s\n" % (
            achall.domain, typ, achall.error.detail))

    return "".join(msg)
Пример #11
0
 def test_unicode_error(self):
     from acme.messages import Error, ERROR_PREFIX, is_acme_error
     arabic_error = Error(
             detail=u'\u0639\u062f\u0627\u0644\u0629', typ=ERROR_PREFIX + 'malformed',
         title='title')
     self.assertTrue(is_acme_error(arabic_error))
Пример #12
0
 def test_with_code(self):
     from acme.messages import Error, is_acme_error
     self.assertTrue(is_acme_error(Error.with_code('badCSR')))
     self.assertRaises(ValueError, Error.with_code, 'not an ACME error code')
Пример #13
0
 def test_is_acme_error(self):
     from acme.messages import is_acme_error
     self.assertTrue(is_acme_error(self.error))
     self.assertTrue(is_acme_error(str(self.error)))
     self.assertFalse(is_acme_error(self.error_custom))
Пример #14
0
 def test_is_acme_error(self):
     from acme.messages import is_acme_error
     self.assertTrue(is_acme_error(self.error))
     self.assertFalse(is_acme_error(self.error_custom))
     self.assertFalse(is_acme_error(self.empty_error))
     self.assertFalse(is_acme_error("must pet all the {dogs|rabbits}"))
Пример #15
0
 def test_with_code(self):
     from acme.messages import Error, is_acme_error
     self.assertTrue(is_acme_error(Error.with_code('badCSR')))
     self.assertRaises(ValueError, Error.with_code, 'not an ACME error code')
Пример #16
0
 def test_is_acme_error(self):
     from acme.messages import is_acme_error
     self.assertTrue(is_acme_error(self.error))
     self.assertTrue(is_acme_error(str(self.error)))
     self.assertFalse(is_acme_error(self.error_custom))
Пример #17
0
 def test_unicode_error(self):
     from acme.messages import Error, ERROR_PREFIX, is_acme_error
     arabic_error = Error(
             detail=u'\u0639\u062f\u0627\u0644\u0629', typ=ERROR_PREFIX + 'malformed',
         title='title')
     self.assertTrue(is_acme_error(arabic_error))