diff options
author | sebres <serg.brester@sebres.de> | 2018-04-26 16:48:23 +0200 |
---|---|---|
committer | sebres <serg.brester@sebres.de> | 2018-04-26 16:48:23 +0200 |
commit | 1b4ba602bac38a067b5abb9a941feab53c36c915 (patch) | |
tree | fc4887abb9a797759b9537e234960e29698b0f21 | |
parent | 60b36c421380b1ded02c5c6118f9e6dda070249d (diff) | |
download | fail2ban-1b4ba602bac38a067b5abb9a941feab53c36c915.tar.gz |
normalize bool conversion, introduced common helper function `_as_bool`
-rw-r--r-- | fail2ban/client/configreader.py | 4 | ||||
-rw-r--r-- | fail2ban/helpers.py | 4 | ||||
-rw-r--r-- | fail2ban/server/server.py | 9 |
3 files changed, 11 insertions, 6 deletions
diff --git a/fail2ban/client/configreader.py b/fail2ban/client/configreader.py index 2248ec34..b03daca9 100644 --- a/fail2ban/client/configreader.py +++ b/fail2ban/client/configreader.py @@ -29,7 +29,7 @@ import os from ConfigParser import NoOptionError, NoSectionError from .configparserinc import sys, SafeConfigParserWithIncludes, logLevel -from ..helpers import getLogger, _merge_dicts, substituteRecursiveTags +from ..helpers import getLogger, _as_bool, _merge_dicts, substituteRecursiveTags # Gets the instance of the logger. logSys = getLogger(__name__) @@ -339,7 +339,7 @@ class DefinitionInitConfigReader(ConfigReader): def _convert_to_boolean(self, value): - return value.lower() in ("1", "yes", "true", "on") + return _as_bool(value) def getCombOption(self, optname): """Get combined definition option (as string) using pre-set and init diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py index 7d6b5ce2..f7b0f1eb 100644 --- a/fail2ban/helpers.py +++ b/fail2ban/helpers.py @@ -244,6 +244,10 @@ else: return uni_decode(x, enc, 'replace') +def _as_bool(val): + return bool(val) if not isinstance(val, basestring) \ + else val.lower() in ('1', 'on', 'true', 'yes') + # # Following function used for parse options from parameter (e.g. `name[p1=0, p2="..."][p3='...']`). # diff --git a/fail2ban/server/server.py b/fail2ban/server/server.py index 17cfe016..5ae3d807 100644 --- a/fail2ban/server/server.py +++ b/fail2ban/server/server.py @@ -37,7 +37,8 @@ from .filter import FileFilter, JournalFilter from .transmitter import Transmitter from .asyncserver import AsyncServer, AsyncServerException from .. import version -from ..helpers import getLogger, extractOptions, str2LogLevel, getVerbosityFormat, excepthook +from ..helpers import getLogger, _as_bool, extractOptions, str2LogLevel, \ + getVerbosityFormat, excepthook # Gets the instance of the logger. logSys = getLogger(__name__) @@ -313,7 +314,7 @@ class Server: # Filter def setIgnoreSelf(self, name, value): - self.__jails[name].filter.ignoreSelf = value + self.__jails[name].filter.ignoreSelf = _as_bool(value) def getIgnoreSelf(self, name): return self.__jails[name].filter.ignoreSelf @@ -628,11 +629,11 @@ class Server: # If handler don't already add date to the message: addtime = logOptions.get('datetime') if addtime is not None: - addtime = addtime in ('1', 'on', 'true', 'yes') + addtime = _as_bool(addtime) else: addtime = systarget not in ("SYSLOG", "SYSOUT") if padding is not None: - padding = padding in ('1', 'on', 'true', 'yes') + padding = _as_bool(padding) else: padding = True # If log-format is redefined in options: |