From 3ec64d19a8ca5431b0106549edafeb4a630dd286 Mon Sep 17 00:00:00 2001 From: Stephen Fromm Date: Wed, 7 Nov 2012 16:43:09 -0800 Subject: [PATCH] Wrap getpwnam in try/except in authorized_key module This also adds module as an argument to keyfile(). --- library/authorized_key | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/library/authorized_key b/library/authorized_key index 0664727bbdb..1b8153aa688 100755 --- a/library/authorized_key +++ b/library/authorized_key @@ -71,7 +71,7 @@ import os.path import tempfile import shutil -def keyfile(user, write=False): +def keyfile(module, user, write=False): """ Calculate name of authorized keys file, optionally creating the directories and file, properly setting permissions. @@ -81,7 +81,10 @@ def keyfile(user, write=False): :return: full path string to authorized_keys for user """ - user_entry = pwd.getpwnam(user) + try: + user_entry = pwd.getpwnam(user) + except KeyError, e: + module.fail_json(msg="Failed to lookup user %s: %s" % (user, str(e))) homedir = user_entry.pw_dir sshdir = os.path.join(homedir, ".ssh") keysfile = os.path.join(sshdir, "authorized_keys") @@ -137,7 +140,7 @@ def enforce_state(module, params): state = params.get("state", "present") # check current state -- just get the filename, don't create file - params["keyfile"] = keyfile(user, write=False) + params["keyfile"] = keyfile(module, user, write=False) keys = readkeys(params["keyfile"]) present = key in keys @@ -146,13 +149,13 @@ def enforce_state(module, params): if present: module.exit_json(changed=False) keys.append(key) - writekeys(module, keyfile(user,write=True), keys) + writekeys(module, keyfile(module, user,write=True), keys) elif state=="absent": if not present: module.exit_json(changed=False) keys.remove(key) - writekeys(module, keyfile(user,write=True), keys) + writekeys(module, keyfile(module, user,write=True), keys) params['changed'] = True return params