Fixing some parsing issues in authorized_key module

Also adds an integration test for authorized_key for future validation.

Fixes #6700
reviewable/pr18780/r1
James Cammarata 11 years ago
parent dae8627e04
commit 09c998f104

@ -199,33 +199,19 @@ def parseoptions(module, options):
''' '''
options_dict = keydict() #ordered dict options_dict = keydict() #ordered dict
if options: if options:
token_exp = [ try:
# matches separator # the following regex will split on commas while
(r',+', False), # ignoring those commas that fall within quotes
# matches option with value, e.g. from="x,y" regex = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
(r'([a-z0-9-]+)="((?:[^"\\]|\\.)*)"', True), parts = regex.split(options)[1:-1]
# matches single option, e.g. no-agent-forwarding for part in parts:
(r'[a-z0-9-]+', True) if "=" in part:
] (key, value) = part.split("=", 1)
options_dict[key] = value
pos = 0 elif part != ",":
while pos < len(options): options_dict[part] = None
match = None except:
for pattern, is_valid_option in token_exp: module.fail_json(msg="invalid option string: %s" % options)
regex = re.compile(pattern, re.IGNORECASE)
match = regex.match(options, pos)
if match:
text = match.group(0)
if is_valid_option:
if len(match.groups()) == 2:
options_dict[match.group(1)] = match.group(2)
else:
options_dict[text] = None
break
if not match:
module.fail_json(msg="invalid option string: %s" % options)
else:
pos = match.end(0)
return options_dict return options_dict
@ -254,7 +240,7 @@ def parsekey(module, raw_key):
# split key safely # split key safely
lex = shlex.shlex(raw_key) lex = shlex.shlex(raw_key)
lex.quotes = ["'", '"'] lex.quotes = []
lex.commenters = '' #keep comment hashes lex.commenters = '' #keep comment hashes
lex.whitespace_split = True lex.whitespace_split = True
key_parts = list(lex) key_parts = list(lex)
@ -315,7 +301,7 @@ def writekeys(module, filename, keys):
option_strings = [] option_strings = []
for option_key in options.keys(): for option_key in options.keys():
if options[option_key]: if options[option_key]:
option_strings.append("%s=\"%s\"" % (option_key, options[option_key])) option_strings.append("%s=%s" % (option_key, options[option_key]))
else: else:
option_strings.append("%s" % option_key) option_strings.append("%s" % option_key)

Loading…
Cancel
Save