mirror of https://github.com/fail2ban/fail2ban
amend with common log-file iterator in fail2ban-regex and test-suite (in sample regex factory also)
parent
9659033523
commit
ccf4f3a07d
|
@ -720,14 +720,6 @@ class Fail2banRegex(object):
|
|||
|
||||
return True
|
||||
|
||||
def file_lines_gen(self, hdlr):
|
||||
while 1:
|
||||
line = hdlr.readline()
|
||||
if line is None:
|
||||
break
|
||||
yield line
|
||||
hdlr.close()
|
||||
|
||||
def start(self, args):
|
||||
|
||||
cmd_log, cmd_regex = args[:2]
|
||||
|
@ -746,10 +738,10 @@ class Fail2banRegex(object):
|
|||
|
||||
if os.path.isfile(cmd_log):
|
||||
try:
|
||||
hdlr = FileContainer(cmd_log, self._encoding, doOpen=True)
|
||||
test_lines = FileContainer(cmd_log, self._encoding, doOpen=True)
|
||||
|
||||
self.output( "Use log file : %s" % cmd_log )
|
||||
self.output( "Use encoding : %s" % self._encoding )
|
||||
test_lines = self.file_lines_gen(hdlr)
|
||||
except IOError as e: # pragma: no cover
|
||||
output( e )
|
||||
return False
|
||||
|
|
|
@ -1280,6 +1280,7 @@ class FileContainer:
|
|||
|
||||
def __init__(self, filename, encoding, tail=False, doOpen=False):
|
||||
self.__filename = filename
|
||||
self.waitForLineEnd = True
|
||||
self.setEncoding(encoding)
|
||||
self.__tail = tail
|
||||
self.__handler = None
|
||||
|
@ -1469,9 +1470,10 @@ class FileContainer:
|
|||
l = r.rstrip('\r\n')
|
||||
if l != r:
|
||||
return l
|
||||
# not fulfilled - seek back and return:
|
||||
self.__handler.seek(-bl, 1)
|
||||
return None
|
||||
if self.waitForLineEnd:
|
||||
# not fulfilled - seek back and return:
|
||||
self.__handler.seek(-bl, 1)
|
||||
return None
|
||||
return l
|
||||
|
||||
def close(self):
|
||||
|
@ -1482,6 +1484,15 @@ class FileContainer:
|
|||
self.__handler.close()
|
||||
self.__handler = None
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
def next(self):
|
||||
line = self.readline()
|
||||
if line is None:
|
||||
self.close()
|
||||
raise StopIteration
|
||||
return line
|
||||
|
||||
_decode_line_warn = Utils.Cache(maxCount=1000, maxTime=24*60*60);
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@ __copyright__ = "Copyright (c) 2013 Steven Hiscocks"
|
|||
__license__ = "GPL"
|
||||
|
||||
import datetime
|
||||
import fileinput
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
|
@ -156,12 +155,15 @@ def testSampleRegexsFactory(name, basedir):
|
|||
i = 0
|
||||
while i < len(filenames):
|
||||
filename = filenames[i]; i += 1;
|
||||
logFile = fileinput.FileInput(os.path.join(TEST_FILES_DIR, "logs",
|
||||
filename), mode='rb')
|
||||
logFile = FileContainer(os.path.join(TEST_FILES_DIR, "logs",
|
||||
filename), 'UTF-8', doOpen=True)
|
||||
# avoid errors if no NL char at end of test log-file:
|
||||
logFile.waitForLineEnd = False
|
||||
|
||||
ignoreBlock = False
|
||||
lnnum = 0
|
||||
for line in logFile:
|
||||
line = FileContainer.decode_line(logFile.filename(), 'UTF-8', line)
|
||||
lnnum += 1
|
||||
jsonREMatch = re.match("^#+ ?(failJSON|(?:file|filter)Options|addFILE):(.+)$", line)
|
||||
if jsonREMatch:
|
||||
try:
|
||||
|
@ -201,9 +203,8 @@ def testSampleRegexsFactory(name, basedir):
|
|||
# failJSON - faildata contains info of the failure to check it.
|
||||
except ValueError as e: # pragma: no cover - we've valid json's
|
||||
raise ValueError("%s: %s:%i" %
|
||||
(e, logFile.filename(), logFile.filelineno()))
|
||||
(e, logFile.getFileName(), lnnum))
|
||||
line = next(logFile)
|
||||
line = FileContainer.decode_line(logFile.filename(), 'UTF-8', line)
|
||||
elif ignoreBlock or line.startswith("#") or not line.strip():
|
||||
continue
|
||||
else: # pragma: no cover - normally unreachable
|
||||
|
@ -298,7 +299,7 @@ def testSampleRegexsFactory(name, basedir):
|
|||
import pprint
|
||||
raise AssertionError("%s: %s on: %s:%i, line:\n %s\nregex (%s):\n %s\n"
|
||||
"faildata: %s\nfail: %s" % (
|
||||
fltName, e, logFile.filename(), logFile.filelineno(),
|
||||
fltName, e, logFile.getFileName(), lnnum,
|
||||
line, failregex, regexList[failregex] if failregex != -1 else None,
|
||||
'\n'.join(pprint.pformat(faildata).splitlines()),
|
||||
'\n'.join(pprint.pformat(fail).splitlines())))
|
||||
|
|
Loading…
Reference in New Issue