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["matches"] = "".join(bTicket.getMatches())
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:
action.execActionBan(aInfo)
return True
else:
logSys.info("[%s] %s already banned" % (self.jail.getName(),
str(aInfo["ip"])))
aInfo["ip"]))
return False
##

View File

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

View File

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