[System] Reworked logging and configuration, breaks configs.

This commit is contained in:
Ferdinand Thiessen 2020-10-28 14:21:20 +01:00
parent a3106ccf1f
commit 216b757740
7 changed files with 96 additions and 88 deletions

View File

@ -3,18 +3,11 @@
Initialize app, CORS, database and add it to the application. Initialize app, CORS, database and add it to the application.
""" """
import yaml
import logging import logging
import pkg_resources import pkg_resources
from pathlib import Path from pathlib import Path
from logging.config import dictConfig
from werkzeug.local import LocalProxy from werkzeug.local import LocalProxy
__version__ = pkg_resources.get_distribution("flaschengeist").version __version__ = pkg_resources.get_distribution("flaschengeist").version
_module_path = Path(__file__).parent _module_path = Path(__file__).parent
logger = LocalProxy(lambda: logging.getLogger(__name__)) logger = LocalProxy(lambda: logging.getLogger(__name__))
with (_module_path / "logging.yml").open(mode="rb") as file:
config = yaml.safe_load(file.read())
logging.config.dictConfig(config)

View File

@ -38,14 +38,14 @@ def __load_plugins(app):
for entry_point in pkg_resources.iter_entry_points("flaschengeist.plugin"): for entry_point in pkg_resources.iter_entry_points("flaschengeist.plugin"):
logger.debug("Found plugin: >{}<".format(entry_point.name)) logger.debug("Found plugin: >{}<".format(entry_point.name))
plugin = None plugin = None
if config.get(entry_point.name, "enabled", fallback=False): if entry_point.name in config and config[entry_point.name].get("enabled", False):
plugin = entry_point.load()(config[entry_point.name] if config.has_section(entry_point.name) else {}) plugin = entry_point.load()(config[entry_point.name])
if plugin.blueprint: if plugin.blueprint:
app.register_blueprint(plugin.blueprint) app.register_blueprint(plugin.blueprint)
logger.info("Load plugin >{}<".format(entry_point.name)) logger.info("Load plugin >{}<".format(entry_point.name))
if isinstance(plugin, AuthPlugin): if isinstance(plugin, AuthPlugin):
logger.debug("Found authentication plugin: %s", entry_point.name) logger.debug("Found authentication plugin: %s", entry_point.name)
if entry_point.name == config["FLASCHENGEIST"]["AUTH"]: if entry_point.name == config["FLASCHENGEIST"]["auth"]:
app.config["FG_AUTH_BACKEND"] = plugin app.config["FG_AUTH_BACKEND"] = plugin
else: else:
del plugin del plugin

View File

@ -1,30 +1,27 @@
[FLASCHENGEIST] [FLASCHENGEIST]
# Select authentication provider (builtin: auth_plain, auth_ldap) # Select authentication provider (builtin: auth_plain, auth_ldap)
AUTH = auth_plain auth = "auth_plain"
# Enable if you run flaschengeist behind a proxy, e.g. nginx + gunicorn # Enable if you run flaschengeist behind a proxy, e.g. nginx + gunicorn
# PROXY = false #proxy = false
# Set root path, prefixes all routes # Set root path, prefixes all routes
# ROOT = / #root = /
# Set secret key
secret_key = "V3ryS3cr3t"
[LOGGING]
file = "/tmp/flaschengeist-debug.log"
# DEBUG INFO WARNING ERROR
#level = "WARNING"
[DATABASE] [DATABASE]
USER = user = "user"
HOST = host = "127.0.0.1"
PASSWORD = password = "password"
DATABASE = database = "database"
[auth_plain] [auth_plain]
enabled = true enabled = true
#[mail]
# enabled = true
# SERVER =
# PORT =
# USER =
# PASSWORD =
# MAIL =
# SSL or STARTLS
# CRYPT = SSL
#[auth_ldap] #[auth_ldap]
# enabled = true # enabled = true
# URL = # URL =
@ -38,8 +35,18 @@ enabled = true
############################ ############################
# Configuration of plugins # # Configuration of plugins #
############################ ############################
#[mail]
# enabled = true
# SERVER =
# PORT =
# USER =
# PASSWORD =
# MAIL =
# SSL or STARTLS
# CRYPT = SSL
[geruecht] [geruecht]
enabled = true enabled = false
[schubu] [schubu]
enabled = false enabled = false

