Fix ssh key printing

This commit is contained in:
Garrett Holmstrom 2012-09-22 17:50:21 -07:00
parent dcf5001c0a
commit bb974cc9d2
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,79 @@
Index: trunk/cloudinit/config/cc_ssh_authkey_fingerprints.py
===================================================================
--- trunk.orig/cloudinit/config/cc_ssh_authkey_fingerprints.py
+++ trunk/cloudinit/config/cc_ssh_authkey_fingerprints.py
@@ -21,7 +21,8 @@ import hashlib
from prettytable import PrettyTable
-from cloudinit import ssh_util
+from cloudinit.ssh_util import extract_authorized_keys as eak
+
from cloudinit import util
@@ -40,8 +41,9 @@ def _gen_fingerprint(b64_text, hash_meth
hasher = hashlib.new(hash_meth)
hasher.update(base64.b64decode(b64_text))
return ":".join(_split_hash(hasher.hexdigest()))
- except TypeError:
- # Raised when b64 not really b64...
+ except (TypeError, ValueError):
+ # Raised when b64 not really b64... or
+ # when the hash type isn't valid
return '?'
@@ -84,13 +86,48 @@ def _pprint_key_entries(user, key_fn, ke
stderr=False, console=True)
+def translate_user_name(uname, distro, log):
+ if not uname:
+ uname = ''
+ uname = uname.strip()
+ real_name = None
+ if uname.lower() == 'default':
+ try:
+ real_name = distro.get_default_user()
+ except NotImplementedError:
+ log.warn("Distro has not implemented default user "
+ "creation. No default user will be translated.")
+ else:
+ real_name = uname
+ return real_name
+
+
def handle(name, cfg, cloud, log, _args):
if 'no_ssh_fingerprints' in cfg:
log.debug(("Skipping module named %s, "
"logging of ssh fingerprints disabled"), name)
+ return
+
+ if not 'users' in cfg:
+ log.debug(("Skipping module named %s, "
+ "logging of ssh fingerprints disabled "
+ "since no user/s provided"), name)
+ return
+
+ users_to_hash = []
+ for user_config in cfg['users']:
+ user_name = None
+ if isinstance(user_config, (basestring, str)):
+ user_name = translate_user_name(user_config, cloud.distro, log)
+ elif isinstance(user_config, (dict)):
+ if 'name' in user_config:
+ user_name = translate_user_name(user_config['name'],
+ cloud.distro, log)
+ if user_name:
+ users_to_hash.append(user_name)
- user_name = util.get_cfg_option_str(cfg, "user", "ubuntu")
hash_meth = util.get_cfg_option_str(cfg, "authkey_hash", "md5")
- extract = ssh_util.extract_authorized_keys
- (auth_key_fn, auth_key_entries) = extract(user_name, cloud.paths)
- _pprint_key_entries(user_name, auth_key_fn, auth_key_entries, hash_meth)
+ for user_name in users_to_hash:
+ (auth_key_fn, auth_key_entries) = eak(user_name, cloud.paths)
+ _pprint_key_entries(user_name, auth_key_fn,
+ auth_key_entries, hash_meth)

View File

@ -16,6 +16,9 @@ Patch0: cloud-init-0.7.0-fedora.patch
# Make Fedora use the same hostname-updating code as Debian (/etc/hostname)
# https://code.launchpad.net/~gholms/cloud-init/hostname-refactor/+merge/125869
Patch1: cloud-init-0.7.0-hostname-refactor.patch
# Fix fingerprint printing caused by recent user code refactoring
# https://code.launchpad.net/~harlowja/cloud-init/patch-ssh-key-users/+merge/125606
Patch2: cloud-init-0.7.0-ssh-key-users.patch
BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
@ -52,6 +55,7 @@ ssh keys and to let the user run various scripts.
%setup -q -n %{name}-%{version}-bzr659
%patch0 -p1
%patch1 -p1
%patch2 -p1
cp -p %{SOURCE2} README.fedora
@ -132,6 +136,7 @@ fi
* Sat Sep 22 2012 Garrett Holmstrom <gholms@fedoraproject.org> - 0.7.0-0.2.bzr659
- Rebased against upstream rev 659
- Fixed hostname persistence
- Fixed ssh key printing
* Mon Sep 17 2012 Garrett Holmstrom <gholms@fedoraproject.org> - 0.7.0-0.1.bzr650
- Rebased against upstream rev 650