# Buggy code
def deploy_cert(
    self, domain, cert_path, key_path, chain_path=None, fullchain_path=None
):
    """Deploys certificate to specified virtual host.

    Currently tries to find the last directives to deploy the cert in
    the VHost associated with the given domain. If it can't find the
    directives, it searches the "included" confs. The function verifies
    that it has located the three directives and finally modifies them
    to point to the correct destination. After the certificate is
    installed, the VirtualHost is enabled if it isn't already.

    .. todo:: Might be nice to remove chain directive if none exists
              This shouldn't happen within certbot though

    :raises errors.PluginError: When unable to deploy certificate due to
        a lack of directives

    """
    vhost = self.choose_vhost(domain)
    self._clean_vhost(vhost)

    # This is done first so that ssl module is enabled and cert_path,
    # cert_key... can all be parsed appropriately
    self.prepare_server_https("443")

    path = {
        "cert_path": self.parser.find_dir("SSLCertificateFile", None, vhost.path),
        "cert_key": self.parser.find_dir("SSLCertificateKeyFile", None, vhost.path),
    }

    # Only include if a certificate chain is specified
    if chain_path is not None:
        path["chain_path"] = self.parser.find_dir(
            "SSLCertificateChainFile", None, vhost.path
        )

    if not path["cert_path"] or not path["cert_key"]:
        # Throw some can't find all of the directives error"
        logger.warning(
            "Cannot find a cert or key directive in %s. VirtualHost was not modified",
            vhost.path,
        )
        # Presumably break here so that the virtualhost is not modified
        raise errors.PluginError("Unable to find cert and/or key directives")

    logger.info("Deploying Certificate for %s to VirtualHost %s", domain, vhost.filep)

    if self.version < (2, 4, 8) or (chain_path and not fullchain_path):
        # install SSLCertificateFile, SSLCertificateKeyFile,
        # and SSLCertificateChainFile directives
        set_cert_path = cert_path
        self.aug.set(path["cert_path"][-1], cert_path)
        self.aug.set(path["cert_key"][-1], key_path)
        if chain_path is not None:
            self.parser.add_dir(vhost.path, "SSLCertificateChainFile", chain_path)
        else:
            raise errors.PluginError(
                "--chain-path is required for your version of Apache"
            )
    else:
        if not fullchain_path:
            raise errors.PluginError(
                "Please provide the --fullchain-path\
 option pointing to your full chain file"
            )
        set_cert_path = fullchain_path
        self.aug.set(path["cert_path"][-1], fullchain_path)
        self.aug.set(path["cert_key"][-1], key_path)

    # Save notes about the transaction that took place
    self.save_notes += (
        "Changed vhost at %s with addresses of %s\n"
        "\tSSLCertificateFile %s\n"
        "\tSSLCertificateKeyFile %s\n"
        % (
            vhost.filep,
            ", ".join(str(addr) for addr in vhost.addrs),
            set_cert_path,
            key_path,
        )
    )
    if chain_path is not None:
        self.save_notes += "\tSSLCertificateChainFile %s\n" % chain_path

    # Make sure vhost is enabled if distro with enabled / available
    if self.conf("handle-sites"):
        if not vhost.enabled:
            self.enable_site(vhost)


# Traceback (PluginError)
Cannot find a cert or key directive in /files/etc/apache2/sites-available/default-ssl.conf/IfModule/VirtualHost. VirtualHost was not modified
Encountered exception:
Traceback (most recent call last):
File "/home/bmw/src/certbot/certbot/client.py", line 439, in deploy_certificate
fullchain_path=fullchain_path)
File "/home/bmw/src/certbot/certbot-apache/certbot_apache/configurator.py", line 269, in deploy_cert
"Unable to find cert and/or key directives")
PluginError: Unable to find cert and/or key directives

