canaille-globuzma/canaille/app/configuration.py

140 lines
3.9 KiB
Python
Raw Normal View History

2021-10-12 16:24:51 +00:00
import os
2021-10-13 08:12:44 +00:00
import smtplib
import socket
from collections.abc import Mapping
from canaille.app.mails import DEFAULT_SMTP_HOST
from canaille.app.mails import DEFAULT_SMTP_PORT
2023-07-01 16:46:11 +00:00
from flask import current_app
2021-12-20 22:57:27 +00:00
ROOT = os.path.dirname(os.path.abspath(__file__))
2021-10-12 16:24:51 +00:00
2021-10-13 08:12:44 +00:00
class ConfigurationException(Exception):
pass
def parse_file_keys(config):
"""
Replaces configuration entries with the '_FILE' suffix with
the matching file content.
"""
SUFFIX = "_FILE"
new_config = {}
for key, value in config.items():
if isinstance(value, Mapping):
new_config[key] = parse_file_keys(value)
elif isinstance(key, str) and key.endswith(SUFFIX) and isinstance(value, str):
with open(value) as f:
value = f.read().rstrip("\n")
root_key = key[: -len(SUFFIX)]
new_config[root_key] = value
else:
new_config[key] = value
return new_config
def setup_config(app, config=None, validate_config=True):
from canaille.oidc.installation import install
try:
import toml
except ImportError: # pragma: no cover
toml = None
app.config.from_mapping(
{
"SESSION_COOKIE_NAME": "canaille",
"OAUTH2_REFRESH_TOKEN_GENERATOR": True,
"OAUTH2_ACCESS_TOKEN_GENERATOR": "canaille.oidc.oauth.generate_access_token",
}
)
if config:
app.config.from_mapping(parse_file_keys(config))
elif toml and "CONFIG" in os.environ:
app.config.from_mapping(parse_file_keys(toml.load(os.environ.get("CONFIG"))))
else:
raise Exception(
"No configuration file found. "
"Either create conf/config.toml or set the 'CONFIG' variable environment."
)
if app.debug: # pragma: no cover
2023-07-01 16:46:11 +00:00
install(app.config, debug=True)
if validate_config:
validate(app.config)
2021-10-12 16:24:51 +00:00
def validate(config, validate_remote=False):
validate_keypair(config)
validate_theme(config)
if not validate_remote:
return
2023-06-05 16:10:37 +00:00
from canaille.backends import BaseBackend
2023-06-05 16:10:37 +00:00
BaseBackend.get().validate(config)
validate_smtp_configuration(config)
def validate_keypair(config):
if (
"OIDC" in config
and "JWT" in config["OIDC"]
2023-07-01 16:46:11 +00:00
and not config["OIDC"]["JWT"].get("PUBLIC_KEY")
and not current_app.debug
):
2023-07-01 16:46:11 +00:00
raise ConfigurationException("No public key has been set")
2021-10-12 16:24:51 +00:00
if (
"OIDC" in config
and "JWT" in config["OIDC"]
2023-07-01 16:46:11 +00:00
and not config["OIDC"]["JWT"].get("PRIVATE_KEY")
and not current_app.debug
):
2023-07-01 16:46:11 +00:00
raise ConfigurationException("No private key has been set")
2021-10-12 16:24:51 +00:00
2021-10-13 08:12:44 +00:00
def validate_smtp_configuration(config):
host = config["SMTP"].get("HOST", DEFAULT_SMTP_HOST)
port = config["SMTP"].get("PORT", DEFAULT_SMTP_PORT)
2021-10-13 08:12:44 +00:00
try:
with smtplib.SMTP(host=host, port=port) as smtp:
2021-10-13 08:12:44 +00:00
if config["SMTP"].get("TLS"):
smtp.starttls()
if config["SMTP"].get("LOGIN"):
smtp.login(
user=config["SMTP"]["LOGIN"],
password=config["SMTP"].get("PASSWORD"),
)
except (socket.gaierror, ConnectionRefusedError) as exc:
raise ConfigurationException(
f"Could not connect to the SMTP server '{host}' on port '{port}'"
2021-10-13 08:12:44 +00:00
) from exc
except smtplib.SMTPAuthenticationError as exc:
raise ConfigurationException(
f'SMTP authentication failed with user \'{config["SMTP"]["LOGIN"]}\''
) from exc
except smtplib.SMTPNotSupportedError as exc:
raise ConfigurationException(exc) from exc
2021-10-12 16:24:51 +00:00
def validate_theme(config):
if not config.get("THEME"):
return
if not os.path.exists(config["THEME"]) and not os.path.exists(
os.path.join(ROOT, "themes", config["THEME"])
):
raise ConfigurationException(f'Cannot find theme \'{config["THEME"]}\'')