cc: add data_dir_abs_path property to EnvironmentConfig

EnvironmentConfig needs to handle environment variables and '~' in its
`data_dir` property. Other components that consume `data_dir` need
environment variables and '~' resolved to an absolute path. Add a
property called `data_dir_abs_path` that calculates the absolute path
from `data_dir`. Since `data_dir` remains unchanged, the
EnvironmentConfig can be saved to file without modifying the `data_dir`
option in the file.
This commit is contained in:
Mike Salvatore 2021-02-16 11:59:27 -05:00
parent 20a3d31852
commit e1209dcb4c
6 changed files with 25 additions and 13 deletions

View File

@ -50,6 +50,10 @@ class EnvironmentConfig:
self.aws = aws
self.data_dir = data_dir
@property
def data_dir_abs_path(self):
return os.path.abspath(os.path.expanduser(os.path.expandvars(self.data_dir)))
def save_to_file(self):
with open(self._server_config_path, "w") as f:
f.write(json.dumps(self.to_dict(), indent=2))

View File

@ -24,7 +24,7 @@ STANDARD_WITH_CREDENTIALS = os.path.join(
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
)
WITH_DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir.json")
WITH_DATA_DIR_HOME = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir_home.json")
@pytest.fixture
def config_file(tmpdir):
@ -124,3 +124,14 @@ def test_generate_default_file(config_file):
def test_data_dir():
environment_config = EnvironmentConfig(WITH_DATA_DIR)
assert environment_config.data_dir == "/test/data/dir"
def set_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))
def test_data_dir_abs_path_from_file(monkeypatch, tmpdir):
set_home_env(monkeypatch, tmpdir)
config = EnvironmentConfig(WITH_DATA_DIR_HOME)
assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir")

View File

@ -35,7 +35,7 @@ MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0"
def main(should_setup_only=False, server_config_filename=DEFAULT_SERVER_CONFIG_PATH):
logger.info("Starting bootloader server")
env_singleton.initialize_from_file(server_config_filename)
initialize_encryptor(env_singleton.config.data_dir)
initialize_encryptor(env_singleton.config.data_dir_abs_path)
mongo_url = os.environ.get('MONGO_URL', env_singleton.env.get_mongo_url())
bootloader_server_thread = Thread(target=BootloaderHttpServer(mongo_url).serve_forever, daemon=True)

View File

@ -16,9 +16,7 @@ class Encryptor:
_PASSWORD_FILENAME = "mongo_key.bin"
def __init__(self, password_file_dir):
password_file = os.path.expanduser(
os.path.join(password_file_dir, self._PASSWORD_FILENAME)
)
password_file = os.path.join(password_file_dir, self._PASSWORD_FILENAME)
if os.path.exists(password_file):
self._load_existing_key(password_file)

View File

@ -33,11 +33,3 @@ def test_create_new_password_file(tmpdir):
initialize_encryptor(tmpdir)
assert os.path.isfile(os.path.join(tmpdir, PASSWORD_FILENAME))
def test_expand_home(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))
initialize_encryptor("~/")
assert os.path.isfile(os.path.join(tmpdir, "mongo_key.bin"))

View File

@ -0,0 +1,7 @@
{
"server_config": "password",
"deployment": "develop",
"user": "test",
"password_hash": "abcdef",
"data_dir": "~/data_dir"
}