From e88f6ed4c46fcb1069fe899606a8b6d95411c13f Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 21 Oct 2014 11:55:16 -0700 Subject: Handle strings/text type for 'ssh_authorized_keys' Instead of only expected a list, tuple, or set type allow for a string type to be passed in, and add log message that occurs if some other type is used that can not be correctly processed. --- cloudinit/distros/__init__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 2599d9f2..d30098eb 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -387,8 +387,17 @@ class Distro(object): # Import SSH keys if 'ssh_authorized_keys' in kwargs: - keys = set(kwargs['ssh_authorized_keys']) or [] - ssh_util.setup_user_keys(keys, name, options=None) + # Try to handle this in a smart manner. + keys = kwargs['ssh_authorized_keys'] + if isinstance(keys, (basestring, str)): + keys = [keys] + if not isinstance(keys, (tuple, list, set)): + util.multi_log("Invalid type detected for" + " 'ssh_authorized_keys', expected list, string" + " or set.") + else: + keys = set(keys) or [] + ssh_util.setup_user_keys(keys, name, options=None) return True -- cgit v1.2.3 From 6fb6cfdea6ec31a69e749ddb638051c39256e7f3 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 21 Oct 2014 12:00:53 -0700 Subject: Also allow a dict to be used When a dict is passed in for 'ssh_authorized_keys' just extract the keys from the values of the dict (and discard the keys). --- cloudinit/distros/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index d30098eb..762529a6 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -391,10 +391,12 @@ class Distro(object): keys = kwargs['ssh_authorized_keys'] if isinstance(keys, (basestring, str)): keys = [keys] + if isinstance(keys, dict): + keys = list(keys.values()) if not isinstance(keys, (tuple, list, set)): util.multi_log("Invalid type detected for" " 'ssh_authorized_keys', expected list, string" - " or set.") + " , dict, or set.") else: keys = set(keys) or [] ssh_util.setup_user_keys(keys, name, options=None) -- cgit v1.2.3 From a6336edea275c409791807d16c1575ebd6895c9c Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 21 Oct 2014 12:08:50 -0700 Subject: Fix the word spacing --- cloudinit/distros/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 762529a6..7b05226a 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -396,7 +396,7 @@ class Distro(object): if not isinstance(keys, (tuple, list, set)): util.multi_log("Invalid type detected for" " 'ssh_authorized_keys', expected list, string" - " , dict, or set.") + ", dict, or set.") else: keys = set(keys) or [] ssh_util.setup_user_keys(keys, name, options=None) -- cgit v1.2.3 From 477a5418d55d45ddad55fcaa16ab3ac53652fdb9 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 21 Oct 2014 12:23:09 -0700 Subject: Use LOG.warn and handle the None case as well --- cloudinit/distros/__init__.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 7b05226a..83c2eebf 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -393,13 +393,14 @@ class Distro(object): keys = [keys] if isinstance(keys, dict): keys = list(keys.values()) - if not isinstance(keys, (tuple, list, set)): - util.multi_log("Invalid type detected for" - " 'ssh_authorized_keys', expected list, string" - ", dict, or set.") - else: - keys = set(keys) or [] - ssh_util.setup_user_keys(keys, name, options=None) + if keys is not None: + if not isinstance(keys, (tuple, list, set)): + LOG.warn("Invalid type '%s' detected for" + " 'ssh_authorized_keys', expected list," + " string, dict, or set.", type(keys)) + else: + keys = set(keys) or [] + ssh_util.setup_user_keys(keys, name, options=None) return True -- cgit v1.2.3