Пример #1
0
    def cleanup(self, achalls):  # pylint: disable=missing-function-docstring
        for achall in achalls:
            root_path = self.full_roots.get(achall.domain, None)
            if root_path is not None:
                validation_path = self._get_validation_path(root_path, achall)
                logger.debug("Removing %s", validation_path)
                os.remove(validation_path)
                self.performed[root_path].remove(achall)

                if not filesystem.POSIX_MODE:
                    web_config_path = os.path.join(root_path, "web.config")
                    if os.path.exists(web_config_path):
                        sha256sum = crypto_util.sha256sum(web_config_path)
                        if sha256sum in _WEB_CONFIG_SHA256SUMS:
                            logger.info("Cleaning web.config file generated by Certbot in %s.",
                                        root_path)
                            os.remove(web_config_path)
                        else:
                            logger.info("Not cleaning up the web.config file in %s "
                                        "because it is not generated by Certbot.", root_path)


        not_removed: List[str] = []
        while self._created_dirs:
            path = self._created_dirs.pop()
            try:
                os.rmdir(path)
            except OSError as exc:
                not_removed.insert(0, path)
                logger.info("Challenge directory %s was not empty, didn't remove", path)
                logger.debug("Error was: %s", exc)
        self._created_dirs = not_removed
        logger.debug("All challenges cleaned up")
Пример #2
0
 def _symlink_to_accounts_dir(self, prev_server_path, server_path):
     accounts_dir = self.config.accounts_dir_for_server_path(server_path)
     if os.path.islink(accounts_dir):
         os.unlink(accounts_dir)
     else:
         os.rmdir(accounts_dir)
     prev_account_dir = self.config.accounts_dir_for_server_path(prev_server_path)
     os.symlink(prev_account_dir, accounts_dir)
Пример #3
0
 def _symlink_to_accounts_dir(self, prev_server_path, server_path):
     accounts_dir = self.config.accounts_dir_for_server_path(server_path)
     if os.path.islink(accounts_dir):
         os.unlink(accounts_dir)
     else:
         os.rmdir(accounts_dir)
     prev_account_dir = self.config.accounts_dir_for_server_path(prev_server_path)
     os.symlink(prev_account_dir, accounts_dir)
Пример #4
0
 def test_no_archive(self):
     archive_dir = os.path.join(self.config.config_dir, "archive", "example.org")
     os.rmdir(archive_dir)
     self._call()
     self.assertFalse(os.path.exists(self.config_file.filename))
     self.assertFalse(os.path.exists(os.path.join(
         self.config.live_dir, "example.org")))
     self.assertFalse(os.path.exists(archive_dir))
Пример #5
0
 def test_no_archive(self):
     archive_dir = os.path.join(self.config.config_dir, "archive", "example.org")
     os.rmdir(archive_dir)
     self._call()
     self.assertFalse(os.path.exists(self.config_file.filename))
     self.assertFalse(os.path.exists(os.path.join(
         self.config.live_dir, "example.org")))
     self.assertFalse(os.path.exists(archive_dir))
Пример #6
0
    def test_cleanup_leftovers(self):
        self.auth.prepare()
        self.auth.perform([self.achall])

        leftover_path = os.path.join(self.root_challenge_path, 'leftover')
        filesystem.mkdir(leftover_path)

        self.auth.cleanup([self.achall])
        self.assertFalse(os.path.exists(self.validation_path))
        self.assertTrue(os.path.exists(self.root_challenge_path))

        os.rmdir(leftover_path)
Пример #7
0
    def test_cleanup_leftovers(self):
        self.auth.prepare()
        self.auth.perform([self.achall])

        leftover_path = os.path.join(self.root_challenge_path, 'leftover')
        os.mkdir(leftover_path)

        self.auth.cleanup([self.achall])
        self.assertFalse(os.path.exists(self.validation_path))
        self.assertTrue(os.path.exists(self.root_challenge_path))

        os.rmdir(leftover_path)
Пример #8
0
    def cleanup(self, achalls):  # pylint: disable=missing-function-docstring
        for achall in achalls:
            root_path = self.full_roots.get(achall.domain, None)
            if root_path is not None:
                validation_path = self._get_validation_path(root_path, achall)
                logger.debug("Removing %s", validation_path)
                os.remove(validation_path)
                self.performed[root_path].remove(achall)

        not_removed = []  # type: List[str]
        while self._created_dirs:
            path = self._created_dirs.pop()
            try:
                os.rmdir(path)
            except OSError as exc:
                not_removed.insert(0, path)
                logger.info("Challenge directory %s was not empty, didn't remove", path)
                logger.debug("Error was: %s", exc)
        self._created_dirs = not_removed
        logger.debug("All challenges cleaned up")
Пример #9
0
    def cleanup(self, achalls):  # pylint: disable=missing-docstring
        for achall in achalls:
            root_path = self.full_roots.get(achall.domain, None)
            if root_path is not None:
                validation_path = self._get_validation_path(root_path, achall)
                logger.debug("Removing %s", validation_path)
                os.remove(validation_path)
                self.performed[root_path].remove(achall)

        not_removed = []  # type: List[str]
        while self._created_dirs:
            path = self._created_dirs.pop()
            try:
                os.rmdir(path)
            except OSError as exc:
                not_removed.insert(0, path)
                logger.info("Challenge directory %s was not empty, didn't remove", path)
                logger.debug("Error was: %s", exc)
        self._created_dirs = not_removed
        logger.debug("All challenges cleaned up")
