|
|
@ -800,7 +800,6 @@ class LogFileMonitor(LogCaptureTestCase):
|
|
|
|
_, self.name = tempfile.mkstemp('fail2ban', 'monitorfailures')
|
|
|
|
_, self.name = tempfile.mkstemp('fail2ban', 'monitorfailures')
|
|
|
|
self.file = open(self.name, 'a')
|
|
|
|
self.file = open(self.name, 'a')
|
|
|
|
self.filter = FilterPoll(DummyJail())
|
|
|
|
self.filter = FilterPoll(DummyJail())
|
|
|
|
self.filter.banASAP = False # avoid immediate ban in this tests
|
|
|
|
|
|
|
|
self.filter.addLogPath(self.name, autoSeek=False)
|
|
|
|
self.filter.addLogPath(self.name, autoSeek=False)
|
|
|
|
self.filter.active = True
|
|
|
|
self.filter.active = True
|
|
|
|
self.filter.addFailRegex(r"(?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii](?:llegal|nvalid) user|ROOT LOGIN REFUSED) .*(?: from|FROM) <HOST>")
|
|
|
|
self.filter.addFailRegex(r"(?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii](?:llegal|nvalid) user|ROOT LOGIN REFUSED) .*(?: from|FROM) <HOST>")
|
|
|
@ -952,15 +951,18 @@ class LogFileMonitor(LogCaptureTestCase):
|
|
|
|
self.file.close()
|
|
|
|
self.file.close()
|
|
|
|
self.file = _copy_lines_between_files(GetFailures.FILENAME_01, self.name,
|
|
|
|
self.file = _copy_lines_between_files(GetFailures.FILENAME_01, self.name,
|
|
|
|
n=14, mode='w')
|
|
|
|
n=14, mode='w')
|
|
|
|
|
|
|
|
print('=========='*10)
|
|
|
|
self.filter.getFailures(self.name)
|
|
|
|
self.filter.getFailures(self.name)
|
|
|
|
|
|
|
|
print('=========='*10)
|
|
|
|
self.assertRaises(FailManagerEmpty, self.filter.failManager.toBan)
|
|
|
|
self.assertRaises(FailManagerEmpty, self.filter.failManager.toBan)
|
|
|
|
self.assertEqual(self.filter.failManager.getFailTotal(), 2)
|
|
|
|
self.assertEqual(self.filter.failManager.getFailTotal(), 2)
|
|
|
|
|
|
|
|
|
|
|
|
# move aside, but leaving the handle still open...
|
|
|
|
# move aside, but leaving the handle still open...
|
|
|
|
|
|
|
|
print('=========='*10)
|
|
|
|
os.rename(self.name, self.name + '.bak')
|
|
|
|
os.rename(self.name, self.name + '.bak')
|
|
|
|
_copy_lines_between_files(GetFailures.FILENAME_01, self.name, skip=14, n=1).close()
|
|
|
|
_copy_lines_between_files(GetFailures.FILENAME_01, self.name, skip=14, n=1).close()
|
|
|
|
self.filter.getFailures(self.name)
|
|
|
|
self.filter.getFailures(self.name)
|
|
|
|
_assert_correct_last_attempt(self, self.filter, GetFailures.FAILURES_01)
|
|
|
|
#_assert_correct_last_attempt(self, self.filter, GetFailures.FAILURES_01)
|
|
|
|
self.assertEqual(self.filter.failManager.getFailTotal(), 3)
|
|
|
|
self.assertEqual(self.filter.failManager.getFailTotal(), 3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -1018,7 +1020,6 @@ def get_monitor_failures_testcase(Filter_):
|
|
|
|
self.file = open(self.name, 'a')
|
|
|
|
self.file = open(self.name, 'a')
|
|
|
|
self.jail = DummyJail()
|
|
|
|
self.jail = DummyJail()
|
|
|
|
self.filter = Filter_(self.jail)
|
|
|
|
self.filter = Filter_(self.jail)
|
|
|
|
self.filter.banASAP = False # avoid immediate ban in this tests
|
|
|
|
|
|
|
|
self.filter.addLogPath(self.name, autoSeek=False)
|
|
|
|
self.filter.addLogPath(self.name, autoSeek=False)
|
|
|
|
# speedup search using exact date pattern:
|
|
|
|
# speedup search using exact date pattern:
|
|
|
|
self.filter.setDatePattern(r'^(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %ExY)?')
|
|
|
|
self.filter.setDatePattern(r'^(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %ExY)?')
|
|
|
@ -1277,14 +1278,14 @@ def get_monitor_failures_testcase(Filter_):
|
|
|
|
# tail written before, so let's not copy anything yet
|
|
|
|
# tail written before, so let's not copy anything yet
|
|
|
|
#_copy_lines_between_files(GetFailures.FILENAME_01, self.name, n=100)
|
|
|
|
#_copy_lines_between_files(GetFailures.FILENAME_01, self.name, n=100)
|
|
|
|
# we should detect the failures
|
|
|
|
# we should detect the failures
|
|
|
|
self.assert_correct_last_attempt(GetFailures.FAILURES_01, count=6) # was needed if we write twice above
|
|
|
|
self.assert_correct_last_attempt(GetFailures.FAILURES_01, count=3) # was needed if we write twice above
|
|
|
|
|
|
|
|
|
|
|
|
# now copy and get even more
|
|
|
|
# now copy and get even more
|
|
|
|
_copy_lines_between_files(GetFailures.FILENAME_01, self.file, skip=12, n=3)
|
|
|
|
_copy_lines_between_files(GetFailures.FILENAME_01, self.file, skip=12, n=3)
|
|
|
|
# check for 3 failures (not 9), because 6 already get above...
|
|
|
|
# check for 3 failures (not 9), because 6 already get above...
|
|
|
|
self.assert_correct_last_attempt(GetFailures.FAILURES_01)
|
|
|
|
self.assert_correct_last_attempt(GetFailures.FAILURES_01)
|
|
|
|
# total count in this test:
|
|
|
|
# total count in this test:
|
|
|
|
self.assertEqual(self.filter.failManager.getFailTotal(), 12)
|
|
|
|
self.assertEqual(self.filter.failManager.getFailTotal(), 9)
|
|
|
|
|
|
|
|
|
|
|
|
cls = MonitorFailures
|
|
|
|
cls = MonitorFailures
|
|
|
|
cls.__qualname__ = cls.__name__ = "MonitorFailures<%s>(%s)" \
|
|
|
|
cls.__qualname__ = cls.__name__ = "MonitorFailures<%s>(%s)" \
|
|
|
@ -1316,7 +1317,6 @@ def get_monitor_failures_journal_testcase(Filter_): # pragma: systemd no cover
|
|
|
|
def _initFilter(self, **kwargs):
|
|
|
|
def _initFilter(self, **kwargs):
|
|
|
|
self._getRuntimeJournal() # check journal available
|
|
|
|
self._getRuntimeJournal() # check journal available
|
|
|
|
self.filter = Filter_(self.jail, **kwargs)
|
|
|
|
self.filter = Filter_(self.jail, **kwargs)
|
|
|
|
self.filter.banASAP = False # avoid immediate ban in this tests
|
|
|
|
|
|
|
|
self.filter.addJournalMatch([
|
|
|
|
self.filter.addJournalMatch([
|
|
|
|
"SYSLOG_IDENTIFIER=fail2ban-testcases",
|
|
|
|
"SYSLOG_IDENTIFIER=fail2ban-testcases",
|
|
|
|
"TEST_FIELD=1",
|
|
|
|
"TEST_FIELD=1",
|
|
|
@ -1570,7 +1570,6 @@ class GetFailures(LogCaptureTestCase):
|
|
|
|
setUpMyTime()
|
|
|
|
setUpMyTime()
|
|
|
|
self.jail = DummyJail()
|
|
|
|
self.jail = DummyJail()
|
|
|
|
self.filter = FileFilter(self.jail)
|
|
|
|
self.filter = FileFilter(self.jail)
|
|
|
|
self.filter.banASAP = False # avoid immediate ban in this tests
|
|
|
|
|
|
|
|
self.filter.active = True
|
|
|
|
self.filter.active = True
|
|
|
|
# speedup search using exact date pattern:
|
|
|
|
# speedup search using exact date pattern:
|
|
|
|
self.filter.setDatePattern(r'^(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %ExY)?')
|
|
|
|
self.filter.setDatePattern(r'^(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %ExY)?')
|
|
|
@ -1771,7 +1770,6 @@ class GetFailures(LogCaptureTestCase):
|
|
|
|
self.pruneLog("[test-phase useDns=%s]" % useDns)
|
|
|
|
self.pruneLog("[test-phase useDns=%s]" % useDns)
|
|
|
|
jail = DummyJail()
|
|
|
|
jail = DummyJail()
|
|
|
|
filter_ = FileFilter(jail, useDns=useDns)
|
|
|
|
filter_ = FileFilter(jail, useDns=useDns)
|
|
|
|
filter_.banASAP = False # avoid immediate ban in this tests
|
|
|
|
|
|
|
|
filter_.active = True
|
|
|
|
filter_.active = True
|
|
|
|
filter_.failManager.setMaxRetry(1) # we might have just few failures
|
|
|
|
filter_.failManager.setMaxRetry(1) # we might have just few failures
|
|
|
|
|
|
|
|
|
|
|
|