Skip to content

Commit 18f9b53

Browse files
committed
Add behaviors and rules for settings use
1 parent b9f7a40 commit 18f9b53

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

main/env_validator.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@ def strip_comment(val):
1313
return val.split("#", 1)[0].strip()
1414

1515

16+
"""
17+
This attempts to enforce the following rules around env files:
18+
- Base env files (i.e. backend.env) will contain default settings
19+
that are safe for all development environments
20+
- Local env files (i.e. backend.local.env) may contain overrides and
21+
settings which cannot have a sensible default (i.e. developer-specific API keys).
22+
It should contain everything in the example file as well
23+
as anything intentionally overridden
24+
- Example env files list only settings which cannot have a sensible default.
25+
It should contain the minimum required set of settings values
26+
necessary to get the application running.
27+
28+
29+
Behavior we want to validate:
30+
1) If a setting is in the example file or local file, but
31+
not the base file it might be missing a default and we emit a warning.
32+
1a) If a setting is in the example file or local file but not the
33+
base because there's no sensible default (i.e. values are specific to a user or local)
34+
we allow a "# local-required" comment on example file to suppress the warning
35+
2) If a setting is in the local file and the base file, but the values
36+
differ we emit a warning. This is to keep users from accidentally overriding defaults
37+
3) If a setting is in the example file but not the local file, we emit a
38+
warning. This indicates that we are likely missing a required local-specific setting
39+
"""
40+
41+
1642
class EnvValidator:
1743
"""Validates environment variable configurations and reports discrepancies."""
1844

main/settings.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,6 @@
5555
profiles_sample_rate=SENTRY_PROFILES_SAMPLE_RATE,
5656
)
5757

58-
# Validate environment configuration on startup
59-
# Skip validation during testing or when explicitly disabled
60-
if not get_bool("SKIP_ENV_VALIDATION", default=False):
61-
try:
62-
from main.env_validator import validate_environment_on_startup
63-
64-
validate_environment_on_startup()
65-
except Exception as e: # noqa: BLE001
66-
# We don't want to block if validation fails.
67-
log.warning("Environment validation failed: %s", e)
68-
6958
BASE_DIR = os.path.dirname( # noqa: PTH120
7059
os.path.dirname(os.path.abspath(__file__)) # noqa: PTH100, PTH120
7160
)
@@ -877,3 +866,14 @@ def get_all_config_keys():
877866
OPENTELEMETRY_TRACES_BATCH_SIZE = get_int("OPENTELEMETRY_TRACES_BATCH_SIZE", 512)
878867
OPENTELEMETRY_EXPORT_TIMEOUT_MS = get_int("OPENTELEMETRY_EXPORT_TIMEOUT_MS", 5000)
879868
CANVAS_TUTORBOT_FOLDER = get_string("CANVAS_TUTORBOT_FOLDER", "web_resources/ai/tutor/")
869+
870+
# Validate environment configuration on startup
871+
# Skip validation during testing or when explicitly disabled
872+
if not get_bool("SKIP_ENV_VALIDATION", default=False):
873+
try:
874+
from main.env_validator import validate_environment_on_startup
875+
876+
validate_environment_on_startup()
877+
except Exception as e: # noqa: BLE001
878+
# We don't want to block if validation fails.
879+
log.warning("Environment validation failed: %s", e)

0 commit comments

Comments
 (0)