Пример #1
0
 def test_no_nesting(self):
     """Test resolving an alias with no nested aliases."""
     data = "Cmnd_Alias SECONDCMDS=/path/to/second/cmd"
     mopen = self.get_mock_open(data)
     with mock.patch(self.open_patch_id, mopen) as _:
         sudoobj = Sudoers(path=self.fake_path)
         res = sudoobj._resolve_aliases("Cmnd_Alias", "SECONDCMDS")
         self.assertEqual(res, ['/path/to/second/cmd'])
Пример #2
0
    def test_easy_parse(self):
        """Correct parameters yield a correctly parsed sudoers file."""
        # Find the path to the test sudoers file
        sudoobj = Sudoers(path=self.test_correct_file)

        # Check all the internal values for aliases
        self.assertIn("SOMECMND", sudoobj._data["Cmnd_Alias"])
        self.assertEqual(sudoobj._data["Cmnd_Alias"]["SOMECMND"],
                         ["/path/to/the/command"])
        self.assertIn("SOMEHOSTS", sudoobj._data["Host_Alias"])
        self.assertEqual(sudoobj._data["Host_Alias"]["SOMEHOSTS"],
                         ["some-host1", "some-host2"])
        self.assertIn("SOMERUNAS", sudoobj._data["Runas_Alias"])
        self.assertEqual(sudoobj._data["Runas_Alias"]["SOMERUNAS"],
                         ["runuser"])
        self.assertIn("SOMEUSERS", sudoobj._data["User_Alias"])
        self.assertEqual(
            sudoobj._data["User_Alias"]["SOMEUSERS"],
            ["user1", "user2", "user3", "user4", "user5", "user6", "user7"])

        # Check internal values for defaults
        self.assertEqual(sudoobj._data["Defaults"],
                         ["Defaults !insults", "Defaults:SOMEUSERS !umask"])
        # Check internal values for rules
        self.assertEqual(sudoobj._data["Rules"], self.test_correct_rules)
Пример #3
0
 def test_at_includedir(self):
     """An includedir with an @ will not cause an exception."""
     # Find the path to the test sudoers file
     data = "@includedir /test/dir/file\n"
     mopen = self.get_mock_open(data)
     with mock.patch(self.open_patch_id, mopen) as _:
         _ = Sudoers(path=self.fake_path)
Пример #4
0
    def test_defaults(self):
        """Parameters are set correctly inside the class using defaults."""
        # Mock out "open" so we don't actually open a file
        mopen = self.get_mock_open()
        with mock.patch(self.open_patch_id, mopen) as mock_file:
            sudoobj = Sudoers(path=self.fake_path)
            mock_file.assert_called_with(self.fake_path, "r", encoding="ascii")

            # Check all the internal values
            self.assertEqual(sudoobj._path, self.fake_path)
Пример #5
0
 def test_single_nesting(self):
     """Test resolving an alias with single level of nested aliases."""
     data = """
         Cmnd_Alias CMDALIAS=FIRSTCMDS, SECONDCMDS
         Cmnd_Alias FIRSTCMDS=/path/to/first/cmd,/path/to/first/cmd2
         Cmnd_Alias SECONDCMDS=/path/to/second/cmd
     """
     mopen = self.get_mock_open(data)
     with mock.patch(self.open_patch_id, mopen) as _:
         sudoobj = Sudoers(path=self.fake_path)
         res1 = sudoobj._resolve_aliases("Cmnd_Alias", "CMDALIAS")
         res2 = sudoobj._resolve_aliases("Cmnd_Alias", "FIRSTCMDS")
         res3 = sudoobj._resolve_aliases("Cmnd_Alias", "SECONDCMDS")
         self.assertEqual(res1, [
             '/path/to/first/cmd', '/path/to/first/cmd2',
             '/path/to/second/cmd'
         ])
         self.assertEqual(res2,
                          ['/path/to/first/cmd', '/path/to/first/cmd2'])
         self.assertEqual(res3, ['/path/to/second/cmd'])
Пример #6
0
    def test_hard_coded(self):
        """Hard coded alias names are locked and shouldn't change."""
        # Mock out "open" so we don't actually open a file
        alias_names = ["Cmnd_Alias", "Host_Alias", "Runas_Alias", "User_Alias"]

        mopen = self.get_mock_open()
        with mock.patch(self.open_patch_id, mopen) as mock_file:
            sudoobj = Sudoers(path=self.fake_path)
            mock_file.assert_called_with(self.fake_path, "r", encoding="ascii")

            # Check all the internal values
            self.assertEqual(sudoobj._alias_types, alias_names)
Пример #7
0
    def test_data_setup(self):
        """Internal _data key structure is setup correctly."""
        # Mock out "open" so we don't actually open a file
        mopen = self.get_mock_open()
        with mock.patch(self.open_patch_id, mopen) as mock_file:
            sudoobj = Sudoers(path=self.fake_path)
            mock_file.assert_called_with(self.fake_path, "r", encoding="ascii")

            # Check all the internal values
            for alias in sudoobj._alias_types:
                self.assertIn(alias, sudoobj._data)

            self.assertIn("Defaults", sudoobj._data)
            self.assertIn("Rules", sudoobj._data)
Пример #8
0
 def test_escaped_split(self):
     """A command alias with embedded commas and escaped charaters will be correctly split."""
     # Find the path to the test sudoers file
     data = r"Cmnd_Alias AUDIT_CMDS = /bin/awk -F\: {OFS=FS; print "
     data += r"$1\,substr($2\,1\,4)\,$3\,$4\,$5\,$6\,$7\,$8\,$9} /var/log/audit.log"
     result = {
         "AUDIT_CMDS": [
             "/bin/awk -F\\:{OFS=FS; print " +
             "$1\\,substr($2\\,1\\,4)\\,$3\\,$4\\,$5\\,$6\\,$7\\,$8\\,$9} /var/log/audit.log"
         ]
     }
     mopen = self.get_mock_open(data)
     with mock.patch(self.open_patch_id, mopen) as _:
         sudoobj = Sudoers(path=self.fake_path)
         self.assertEqual(sudoobj.cmnd_aliases, result)
Пример #9
0
 def test_mapping(self):
     """Test resolving different alias types using accessor methods."""
     data = """
     Cmnd_Alias SOMECMDS=/path/to/second/cmd
     Host_Alias SOMEHOSTS=host1, host2,host3
     Runas_Alias SOMERUNAS=user1,user2
     User_Alias SOMEUSERS=user3, user4
     """
     mopen = self.get_mock_open(data)
     with mock.patch(self.open_patch_id, mopen) as _:
         sudoobj = Sudoers(path=self.fake_path)
         cmnd_res = sudoobj.resolve_command("SOMECMDS")
         host_res = sudoobj.resolve_host("SOMEHOSTS")
         runas_res = sudoobj.resolve_runas("SOMERUNAS")
         user_res = sudoobj.resolve_user("SOMEUSERS")
         self.assertEqual(cmnd_res, ["/path/to/second/cmd"])
         self.assertEqual(host_res, ["host1", "host2", "host3"])
         self.assertEqual(runas_res, ["user1", "user2"])
         self.assertEqual(user_res, ["user3", "user4"])
Пример #10
0
    def setUp(self):
        """Set up class-wide variables and mocks."""
        super().setUp()

        self.sudoobj = Sudoers(path=self.test_correct_file)