Exemple #1
0
 def test_right_mode(self):
     fd1, name1 = self._call(0o700)
     fd2, name2 = self._call(0o600)
     self.assertTrue(misc.compare_file_modes(0o700, os.stat(name1).st_mode))
     self.assertTrue(misc.compare_file_modes(0o600, os.stat(name2).st_mode))
     fd1.close()
     fd2.close()
Exemple #2
0
 def test_right_mode(self):
     fd1, name1 = self._call(0o700)
     fd2, name2 = self._call(0o600)
     self.assertTrue(misc.compare_file_modes(0o700, os.stat(name1).st_mode))
     self.assertTrue(misc.compare_file_modes(0o600, os.stat(name2).st_mode))
     fd1.close()
     fd2.close()
Exemple #3
0
 def test_save_successor_maintains_group_mode(self, mock_rv):
     # Mock relevant_values() to claim that all values are relevant here
     # (to avoid instantiating parser)
     mock_rv.side_effect = lambda x: x
     for kind in ALL_FOUR:
         self._write_out_kind(kind, 1)
     self.test_rc.update_all_links_to(1)
     self.assertTrue(
         misc.compare_file_modes(
             os.stat(self.test_rc.version("privkey", 1)).st_mode, 0o600))
     filesystem.chmod(self.test_rc.version("privkey", 1), 0o444)
     # If no new key, permissions should be the same (we didn't write any keys)
     self.test_rc.save_successor(1, b"newcert", None, b"new chain",
                                 self.config)
     self.assertTrue(
         misc.compare_file_modes(
             os.stat(self.test_rc.version("privkey", 2)).st_mode, 0o444))
     # If new key, permissions should be kept as 644
     self.test_rc.save_successor(2, b"newcert", b"new_privkey",
                                 b"new chain", self.config)
     self.assertTrue(
         misc.compare_file_modes(
             os.stat(self.test_rc.version("privkey", 3)).st_mode, 0o644))
     # If permissions reverted, next renewal will also revert permissions of new key
     filesystem.chmod(self.test_rc.version("privkey", 3), 0o400)
     self.test_rc.save_successor(3, b"newcert", b"new_privkey",
                                 b"new chain", self.config)
     self.assertTrue(
         misc.compare_file_modes(
             os.stat(self.test_rc.version("privkey", 4)).st_mode, 0o600))
Exemple #4
0
    def test_new_lineage(self, mock_rv):
        """Test for new_lineage() class method."""
        # Mock relevant_values to say everything is relevant here (so we
        # don't have to mock the parser to help it decide!)
        mock_rv.side_effect = lambda x: x

        from certbot import storage
        result = storage.RenewableCert.new_lineage("the-lineage.com", b"cert",
                                                   b"privkey", b"chain",
                                                   self.config)
        # This consistency check tests most relevant properties about the
        # newly created cert lineage.
        # pylint: disable=protected-access
        self.assertTrue(result._consistent())
        self.assertTrue(
            os.path.exists(
                os.path.join(self.config.renewal_configs_dir,
                             "the-lineage.com.conf")))
        self.assertTrue(
            os.path.exists(os.path.join(self.config.live_dir, "README")))
        self.assertTrue(
            os.path.exists(
                os.path.join(self.config.live_dir, "the-lineage.com",
                             "README")))
        self.assertTrue(
            misc.compare_file_modes(os.stat(result.key_path).st_mode, 0o600))
        with open(result.fullchain, "rb") as f:
            self.assertEqual(f.read(), b"cert" + b"chain")
        # Let's do it again and make sure it makes a different lineage
        result = storage.RenewableCert.new_lineage("the-lineage.com", b"cert2",
                                                   b"privkey2", b"chain2",
                                                   self.config)
        self.assertTrue(
            os.path.exists(
                os.path.join(self.config.renewal_configs_dir,
                             "the-lineage.com-0001.conf")))
        self.assertTrue(
            os.path.exists(
                os.path.join(self.config.live_dir, "the-lineage.com-0001",
                             "README")))
        # Now trigger the detection of already existing files
        filesystem.mkdir(
            os.path.join(self.config.live_dir, "the-lineage.com-0002"))
        self.assertRaises(errors.CertStorageError,
                          storage.RenewableCert.new_lineage, "the-lineage.com",
                          b"cert3", b"privkey3", b"chain3", self.config)
        filesystem.mkdir(
            os.path.join(self.config.default_archive_dir, "other-example.com"))
        self.assertRaises(errors.CertStorageError,
                          storage.RenewableCert.new_lineage,
                          "other-example.com", b"cert4", b"privkey4",
                          b"chain4", self.config)
        # Make sure it can accept renewal parameters
        result = storage.RenewableCert.new_lineage("the-lineage.com", b"cert2",
                                                   b"privkey2", b"chain2",
                                                   self.config)
