From 99e9954beebdaef94c13a35f3d0bbdf629d89185 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Thu, 20 Oct 2016 10:18:50 -0700 Subject: Revert "Fix authorized_key module to preserve the order of options" --- system/authorized_key.py | 92 ++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 61 deletions(-) diff --git a/system/authorized_key.py b/system/authorized_key.py index fb82579b..f8bbbadc 100644 --- a/system/authorized_key.py +++ b/system/authorized_key.py @@ -143,6 +143,7 @@ EXAMPLES = ''' # # see example in examples/playbooks +import sys import os import pwd import os.path @@ -150,74 +151,26 @@ import tempfile import re import shlex -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception -from ansible.module_utils.urls import fetch_url - class keydict(dict): - """ a dictionary that maintains the order of keys as they are added - - This has become an abuse of the dict interface. Probably should be - rewritten to be an entirely custom object with methods instead of - bracket-notation. - - Our requirements are for a data structure that: - * Preserves insertion order - * Can store multiple values for a single key. - - The present implementation has the following functions used by the rest of - the code: - - * __setitem__(): to add a key=value. The value can never be disassociated - with the key, only new values can be added in addition. - * items(): to retrieve the key, value pairs. - - Other dict methods should work but may be surprising. For instance, there - will be multiple keys that are the same in keys() and __getitem__() will - return a list of the values that have been set via __setitem__. - """ + """ a dictionary that maintains the order of keys as they are added """ # http://stackoverflow.com/questions/2328235/pythonextend-the-dict-class def __init__(self, *args, **kw): super(keydict,self).__init__(*args, **kw) self.itemlist = list(super(keydict,self).keys()) - def __setitem__(self, key, value): self.itemlist.append(key) - if key in self: - self[key].append(value) - else: - super(keydict, self).__setitem__(key, [value]) - + super(keydict,self).__setitem__(key, value) def __iter__(self): return iter(self.itemlist) - def keys(self): - return self.itemlist - - def _item_generator(self): - indexes = {} - for key in self.itemlist: - if key in indexes: - indexes[key] += 1 - else: - indexes[key] = 0 - yield key, self[key][indexes[key]] - - def iteritems(self): - return self._item_generator() - - def items(self): - return list(self.iteritems()) - - def itervalues(self): - return (item[1] for item in self.iteritems()) - + return list(set(self.itemlist)) def values(self): - return list(self.itervalues()) - + return [self[key] for key in self] + def itervalues(self): + return (self[key] for key in self) def keyfile(module, user, write=False, path=None, manage_dir=True): """ @@ -297,7 +250,13 @@ def parseoptions(module, options): for part in parts: if "=" in part: (key, value) = part.split("=", 1) - options_dict[key] = value + if key in options_dict: + if isinstance(options_dict[key], list): + options_dict[key].append(value) + else: + options_dict[key] = [options_dict[key], value] + else: + options_dict[key] = value elif part != ",": options_dict[part] = None @@ -387,11 +346,15 @@ def writekeys(module, filename, keys): option_str = "" if options: option_strings = [] - for option_key, value in options.items(): - if value is None: - option_strings.append("%s" % option_key) + for option_key in options.keys(): + if options[option_key]: + if isinstance(options[option_key], list): + for value in options[option_key]: + option_strings.append("%s=%s" % (option_key, value)) + else: + option_strings.append("%s=%s" % (option_key, options[option_key])) else: - option_strings.append("%s=%s" % (option_key, value)) + option_strings.append("%s" % option_key) option_str = ",".join(option_strings) option_str += " " key_line = "%s%s %s %s\n" % (option_str, type, keyhash, comment) @@ -416,6 +379,7 @@ def enforce_state(module, params): state = params.get("state", "present") key_options = params.get("key_options", None) exclusive = params.get("exclusive", False) + validate_certs = params.get("validate_certs", True) error_msg = "Error getting key from: %s" # if the key is a url, request it and use it as key source @@ -452,10 +416,12 @@ def enforce_state(module, params): parsed_options = parseoptions(module, key_options) parsed_new_key = (parsed_new_key[0], parsed_new_key[1], parsed_options, parsed_new_key[3]) + present = False matched = False non_matching_keys = [] if parsed_new_key[0] in existing_keys: + present = True # Then we check if everything matches, including # the key type and options. If not, we append this # existing key to the non-matching list @@ -505,6 +471,7 @@ def enforce_state(module, params): return params def main(): + module = AnsibleModule( argument_spec = dict( user = dict(required=True, type='str'), @@ -523,5 +490,8 @@ def main(): results = enforce_state(module, module.params) module.exit_json(**results) -if __name__ == '__main__': - main() +# import module snippets +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.urls import fetch_url +from ansible.module_utils.pycompat24 import get_exception +main() -- cgit v1.2.1