observer API simplification (no failmanager in call of failureFound, jail.filter.failManager is enough)

pull/2990/merge
sebres 2022-01-26 17:56:00 +01:00
parent 06d2623c5e
commit f4641dfc00
3 changed files with 12 additions and 17 deletions

View File

@ -724,7 +724,7 @@ class Filter(JailThread):
self.performBan(ip) self.performBan(ip)
# report to observer - failure was found, for possibly increasing of it retry counter (asynchronous) # report to observer - failure was found, for possibly increasing of it retry counter (asynchronous)
if Observers.Main is not None: if Observers.Main is not None:
Observers.Main.add('failureFound', self.failManager, self.jail, tick) Observers.Main.add('failureFound', self.jail, tick)
self.procLines += 1 self.procLines += 1
# every 100 lines check need to perform service tasks: # every 100 lines check need to perform service tasks:
if self.procLines % 100 == 0: if self.procLines % 100 == 0:

View File

@ -364,7 +364,7 @@ class ObserverThread(JailThread):
## [Async] ban time increment functionality ... ## [Async] ban time increment functionality ...
## ----------------------------------------- ## -----------------------------------------
def failureFound(self, failManager, jail, ticket): def failureFound(self, jail, ticket):
""" Notify observer a failure for ip was found """ Notify observer a failure for ip was found
Observer will check ip was known (bad) and possibly increase an retry count Observer will check ip was known (bad) and possibly increase an retry count
@ -380,7 +380,7 @@ class ObserverThread(JailThread):
retryCount = 1 retryCount = 1
timeOfBan = None timeOfBan = None
try: try:
maxRetry = failManager.getMaxRetry() maxRetry = jail.filter.failManager.getMaxRetry()
db = jail.database db = jail.database
if db is not None: if db is not None:
for banCount, timeOfBan, lastBanTime in db.getBan(ip, jail): for banCount, timeOfBan, lastBanTime in db.getBan(ip, jail):
@ -403,18 +403,12 @@ class ObserverThread(JailThread):
MyTime.time2str(unixTime), banCount, retryCount, MyTime.time2str(unixTime), banCount, retryCount,
(', Ban' if retryCount >= maxRetry else '')) (', Ban' if retryCount >= maxRetry else ''))
# retryCount-1, because a ticket was already once incremented by filter self # retryCount-1, because a ticket was already once incremented by filter self
retryCount = failManager.addFailure(ticket, retryCount - 1, True) retryCount = jail.filter.failManager.addFailure(ticket, retryCount - 1, True)
ticket.setBanCount(banCount) ticket.setBanCount(banCount)
# after observe we have increased attempt count, compare it >= maxretry ... # after observe we have increased attempt count, compare it >= maxretry ...
if retryCount >= maxRetry: if retryCount >= maxRetry:
# perform the banning of the IP now (again) # perform the banning of the IP now (again)
# [todo]: this code part will be used multiple times - optimize it later. jail.filter.performBan(ip)
try: # pragma: no branch - exception is the only way out
while True:
ticket = failManager.toBan(ip)
jail.putFailTicket(ticket)
except FailManagerEmpty:
failManager.cleanup(MyTime.time())
except Exception as e: except Exception as e:
logSys.error('%s', e, exc_info=logSys.getEffectiveLevel()<=logging.DEBUG) logSys.error('%s', e, exc_info=logSys.getEffectiveLevel()<=logging.DEBUG)

View File

@ -450,7 +450,8 @@ class BanTimeIncrDB(LogCaptureTestCase):
def testObserver(self): def testObserver(self):
if Fail2BanDb is None: # pragma: no cover if Fail2BanDb is None: # pragma: no cover
return return
jail = self.jail jail = self.jail = DummyJail(backend='polling')
jail.database = self.db
self.db.addJail(jail) self.db.addJail(jail)
# we tests with initial ban time = 10 seconds: # we tests with initial ban time = 10 seconds:
jail.actions.setBanTime(10) jail.actions.setBanTime(10)
@ -480,27 +481,27 @@ class BanTimeIncrDB(LogCaptureTestCase):
# add failure: # add failure:
ip = "192.0.2.1" ip = "192.0.2.1"
ticket = FailTicket(ip, stime-120, []) ticket = FailTicket(ip, stime-120, [])
failManager = FailManager() failManager = jail.filter.failManager = FailManager()
failManager.setMaxRetry(3) failManager.setMaxRetry(3)
for i in xrange(3): for i in xrange(3):
failManager.addFailure(ticket) failManager.addFailure(ticket)
obs.add('failureFound', failManager, jail, ticket) obs.add('failureFound', jail, ticket)
obs.wait_empty(5) obs.wait_empty(5)
self.assertEqual(ticket.getBanCount(), 0) self.assertEqual(ticket.getBanCount(), 0)
# check still not ban : # check still not ban :
self.assertTrue(not jail.getFailTicket()) self.assertTrue(not jail.getFailTicket())
# add manually 4th times banned (added to bips - make ip bad): # add manually 4th times banned (added to bips - make ip bad):
ticket.setBanCount(4) ticket.setBanCount(4)
self.db.addBan(self.jail, ticket) self.db.addBan(jail, ticket)
restored_tickets = self.db.getCurrentBans(jail=jail, fromtime=stime-120, correctBanTime=False) restored_tickets = self.db.getCurrentBans(jail=jail, fromtime=stime-120, correctBanTime=False)
self.assertEqual(len(restored_tickets), 1) self.assertEqual(len(restored_tickets), 1)
# check again, new ticket, new failmanager: # check again, new ticket, new failmanager:
ticket = FailTicket(ip, stime, []) ticket = FailTicket(ip, stime, [])
failManager = FailManager() failManager = jail.filter.failManager = FailManager()
failManager.setMaxRetry(3) failManager.setMaxRetry(3)
# add once only - but bad - should be banned: # add once only - but bad - should be banned:
failManager.addFailure(ticket) failManager.addFailure(ticket)
obs.add('failureFound', failManager, self.jail, ticket) obs.add('failureFound', jail, ticket)
obs.wait_empty(5) obs.wait_empty(5)
# wait until ticket transfered from failmanager into jail: # wait until ticket transfered from failmanager into jail:
ticket2 = Utils.wait_for(jail.getFailTicket, 10) ticket2 = Utils.wait_for(jail.getFailTicket, 10)