From f8abd36fb34d726f6e83b772aba19e6f295db2a5 Mon Sep 17 00:00:00 2001 From: Constantin M Adam Date: Sat, 23 Dec 2023 11:38:48 -0500 Subject: [PATCH 1/2] Use environment variables in configuration files --- packages/kestrel_core/src/kestrel/config.py | 4 +- packages/kestrel_core/tests/test_config.py | 60 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 packages/kestrel_core/tests/test_config.py diff --git a/packages/kestrel_core/src/kestrel/config.py b/packages/kestrel_core/src/kestrel/config.py index 9955c4f3..9bc10275 100644 --- a/packages/kestrel_core/src/kestrel/config.py +++ b/packages/kestrel_core/src/kestrel/config.py @@ -16,7 +16,7 @@ def load_default_config(): _logger.debug(f"Loading default config file...") default_config = load_data_file("kestrel", "config.yaml") - return yaml.safe_load(default_config) + return yaml.safe_load(os.path.expandvars(default_config)) def load_user_config(config_path_env_var, config_path_default): @@ -27,7 +27,7 @@ def load_user_config(config_path_env_var, config_path_default): try: with open(config_path, "r") as fp: _logger.debug(f"User configuration file found: {config_path}") - config = yaml.safe_load(fp) + config = yaml.safe_load(os.path.expandvars(fp.read())) except FileNotFoundError: _logger.debug(f"User configuration file not exist.") return config diff --git a/packages/kestrel_core/tests/test_config.py b/packages/kestrel_core/tests/test_config.py new file mode 100644 index 00000000..d3ee2830 --- /dev/null +++ b/packages/kestrel_core/tests/test_config.py @@ -0,0 +1,60 @@ +import kestrel.config.utils as cfg +import os + + +def test_env_vars_in_config(): + + test_config = """--- +credentials: + username: $TEST_USER + password: $TEST_PASSWORD + """ + os.environ["TEST_USER"] = "test-user" + os.environ["TEST_PASSWORD"] = "test-password" + os.environ["KESTREL_CONFIG"] = os.path.join(os.sep, "tmp", "config.yaml") + + with open(os.getenv("KESTREL_CONFIG"), "w") as fp: + fp.write(test_config) + config = cfg.load_config() + assert config["credentials"]["username"] == "test-user" + assert config["credentials"]["password"] == "test-password" + + +def test_env_vars_in_config_overwrite(): + + test_config = """--- +credentials: + username: ${TEST_USER} + password: ${TEST_PASSWORD} +debug: + cache_directory_prefix: $KESTREL_CACHE_DIRECTORY_PREFIX + """ + os.environ["TEST_USER"] = "test-user" + os.environ["TEST_PASSWORD"] = "test-password" + os.environ["KESTREL_CONFIG"] = os.path.join(os.sep, "tmp", "config.yaml") + os.environ["KESTREL_CACHE_DIRECTORY_PREFIX"] = "Kestrel2.0-" + with open(os.getenv("KESTREL_CONFIG"), "w") as fp: + fp.write(test_config) + config = cfg.load_config() + assert config["credentials"]["username"] == "test-user" + assert config["credentials"]["password"] == "test-password" + assert config["debug"]["cache_directory_prefix"] == "Kestrel2.0-" + +def test_empty_env_var_in_config(): + test_config = """--- +credentials: + username: ${TEST_USER} + password: ${TEST_PASSWORD} +debug: + cache_directory_prefix: $I_DONT_EXIST + """ + os.environ["TEST_USER"] = "test-user" + os.environ["TEST_PASSWORD"] = "test-password" + os.environ["KESTREL_CONFIG"] = os.path.join(os.sep, "tmp", "config.yaml") + os.environ["KESTREL_CACHE_DIRECTORY_PREFIX"] = "Kestrel2.0-" + with open(os.getenv("KESTREL_CONFIG"), "w") as fp: + fp.write(test_config) + config = cfg.load_config() + assert config["credentials"]["username"] == "test-user" + assert config["credentials"]["password"] == "test-password" + assert config["debug"]["cache_directory_prefix"] == "$I_DONT_EXIST" From e32bc67030d62a62c5d4483b79083ba052b6ba3e Mon Sep 17 00:00:00 2001 From: Constantin M Adam Date: Sat, 23 Dec 2023 11:46:08 -0500 Subject: [PATCH 2/2] Use environment variables in configuration files --- packages/kestrel_core/tests/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kestrel_core/tests/test_config.py b/packages/kestrel_core/tests/test_config.py index d3ee2830..985a3a6f 100644 --- a/packages/kestrel_core/tests/test_config.py +++ b/packages/kestrel_core/tests/test_config.py @@ -1,4 +1,4 @@ -import kestrel.config.utils as cfg +import kestrel.config as cfg import os