Skip to content
Snippets Groups Projects
Commit 7bd70e6b authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

ssh.GetUserFiles: Parameter to disable directory check


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: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 879d9290
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment