make Ticket.getIP() return IPAddr

Store the ip internally as IPAddr, converting the data if needed. The
original data can bei either a str or IPAddr, anything else (like an
int) will cause an error. Fall back to the id only if it is an IPAddr.
All testcases pass again.
This commit is contained in:
Marko Hauptvogel 2025-11-11 10:54:08 +01:00
parent cafea3235f
commit 5663e9e029
3 changed files with 22 additions and 5 deletions

View file

@ -28,6 +28,7 @@ ver. 1.1.1-dev-1 (20??/??/??) - development nightly edition
e. g. setting `blocktype="DROP"` via jail for action would now apply for IPv4 and IPv6 chains,
to submit different `blocktype` for IPv4 and IPv6 from jail, one can pass them like in this example:
`banaction = iptables-ipset[blocktype="...", blocktype?family=inet6="..."]`
* fixes restoring bans with custom failure-id
* `jail.conf`:
- default banactions need to be specified in `paths-*.conf` (maintainer level) now
- since stock fail2ban includes `paths-debian.conf` by default, banactions are `nftables`

View file

@ -25,7 +25,7 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
from ..helpers import getLogger
from .ipdns import IPAddr
from .ipdns import IPAddr, asip
from .mytime import MyTime
# Gets the instance of the logger.
@ -56,8 +56,11 @@ class Ticket(object):
self._data = {'matches': matches or [], 'failures': 0}
if data is not None:
for k,v in data.items():
if v is not None:
self._data[k] = v
if v is None:
continue
if k == 'ip':
v = asip(v)
self._data[k] = v
if ticket:
# ticket available - copy whole information from ticket:
self.update(ticket)
@ -95,8 +98,12 @@ class Ticket(object):
def getID(self):
return self._id
def getIP(self):
return self._data.get('ip', self._id)
def getIP(self) -> IPAddr:
if 'ip' in self._data:
return self._data['ip']
if isinstance(self._id, IPAddr):
return self._id
raise ValueError("No IP available")
def setTime(self, value):
self._time = value

View file

@ -135,6 +135,15 @@ class TicketTests(unittest.TestCase):
self.assertIsInstance(t.getIP(), IPAddr)
self.assertEqual(t.getIP(), '192.0.2.1')
# invalid ip type causes an error
with self.assertRaises(TypeError):
Ticket('123-456-789', tm, data={'ip':192021})
# no IPAddr causes an error
t = Ticket(('192.0.2.1', '5000'), tm, data={})
with self.assertRaises(ValueError):
t.getIP()
def testTicketFlags(self):
flags = ('restored', 'banned')
ticket = Ticket('test', 0)