From 7bd70e6b7dc7df91a066ca11f065b8c55b845607 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Wed, 24 Oct 2012 00:55:39 +0200 Subject: [PATCH] ssh.GetUserFiles: Parameter to disable directory check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without this parameter, either an error would be raised or β.sshβ would have to be created. Now it is possible to retrieve the paths without requiring the β.sshβ directory to exist. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/ssh.py | 9 ++++++--- test/ganeti.ssh_unittest.py | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/ssh.py b/lib/ssh.py index 009071c9b..cec442df1 100644 --- a/lib/ssh.py +++ b/lib/ssh.py @@ -48,7 +48,7 @@ def FormatParamikoFingerprint(fingerprint): return ":".join(re.findall(r"..", fingerprint.lower())) -def GetUserFiles(user, mkdir=False, kind=constants.SSHK_DSA, +def GetUserFiles(user, mkdir=False, dircheck=True, kind=constants.SSHK_DSA, _homedir_fn=None): """Return the paths of a user's SSH files. @@ -56,6 +56,8 @@ def GetUserFiles(user, mkdir=False, kind=constants.SSHK_DSA, @param user: Username @type mkdir: bool @param mkdir: Whether to create ".ssh" directory if it doesn't exist + @type dircheck: bool + @param dircheck: Whether to check if ".ssh" directory exists @type kind: string @param kind: One of L{constants.SSHK_ALL} @rtype: tuple; (string, string, string) @@ -64,7 +66,8 @@ def GetUserFiles(user, mkdir=False, kind=constants.SSHK_DSA, @raise errors.OpExecError: When home directory of the user can not be determined @raise errors.OpExecError: Regardless of the C{mkdir} parameters, this - exception is raised if C{~$user/.ssh} is not a directory + exception is raised if C{~$user/.ssh} is not a directory and C{dircheck} + is set to C{True} """ if _homedir_fn is None: @@ -84,7 +87,7 @@ def GetUserFiles(user, mkdir=False, kind=constants.SSHK_DSA, ssh_dir = utils.PathJoin(user_dir, ".ssh") if mkdir: utils.EnsureDirs([(ssh_dir, constants.SECURE_DIR_MODE)]) - elif not os.path.isdir(ssh_dir): + elif dircheck and not os.path.isdir(ssh_dir): raise errors.OpExecError("Path %s is not a directory" % ssh_dir) return [utils.PathJoin(ssh_dir, base) diff --git a/test/ganeti.ssh_unittest.py b/test/ganeti.ssh_unittest.py index 77960c237..419c05e3e 100755 --- a/test/ganeti.ssh_unittest.py +++ b/test/ganeti.ssh_unittest.py @@ -124,6 +124,14 @@ class TestGetUserFiles(unittest.TestCase): self.assertEqual(os.listdir(self.tmpdir), [".ssh"]) self.assertEqual(os.listdir(sshdir), []) + def testNoDirCheck(self): + self.assertEqual(os.listdir(self.tmpdir), []) + + for kind in constants.SSHK_ALL: + ssh.GetUserFiles("example14528", mkdir=False, dircheck=False, kind=kind, + _homedir_fn=self._GetTempHomedir) + self.assertEqual(os.listdir(self.tmpdir), []) + if __name__ == "__main__": testutils.GanetiTestProgram() -- GitLab