Log an exception when non-string val/expr passed to expr_match

This also removes inaccurate logging from check_whitelist_blacklist
This commit is contained in:
Erik Johnson 2018-03-27 16:13:04 -05:00
parent dac42a672b
commit ae0f112a49
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -292,11 +292,11 @@ def build_whitespace_split_regex(text):
return r'(?m)^{0}$'.format(regex)
def expr_match(line, expr):
def expr_match(val, expr):
'''
Evaluate a line of text against an expression. Tries to match expr first as
a glob using fnmatch.fnmatch(), and then tries to match expr as a regular
expression. Originally designed to match minion IDs for
Checks whether or not val matches the specified expression. Tries to match
expr first as a glob using fnmatch.fnmatch(), and then tries to match expr
as a regular expression. Originally designed to match minion IDs for
whitelists/blacklists.
Note that this also does exact matches, as fnmatch.fnmatch() will return
@ -308,15 +308,15 @@ def expr_match(line, expr):
True
'''
try:
if fnmatch.fnmatch(line, expr):
if fnmatch.fnmatch(val, expr):
return True
try:
if re.match(r'\A{0}\Z'.format(expr), val):
return True
except re.error:
pass
except TypeError:
pass
try:
if re.match(r'\A{0}\Z'.format(expr), line):
return True
except re.error:
pass
log.exception('Value %r or expression %r is not a string', val, expr)
return False
@ -346,22 +346,16 @@ def check_whitelist_blacklist(value, whitelist=None, blacklist=None):
if blacklist is not None:
if not hasattr(blacklist, '__iter__'):
blacklist = [blacklist]
try:
for expr in blacklist:
if expr_match(value, expr):
return False
except TypeError:
log.error('Non-iterable blacklist %s', blacklist)
for expr in blacklist:
if expr_match(value, expr):
return False
if whitelist:
if not hasattr(whitelist, '__iter__'):
whitelist = [whitelist]
try:
for expr in whitelist:
if expr_match(value, expr):
return True
except TypeError:
log.error('Non-iterable whitelist %s', whitelist)
for expr in whitelist:
if expr_match(value, expr):
return True
else:
return True