diff --git a/src/config.py b/src/config.py index b6e2bfdd9..3d53323fc 100644 --- a/src/config.py +++ b/src/config.py @@ -167,7 +167,7 @@ def _validate_temp_dir_path(cls, value): return value -def load(path: str | None = None): +def load(path: str | Path | None = None): """Load the config file located at ``path``. The file must be a toml file and is read into instances of :py:class:`~config.Backend`, :py:class:`~config.Frontend` and :py:class:`~config.Common`. diff --git a/src/test/data/fact-core-config.toml b/src/test/data/fact-core-config.toml deleted file mode 100644 index 7b5d234ad..000000000 --- a/src/test/data/fact-core-config.toml +++ /dev/null @@ -1,123 +0,0 @@ -[common] -# Test if default value gets set -# temp-dir-path = "/tmp" -# Directory that will be used to share data from the host to docker containers -# Permissions have to be 0o770 and the group has to be 'docker'. -# Will be created if it does not exist -docker-mount-base-dir = "/tmp/fact-docker-mount-base-dir" - -[common.redis] -fact-db = 3 -test-db = 13 -host = "localhost" -port = 6379 -# Set the password for redis here if you have AUTH enabled -# password = - -[common.logging] - -[common.postgres] -server = "localhost" -port = 5432 -database = "fact_db" -test-database = "fact_test" -ro-user = "fact_user_ro" -ro-pw = "change_me_ro" -rw-user = "fact_user_rw" -rw-pw = "change_me_rw" -del-user = "fact_user_del" -del-pw = "change_me_del" -admin-user = "fact_admin" -admin-pw = "change_me_admin" - -[[common.analysis_preset]] -name = "default" -# choose preselected plugins -plugins = [ - "cpu_architecture", - "crypto_material", - "cve_lookup", - "exploit_mitigations", - "known_vulnerabilities", - "software_components", - "users_and_passwords", -] - -[[common.analysis_preset]] -name = "minimal" -plugins = [] - -[backend] -firmware-file-storage-directory = "/media/data/fact_fw_data" -block-delay = 0.1 -ssdeep-ignore = 1 -intercom-poll-delay = 1.0 -throw-exceptions = false - -[backend.plugin-defaults] -# Default number of processes for plugins. -# Can be overwritten by setting backend.plugin.processes -processes = 4 - -[backend.unpacking] -processes = 4 -# file types in whitelist are not extracted -whitelist = [ - "application/x-object", - "application/x-shockwave-flash", - "audio/mpeg", - "image/gif", - "image/jpeg", - "image/png", - "text/plain", - "video/mp4", - "video/mpeg", - "video/ogg", - "video/quicktime", - "video/x-msvideo", -] -# extract until this layer -max-depth = 8 -# Memory limit in MiB. -memory-limit = 2048 -threshold = 0.8 -throttle-limit = 50 -# tcp port(s) for task server -base-port = 9900 -# if you experience FileNotFound errors during unpacking, increasing this value slightly might help -delay = 0.0 - -[backend.binary-search] -# maximum number of matching strings stored per match -max-strings-per-match = 10 - - -[[backend.plugin]] -name = "cpu_architecture" -processes = 4 - -[[backend.plugin]] -name = "cve_loookup" -processes = 2 - - -[frontend] -results-per-page = 10 -number-of-latest-firmwares-to-display = 10 -ajax-stats-reload-time = 10000 -max-elements-per-chart = 10 -# This is used in redirecting to the radare web service. -# If you use our nginx config this must be set to "https://localhost/radare" -# otherwise it must be set to "http://localhost:8000" -radare2-url = "http://localhost:8000" -communication-timeout = 60 - - -[frontend.authentication] -enabled = false -user-database = "sqlite:////media/data/fact_auth_data/fact_users.db" -password-salt = "5up3r5tr0n6_p455w0rd_5417" - - -[frontend.hasura] -admin-secret = "4dM1n_S3cR3T_changemeplz" diff --git a/src/test/data/fact-core-config.toml-missing-entrys b/src/test/data/fact-core-config.toml-missing-entrys deleted file mode 100644 index 81d4d680b..000000000 --- a/src/test/data/fact-core-config.toml-missing-entrys +++ /dev/null @@ -1,123 +0,0 @@ -[common] -temp-dir-path = "/tmp" -# Directory that will be used to share data from the host to docker containers -# Permissions have to be 0o770 and the group has to be 'docker'. -# Will be created if it does not exist -docker-mount-base-dir = "/tmp/fact-docker-mount-base-dir" - -[common.redis] -fact-db = 3 -test-db = 13 -host = "localhost" -port = 6379 -# Set the password for redis here if you have AUTH enabled -# password = - -[common.logging] -level = "WARNING" -file = "/tmp/fact_main.log" - -[common.postgres] -port = 5432 -database = "fact_db" -test-database = "fact_test" -ro-user = "fact_user_ro" -ro-pw = "change_me_ro" -rw-user = "fact_user_rw" -rw-pw = "change_me_rw" -del-user = "fact_user_del" -del-pw = "change_me_del" -admin-user = "fact_admin" -admin-pw = "change_me_admin" - -[[common.analysis_preset]] -name = "default" -# choose preselected plugins -plugins = [ - "cpu_architecture", - "crypto_material", - "cve_lookup", - "exploit_mitigations", - "known_vulnerabilities", - "software_components", - "users_and_passwords", -] - -[[common.analysis_preset]] -name = "minimal" -plugins = [] - -[backend] -firmware-file-storage-directory = "/media/data/fact_fw_data" -block-delay = 0.1 -ssdeep-ignore = 1 -intercom-poll-delay = 1.0 -throw-exceptions = false - -[backend.plugin-defaults] -# Default number of processes for plugins. -# Can be overwritten by setting backend.plugin.processes -processes = 4 - -[backend.unpacking] -processes = 4 -# file types in whitelist are not extracted -whitelist = [ - "application/x-object", - "application/x-shockwave-flash", - "audio/mpeg", - "image/gif", - "image/jpeg", - "image/png", - "text/plain", - "video/mp4", - "video/mpeg", - "video/ogg", - "video/quicktime", - "video/x-msvideo", -] -# extract until this layer -max-depth = 8 -# Memory limit in MiB. -memory-limit = 2048 -threshold = 0.8 -throttle-limit = 50 -# tcp port(s) for task server -base-port = 9900 -# if you experience FileNotFound errors during unpacking, increasing this value slightly might help -delay = 0.0 - -[backend.binary-search] -# maximum number of matching strings stored per match -max-strings-per-match = 10 - - -[[backend.plugin]] -name = "cpu_architecture" -processes = 4 - -[[backend.plugin]] -name = "cve_loookup" -processes = 2 - - -[frontend] -results-per-page = 10 -number-of-latest-firmwares-to-display = 10 -ajax-stats-reload-time = 10000 -max-elements-per-chart = 10 -# This is used in redirecting to the radare web service. -# If you use our nginx config this must be set to "https://localhost/radare" -# otherwise it must be set to "http://localhost:8000" -radare2-url = "http://localhost:8000" -communication-timeout = 60 - - -[frontend.authentication] -enabled = false -user-database = "sqlite:////media/data/fact_auth_data/fact_users.db" -password-salt = "5up3r5tr0n6_p455w0rd_5417" - - -[frontend.hasura] -admin-secret = "4dM1n_S3cR3T_changemeplz" diff --git a/src/test/unit/test_config.py b/src/test/unit/test_config.py index 351a25df6..a9e77040a 100644 --- a/src/test/unit/test_config.py +++ b/src/test/unit/test_config.py @@ -1,3 +1,6 @@ +from pathlib import Path +from tempfile import TemporaryDirectory + import pydantic import pytest @@ -6,14 +9,15 @@ # We explicitly don't want the patch_cfg fixture to be able to patch this function # This is why we import it here from config import load -from test.common_helper import get_test_data_dir +from helperFunctions.fileSystem import get_config_dir + +CONFIG_PATH = Path(get_config_dir()) / 'fact-core-config.toml' def test_load(monkeypatch): # Undo all monkeypatching which includes what `patch_config` patched. monkeypatch.undo() - cfg_path = f'{get_test_data_dir()}/fact-core-config.toml' - load(path=cfg_path) + load(path=CONFIG_PATH) assert config.common is not None, 'common global was not set' assert config.backend is not None, 'backend global was not set' @@ -23,6 +27,12 @@ def test_load(monkeypatch): def test_load_missing_entries(): - cfg_path = get_test_data_dir() + '/fact-core-config.toml-missing-entrys' - with pytest.raises(pydantic.ValidationError, match='server'): - load(path=cfg_path) + cfg_contents = CONFIG_PATH.read_text() + assert '[common.postgres]\nserver =' in cfg_contents + # comment out server + cfg_contents = cfg_contents.replace('[common.postgres]\nserver =', '[common.postgres]\n# server =') + with TemporaryDirectory() as tmp_dir: + cfg_path = Path(tmp_dir) / 'config.toml' + cfg_path.write_text(cfg_contents) + with pytest.raises(pydantic.ValidationError, match='server'): + load(path=cfg_path)