Add function for Flask app to read config from a directory.

The application now checks for an environment variable,
OVERRIDE_CONFIG_DIRECTORY. If that value is set, it loops all the files
in the specified directory and checks if their names correspond to known
configuration settings. The contents of any matching files are read and
set as the new configuration value for that setting. This will allow us
to read mounted Azure Key Vault items as config values.

This also moves the functionality for applying environment variables to
the config into an analogous function.
This commit is contained in:
dandds
2019-12-05 14:11:35 -05:00
parent 972cf14a66
commit f8c31e4dcf
2 changed files with 81 additions and 8 deletions

View File

@@ -1,8 +1,13 @@
import os
from configparser import ConfigParser
import pytest
from atst.app import make_crl_validator
from atst.app import (
make_crl_validator,
apply_config_from_directory,
apply_config_from_environment,
)
@pytest.fixture
@@ -22,3 +27,43 @@ def test_make_crl_validator_creates_crl_dir(app, tmpdir, replace_crl_dir_config)
replace_crl_dir_config(crl_dir)
make_crl_validator(app)
assert os.path.isdir(crl_dir)
@pytest.fixture
def config_object():
config = ConfigParser()
config.optionxform = str
config.read_string("[default]\nFOO=BALONEY")
return config
def test_apply_config_from_directory(tmpdir, config_object):
config_setting = tmpdir.join("FOO")
with open(config_setting, "w") as conf_file:
conf_file.write("MAYO")
apply_config_from_directory(tmpdir, config_object)
assert config_object.get("default", "FOO") == "MAYO"
def test_apply_config_from_directory_skips_unknown_settings(tmpdir, config_object):
config_setting = tmpdir.join("FLARF")
with open(config_setting, "w") as conf_file:
conf_file.write("MAYO")
apply_config_from_directory(tmpdir, config_object)
assert "FLARF" not in config_object.options("default")
def test_apply_config_from_environment(monkeypatch, config_object):
monkeypatch.setenv("FOO", "MAYO")
apply_config_from_environment(config_object)
assert config_object.get("default", "FOO") == "MAYO"
def test_apply_config_from_environment_skips_unknown_settings(
monkeypatch, config_object
):
monkeypatch.setenv("FLARF", "MAYO")
apply_config_from_environment(config_object)
assert "FLARF" not in config_object.options("default")