mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix for whitelist/blacklist checking for non-list iterables
A recent PY3-compatibility fix was made to `check_whitelist_blacklist`. The function was previously checking for the `__iter__` attribute to check if the blacklist and/or whitelist was a sequence of expressions or a single expression. However, the `str` type has the `__iter__` attribute in PY3 while it does not in PY2. To fix this, the check was changed to see if the blacklist and/or whitelist were `list` types. This however causes problems in `salt.daemons.masterapi` when we pass `set` types. To fix this, we just check if the whitelist and/or blacklist are string types, rather than that they are _not_ some sort of sequence type.
This commit is contained in:
parent
9559ac7679
commit
17398efcf7
2 changed files with 130 additions and 3 deletions
|
@ -362,14 +362,14 @@ def check_whitelist_blacklist(value, whitelist=None, blacklist=None):
|
|||
found in the whitelist, the function returns ``False``.
|
||||
'''
|
||||
if blacklist is not None:
|
||||
if not isinstance(blacklist, list):
|
||||
if isinstance(blacklist, six.string_types):
|
||||
blacklist = [blacklist]
|
||||
for expr in blacklist:
|
||||
if expr_match(value, expr):
|
||||
return False
|
||||
|
||||
if whitelist:
|
||||
if not isinstance(whitelist, list):
|
||||
if whitelist is not None:
|
||||
if isinstance(whitelist, six.string_types):
|
||||
whitelist = [whitelist]
|
||||
for expr in whitelist:
|
||||
if expr_match(value, expr):
|
||||
|
|
|
@ -204,3 +204,130 @@ class StringutilsTestCase(TestCase):
|
|||
self.assertTrue(salt.utils.stringutils.expr_match(val, 'foo/*/baz'))
|
||||
# Glob non-match
|
||||
self.assertFalse(salt.utils.stringutils.expr_match(val, 'foo/*/bar'))
|
||||
|
||||
def test_check_whitelist_blacklist(self):
|
||||
'''
|
||||
Ensure that whitelist matching works on both PY2 and PY3
|
||||
'''
|
||||
whitelist = ['one/two/three', r'web[0-9]']
|
||||
blacklist = ['four/five/six', r'web[5-9]']
|
||||
|
||||
# Tests with string whitelist/blacklist
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web_one',
|
||||
whitelist=whitelist[1],
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web1',
|
||||
whitelist=whitelist[1],
|
||||
)
|
||||
)
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web5',
|
||||
blacklist=blacklist[1],
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web_five',
|
||||
blacklist=blacklist[1],
|
||||
)
|
||||
)
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web5',
|
||||
whitelist=whitelist[1],
|
||||
blacklist=blacklist[1],
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web4',
|
||||
whitelist=whitelist[1],
|
||||
blacklist=blacklist[1],
|
||||
)
|
||||
)
|
||||
|
||||
# Tests with list whitelist/blacklist
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web_one',
|
||||
whitelist=whitelist,
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web1',
|
||||
whitelist=whitelist,
|
||||
)
|
||||
)
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web5',
|
||||
blacklist=blacklist,
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web_five',
|
||||
blacklist=blacklist,
|
||||
)
|
||||
)
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web5',
|
||||
whitelist=whitelist,
|
||||
blacklist=blacklist,
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web4',
|
||||
whitelist=whitelist,
|
||||
blacklist=blacklist,
|
||||
)
|
||||
)
|
||||
|
||||
# Tests with list whitelist/blacklist
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web_one',
|
||||
whitelist=set(whitelist),
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web1',
|
||||
whitelist=set(whitelist),
|
||||
)
|
||||
)
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web5',
|
||||
blacklist=set(blacklist),
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web_five',
|
||||
blacklist=set(blacklist),
|
||||
)
|
||||
)
|
||||
self.assertFalse(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web5',
|
||||
whitelist=set(whitelist),
|
||||
blacklist=set(blacklist),
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
salt.utils.stringutils.check_whitelist_blacklist(
|
||||
'web4',
|
||||
whitelist=set(whitelist),
|
||||
blacklist=set(blacklist),
|
||||
)
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue