updated looups to use set_option in custom parse (#82425)

limit password params
  updated test error message catching
  make sure we reset params for each term
  ensure we only update options if we have em
pull/81765/head
Brian Coca 4 months ago committed by GitHub
parent f73d72e830
commit 8b2dd5fdd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- All core lookups now use set_option(s) even when doing their own custom parsing. This ensures that the options are always the proper type.

@ -160,6 +160,7 @@ class LookupModule(LookupBase):
# parameters override per term using k/v # parameters override per term using k/v
try: try:
reset_params = False
for name, value in kv.items(): for name, value in kv.items():
if name == '_raw_params': if name == '_raw_params':
continue continue
@ -167,7 +168,11 @@ class LookupModule(LookupBase):
raise AnsibleAssertionError('%s is not a valid option' % name) raise AnsibleAssertionError('%s is not a valid option' % name)
self._deprecate_inline_kv() self._deprecate_inline_kv()
paramvals[name] = value self.set_option(name, value)
reset_params = True
if reset_params:
paramvals = self.get_options()
except (ValueError, AssertionError) as e: except (ValueError, AssertionError) as e:
raise AnsibleError(e) raise AnsibleError(e)

@ -154,16 +154,20 @@ class LookupModule(LookupBase):
params = _parse_params(term, paramvals) params = _parse_params(term, paramvals)
try: try:
updated_key = False updated_key = False
updated_options = False
for param in params: for param in params:
if '=' in param: if '=' in param:
name, value = param.split('=') name, value = param.split('=')
if name not in paramvals: if name not in paramvals:
raise AnsibleLookupError('%s is not a valid option.' % name) raise AnsibleLookupError('%s is not a valid option.' % name)
paramvals[name] = value self.set_option(name, value)
updated_options = True
elif key == term: elif key == term:
# only take first, this format never supported multiple keys inline # only take first, this format never supported multiple keys inline
key = param key = param
updated_key = True updated_key = True
if updated_options:
paramvals = self.get_options()
except ValueError as e: except ValueError as e:
# bad params passed # bad params passed
raise AnsibleLookupError("Could not use '%s' from '%s': %s" % (param, params, to_native(e)), orig_exc=e) raise AnsibleLookupError("Could not use '%s' from '%s': %s" % (param, params, to_native(e)), orig_exc=e)

@ -331,29 +331,32 @@ class LookupModule(LookupBase):
if invalid_params: if invalid_params:
raise AnsibleError('Unrecognized parameter(s) given to password lookup: %s' % ', '.join(invalid_params)) raise AnsibleError('Unrecognized parameter(s) given to password lookup: %s' % ', '.join(invalid_params))
# Set defaults # update options with what we got
params['length'] = int(params.get('length', self.get_option('length'))) if params:
params['encrypt'] = params.get('encrypt', self.get_option('encrypt')) self.set_options(direct=params)
params['ident'] = params.get('ident', self.get_option('ident'))
params['seed'] = params.get('seed', self.get_option('seed')) # chars still might need more
chars = params.get('chars', self.get_option('chars'))
params['chars'] = params.get('chars', self.get_option('chars')) if chars and isinstance(chars, string_types):
if params['chars'] and isinstance(params['chars'], string_types):
tmp_chars = [] tmp_chars = []
if u',,' in params['chars']: if u',,' in chars:
tmp_chars.append(u',') tmp_chars.append(u',')
tmp_chars.extend(c for c in params['chars'].replace(u',,', u',').split(u',') if c) tmp_chars.extend(c for c in chars.replace(u',,', u',').split(u',') if c)
params['chars'] = tmp_chars self.set_option('chars', tmp_chars)
# return processed params
for field in VALID_PARAMS:
params[field] = self.get_option(field)
return relpath, params return relpath, params
def run(self, terms, variables, **kwargs): def run(self, terms, variables, **kwargs):
ret = [] ret = []
self.set_options(var_options=variables, direct=kwargs)
for term in terms: for term in terms:
self.set_options(var_options=variables, direct=kwargs)
changed = None changed = None
relpath, params = self._parse_parameters(term) relpath, params = self._parse_parameters(term)
path = self._loader.path_dwim(relpath) path = self._loader.path_dwim(relpath)

@ -19,21 +19,21 @@ DOCUMENTATION = """
options: options:
start: start:
description: number at which to start the sequence description: number at which to start the sequence
default: 0 default: 1
type: integer type: integer
end: end:
description: number at which to end the sequence, dont use this with count description: number at which to end the sequence, dont use this with count
type: integer type: integer
default: 0
count: count:
description: number of elements in the sequence, this is not to be used with end description: number of elements in the sequence, this is not to be used with end
type: integer type: integer
default: 0
stride: stride:
description: increments between sequence numbers, the default is 1 unless the end is less than the start, then it is -1. description: increments between sequence numbers, the default is 1 unless the end is less than the start, then it is -1.
type: integer type: integer
default: 1
format: format:
description: return a string with the generated number formatted in description: return a string with the generated number formatted in
default: "%d"
""" """
EXAMPLES = """ EXAMPLES = """
@ -97,6 +97,7 @@ SHORTCUT = re_compile(
"(:(.+))?$", # Group 5, Group 6: Format String "(:(.+))?$", # Group 5, Group 6: Format String
IGNORECASE IGNORECASE
) )
FIELDS = frozenset(('start', 'end', 'stride', 'count', 'format'))
class LookupModule(LookupBase): class LookupModule(LookupBase):
@ -138,30 +139,12 @@ class LookupModule(LookupBase):
calculating the number of entries in a sequence when a stride is specified. calculating the number of entries in a sequence when a stride is specified.
""" """
def reset(self):
"""set sensible defaults"""
self.start = 1
self.count = None
self.end = None
self.stride = 1
self.format = "%d"
def parse_kv_args(self, args): def parse_kv_args(self, args):
"""parse key-value style arguments""" """parse key-value style arguments"""
for arg in ["start", "end", "count", "stride"]: for arg in FIELDS:
try: value = args.pop(arg, None)
arg_raw = args.pop(arg, None) if value is not None:
if arg_raw is None: self.set_option(arg, value)
continue
arg_cooked = int(arg_raw, 0)
setattr(self, arg, arg_cooked)
except ValueError:
raise AnsibleError(
"can't parse %s=%s as integer"
% (arg, arg_raw)
)
if 'format' in args:
self.format = args.pop("format")
if args: if args:
raise AnsibleError( raise AnsibleError(
"unrecognized arguments to with_sequence: %s" "unrecognized arguments to with_sequence: %s"
@ -176,33 +159,17 @@ class LookupModule(LookupBase):
dummy, start, end, dummy, stride, dummy, format = match.groups() dummy, start, end, dummy, stride, dummy, format = match.groups()
if start is not None: for key in FIELDS:
try: value = locals().get(key, None)
start = int(start, 0) if value is not None:
except ValueError: self.set_option(key, value)
raise AnsibleError("can't parse start=%s as integer" % start)
if end is not None:
try:
end = int(end, 0)
except ValueError:
raise AnsibleError("can't parse end=%s as integer" % end)
if stride is not None:
try:
stride = int(stride, 0)
except ValueError:
raise AnsibleError("can't parse stride=%s as integer" % stride)
if start is not None:
self.start = start
if end is not None:
self.end = end
if stride is not None:
self.stride = stride
if format is not None:
self.format = format
return True return True
def set_fields(self):
for f in FIELDS:
setattr(self, f, self.get_option(f))
def sanity_check(self): def sanity_check(self):
if self.count is None and self.end is None: if self.count is None and self.end is None:
raise AnsibleError("must specify count or end in with_sequence") raise AnsibleError("must specify count or end in with_sequence")
@ -245,7 +212,8 @@ class LookupModule(LookupBase):
for term in terms: for term in terms:
try: try:
self.reset() # clear out things for this iteration # set defaults/global
self.set_options(direct=kwargs)
try: try:
if not self.parse_simple_args(term): if not self.parse_simple_args(term):
self.parse_kv_args(parse_kv(term)) self.parse_kv_args(parse_kv(term))
@ -254,7 +222,9 @@ class LookupModule(LookupBase):
except Exception as e: except Exception as e:
raise AnsibleError("unknown error parsing with_sequence arguments: %r. Error was: %s" % (term, e)) raise AnsibleError("unknown error parsing with_sequence arguments: %r. Error was: %s" % (term, e))
self.set_fields()
self.sanity_check() self.sanity_check()
if self.stride != 0: if self.stride != 0:
results.extend(self.generate_sequence()) results.extend(self.generate_sequence())
except AnsibleError: except AnsibleError:

@ -89,7 +89,7 @@
- assert: - assert:
that: that:
- ansible_failed_task.name == "EXPECTED FAILURE - test bad kv value" - ansible_failed_task.name == "EXPECTED FAILURE - test bad kv value"
- ansible_failed_result.msg == "can't parse start=A as integer" - ansible_failed_result.msg.startswith("Invalid type for")
- block: - block:
- name: EXPECTED FAILURE - test bad simple form start value - name: EXPECTED FAILURE - test bad simple form start value
@ -102,7 +102,7 @@
- assert: - assert:
that: that:
- ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form start value" - ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form start value"
- ansible_failed_result.msg == "can't parse start=A as integer" - ansible_failed_result.msg.startswith("Invalid type for")
- block: - block:
- name: EXPECTED FAILURE - test bad simple form end value - name: EXPECTED FAILURE - test bad simple form end value
@ -115,7 +115,7 @@
- assert: - assert:
that: that:
- ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form end value" - ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form end value"
- ansible_failed_result.msg == "can't parse end=B as integer" - ansible_failed_result.msg.startswith("Invalid type for")
- block: - block:
- name: EXPECTED FAILURE - test bad simple form stride value - name: EXPECTED FAILURE - test bad simple form stride value
@ -128,7 +128,7 @@
- assert: - assert:
that: that:
- ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form stride value" - ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form stride value"
- ansible_failed_result.msg == "can't parse stride=C as integer" - ansible_failed_result.msg.startswith("Invalid type for")
- block: - block:
- name: EXPECTED FAILURE - test no count or end - name: EXPECTED FAILURE - test no count or end

Loading…
Cancel
Save