Exemple #1
0
def get_apache_configurator(config_path,
                            config_dir,
                            work_dir,
                            ssl_options,
                            version=(2, 4, 7)):
    """Create an Apache Configurator with the specified options."""

    backups = os.path.join(work_dir, "backups")

    with mock.patch("letsencrypt.client.apache.configurator."
                    "subprocess.Popen") as mock_popen:
        # This just states that the ssl module is already loaded
        mock_popen().communicate.return_value = ("ssl_module", "")
        config = configurator.ApacheConfigurator(
            mock.MagicMock(
                apache_server_root=config_path,
                apache_mod_ssl_conf=ssl_options,
                le_vhost_ext="-le-ssl.conf",
                backup_dir=backups,
                config_dir=config_dir,
                temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"),
                in_progress_dir=os.path.join(backups, "IN_PROGRESS"),
                work_dir=work_dir), version)

    return config
Exemple #2
0
def determine_installer(config):
    """Returns a valid installer if one exists.

    :param config: Configuration.
    :type config: :class:`letsencrypt.client.interfaces.IConfig`

    """
    try:
        return configurator.ApacheConfigurator(config)
    except errors.LetsEncryptNoInstallationError:
        logging.info("Unable to find a way to install the certificate.")
Exemple #3
0
def determine_authenticator(config):
    """Returns a valid IAuthenticator.

    :param config: Configuration.
    :type config: :class:`letsencrypt.client.interfaces.IConfig`

    """
    try:
        return configurator.ApacheConfigurator(config)
    except errors.LetsEncryptNoInstallationError:
        logging.info("Unable to determine a way to authenticate the server")
def determine_installer(config):
    """Returns a valid installer if one exists.

    :param config: Configuration.
    :type config: :class:`letsencrypt.client.interfaces.IConfig`

    :returns: IInstaller or `None`
    :rtype: :class:`~letsencrypt.client.interfaces.IInstaller` or `None`

    """
    installer = configurator.ApacheConfigurator(config)
    try:
        installer.prepare()
        return installer
    except errors.LetsEncryptNoInstallationError:
        logging.info("Unable to find a way to install the certificate.")
        return
    except errors.LetsEncryptMisconfigurationError:
        # This will have to be changed in the future...
        return installer
def main():  # pylint: disable=too-many-branches, too-many-statements
    """Command line argument parsing and main script execution."""
    # note: arg parser internally handles --help (and exits afterwards)
    args = create_parser().parse_args()
    config = configuration.NamespaceConfig(args)

    # note: check is done after arg parsing as --help should work w/o root also.
    if not os.geteuid() == 0:
        sys.exit(
            "{0}Root is required to run letsencrypt.  Please use sudo.{0}".
            format(os.linesep))

    # Set up logging
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    if args.use_curses:
        logger.addHandler(log.DialogHandler())
        displayer = display_util.NcursesDisplay()
    else:
        displayer = display_util.FileDisplay(sys.stdout)

    zope.component.provideUtility(displayer)

    if args.view_config_changes:
        client.view_config_changes(config)
        sys.exit()

    if args.revoke or args.rev_cert is not None or args.rev_key is not None:
        client.revoke(config, args.no_confirm, args.rev_cert, args.rev_key)
        sys.exit()

    if args.rollback > 0:
        client.rollback(args.rollback, config)
        sys.exit()

    if not args.eula:
        display_eula()

    all_auths = [
        configurator.ApacheConfigurator(config),
        standalone.StandaloneAuthenticator(),
    ]
    try:
        auth = client.determine_authenticator(all_auths)
    except errors.LetsEncryptClientError:
        logging.critical("No authentication mechanisms were found on your "
                         "system.")
        sys.exit(1)

    if auth is None:
        sys.exit(0)

    # Use the same object if possible
    if interfaces.IInstaller.providedBy(auth):  # pylint: disable=no-member
        installer = auth
    else:
        # This is simple and avoids confusion right now.
        installer = None

    if args.domains is None:
        doms = display_ops.choose_names(installer)
    else:
        doms = args.domains

    if not doms:
        sys.exit(0)

    # Prepare for init of Client
    if args.authkey is None:
        authkey = client.init_key(args.rsa_key_size, config.key_dir)
    else:
        authkey = le_util.Key(args.authkey[0], args.authkey[1])

    acme = client.Client(config, authkey, auth, installer)

    # Validate the key and csr
    client.validate_key_csr(authkey)

    # This more closely mimics the capabilities of the CLI
    # It should be possible for reconfig only, install-only, no-install
    # I am not sure the best way to handle all of the unimplemented abilities,
    # but this code should be safe on all environments.
    cert_file = None
    if auth is not None:
        cert_file, chain_file = acme.obtain_certificate(doms)
    if installer is not None and cert_file is not None:
        acme.deploy_certificate(doms, authkey, cert_file, chain_file)
    if installer is not None:
        acme.enhance_config(doms, args.redirect)