Skip to content

Commit

Permalink
config: prefer configuration specified files
Browse files Browse the repository at this point in the history
Suricata-Update was preferring the existence of "disable.conf" in
$sysconfdir over it be specified in the update.yaml.

Refactor the auto-conf to only search and apply the default
$sysconfdir files if they don't already exist in the config.

Additonally, now that the default, if not set or found will be none,
log a warning if a specific configuration file is not found instead of
silently ignoring.

Ticket: #6172
  • Loading branch information
jasonish committed Jul 4, 2023
1 parent 1a870c0 commit d2f2ca5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
35 changes: 19 additions & 16 deletions suricata/update/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@
]

DEFAULT_CONFIG = {
"disable-conf": "/etc/suricata/disable.conf",
"enable-conf": "/etc/suricata/enable.conf",
"drop-conf": "/etc/suricata/drop.conf",
"modify-conf": "/etc/suricata/modify.conf",
"sources": [],
LOCAL_CONF_KEY: [],

Expand Down Expand Up @@ -228,23 +224,30 @@ def init(args):

# Fixup the default locations for Suricata-Update configuration files, but only if
# they exist, otherwise keep the defaults.
conf_search_path = ["/etc"]
if "sysconfdir" in build_info:
configs = (
("disable-conf", "disable.conf"),
("enable-conf", "enable.conf"),
("drop-conf", "drop.conf"),
("modify-conf", "modify.conf"),
)
sysconfdir = build_info["sysconfdir"]
for key, filename in configs:
config_path = os.path.join(sysconfdir, "suricata", filename)
if not sysconfdir in conf_search_path:
conf_search_path.insert(0, sysconfdir)
configs = (
("disable-conf", "disable.conf"),
("enable-conf", "enable.conf"),
("drop-conf", "drop.conf"),
("modify-conf", "modify.conf"),
)
for key, filename in configs:
if getattr(args, key.replace("-", "_"), None) is not None:
continue
if _config.get(key) is not None:
continue
for conf_dir in conf_search_path:
config_path = os.path.join(conf_dir, "suricata", filename)
logger.debug("Looking for {}".format(config_path))
if os.path.exists(config_path):
logger.debug("Found {}".format(config_path))
val = getattr(args, key.replace("-", "_"), None)
if val is None:
logger.debug("Changing default for {} to {}".format(key, config_path))
_config[key] = config_path
logger.debug("Using {} for {}".format(config_path, key))
_config[key] = config_path
break

# If suricata-conf not provided on the command line or in the
# configuration file, look for it.
Expand Down
36 changes: 24 additions & 12 deletions suricata/update/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,27 +1135,39 @@ def _main():

# Load user provided disable filters.
disable_conf_filename = config.get("disable-conf")
if disable_conf_filename and os.path.exists(disable_conf_filename):
logger.info("Loading %s.", disable_conf_filename)
disable_matchers += load_matchers(disable_conf_filename)
if disable_conf_filename:
if os.path.exists(disable_conf_filename):
logger.info("Loading %s.", disable_conf_filename)
disable_matchers += load_matchers(disable_conf_filename)
else:
logger.warn("disable-conf file does not exist: {}".format(disable_conf_filename))

# Load user provided enable filters.
enable_conf_filename = config.get("enable-conf")
if enable_conf_filename and os.path.exists(enable_conf_filename):
logger.info("Loading %s.", enable_conf_filename)
enable_matchers += load_matchers(enable_conf_filename)
if enable_conf_filename:
if os.path.exists(enable_conf_filename):
logger.info("Loading %s.", enable_conf_filename)
enable_matchers += load_matchers(enable_conf_filename)
else:
logger.warn("enable-conf file does not exist: {}".format(enable_conf_filename))

# Load user provided modify filters.
modify_conf_filename = config.get("modify-conf")
if modify_conf_filename and os.path.exists(modify_conf_filename):
logger.info("Loading %s.", modify_conf_filename)
modify_filters += load_filters(modify_conf_filename)
if modify_conf_filename:
if os.path.exists(modify_conf_filename):
logger.info("Loading %s.", modify_conf_filename)
modify_filters += load_filters(modify_conf_filename)
else:
logger.warn("modify-conf file does not exist: {}".format(modify_conf_filename))

# Load user provided drop filters.
drop_conf_filename = config.get("drop-conf")
if drop_conf_filename and os.path.exists(drop_conf_filename):
logger.info("Loading %s.", drop_conf_filename)
drop_filters += load_drop_filters(drop_conf_filename)
if drop_conf_filename:
if os.path.exists(drop_conf_filename):
logger.info("Loading %s.", drop_conf_filename)
drop_filters += load_drop_filters(drop_conf_filename)
else:
logger.warn("drop-conf file does not exist: {}".format(drop_conf_filename))

# Load the Suricata configuration if we can.
suriconf = None
Expand Down

0 comments on commit d2f2ca5

Please sign in to comment.