diff --git a/lib/utils.py b/lib/utils.py index e876e2c3e6433273e520d1766e192321b19e2f0c..3a0881fcd36e3b1f574115d5a915caca9ec3f1a6 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1785,6 +1785,13 @@ def SetupLogging(logfile, debug=False, stderr_logging=False, program="", # we need to re-raise the exception raise +def IsNormAbsPath(path): + """Check whether a path is absolute and also "normal". + + This avoids things like /dir/../../other/path to be valid. + + """ + return os.path.normpath(path) == path and os.path.isabs(path) def TailFile(fname, lines=20): """Return the last lines from a file. diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index 1c2992c3d67ab4353c4e9fc9c582133cbc3b2e23..433bfe3364d944e1fc3f32f7fd1eb671eb0a1808 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -44,7 +44,7 @@ from ganeti.utils import IsProcessAlive, RunCmd, \ ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \ ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \ SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree, OwnIpAddress, \ - TailFile, ForceDictType + TailFile, ForceDictType, IsNormAbsPath from ganeti.errors import LockError, UnitParseError, GenericError, \ ProgrammerError @@ -969,5 +969,23 @@ class TestForceDictType(unittest.TestCase): self.assertRaises(errors.TypeEnforcementError, self._fdt, {'d': '4 L'}) +class TestIsAbsNormPath(unittest.TestCase): + """Testing case for IsProcessAlive""" + + def _pathTestHelper(self, path, result): + if result: + self.assert_(IsNormAbsPath(path), + "Path %s should be absolute and normal" % path) + else: + self.assert_(not IsNormAbsPath(path), + "Path %s should not be absolute and normal" % path) + + def testBase(self): + self._pathTestHelper('/etc', True) + self._pathTestHelper('/srv', True) + self._pathTestHelper('etc', False) + self._pathTestHelper('/etc/../root', False) + self._pathTestHelper('/etc/', False) + if __name__ == '__main__': unittest.main()