Exemple #5
0
    def test_perform_permissions(self):
        self.auth.prepare()

        # Remove exec bit from permission check, so that it
        # matches the file
        self.auth.perform([self.achall])
        self.assertTrue(misc.compare_file_modes(os.stat(self.validation_path).st_mode, 0o644))

        # Check permissions of the directories

        for dirpath, dirnames, _ in os.walk(self.path):
            for directory in dirnames:
                full_path = os.path.join(dirpath, directory)
                self.assertTrue(misc.compare_file_modes(os.stat(full_path).st_mode, 0o755))

        parent_gid = os.stat(self.path).st_gid
        parent_uid = os.stat(self.path).st_uid

        self.assertEqual(os.stat(self.validation_path).st_gid, parent_gid)
        self.assertEqual(os.stat(self.validation_path).st_uid, parent_uid)
Exemple #6
0
    def test_perform_permissions(self):
        self.auth.prepare()

        # Remove exec bit from permission check, so that it
        # matches the file
        self.auth.perform([self.achall])
        self.assertTrue(misc.compare_file_modes(os.stat(self.validation_path).st_mode, 0o644))

        # Check permissions of the directories

        for dirpath, dirnames, _ in os.walk(self.path):
            for directory in dirnames:
                full_path = os.path.join(dirpath, directory)
                self.assertTrue(misc.compare_file_modes(os.stat(full_path).st_mode, 0o755))

        parent_gid = os.stat(self.path).st_gid
        parent_uid = os.stat(self.path).st_uid

        self.assertEqual(os.stat(self.validation_path).st_gid, parent_gid)
        self.assertEqual(os.stat(self.validation_path).st_uid, parent_uid)
Exemple #7
0
def check_permissions(filepath, mode, uid=0):
    """Check file or directory permissions.

    :param str filepath: Path to the tested file (or directory).
    :param int mode: Expected file mode.
    :param int uid: Expected file owner.

    :returns: True if `mode` and `uid` match, False otherwise.
    :rtype: bool

    """
    file_stat = os.stat(filepath)
    return misc.compare_file_modes(file_stat.st_mode, mode) and file_stat.st_uid == uid
Exemple #8
0
 def test_save_successor_maintains_group_mode(self, mock_rv):
     # Mock relevant_values() to claim that all values are relevant here
     # (to avoid instantiating parser)
     mock_rv.side_effect = lambda x: x
     for kind in ALL_FOUR:
         self._write_out_kind(kind, 1)
     self.test_rc.update_all_links_to(1)
     self.assertTrue(misc.compare_file_modes(
         os.stat(self.test_rc.version("privkey", 1)).st_mode, 0o600))
     os.chmod(self.test_rc.version("privkey", 1), 0o444)
     # If no new key, permissions should be the same (we didn't write any keys)
     self.test_rc.save_successor(1, b"newcert", None, b"new chain", self.config)
     self.assertTrue(misc.compare_file_modes(
         os.stat(self.test_rc.version("privkey", 2)).st_mode, 0o444))
     # If new key, permissions should be kept as 644
     self.test_rc.save_successor(2, b"newcert", b"new_privkey", b"new chain", self.config)
     self.assertTrue(misc.compare_file_modes(
         os.stat(self.test_rc.version("privkey", 3)).st_mode, 0o644))
     # If permissions reverted, next renewal will also revert permissions of new key
     os.chmod(self.test_rc.version("privkey", 3), 0o400)
     self.test_rc.save_successor(3, b"newcert", b"new_privkey", b"new chain", self.config)
     self.assertTrue(misc.compare_file_modes(
         os.stat(self.test_rc.version("privkey", 4)).st_mode, 0o600))
