mirror of https://github.com/fail2ban/fail2ban
Merge branch 'database-persistent-bans' of https://github.com/kwirk/fail2ban
* 'database-persistent-bans' of https://github.com/kwirk/fail2ban: BF: bantime < 0 database should return all bans, as they are persistent Conflicts: ChangeLog - kept all ;)pull/661/merge
commit
1f8b554d31
|
@ -24,6 +24,7 @@ ver. 0.9.1 (2014/xx/xx) - better, faster, stronger
|
||||||
multiline regex
|
multiline regex
|
||||||
* Fix actions failing to execute for Python 3.4.0. Workaround for
|
* Fix actions failing to execute for Python 3.4.0. Workaround for
|
||||||
http://bugs.python.org/issue21207
|
http://bugs.python.org/issue21207
|
||||||
|
* Database now returns persistent bans on restart (bantime < 0)
|
||||||
|
|
||||||
- New features:
|
- New features:
|
||||||
|
|
||||||
|
|
|
@ -380,7 +380,7 @@ class Fail2BanDb(object):
|
||||||
if jail is not None:
|
if jail is not None:
|
||||||
query += " AND jail=?"
|
query += " AND jail=?"
|
||||||
queryArgs.append(jail.name)
|
queryArgs.append(jail.name)
|
||||||
if bantime is not None:
|
if bantime is not None and bantime >= 0:
|
||||||
query += " AND timeofban > ?"
|
query += " AND timeofban > ?"
|
||||||
queryArgs.append(MyTime.time() - bantime)
|
queryArgs.append(MyTime.time() - bantime)
|
||||||
if ip is not None:
|
if ip is not None:
|
||||||
|
@ -399,7 +399,8 @@ class Fail2BanDb(object):
|
||||||
Jail that the ban belongs to. Default `None`; all jails.
|
Jail that the ban belongs to. Default `None`; all jails.
|
||||||
bantime : int
|
bantime : int
|
||||||
Ban time in seconds, such that bans returned would still be
|
Ban time in seconds, such that bans returned would still be
|
||||||
valid now. Default `None`; no limit.
|
valid now. Negative values are equivalent to `None`.
|
||||||
|
Default `None`; no limit.
|
||||||
ip : str
|
ip : str
|
||||||
IP Address to filter bans by. Default `None`; all IPs.
|
IP Address to filter bans by. Default `None`; all IPs.
|
||||||
|
|
||||||
|
@ -427,7 +428,8 @@ class Fail2BanDb(object):
|
||||||
Jail that the ban belongs to. Default `None`; all jails.
|
Jail that the ban belongs to. Default `None`; all jails.
|
||||||
bantime : int
|
bantime : int
|
||||||
Ban time in seconds, such that bans returned would still be
|
Ban time in seconds, such that bans returned would still be
|
||||||
valid now. Default `None`; no limit.
|
valid now. Negative values are equivalent to `None`.
|
||||||
|
Default `None`; no limit.
|
||||||
ip : str
|
ip : str
|
||||||
IP Address to filter bans by. Default `None`; all IPs.
|
IP Address to filter bans by. Default `None`; all IPs.
|
||||||
|
|
||||||
|
@ -438,7 +440,8 @@ class Fail2BanDb(object):
|
||||||
in a list. When `ip` argument passed, a single `Ticket` is
|
in a list. When `ip` argument passed, a single `Ticket` is
|
||||||
returned.
|
returned.
|
||||||
"""
|
"""
|
||||||
if bantime is None:
|
cacheKey = None
|
||||||
|
if bantime is None or bantime < 0:
|
||||||
cacheKey = (ip, jail)
|
cacheKey = (ip, jail)
|
||||||
if cacheKey in self._bansMergedCache:
|
if cacheKey in self._bansMergedCache:
|
||||||
return self._bansMergedCache[cacheKey]
|
return self._bansMergedCache[cacheKey]
|
||||||
|
@ -468,7 +471,7 @@ class Fail2BanDb(object):
|
||||||
ticket.setAttempt(failures)
|
ticket.setAttempt(failures)
|
||||||
tickets.append(ticket)
|
tickets.append(ticket)
|
||||||
|
|
||||||
if bantime is None:
|
if cacheKey:
|
||||||
self._bansMergedCache[cacheKey] = tickets if ip is None else ticket
|
self._bansMergedCache[cacheKey] = tickets if ip is None else ticket
|
||||||
return tickets if ip is None else ticket
|
return tickets if ip is None else ticket
|
||||||
|
|
||||||
|
|
|
@ -177,10 +177,15 @@ class DatabaseTest(unittest.TestCase):
|
||||||
if Fail2BanDb is None: # pragma: no cover
|
if Fail2BanDb is None: # pragma: no cover
|
||||||
return
|
return
|
||||||
self.testAddJail()
|
self.testAddJail()
|
||||||
ticket = FailTicket("127.0.0.1", MyTime.time() - 40, ["abc\n"])
|
self.db.addBan(
|
||||||
self.db.addBan(self.jail, ticket)
|
self.jail, FailTicket("127.0.0.1", MyTime.time() - 60, ["abc\n"]))
|
||||||
|
self.db.addBan(
|
||||||
|
self.jail, FailTicket("127.0.0.1", MyTime.time() - 40, ["abc\n"]))
|
||||||
self.assertEqual(len(self.db.getBans(jail=self.jail,bantime=50)), 1)
|
self.assertEqual(len(self.db.getBans(jail=self.jail,bantime=50)), 1)
|
||||||
self.assertEqual(len(self.db.getBans(jail=self.jail,bantime=20)), 0)
|
self.assertEqual(len(self.db.getBans(jail=self.jail,bantime=20)), 0)
|
||||||
|
# Negative values are for persistent bans, and such all bans should
|
||||||
|
# be returned
|
||||||
|
self.assertEqual(len(self.db.getBans(jail=self.jail,bantime=-1)), 2)
|
||||||
|
|
||||||
def testGetBansMerged(self):
|
def testGetBansMerged(self):
|
||||||
if Fail2BanDb is None: # pragma: no cover
|
if Fail2BanDb is None: # pragma: no cover
|
||||||
|
@ -251,6 +256,10 @@ class DatabaseTest(unittest.TestCase):
|
||||||
self.assertEqual(len(tickets), 1)
|
self.assertEqual(len(tickets), 1)
|
||||||
tickets = self.db.getBansMerged(bantime=5)
|
tickets = self.db.getBansMerged(bantime=5)
|
||||||
self.assertEqual(len(tickets), 0)
|
self.assertEqual(len(tickets), 0)
|
||||||
|
# Negative values are for persistent bans, and such all bans should
|
||||||
|
# be returned
|
||||||
|
tickets = self.db.getBansMerged(bantime=-1)
|
||||||
|
self.assertEqual(len(tickets), 2)
|
||||||
|
|
||||||
def testPurge(self):
|
def testPurge(self):
|
||||||
if Fail2BanDb is None: # pragma: no cover
|
if Fail2BanDb is None: # pragma: no cover
|
||||||
|
|
Loading…
Reference in New Issue