View File

@ -0,0 +1,29 @@
version = 1
disable_existing_loggers = false
[formatters]
[formatters.simple]
format = "%(asctime)s - %(name)s - %(message)s"
[formatters.extended]
format = "%(asctime)s — %(filename)s - %(funcName)s - %(lineno)d - %(threadName)s - %(name)s — %(levelname)s — %(message)s"
[handlers]
[handlers.console]
class = "logging.StreamHandler"
level = "DEBUG"
formatter = "simple"
stream = "ext://sys.stdout"
[handlers.file]
class = "logging.handlers.WatchedFileHandler"
level = "WARNING"
formatter = "extended"
encoding = "utf8"
filename = "flaschengeist.log"
[loggers]
[loggers.werkzeug]
level = "WARNING"
[root]
level = "WARNING"
handlers = ["console"]

View File

@ -1,31 +0,0 @@
version: 1
disable_existing_loggers: True
formatters:
debug:
format: "%(asctime)s — %(filename)s - %(funcName)s - %(lineno)d - %(threadName)s - %(name)s — %(levelname)s — %(message)s"
simple:
format: "%(asctime)s - %(name)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: debug
stream: ext://sys.stdout
debug:
class: logging.handlers.WatchedFileHandler
level: DEBUG
formatter: debug
filename: flaschengeist-debug.log
encoding: utf8
loggers:
werkzeug:
level: WARNING
root:
level: DEBUG
handlers: [console, debug]

View File

