mirror of https://github.com/fail2ban/fail2ban
code review + more test cases (embedded replace in a string)
parent
6b42878b8c
commit
d0b932aaca
|
@ -362,6 +362,7 @@ class CommandAction(ActionBase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def substituteRecursiveTags(cls, tags):
|
def substituteRecursiveTags(cls, tags):
|
||||||
"""Sort out tag definitions within other tags.
|
"""Sort out tag definitions within other tags.
|
||||||
|
Since v.0.9.2 supports embedded interpolation (see test cases for examples).
|
||||||
|
|
||||||
so: becomes:
|
so: becomes:
|
||||||
a = 3 a = 3
|
a = 3 a = 3
|
||||||
|
@ -379,13 +380,16 @@ class CommandAction(ActionBase):
|
||||||
within the values recursively replaced.
|
within the values recursively replaced.
|
||||||
"""
|
"""
|
||||||
t = re.compile(r'<([^ <>]+)>')
|
t = re.compile(r'<([^ <>]+)>')
|
||||||
|
# repeat substitution while embedded-recursive (repFlag is True)
|
||||||
while True:
|
while True:
|
||||||
repFlag = False
|
repFlag = False
|
||||||
|
# substitute each value:
|
||||||
for tag in tags.iterkeys():
|
for tag in tags.iterkeys():
|
||||||
if tag in cls._escapedTags:
|
if tag in cls._escapedTags:
|
||||||
# Escaped so won't match
|
# Escaped so won't match
|
||||||
continue
|
continue
|
||||||
value = str(tags[tag])
|
value = str(tags[tag])
|
||||||
|
# search and replace all tags within value, that can be interpolated using other tags:
|
||||||
m = t.search(value)
|
m = t.search(value)
|
||||||
done = []
|
done = []
|
||||||
#logSys.log(5, 'TAG: %s, value: %s' % (tag, value))
|
#logSys.log(5, 'TAG: %s, value: %s' % (tag, value))
|
||||||
|
@ -409,8 +413,8 @@ class CommandAction(ActionBase):
|
||||||
#logSys.log(5, 'TAG: %s, newvalue: %s' % (tag, value))
|
#logSys.log(5, 'TAG: %s, newvalue: %s' % (tag, value))
|
||||||
# was substituted?
|
# was substituted?
|
||||||
if tags[tag] != value:
|
if tags[tag] != value:
|
||||||
# check again later if embedded-recursive substitution:
|
# check still contains any tag - should be repeated (possible embedded-recursive substitution):
|
||||||
if value.startswith('<') and value.endswith('>'):
|
if t.search(value):
|
||||||
repFlag = True
|
repFlag = True
|
||||||
tags[tag] = value
|
tags[tag] = value
|
||||||
if not repFlag:
|
if not repFlag:
|
||||||
|
|
|
@ -78,6 +78,9 @@ class CommandActionTest(LogCaptureTestCase):
|
||||||
{'A': '<IPV4HOST>', 'PREF': 'IPV4'})
|
{'A': '<IPV4HOST>', 'PREF': 'IPV4'})
|
||||||
self.assertEqual(CommandAction.substituteRecursiveTags({'A': '<<PREF>HOST>', 'PREF': 'IPV4', 'IPV4HOST': '1.2.3.4'}),
|
self.assertEqual(CommandAction.substituteRecursiveTags({'A': '<<PREF>HOST>', 'PREF': 'IPV4', 'IPV4HOST': '1.2.3.4'}),
|
||||||
{'A': '1.2.3.4', 'PREF': 'IPV4', 'IPV4HOST': '1.2.3.4'})
|
{'A': '1.2.3.4', 'PREF': 'IPV4', 'IPV4HOST': '1.2.3.4'})
|
||||||
|
# more embedded within a string and two interpolations
|
||||||
|
self.assertEqual(CommandAction.substituteRecursiveTags({'A': 'A <IP<PREF>HOST> B IP<PREF> C', 'PREF': 'V4', 'IPV4HOST': '1.2.3.4'}),
|
||||||
|
{'A': 'A 1.2.3.4 B IPV4 C', 'PREF': 'V4', 'IPV4HOST': '1.2.3.4'})
|
||||||
|
|
||||||
def testReplaceTag(self):
|
def testReplaceTag(self):
|
||||||
aInfo = {
|
aInfo = {
|
||||||
|
|
Loading…
Reference in New Issue