mirror of https://github.com/fail2ban/fail2ban
introduces new command-line options `--dp`, `--dump-pretty` to dump the configuration using more human readable representation;
allow dump of configuration, also if log-file is not available (warning only)pull/1889/head
parent
fd83260bd8
commit
b698a74902
|
@ -76,9 +76,9 @@ class Configurator:
|
||||||
self.__fail2ban.getOptions(updateMainOpt)
|
self.__fail2ban.getOptions(updateMainOpt)
|
||||||
return self.__jails.getOptions(jail, ignoreWrong=ignoreWrong)
|
return self.__jails.getOptions(jail, ignoreWrong=ignoreWrong)
|
||||||
|
|
||||||
def convertToProtocol(self):
|
def convertToProtocol(self, allow_no_files=False):
|
||||||
self.__streams["general"] = self.__fail2ban.convert()
|
self.__streams["general"] = self.__fail2ban.convert()
|
||||||
self.__streams["jails"] = self.__jails.convert()
|
self.__streams["jails"] = self.__jails.convert(allow_no_files=allow_no_files)
|
||||||
|
|
||||||
def getConfigStream(self):
|
def getConfigStream(self):
|
||||||
cmds = list()
|
cmds = list()
|
||||||
|
|
|
@ -102,6 +102,7 @@ class Fail2banCmdLine():
|
||||||
output(" --logtarget <FILE>|STDOUT|STDERR|SYSLOG")
|
output(" --logtarget <FILE>|STDOUT|STDERR|SYSLOG")
|
||||||
output(" --syslogsocket auto|<FILE>")
|
output(" --syslogsocket auto|<FILE>")
|
||||||
output(" -d dump configuration. For debugging")
|
output(" -d dump configuration. For debugging")
|
||||||
|
output(" --dp, --dump-pretty dump the configuration using more human readable representation")
|
||||||
output(" -t, --test test configuration (can be also specified with start parameters)")
|
output(" -t, --test test configuration (can be also specified with start parameters)")
|
||||||
output(" -i interactive mode")
|
output(" -i interactive mode")
|
||||||
output(" -v increase verbosity")
|
output(" -v increase verbosity")
|
||||||
|
@ -137,8 +138,8 @@ class Fail2banCmdLine():
|
||||||
self._conf["pidfile"] = opt[1]
|
self._conf["pidfile"] = opt[1]
|
||||||
elif o.startswith("--log") or o.startswith("--sys"):
|
elif o.startswith("--log") or o.startswith("--sys"):
|
||||||
self._conf[ o[2:] ] = opt[1]
|
self._conf[ o[2:] ] = opt[1]
|
||||||
elif o == "-d":
|
elif o in ["-d", "--dp", "--dump-pretty"]:
|
||||||
self._conf["dump"] = True
|
self._conf["dump"] = True if o == "-d" else 2
|
||||||
elif o == "-t" or o == "--test":
|
elif o == "-t" or o == "--test":
|
||||||
self.cleanConfOnly = True
|
self.cleanConfOnly = True
|
||||||
self._conf["test"] = True
|
self._conf["test"] = True
|
||||||
|
@ -184,7 +185,8 @@ class Fail2banCmdLine():
|
||||||
# Reads the command line options.
|
# Reads the command line options.
|
||||||
try:
|
try:
|
||||||
cmdOpts = 'hc:s:p:xfbdtviqV'
|
cmdOpts = 'hc:s:p:xfbdtviqV'
|
||||||
cmdLongOpts = ['loglevel=', 'logtarget=', 'syslogsocket=', 'test', 'async', 'timeout=', 'str2sec=', 'help', 'version']
|
cmdLongOpts = ['loglevel=', 'logtarget=', 'syslogsocket=', 'test', 'async',
|
||||||
|
'timeout=', 'str2sec=', 'help', 'version', 'dp', '--dump-pretty']
|
||||||
optList, self._args = getopt.getopt(self._argv[1:], cmdOpts, cmdLongOpts)
|
optList, self._args = getopt.getopt(self._argv[1:], cmdOpts, cmdLongOpts)
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
self.dispUsage()
|
self.dispUsage()
|
||||||
|
@ -240,7 +242,10 @@ class Fail2banCmdLine():
|
||||||
if readcfg:
|
if readcfg:
|
||||||
ret, stream = self.readConfig()
|
ret, stream = self.readConfig()
|
||||||
readcfg = False
|
readcfg = False
|
||||||
self.dumpConfig(stream)
|
if stream is not None:
|
||||||
|
self.dumpConfig(stream, self._conf["dump"] == 2)
|
||||||
|
else: # pragma: no cover
|
||||||
|
output("ERROR: The configuration stream failed because of the invalid syntax.")
|
||||||
if not self._conf.get("test", False):
|
if not self._conf.get("test", False):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
@ -275,7 +280,8 @@ class Fail2banCmdLine():
|
||||||
self.configurator.readAll()
|
self.configurator.readAll()
|
||||||
ret = self.configurator.getOptions(jail, self._conf,
|
ret = self.configurator.getOptions(jail, self._conf,
|
||||||
ignoreWrong=not self.cleanConfOnly)
|
ignoreWrong=not self.cleanConfOnly)
|
||||||
self.configurator.convertToProtocol()
|
self.configurator.convertToProtocol(
|
||||||
|
allow_no_files=self._conf.get("dump", False))
|
||||||
stream = self.configurator.getConfigStream()
|
stream = self.configurator.getConfigStream()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logSys.error("Failed during configuration: %s" % e)
|
logSys.error("Failed during configuration: %s" % e)
|
||||||
|
@ -283,9 +289,15 @@ class Fail2banCmdLine():
|
||||||
return ret, stream
|
return ret, stream
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def dumpConfig(cmd):
|
def dumpConfig(cmd, pretty=False):
|
||||||
|
if pretty:
|
||||||
|
from pprint import pformat
|
||||||
|
def _output(s):
|
||||||
|
output(pformat(s, width=1000, indent=2))
|
||||||
|
else:
|
||||||
|
_output = output
|
||||||
for c in cmd:
|
for c in cmd:
|
||||||
output(c)
|
_output(c)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
|
@ -235,9 +235,12 @@ class JailReader(ConfigReader):
|
||||||
found_files += 1
|
found_files += 1
|
||||||
stream.append(
|
stream.append(
|
||||||
["set", self.__name, "addlogpath", p, tail])
|
["set", self.__name, "addlogpath", p, tail])
|
||||||
if not (found_files or allow_no_files):
|
if not found_files:
|
||||||
raise ValueError(
|
msg = "Have not found any log file for %s jail" % self.__name
|
||||||
"Have not found any log file for %s jail" % self.__name)
|
if not allow_no_files:
|
||||||
|
raise ValueError(msg)
|
||||||
|
logSys.warning(msg)
|
||||||
|
|
||||||
elif opt == "logencoding":
|
elif opt == "logencoding":
|
||||||
stream.append(["set", self.__name, "logencoding", value])
|
stream.append(["set", self.__name, "logencoding", value])
|
||||||
elif opt == "backend":
|
elif opt == "backend":
|
||||||
|
|
|
@ -426,7 +426,11 @@ class Fail2banClientTest(Fail2banClientServerBase):
|
||||||
startparams = _start_params(tmp, True)
|
startparams = _start_params(tmp, True)
|
||||||
self.execSuccess(startparams, "-vvd")
|
self.execSuccess(startparams, "-vvd")
|
||||||
self.assertLogged("Loading files")
|
self.assertLogged("Loading files")
|
||||||
self.assertLogged("logtarget")
|
self.assertLogged("['set', 'logtarget',")
|
||||||
|
self.pruneLog()
|
||||||
|
# pretty dump:
|
||||||
|
self.execSuccess(startparams, "--dp")
|
||||||
|
self.assertLogged("['set', 'logtarget',")
|
||||||
|
|
||||||
@with_tmpdir
|
@with_tmpdir
|
||||||
@with_kill_srv
|
@with_kill_srv
|
||||||
|
|
Loading…
Reference in New Issue