Пример #10
0
 def test_find_all_accounts_dir_absent(self):
     os.rmdir(self.config.accounts_dir)
     self.assertEqual([], self.storage.find_all())
Пример #11
0
 def _delete_accounts_dir_for_server_path(self, server_path):
     link_func = self.config.accounts_dir_for_server_path
     nonsymlinked_dir = self._delete_links_and_find_target_dir(server_path, link_func)
     os.rmdir(nonsymlinked_dir)
Пример #12
0
def delete_files(config, certname):
    """Delete all files related to the certificate.

    If some files are not found, ignore them and continue.
    """
    renewal_filename = renewal_file_for_certname(config, certname)
    # file exists
    full_default_archive_dir = full_archive_path(None, config, certname)
    full_default_live_dir = _full_live_path(config, certname)
    try:
        renewal_config = configobj.ConfigObj(renewal_filename)
    except configobj.ConfigObjError:
        # config is corrupted
        logger.warning(
            "Could not parse %s. You may wish to manually "
            "delete the contents of %s and %s.", renewal_filename,
            full_default_live_dir, full_default_archive_dir)
        raise errors.CertStorageError(
            "error parsing {0}".format(renewal_filename))
    finally:
        # we couldn't read it, but let's at least delete it
        # if this was going to fail, it already would have.
        os.remove(renewal_filename)
        logger.debug("Removed %s", renewal_filename)

    # cert files and (hopefully) live directory
    # it's not guaranteed that the files are in our default storage
    # structure. so, first delete the cert files.
    directory_names = set()
    for kind in ALL_FOUR:
        link = renewal_config.get(kind)
        try:
            os.remove(link)
            logger.debug("Removed %s", link)
        except OSError:
            logger.debug("Unable to delete %s", link)
        directory = os.path.dirname(link)
        directory_names.add(directory)

    # if all four were in the same directory, and the only thing left
    # is the README file (or nothing), delete that directory.
    # this will be wrong in very few but some cases.
    if len(directory_names) == 1:
        # delete the README file
        directory = directory_names.pop()
        readme_path = os.path.join(directory, README)
        try:
            os.remove(readme_path)
            logger.debug("Removed %s", readme_path)
        except OSError:
            logger.debug("Unable to delete %s", readme_path)
        # if it's now empty, delete the directory
        try:
            os.rmdir(directory)  # only removes empty directories
            logger.debug("Removed %s", directory)
        except OSError:
            logger.debug("Unable to remove %s; may not be empty.", directory)

    # archive directory
    try:
        archive_path = full_archive_path(renewal_config, config, certname)
        shutil.rmtree(archive_path)
        logger.debug("Removed %s", archive_path)
    except OSError:
        logger.debug("Unable to remove %s", archive_path)
Пример #13
0
 def _delete_accounts_dir_for_server_path(self, server_path):
     link_func = self.config.accounts_dir_for_server_path
     nonsymlinked_dir = self._delete_links_and_find_target_dir(server_path, link_func)
     os.rmdir(nonsymlinked_dir)
Пример #14
0
def delete_files(config, certname):
    """Delete all files related to the certificate.

    If some files are not found, ignore them and continue.
    """
    renewal_filename = renewal_file_for_certname(config, certname)
    # file exists
    full_default_archive_dir = full_archive_path(None, config, certname)
    full_default_live_dir = _full_live_path(config, certname)
    try:
        renewal_config = configobj.ConfigObj(renewal_filename)
    except configobj.ConfigObjError:
        # config is corrupted
        logger.warning("Could not parse %s. You may wish to manually "
            "delete the contents of %s and %s.", renewal_filename,
            full_default_live_dir, full_default_archive_dir)
        raise errors.CertStorageError(
            "error parsing {0}".format(renewal_filename))
    finally:
        # we couldn't read it, but let's at least delete it
        # if this was going to fail, it already would have.
        os.remove(renewal_filename)
        logger.debug("Removed %s", renewal_filename)

    # cert files and (hopefully) live directory
    # it's not guaranteed that the files are in our default storage
    # structure. so, first delete the cert files.
    directory_names = set()
    for kind in ALL_FOUR:
        link = renewal_config.get(kind)
        try:
            os.remove(link)
            logger.debug("Removed %s", link)
        except OSError:
            logger.debug("Unable to delete %s", link)
        directory = os.path.dirname(link)
        directory_names.add(directory)

    # if all four were in the same directory, and the only thing left
    # is the README file (or nothing), delete that directory.
    # this will be wrong in very few but some cases.
    if len(directory_names) == 1:
        # delete the README file
        directory = directory_names.pop()
        readme_path = os.path.join(directory, README)
        try:
            os.remove(readme_path)
            logger.debug("Removed %s", readme_path)
        except OSError:
            logger.debug("Unable to delete %s", readme_path)
        # if it's now empty, delete the directory
        try:
            os.rmdir(directory) # only removes empty directories
            logger.debug("Removed %s", directory)
        except OSError:
            logger.debug("Unable to remove %s; may not be empty.", directory)

    # archive directory
    try:
        archive_path = full_archive_path(renewal_config, config, certname)
        shutil.rmtree(archive_path)
        logger.debug("Removed %s", archive_path)
    except OSError:
        logger.debug("Unable to remove %s", archive_path)
Пример #15
0
 def test_find_all_accounts_dir_absent(self):
     os.rmdir(self.config.accounts_dir)
     self.assertEqual([], self.storage.find_all())