Fix reading (boolean) settings from the environment, incl. SECURITY_TWO_FACTOR#2341
Open
Flix6x wants to merge 3 commits into
Open
Fix reading (boolean) settings from the environment, incl. SECURITY_TWO_FACTOR#2341Flix6x wants to merge 3 commits into
Flix6x wants to merge 3 commits into
Conversation
The installation docs (and now the configuration docs, via PR #2340) advise enabling 2FA with 'export SECURITY_TWO_FACTOR=True', but read_env_vars never read that variable, so the app silently stayed on the default (False). Parse it explicitly as a boolean, since env values arrive as strings. Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
6 tasks
Documentation build overview
|
Environment values are strings, so any non-empty value - including 'False' or '0' - counted as enabled for DEBUG, and reached consumers raw for MAIL_USE_TLS, MAIL_USE_SSL and FLEXMEASURES_JSON_COMPACT. Coerce env values to booleans whenever the setting's default is a boolean, and drop the now-redundant string special-casing for FLEXMEASURES_JSON_COMPACT in app.py. Signed-off-by: F.N. Claessen <claessen@seita.nl>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves FlexMeasures’ environment-variable configuration handling by ensuring boolean settings are correctly coerced from strings and by adding missing support for SECURITY_TWO_FACTOR when configured via the environment.
Changes:
- Add boolean parsing for env vars whose config default is a boolean, and include
SECURITY_TWO_FACTOR/DEBUGin env processing. - Simplify
FLEXMEASURES_JSON_COMPACThandling in app initialization now that env parsing is centralized. - Add a changelog entry documenting the bugfix and behavior change.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| flexmeasures/utils/config_utils.py | Adds parse_bool_env and updates read_env_vars to coerce boolean env vars and read additional settings |
| flexmeasures/app.py | Simplifies JSON compactness configuration by relying on the config value |
| documentation/changelog.rst | Documents the environment-boolean parsing and SECURITY_TWO_FACTOR env-reading fix |
Comment on lines
+215
to
+216
| def parse_bool_env(value: str) -> bool: | ||
| return value.lower() in ("true", "1", "yes", "on") |
Comment on lines
237
to
241
| "LOGGING_LEVEL", | ||
| "MAPBOX_ACCESS_TOKEN", | ||
| "SENTRY_SDN", | ||
| "FLEXMEASURES_PLUGINS", | ||
| "FLEXMEASURES_JSON_COMPACT", |
| app.json.compact = True | ||
| else: | ||
| app.json.compact = False | ||
| app.json.compact = bool(app.config.get("FLEXMEASURES_JSON_COMPACT", False)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two related fixes to
read_env_vars:SECURITY_TWO_FACTORwas never read from the environment, although the installation docs (and, since Document SECURITY_TWO_FACTOR and related 2FA config settings #2340, the configuration docs) advise enabling 2FA withexport SECURITY_TWO_FACTOR=True. The app silently kept the default (False), which also meantSECURITY_TOTP_SECRETSfrom the environment was ignored (its handling inapp_utils.set_totp_secretsis only reached when 2FA is on).Falseor0— counted as enabled forDEBUG, and reached consumers as raw strings forMAIL_USE_TLS,MAIL_USE_SSLandFLEXMEASURES_JSON_COMPACT(the latter had ad-hoc string special-casing inapp.py, now removed). Env values are now coerced to booleans whenever the setting's default is a boolean.How to test
Related Items
Surfaced while reviewing #2340; relates to #1760.
Sign-off