@ -12,20 +12,18 @@ import flaschengeist.system.controller.userController as userController
class AuthLDAP(AuthPlugin): class AuthLDAP(AuthPlugin):
def __init__(self, config): def __init__(self, cfg):
super().__init__() super().__init__()
defaults = {"PORT": "389", "USE_SSL": "False"} config = {"PORT": 389, "USE_SSL": False}
for name in defaults: config.update(cfg)
if name not in config:
config[name] = defaults[name]
app.config.update( app.config.update(
LDAP_SERVER=config["URL"], LDAP_SERVER=config["URL"],
LDAP_PORT=config.getint("PORT"), LDAP_PORT=config["PORT"],
LDAP_BINDDN=config["BINDDN"], LDAP_BINDDN=config["BINDDN"],
LDAP_USE_TLS=False, LDAP_USE_TLS=False,
LDAP_USE_SSL=config.getboolean("USE_SSL"), LDAP_USE_SSL=config["USE_SSL"],
LDAP_TLS_VERSION=ssl.PROTOCOL_TLSv1_2, LDAP_TLS_VERSION=ssl.PROTOCOL_TLSv1_2,
LDAP_REQUIRE_CERT=ssl.CERT_NONE, LDAP_REQUIRE_CERT=ssl.CERT_NONE,
FORCE_ATTRIBUTE_VALUE_AS_LIST=True, FORCE_ATTRIBUTE_VALUE_AS_LIST=True,
@ -34,6 +32,7 @@ class AuthLDAP(AuthPlugin):
app.config["LDAP_SECRET"] = (config["SECRET"],) app.config["LDAP_SECRET"] = (config["SECRET"],)
self.ldap = LDAPConn(app) self.ldap = LDAPConn(app)
self.dn = config["BASEDN"] self.dn = config["BASEDN"]
if "ADMIN_DN" in config:
self.admin_dn = config["ADMIN_DN"] self.admin_dn = config["ADMIN_DN"]
self.admin_secret = config["ADMIN_SECRET"] self.admin_secret = config["ADMIN_SECRET"]

View File

@ -1,44 +1,55 @@
import logging.config
import os import os
import configparser import toml
from pathlib import Path from pathlib import Path
from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.middleware.proxy_fix import ProxyFix
from .. import _module_path, logger from .. import _module_path, logger
default = {"MAIL": {"CRYPT": "SSL/STARTLS"}} # Default config:
config = {}
config = configparser.ConfigParser()
config.read_dict(default)
paths = [_module_path, Path.home() / ".config"] paths = [_module_path, Path.home() / ".config"]
if "FLASCHENGEIST_CONF" in os.environ: if "FLASCHENGEIST_CONF" in os.environ:
paths.append(Path(os.environ.get("FLASCHENGEIST_CONF"))) paths.append(Path(os.environ.get("FLASCHENGEIST_CONF")))
for loc in paths: for loc in paths:
try: try:
with (loc / "flaschengeist.cfg").open() as source: with (loc / "flaschengeist.toml").open() as source:
logger.info("Reading config file from >{}<".format(loc)) logger.info("Reading config file from >{}<".format(loc))
config.read_file(source) config.update(toml.load(source))
except IOError: except IOError:
pass pass
# Always enable this builtin plugins! # Always enable this builtin plugins!
config.read_dict({"auth": {"enabled": True}, "roles": {"enabled": True}, "users": {"enabled": True}}) config.update({"auth": {"enabled": True}, "roles": {"enabled": True}, "users": {"enabled": True}})
def configure_app(app): def configure_app(app):
if not config.has_option("FLASCHENGEIST", "SECRET_KEY"): logger_config = toml.load(_module_path / "logging.toml")
if "LOGGING" in config:
if "level" in config["LOGGING"]:
logger_config["loggers"]["flaschengeist"] = {"level": config["LOGGING"]["level"]}
if "file" in config["LOGGING"]:
logger_config["root"]["handlers"].append("file")
logger_config["handlers"]["file"]["filename"] = config["LOGGING"]["file"]
path = Path(config["LOGGING"]["file"])
path.parent.mkdir(parents=True, exist_ok=True)
logging.config.dictConfig(logger_config)
if "secret_key" not in config["FLASCHENGEIST"]:
logger.warn("No secret key was configured, please configure one for production systems!") logger.warn("No secret key was configured, please configure one for production systems!")
app.config["SECRET_KEY"] = config.get("FLASCHENGEIST", "SECRET_KEY", fallback="0a657b97ef546da90b2db91862ad4e29") app.config["SECRET_KEY"] = "0a657b97ef546da90b2db91862ad4e29"
else:
app.config["SECRET_KEY"] = config["FLASCHENGEIST"]["secret_key"]
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://{user}:{passwd}@{host}/{database}".format( app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://{user}:{passwd}@{host}/{database}".format(
user=config["DATABASE"]["USER"], user=config["DATABASE"]["user"],
passwd=config["DATABASE"]["PASSWORD"], passwd=config["DATABASE"]["password"],
host=config["DATABASE"]["HOST"], host=config["DATABASE"]["host"],
database=config["DATABASE"]["DATABASE"], database=config["DATABASE"]["database"]
) )
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
if config.has_option("FLASCHENGEIST", "ROOT"): if "root" in config["FLASCHENGEIST"]:
logger.debug("Setting application root to >{}<".format(config["FLASCHENGEIST"]["ROOT"])) logger.debug("Setting application root to >{}<".format(config["FLASCHENGEIST"]["root"]))
app.config["APPLICATION_ROOT"] = config["FLASCHENGEIST"]["ROOT"] app.config["APPLICATION_ROOT"] = config["FLASCHENGEIST"]["root"]
if config.getboolean("FLASCHENGEIST", "PROXY", fallback=False): if config["FLASCHENGEIST"].get("proxy", False):
logger.debug("Fixing wsgi_app for using behind a proxy server") logger.debug("Fixing wsgi_app for using behind a proxy server")
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)