From 4502c0f6ba0af2b1d1d6ec41ce772c3e8927554a Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Mon, 25 Sep 2006 17:03:48 +0000 Subject: - Organized imports (pylint) git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@382 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- client/csocket.py | 7 +++--- server/action.py | 4 ++-- server/actions.py | 3 +-- server/banticket.py | 2 +- server/dateepoch.py | 2 +- server/datestrptime.py | 2 +- server/datetai64n.py | 2 +- server/datetemplate.py | 2 +- server/faildata.py | 2 +- server/failmanager.py | 2 +- server/failticket.py | 2 +- server/filter.py | 7 +++--- server/filtergamin.py | 3 --- server/filterpoll.py | 3 --- server/jailthread.py | 4 ++++ server/server.py | 4 ++-- server/ssocket.py | 7 +++--- server/ticket.py | 2 +- server/transmitter.py | 2 +- testcases/banmanagertestcase.py | 16 +++++++------- testcases/datedetectortestcase.py | 20 ++++++++--------- testcases/failmanagertestcase.py | 46 +++++++++++++++++++-------------------- testcases/filtertestcase.py | 40 +++++++++++++++++----------------- testcases/servertestcase.py | 30 ++++++++++++------------- 24 files changed, 106 insertions(+), 108 deletions(-) diff --git a/client/csocket.py b/client/csocket.py index b64b8753..4ab3da80 100644 --- a/client/csocket.py +++ b/client/csocket.py @@ -24,7 +24,8 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import socket, cPickle +from cPickle import dumps, loads +import socket class CSocket: @@ -40,7 +41,7 @@ class CSocket: def send(self, msg): # Convert every list member to string - obj = cPickle.dumps(map(str, msg)) + obj = dumps(map(str, msg)) self.__csock.send(obj + CSocket.END_STRING) ret = self.receive(self.__csock) self.__csock.close() @@ -53,4 +54,4 @@ class CSocket: if chunk == '': raise RuntimeError, "socket connection broken" msg = msg + chunk - return cPickle.loads(msg) + return loads(msg) diff --git a/server/action.py b/server/action.py index 6d10c3df..b69b0946 100644 --- a/server/action.py +++ b/server/action.py @@ -24,8 +24,8 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import time, logging, os -from subprocess import call +import logging, os +#from subprocess import call # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.actions.action") diff --git a/server/actions.py b/server/actions.py index e9cc2d3e..ead21b65 100644 --- a/server/actions.py +++ b/server/actions.py @@ -25,10 +25,9 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" from banmanager import BanManager -from failmanager import FailManager, FailManagerEmpty from jailthread import JailThread from action import Action -import time, logging, os +import time, logging # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.actions") diff --git a/server/banticket.py b/server/banticket.py index 94c03254..67e5def1 100644 --- a/server/banticket.py +++ b/server/banticket.py @@ -24,7 +24,7 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import time, logging +import logging from ticket import Ticket # Gets the instance of the logger. diff --git a/server/dateepoch.py b/server/dateepoch.py index ef5cb2ba..bc0ca667 100644 --- a/server/dateepoch.py +++ b/server/dateepoch.py @@ -24,7 +24,7 @@ __date__ = "$Date: 2006-09-04 21:19:58 +0200 (Mon, 04 Sep 2006) $" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import re, time +import time from datetemplate import DateTemplate diff --git a/server/datestrptime.py b/server/datestrptime.py index 4cbc32db..21516d58 100644 --- a/server/datestrptime.py +++ b/server/datestrptime.py @@ -25,7 +25,7 @@ __date__ = "$Date: 2006-09-04 21:19:58 +0200 (Mon, 04 Sep 2006) $" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import re, time +import time from datetemplate import DateTemplate diff --git a/server/datetai64n.py b/server/datetai64n.py index 064b6643..d40a7f08 100644 --- a/server/datetai64n.py +++ b/server/datetai64n.py @@ -24,7 +24,7 @@ __date__ = "$Date: 2006-09-04 21:19:58 +0200 (Mon, 04 Sep 2006) $" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import re, time +import time from datetemplate import DateTemplate diff --git a/server/datetemplate.py b/server/datetemplate.py index 69b945d7..c4bb1d12 100644 --- a/server/datetemplate.py +++ b/server/datetemplate.py @@ -24,7 +24,7 @@ __date__ = "$Date: 2006-09-04 21:19:58 +0200 (Mon, 04 Sep 2006) $" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import re, time +import re class DateTemplate: diff --git a/server/faildata.py b/server/faildata.py index 391e6b6e..bcd945d8 100644 --- a/server/faildata.py +++ b/server/faildata.py @@ -24,7 +24,7 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import time, logging +import logging # Gets the instance of the logger. logSys = logging.getLogger("fail2ban") diff --git a/server/failmanager.py b/server/failmanager.py index 1c931b82..2a43c9f1 100644 --- a/server/failmanager.py +++ b/server/failmanager.py @@ -27,7 +27,7 @@ __license__ = "GPL" from faildata import FailData from failticket import FailTicket from threading import Lock -import time, logging +import logging # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.filter") diff --git a/server/failticket.py b/server/failticket.py index 8a14b8d9..bc8e6c4d 100644 --- a/server/failticket.py +++ b/server/failticket.py @@ -24,7 +24,7 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import time, logging +import logging from ticket import Ticket # Gets the instance of the logger. diff --git a/server/filter.py b/server/filter.py index 229496e5..7b7c3c47 100644 --- a/server/filter.py +++ b/server/filter.py @@ -25,12 +25,11 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" from failmanager import FailManager -from failmanager import FailManagerEmpty from failticket import FailTicket from jailthread import JailThread from datedetector import DateDetector -import time, logging, os, re, sys, socket +import time, logging, re, socket # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.filter") @@ -286,7 +285,7 @@ class Filter(JailThread): ## # Open the log file. - def openLogFile(self, filename): + def __openLogFile(self, filename): """ Opens the log file specified on init. """ try: @@ -343,7 +342,7 @@ class Filter(JailThread): def getFailures(self, filename): ipList = dict() - ret = self.openLogFile(filename) + ret = self.__openLogFile(filename) if not ret: logSys.error("Unable to get failures in " + filename) return False diff --git a/server/filtergamin.py b/server/filtergamin.py index 670518ee..68b1f24e 100644 --- a/server/filtergamin.py +++ b/server/filtergamin.py @@ -24,10 +24,7 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -from failmanager import FailManager from failmanager import FailManagerEmpty -from failticket import FailTicket -from datedetector import DateDetector from filter import Filter import time, logging, gamin diff --git a/server/filterpoll.py b/server/filterpoll.py index 0950046d..78d37b58 100644 --- a/server/filterpoll.py +++ b/server/filterpoll.py @@ -24,10 +24,7 @@ __date__ = "$Date: 2006-09-13 23:31:22 +0200 (Wed, 13 Sep 2006) $" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -from failmanager import FailManager from failmanager import FailManagerEmpty -from failticket import FailTicket -from datedetector import DateDetector from filter import Filter import time, logging, os diff --git a/server/jailthread.py b/server/jailthread.py index 6bc0c397..02ebe449 100644 --- a/server/jailthread.py +++ b/server/jailthread.py @@ -25,6 +25,10 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" from threading import Thread +import logging + +# Gets the instance of the logger. +logSys = logging.getLogger("fail2ban.server") class JailThread(Thread): diff --git a/server/server.py b/server/server.py index 97747db3..932d8e86 100644 --- a/server/server.py +++ b/server/server.py @@ -26,7 +26,7 @@ __license__ = "GPL" from jails import Jails from transmitter import Transmitter -import locale, logging, logging.handlers, sys, os, signal +import logging, logging.handlers, sys, os, signal # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.server") @@ -126,7 +126,7 @@ class Server: def setFindTime(self, name, value): self.__jails.getFilter(name).setFindTime(value) - def getFindTime(self): + def getFindTime(self, name): return self.__jails.getFilter(name).getFindTime() def setFailRegex(self, name, value): diff --git a/server/ssocket.py b/server/ssocket.py index 01ca99c0..59d0c5e9 100644 --- a/server/ssocket.py +++ b/server/ssocket.py @@ -25,7 +25,8 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" from threading import Thread -import socket, time, logging, cPickle, os, os.path +from cPickle import dumps, loads +import socket, logging, os, os.path # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.comm") @@ -109,7 +110,7 @@ class SocketWorker(Thread): logSys.debug("Connection closed") def __send(self, socket, msg): - obj = cPickle.dumps(msg) + obj = dumps(msg) socket.send(obj + SSocket.END_STRING) def __receive(self, socket): @@ -119,7 +120,7 @@ class SocketWorker(Thread): if chunk == '': raise RuntimeError, "socket connection broken" msg = msg + chunk - return cPickle.loads(msg) + return loads(msg) class SSocketErrorException(Exception): diff --git a/server/ticket.py b/server/ticket.py index fd06b781..ee274eab 100644 --- a/server/ticket.py +++ b/server/ticket.py @@ -24,7 +24,7 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import time, logging +import logging # Gets the instance of the logger. logSys = logging.getLogger("fail2ban") diff --git a/server/transmitter.py b/server/transmitter.py index 487c6593..8740fa55 100644 --- a/server/transmitter.py +++ b/server/transmitter.py @@ -27,7 +27,7 @@ __license__ = "GPL" from ssocket import SSocket from ssocket import SSocketErrorException from threading import Lock -import re, pickle, logging +import logging, time # Gets the instance of the logger. logSys = logging.getLogger("fail2ban.comm") diff --git a/testcases/banmanagertestcase.py b/testcases/banmanagertestcase.py index 0f375a11..fee58631 100644 --- a/testcases/banmanagertestcase.py +++ b/testcases/banmanagertestcase.py @@ -32,25 +32,25 @@ class AddFailure(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.ticket = BanTicket('193.168.0.128', 1167605999.0) - self.banManager = BanManager() - self.assertTrue(self.banManager.addBanTicket(self.ticket)) + self.__ticket = BanTicket('193.168.0.128', 1167605999.0) + self.__banManager = BanManager() + self.assertTrue(self.__banManager.addBanTicket(self.__ticket)) def tearDown(self): """Call after every test case.""" def testAdd(self): - self.assertEqual(self.banManager.size(), 1) + self.assertEqual(self.__banManager.size(), 1) def testAddDuplicate(self): - self.assertFalse(self.banManager.addBanTicket(self.ticket)) - self.assertEqual(self.banManager.size(), 1) + self.assertFalse(self.__banManager.addBanTicket(self.__ticket)) + self.assertEqual(self.__banManager.size(), 1) def _testInListOK(self): ticket = BanTicket('193.168.0.128', 1167605999.0) - self.assertTrue(self.banManager.inBanList(ticket)) + self.assertTrue(self.__banManager.inBanList(ticket)) def _testInListNOK(self): ticket = BanTicket('111.111.1.111', 1167605999.0) - self.assertFalse(self.banManager.inBanList(ticket)) + self.assertFalse(self.__banManager.inBanList(ticket)) \ No newline at end of file diff --git a/testcases/datedetectortestcase.py b/testcases/datedetectortestcase.py index d5e554d0..1c311c06 100644 --- a/testcases/datedetectortestcase.py +++ b/testcases/datedetectortestcase.py @@ -32,8 +32,8 @@ class DateDetectorTest(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.datedetector = DateDetector() - self.datedetector.addDefaultTemplate() + self.__datedetector = DateDetector() + self.__datedetector.addDefaultTemplate() def tearDown(self): """Call after every test case.""" @@ -43,18 +43,18 @@ class DateDetectorTest(unittest.TestCase): date = [2006, 1, 23, 20, 59, 59, 0, 23, 0] dateUnix = 1138046399.0 - self.assertEqual(self.datedetector.getTime(log), date) - self.assertEqual(self.datedetector.getUnixTime(log), dateUnix) + self.assertEqual(self.__datedetector.getTime(log), date) + self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) def testGetTime(self): log = "Jan 23 21:59:59 [sshd] error: PAM: Authentication failure" date = [2006, 1, 23, 21, 59, 59, 1, 23, -1] dateUnix = 1138049999.0 - self.assertEqual(self.datedetector.getTime(log), date) - self.assertEqual(self.datedetector.getTime(log), date) - self.assertEqual(self.datedetector.getTime(log), date) - self.assertEqual(self.datedetector.getUnixTime(log), dateUnix) - self.assertEqual(self.datedetector.getUnixTime(log), dateUnix) - self.assertEqual(self.datedetector.getUnixTime(log), dateUnix) + self.assertEqual(self.__datedetector.getTime(log), date) + self.assertEqual(self.__datedetector.getTime(log), date) + self.assertEqual(self.__datedetector.getTime(log), date) + self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) + self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) + self.assertEqual(self.__datedetector.getUnixTime(log), dateUnix) \ No newline at end of file diff --git a/testcases/failmanagertestcase.py b/testcases/failmanagertestcase.py index e4a101e7..1e761b6c 100644 --- a/testcases/failmanagertestcase.py +++ b/testcases/failmanagertestcase.py @@ -33,47 +33,47 @@ 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], - ['87.142.124.10', 1167605999.0], - ['87.142.124.10', 1167605999.0], - ['87.142.124.10', 1167605999.0]] + 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], + ['87.142.124.10', 1167605999.0], + ['87.142.124.10', 1167605999.0], + ['87.142.124.10', 1167605999.0]] - self.failManager = FailManager() - for i in self.items: - self.failManager.addFailure(FailTicket(i[0], i[1])) + self.__failManager = FailManager() + for i in self.__items: + self.__failManager.addFailure(FailTicket(i[0], i[1])) def tearDown(self): """Call after every test case.""" def testAdd(self): - self.assertEqual(self.failManager.size(), 2) + self.assertEqual(self.__failManager.size(), 2) def _testDel(self): - self.failManager.delFailure('193.168.0.128') - self.failManager.delFailure('111.111.1.111') + self.__failManager.delFailure('193.168.0.128') + self.__failManager.delFailure('111.111.1.111') - self.assertEqual(self.failManager.size(), 1) + self.assertEqual(self.__failManager.size(), 1) def testCleanupOK(self): timestamp = 1167606999.0 - self.failManager.cleanup(timestamp) - self.assertEqual(self.failManager.size(), 0) + self.__failManager.cleanup(timestamp) + self.assertEqual(self.__failManager.size(), 0) def testCleanupNOK(self): timestamp = 1167605990.0 - self.failManager.cleanup(timestamp) - self.assertEqual(self.failManager.size(), 2) + self.__failManager.cleanup(timestamp) + self.assertEqual(self.__failManager.size(), 2) def testbanOK(self): - self.failManager.setMaxRetry(5) + self.__failManager.setMaxRetry(5) #ticket = FailTicket('193.168.0.128', None) - ticket = self.failManager.toBan() + ticket = self.__failManager.toBan() self.assertEqual(ticket.getIP(), "193.168.0.128") def testbanNOK(self): - self.failManager.setMaxRetry(10) - self.assertRaises(FailManagerEmpty, self.failManager.toBan) + self.__failManager.setMaxRetry(10) + self.assertRaises(FailManagerEmpty, self.__failManager.toBan) diff --git a/testcases/filtertestcase.py b/testcases/filtertestcase.py index 6fdabf3b..af1fbfba 100644 --- a/testcases/filtertestcase.py +++ b/testcases/filtertestcase.py @@ -32,7 +32,7 @@ class IgnoreIP(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.filter = FilterPoll(None) + self.__filter = FilterPoll(None) def tearDown(self): """Call after every test case.""" @@ -40,44 +40,44 @@ class IgnoreIP(unittest.TestCase): def testIgnoreIPOK(self): ipList = "127.0.0.1", "192.168.0.1", "255.255.255.255", "99.99.99.99" for ip in ipList: - self.filter.addIgnoreIP(ip) - self.assertTrue(self.filter.inIgnoreIPList(ip)) + self.__filter.addIgnoreIP(ip) + self.assertTrue(self.__filter.inIgnoreIPList(ip)) def testIgnoreIPNOK(self): ipList = "", "999.999.999.999", "abcdef", "192.168.0." for ip in ipList: - self.filter.addIgnoreIP(ip) - self.assertFalse(self.filter.inIgnoreIPList(ip)) + self.__filter.addIgnoreIP(ip) + self.assertFalse(self.__filter.inIgnoreIPList(ip)) class LogFile(unittest.TestCase): - filename = "testcases/files/testcase01.log" + FILENAME = "testcases/files/testcase01.log" def setUp(self): """Call before every test case.""" - self.filter = FilterPoll(None) - self.filter.addLogPath(LogFile.filename) + self.__filter = FilterPoll(None) + self.__filter.addLogPath(LogFile.FILENAME) def tearDown(self): """Call after every test case.""" - def testOpen(self): - self.filter.openLogFile(LogFile.filename) + #def testOpen(self): + # self.__filter.openLogFile(LogFile.FILENAME) def testIsModified(self): - self.assertTrue(self.filter.isModified(LogFile.filename)) + self.assertTrue(self.__filter.isModified(LogFile.FILENAME)) class GetFailures(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.filter = FilterPoll(None) - self.filter.addLogPath("testcases/files/testcase01.log") - self.filter.setTimeRegex("\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}") - self.filter.setTimePattern("%b %d %H:%M:%S") - self.filter.setFailRegex("(?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii](?:llegal|nvalid) user|ROOT LOGIN REFUSED) .*(?: from|FROM) (?:::f{4,6}:)?(?P\S*)") + self.__filter = FilterPoll(None) + self.__filter.addLogPath("testcases/files/testcase01.log") + self.__filter.setTimeRegex("\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}") + self.__filter.setTimePattern("%b %d %H:%M:%S") + self.__filter.setFailRegex("(?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii](?:llegal|nvalid) user|ROOT LOGIN REFUSED) .*(?: from|FROM) (?:::f{4,6}:)?(?P\S*)") def tearDown(self): """Call after every test case.""" @@ -86,12 +86,12 @@ class GetFailures(unittest.TestCase): output = [('87.142.124.10', 3, 1167605999.0), ('193.168.0.128', 3, 1167605999.0)] - self.filter.openLogFile() - self.filter.getFailures() + self.__filter.openLogFile() + self.__filter.getFailures() found = [] - for ip in self.filter.failManager.failList: - fData = self.filter.failManager.failList[ip] + for ip in self.__filter.failManager.failList: + fData = self.__filter.failManager.failList[ip] retry = fData.getRetry() lTime = fData.getLastTime() found.append((ip, retry, lTime)) diff --git a/testcases/servertestcase.py b/testcases/servertestcase.py index 42627111..90ca92e4 100644 --- a/testcases/servertestcase.py +++ b/testcases/servertestcase.py @@ -31,33 +31,33 @@ class StartStop(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.server = Server() - self.server.setLogLevel(0) - self.server.start(False) + self.__server = Server() + self.__server.setLogLevel(0) + self.__server.start(False) def tearDown(self): """Call after every test case.""" - self.server.quit() + self.__server.quit() def testStartStopJail(self): name = "TestCase" - self.server.addJail(name) - self.server.startJail(name) + self.__server.addJail(name) + self.__server.startJail(name) time.sleep(1) - self.server.stopJail(name) + self.__server.stopJail(name) class Transmitter(unittest.TestCase): def setUp(self): """Call before every test case.""" - self.server = Server() - self.server.setLogLevel(0) - self.server.start(False) + self.__server = Server() + self.__server.setLogLevel(0) + self.__server.start(False) def tearDown(self): """Call after every test case.""" - self.server.quit() + self.__server.quit() def testSetActionOK(self): name = "TestCase" @@ -79,7 +79,7 @@ class Transmitter(unittest.TestCase): cnt = 0 for cmd in cmdList: - self.assertEqual(self.server.transm.proceed(cmd), outList[cnt]) + self.assertEqual(self.__server.transm.proceed(cmd), outList[cnt]) cnt += 1 def testSetActionNOK(self): @@ -102,7 +102,7 @@ class Transmitter(unittest.TestCase): cnt = 0 for cmd in cmdList: - msg = self.server.transm.proceed(cmd) + msg = self.__server.transm.proceed(cmd) self.assertEqual(msg[0], outList[cnt]) cnt += 1 @@ -118,10 +118,10 @@ class Transmitter(unittest.TestCase): ["quit"]] for cmd in cmdList: - self.server.transm.proceed(cmd) + self.__server.transm.proceed(cmd) if cmd == ["start", name]: time.sleep(2) - jail = self.server.jails[name] + jail = self.__server.jails[name] self.assertEqual(jail.getFilter().failManager.size(), 0) self.assertEqual(jail.getAction().banManager.size(), 2) \ No newline at end of file -- cgit v1.2.1