Calling registered functions
Reporting to user: Unable to install the certificate
Exiting abnormally:
Traceback (most recent call last):
File "/usr/local/bin/certbot", line 11, in <module>
load_entry_point('certbot', 'console_scripts', 'certbot')()
File "/home/bmw/src/certbot/certbot/main.py", line 743, in main
return config.func(config, plugins)
File "/home/bmw/src/certbot/certbot/main.py", line 604, in run
_install_cert(config, le_client, domains, new_lineage)
File "/home/bmw/src/certbot/certbot/main.py", line 469, in _install_cert
path_provider.cert_path, path_provider.chain_path, path_provider.fullchain_path)
File "/home/bmw/src/certbot/certbot/client.py", line 439, in deploy_certificate
fullchain_path=fullchain_path)
File "/home/bmw/src/certbot/certbot-apache/certbot_apache/configurator.py", line 269, in deploy_cert
"Unable to find cert and/or key directives")
PluginError: Unable to find cert and/or key directives

# Fixed code
def deploy_cert(
    self, domain, cert_path, key_path, chain_path=None, fullchain_path=None
):
    """Deploys certificate to specified virtual host.

    Currently tries to find the last directives to deploy the cert in
    the VHost associated with the given domain. If it can't find the
    directives, it searches the "included" confs. The function verifies
    that it has located the three directives and finally modifies them
    to point to the correct destination. After the certificate is
    installed, the VirtualHost is enabled if it isn't already.

    .. todo:: Might be nice to remove chain directive if none exists
              This shouldn't happen within certbot though

    :raises errors.PluginError: When unable to deploy certificate due to
        a lack of directives

    """
    # Choose vhost before (possible) enabling of mod_ssl, to keep the
    # vhost choice namespace similar with the pre-validation one.
    vhost = self.choose_vhost(domain)

    # This is done first so that ssl module is enabled and cert_path,
    # cert_key... can all be parsed appropriately
    self.prepare_server_https("443")

    # Add directives and remove duplicates
    self._add_dummy_ssl_directives(vhost.path)
    self._clean_vhost(vhost)

    path = {
        "cert_path": self.parser.find_dir("SSLCertificateFile", None, vhost.path),
        "cert_key": self.parser.find_dir("SSLCertificateKeyFile", None, vhost.path),
    }

    # Only include if a certificate chain is specified
    if chain_path is not None:
        path["chain_path"] = self.parser.find_dir(
            "SSLCertificateChainFile", None, vhost.path
        )

    if not path["cert_path"] or not path["cert_key"]:
        # Throw some can't find all of the directives error"
        logger.warning(
            "Cannot find a cert or key directive in %s. VirtualHost was not modified",
            vhost.path,
        )
        # Presumably break here so that the virtualhost is not modified
        raise errors.PluginError("Unable to find cert and/or key directives")

    logger.info("Deploying Certificate for %s to VirtualHost %s", domain, vhost.filep)

    if self.version < (2, 4, 8) or (chain_path and not fullchain_path):
        # install SSLCertificateFile, SSLCertificateKeyFile,
        # and SSLCertificateChainFile directives
        set_cert_path = cert_path
        self.aug.set(path["cert_path"][-1], cert_path)
        self.aug.set(path["cert_key"][-1], key_path)
        if chain_path is not None:
            self.parser.add_dir(vhost.path, "SSLCertificateChainFile", chain_path)
        else:
            raise errors.PluginError(
                "--chain-path is required for your version of Apache"
            )
    else:
        if not fullchain_path:
            raise errors.PluginError(
                "Please provide the --fullchain-path\
 option pointing to your full chain file"
            )
        set_cert_path = fullchain_path
        self.aug.set(path["cert_path"][-1], fullchain_path)
        self.aug.set(path["cert_key"][-1], key_path)

    # Enable the new vhost if needed
    if not vhost.enabled:
        self.enable_site(vhost)

    # Save notes about the transaction that took place
    self.save_notes += (
        "Changed vhost at %s with addresses of %s\n"
        "\tSSLCertificateFile %s\n"
        "\tSSLCertificateKeyFile %s\n"
        % (
            vhost.filep,
            ", ".join(str(addr) for addr in vhost.addrs),
            set_cert_path,
            key_path,
        )
    )
    if chain_path is not None:
        self.save_notes += "\tSSLCertificateChainFile %s\n" % chain_path