Skip to content

Commit

Permalink
add ssm safety check
Browse files Browse the repository at this point in the history
  • Loading branch information
adamshire123 committed Feb 22, 2023
1 parent 237c1a2 commit 767c02d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
27 changes: 18 additions & 9 deletions sapinvoices/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ def configure_sentry() -> str:


def load_config_values() -> dict:
result = {}
for setting in CONFIG:
result[setting] = os.getenv(setting)
# all settings are required in prod and stage
if os.getenv("WORKSPACE") in ["prod", "stage"]:
for setting, value in result.items():
if not value:
raise RuntimeError(f"Required env variable {setting} is not set")
return result
settings = {}
for variable in CONFIG:
settings[variable] = os.getenv(variable)
# all settings are required in prod and stage
if os.getenv("WORKSPACE") in ["prod", "stage"]:
for setting, value in settings.items():
if not value:
raise RuntimeError(f"Required env variable {setting} is not set")
ssm_safety_check(settings)
return settings


def ssm_safety_check(settings):
if "prod" in settings["SSM_PATH"] and settings["WORKSPACE"] != "prod":
raise Exception(
"Production SSM_PATH may ONLY be used in the production "
"environment. Check your env variables and try again."
)
11 changes: 11 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,14 @@ def test_dev_missing_config_values_is_ok(monkeypatch):
monkeypatch.delenv("ALMA_API_URL", raising=False)
settings = load_config_values()
assert not all(settings.values())


def test_ssm_safety_check_raises_error(monkeypatch):
monkeypatch.setenv("WORKSPACE", "whatever")
monkeypatch.setenv("SSM_PATH", "/test/example/prod")
with pytest.raises(Exception) as e:
load_config_values()
assert str(e.value) == (
"Production SSM_PATH may ONLY be used in the production environment. "
"Check your env variables and try again."
)

0 comments on commit 767c02d

Please sign in to comment.