Exemple #9
0
    def test_new_lineage(self, mock_rv):
        """Test for new_lineage() class method."""
        # Mock relevant_values to say everything is relevant here (so we
        # don't have to mock the parser to help it decide!)
        mock_rv.side_effect = lambda x: x

        from certbot import storage
        result = storage.RenewableCert.new_lineage(
            "the-lineage.com", b"cert", b"privkey", b"chain", self.config)
        # This consistency check tests most relevant properties about the
        # newly created cert lineage.
        # pylint: disable=protected-access
        self.assertTrue(result._consistent())
        self.assertTrue(os.path.exists(os.path.join(
            self.config.renewal_configs_dir, "the-lineage.com.conf")))
        self.assertTrue(os.path.exists(os.path.join(
            self.config.live_dir, "README")))
        self.assertTrue(os.path.exists(os.path.join(
            self.config.live_dir, "the-lineage.com", "README")))
        self.assertTrue(misc.compare_file_modes(os.stat(result.key_path).st_mode, 0o600))
        with open(result.fullchain, "rb") as f:
            self.assertEqual(f.read(), b"cert" + b"chain")
        # Let's do it again and make sure it makes a different lineage
        result = storage.RenewableCert.new_lineage(
            "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config)
        self.assertTrue(os.path.exists(os.path.join(
            self.config.renewal_configs_dir, "the-lineage.com-0001.conf")))
        self.assertTrue(os.path.exists(os.path.join(
            self.config.live_dir, "the-lineage.com-0001", "README")))
        # Now trigger the detection of already existing files
        os.mkdir(os.path.join(
            self.config.live_dir, "the-lineage.com-0002"))
        self.assertRaises(errors.CertStorageError,
                          storage.RenewableCert.new_lineage, "the-lineage.com",
                          b"cert3", b"privkey3", b"chain3", self.config)
        os.mkdir(os.path.join(self.config.default_archive_dir, "other-example.com"))
        self.assertRaises(errors.CertStorageError,
                          storage.RenewableCert.new_lineage,
                          "other-example.com", b"cert4",
                          b"privkey4", b"chain4", self.config)
        # Make sure it can accept renewal parameters
        result = storage.RenewableCert.new_lineage(
            "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config)
Exemple #10
0
 def test_existing_correct_mode_does_not_fail(self):
     self._call(self.path, 0o600)
     self.assertTrue(
         misc.compare_file_modes(os.stat(self.path).st_mode, 0o600))
Exemple #11
0
 def test_creates_dir_when_missing(self):
     path = os.path.join(self.tempdir, "bar")
     self._call(path, 0o650)
     self.assertTrue(os.path.isdir(path))
     self.assertTrue(misc.compare_file_modes(os.stat(path).st_mode, 0o650))
Exemple #12
0
 def test_existing_correct_mode_does_not_fail(self):
     self._call(self.path, 0o600)
     self.assertTrue(misc.compare_file_modes(os.stat(self.path).st_mode, 0o600))
Exemple #13
0
 def test_creates_dir_when_missing(self):
     path = os.path.join(self.tempdir, "bar")
     self._call(path, 0o650)
     self.assertTrue(os.path.isdir(path))
     self.assertTrue(misc.compare_file_modes(os.stat(path).st_mode, 0o650))