Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce FLEXMEASURES_FORCE_HTTPS #1008

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/spellright.dict
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,9 @@ cron
CSV
UI
frontend
http
https
balancer
url
HTTPS
Werkzeug
2 changes: 2 additions & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ FlexMeasures Changelog
v0.20.0 | April XX, 2024
============================

.. warning:: From this version on, the config setting `FLEXMEASURES_FORCE_HTTPS` decides whether to enforce HTTPS on requests - and it defaults to `False`. Previously, this was governed by `Flask_ENV` or `FLEXMEASURES_ENV` being set to something else than "documentation" or "development". This new way is more clear, but you might be in need of using this setting before upgrading.

New features
-------------

Expand Down
8 changes: 8 additions & 0 deletions documentation/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ Allows users to make authenticated requests. If true, injects the Access-Control
Default: ``True``


FLEXMEASURES_FORCE_HTTPS
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Set to ``True`` if all requests should be forced to be HTTPS.

Default: ``False``


FLEXMEASURES_ENFORCE_SECURE_CONTENT_POLICY
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion flexmeasures/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def create( # noqa C901
set_secret_key(app)
if app.config.get("SECURITY_PASSWORD_SALT", None) is None:
app.config["SECURITY_PASSWORD_SALT"] = app.config["SECRET_KEY"]
if app.config.get("FLEXMEASURES_ENV") not in ("documentation", "development"):
if app.config.get("FLEXMEASURES_FORCE_HTTPS", False):
SSLify(app)

# Prepare profiling, if needed
Expand Down
30 changes: 22 additions & 8 deletions flexmeasures/ui/crud/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,28 @@ def _maybe_raise(
if response.status_code not in do_not_raise_for:
response.raise_for_status()

def _url_root(self) -> str:
"""
Get the root for the URLs this API should use to call FlexMeasures.
"""
url_root = request.url_root
if current_app.config.get("FLEXMEASURES_FORCE_HTTPS", False):
# this replacement is for the case we are behind a load balancer who talks http internally
url_root = url_root.replace("http://", "https://")
return url_root

def get(
self,
url: str,
query: dict[str, Any] | None = None,
do_not_raise_for: list | None = None,
) -> requests.Response:
full_url = f"{self._url_root()}{url}"
current_app.logger.debug(
f"{self._log_prefix} GETting {url} with query {query} ..."
f"{self._log_prefix} Calling GET to {full_url} with query {query} ..."
)
response = requests.get(
f"{request.url_root}{url}",
full_url,
params=query,
headers=self._auth_headers(),
)
Expand All @@ -64,11 +75,12 @@ def post(
args: dict | None = None,
do_not_raise_for: list | None = None,
) -> requests.Response:
full_url = f"{self._url_root()}{url}"
current_app.logger.debug(
f"{self._log_prefix} POSTing {url} with json data {args} ..."
f"{self._log_prefix} Call POST to {full_url} with json data {args} ..."
)
response = requests.post(
f"{request.url_root}{url}",
full_url,
headers=self._auth_headers(),
json=args if args else {},
)
Expand All @@ -81,11 +93,12 @@ def patch(
args: dict | None = None,
do_not_raise_for: list | None = None,
) -> requests.Response:
full_url = f"{self._url_root()}{url}"
current_app.logger.debug(
f"{self._log_prefix} PATCHing {url} with json data {args} ..."
f"{self._log_prefix} Calling PATCH to {full_url} with json data {args} ..."
)
response = requests.patch(
f"{request.url_root}{url}",
full_url,
headers=self._auth_headers(),
json=args if args else {},
)
Expand All @@ -97,9 +110,10 @@ def delete(
url: str,
do_not_raise_for: list | None = None,
) -> requests.Response:
current_app.logger.debug(f"{self._log_prefix} DELETEing {url} ...")
full_url = f"{self._url_root()}{url}"
current_app.logger.debug(f"{self._log_prefix} Calling DELETE to {full_url} ...")
response = requests.delete(
f"{request.url_root}{url}",
full_url,
headers=self._auth_headers(),
)
self._maybe_raise(response, do_not_raise_for)
Expand Down
2 changes: 2 additions & 0 deletions flexmeasures/utils/config_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class Config(object):
FLEXMEASURES_API_SUNSET_DATE: str | None = None # e.g. 2023-05-01
FLEXMEASURES_API_SUNSET_LINK: str | None = None # e.g. https://flexmeasures.readthedocs.io/en/latest/api/introduction.html#deprecation-and-sunset

# if True, all requests are forced to be via HTTPS.
FLEXMEASURES_FORCE_HTTPS: bool = False
# if True, the content could be accessed via HTTPS.
FLEXMEASURES_ENFORCE_SECURE_CONTENT_POLICY: bool = False

Expand Down
Loading