BF: guarantee that IP is stored as a base, non-unicode str (Closes gh-91)

Otherwise it might lead to spurious characters dumped into the
terminal at few places, unless casted upon every use in the logs.  To
prevent those issues in the source, store IP as a basic string if it
is a string at all
pull/95/head
Yaroslav Halchenko 2012-11-26 12:01:42 -05:00
parent 03b31398aa
commit 37a2e59d69
3 changed files with 12 additions and 8 deletions

View File

@ -176,13 +176,13 @@ class Actions(JailThread):
aInfo["time"] = bTicket.getTime() aInfo["time"] = bTicket.getTime()
aInfo["matches"] = "".join(bTicket.getMatches()) aInfo["matches"] = "".join(bTicket.getMatches())
if self.__banManager.addBanTicket(bTicket): if self.__banManager.addBanTicket(bTicket):
logSys.warn("[%s] Ban %s" % (self.jail.getName(), str(aInfo["ip"]))) logSys.warn("[%s] Ban %s" % (self.jail.getName(), aInfo["ip"]))
for action in self.__actions: for action in self.__actions:
action.execActionBan(aInfo) action.execActionBan(aInfo)
return True return True
else: else:
logSys.info("[%s] %s already banned" % (self.jail.getName(), logSys.info("[%s] %s already banned" % (self.jail.getName(),
str(aInfo["ip"]))) aInfo["ip"]))
return False return False
## ##

View File

@ -42,7 +42,7 @@ class Ticket:
@param matches (log) lines caused the ticket @param matches (log) lines caused the ticket
""" """
self.__ip = ip self.setIP(ip)
self.__time = time self.__time = time
self.__attempt = 0 self.__attempt = 0
self.__file = None self.__file = None
@ -54,6 +54,9 @@ class Ticket:
def setIP(self, value): def setIP(self, value):
if isinstance(value, basestring):
# guarantee using regular str instead of unicode for the IP
value = str(value)
self.__ip = value self.__ip = value
def getIP(self): def getIP(self):

View File

@ -35,11 +35,11 @@ class AddFailure(unittest.TestCase):
def setUp(self): def setUp(self):
"""Call before every test case.""" """Call before every test case."""
self.__items = [['193.168.0.128', 1167605999.0], self.__items = [[u'193.168.0.128', 1167605999.0],
['193.168.0.128', 1167605999.0], [u'193.168.0.128', 1167605999.0],
['193.168.0.128', 1167605999.0], [u'193.168.0.128', 1167605999.0],
['193.168.0.128', 1167605999.0], [u'193.168.0.128', 1167605999.0],
['193.168.0.128', 1167605999.0], [u'193.168.0.128', 1167605999.0],
['87.142.124.10', 1167605999.0], ['87.142.124.10', 1167605999.0],
['87.142.124.10', 1167605999.0], ['87.142.124.10', 1167605999.0],
['87.142.124.10', 1167605999.0], ['87.142.124.10', 1167605999.0],
@ -80,6 +80,7 @@ class AddFailure(unittest.TestCase):
#ticket = FailTicket('193.168.0.128', None) #ticket = FailTicket('193.168.0.128', None)
ticket = self.__failManager.toBan() ticket = self.__failManager.toBan()
self.assertEqual(ticket.getIP(), "193.168.0.128") self.assertEqual(ticket.getIP(), "193.168.0.128")
self.assertTrue(isinstance(ticket.getIP(), str))
def testbanNOK(self): def testbanNOK(self):
self.__failManager.setMaxRetry(10) self.__failManager.setMaxRetry(10)