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