mirror of https://github.com/fail2ban/fail2ban
ENH: allow_no_files option for jail's convert to allow testing of stock jail.conf
parent
61f81c6b2f
commit
057f0ad135
|
@ -103,7 +103,16 @@ class JailReader(ConfigReader):
|
||||||
logSys.warn("No actions were defined for %s" % self.__name)
|
logSys.warn("No actions were defined for %s" % self.__name)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def convert(self):
|
def convert(self, allow_no_files=False):
|
||||||
|
"""Convert read before __opts to the commands stream
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
allow_missing : bool
|
||||||
|
Either to allow log files to be missing entirely. Primarily is
|
||||||
|
used for testing
|
||||||
|
"""
|
||||||
|
|
||||||
stream = []
|
stream = []
|
||||||
for opt in self.__opts:
|
for opt in self.__opts:
|
||||||
if opt == "logpath":
|
if opt == "logpath":
|
||||||
|
@ -115,7 +124,7 @@ class JailReader(ConfigReader):
|
||||||
for p in pathList:
|
for p in pathList:
|
||||||
found_files += 1
|
found_files += 1
|
||||||
stream.append(["set", self.__name, "addlogpath", p])
|
stream.append(["set", self.__name, "addlogpath", p])
|
||||||
if not found_files:
|
if not (found_files or allow_no_files):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Have not found any log file for %s jail" % self.__name)
|
"Have not found any log file for %s jail" % self.__name)
|
||||||
elif opt == "backend":
|
elif opt == "backend":
|
||||||
|
|
|
@ -79,14 +79,23 @@ class JailsReader(ConfigReader):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def convert(self):
|
def convert(self, allow_no_files=False):
|
||||||
|
"""Convert read before __opts and jails to the commands stream
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
allow_missing : bool
|
||||||
|
Either to allow log files to be missing entirely. Primarily is
|
||||||
|
used for testing
|
||||||
|
"""
|
||||||
|
|
||||||
stream = list()
|
stream = list()
|
||||||
for opt in self.__opts:
|
for opt in self.__opts:
|
||||||
if opt == "":
|
if opt == "":
|
||||||
stream.append([])
|
stream.append([])
|
||||||
# Convert jails
|
# Convert jails
|
||||||
for jail in self.__jails:
|
for jail in self.__jails:
|
||||||
stream.extend(jail.convert())
|
stream.extend(jail.convert(allow_no_files=allow_no_files))
|
||||||
# Start jails
|
# Start jails
|
||||||
for jail in self.__jails:
|
for jail in self.__jails:
|
||||||
stream.append(["start", jail.getName()])
|
stream.append(["start", jail.getName()])
|
||||||
|
|
|
@ -132,7 +132,7 @@ class JailsReaderTest(unittest.TestCase):
|
||||||
jails = JailsReader(basedir='config', force_enable=True) # we are running tests from root project dir atm
|
jails = JailsReader(basedir='config', force_enable=True) # we are running tests from root project dir atm
|
||||||
self.assertTrue(jails.read()) # opens fine
|
self.assertTrue(jails.read()) # opens fine
|
||||||
self.assertTrue(jails.getOptions()) # reads fine
|
self.assertTrue(jails.getOptions()) # reads fine
|
||||||
comm_commands = jails.convert()
|
comm_commands = jails.convert(allow_no_files=True)
|
||||||
|
|
||||||
# by default we have lots of jails ;)
|
# by default we have lots of jails ;)
|
||||||
self.assertTrue(len(comm_commands))
|
self.assertTrue(len(comm_commands))
|
||||||
|
|
Loading…
Reference in New Issue