Skip to content

Instantly share code, notes, and snippets.

@jimi-c
Created October 28, 2013 16:55
Show Gist options
  • Save jimi-c/7200498 to your computer and use it in GitHub Desktop.
Save jimi-c/7200498 to your computer and use it in GitHub Desktop.
diff --git a/library/system/authorized_key b/library/system/authorized_key
index 54c9576..af4810d 100644
--- a/library/system/authorized_key
+++ b/library/system/authorized_key
@@ -210,29 +210,36 @@ def parsekey(raw_key):
'ssh-rsa',
]
+ options = None # connection options
+ key = None # encrypted key string
+ key_type = None # type of ssh key
+ type_index = None # index of keytype in key string|list
+
key_parts = shlex.split(raw_key)
- if len(key_parts) >= 4 and key_parts[1] in VALID_SSH2_KEY_TYPES:
- # this line contains options
- (options,type,key) = key_parts[0:3]
- comment = " ".join(key_parts[3:])
- elif len(key_parts) >= 3 and key_parts[0] in VALID_SSH2_KEY_TYPES:
- # this line is just 'type key user@host'
- (type,key) = key_parts[0:2]
- comment = " ".join(key_parts[2:])
- options = None
- elif len(key_parts) == 2 and key_parts[0] in VALID_SSH2_KEY_TYPES:
- # assuming just a type/key with no comment
- (type,key) = key_parts
- comment = ""
- options = None
- else:
- # invalid key, maybe a comment?
+ for i in range(0, len(key_parts)):
+ if key_parts[i] in VALID_SSH2_KEY_TYPES:
+ type_index = i
+ key_type = key_parts[i]
+ break
+
+ # check for options
+ if type_index is None:
return None
-
- if options:
+ elif type_index == 1:
# parse the options and store them
- options = parseoptions(options)
- return (key, type, options, comment)
+ options = key_parts[0]
+
+ # parse the options (if any)
+ options = parseoptions(options)
+
+ # get key after the type index
+ key = key_parts[(type_index + 1)]
+
+ # set comment to everything after the key
+ if len(key_parts) > (type_index + 1):
+ comment = " ".join(key_parts[(type_index + 2):])
+
+ return (key, key_type, options, comment)
def readkeys(filename):
@@ -268,7 +275,7 @@ def writekeys(module, filename, keys):
if options[option_key]:
option_strings.append("%s=\"%s\"" % (option_key, options[option_key]))
else:
- option_strings.append("%s " % option_key)
+ option_strings.append("%s" % option_key)
option_str = ",".join(option_strings)
option_str += " "
@@ -331,8 +338,9 @@ def enforce_state(module, params):
if state=="present":
if unique and len(non_matching_keys) > 0:
for non_matching_key in non_matching_keys:
- del existing_keys[non_matching_key[0]]
- do_write = True
+ if non_matching_key[0] in existing_keys:
+ del existing_keys[non_matching_key[0]]
+ do_write = True
if not matched:
existing_keys[parsed_new_key[0]] = parsed_new_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment