From 0ded9d3823ba38dbca2f3ae9479626de8d372831 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 26 Jan 2023 16:11:32 +0100 Subject: [PATCH] Initialize settings --- web/__init__.py | 52 ++++++++++++++++++++++++++++++++----------------- web/db.py | 10 ---------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/web/__init__.py b/web/__init__.py index b8d9f61..3270862 100644 --- a/web/__init__.py +++ b/web/__init__.py @@ -1,4 +1,5 @@ import os +import secrets import flask import flask_ldap3_login @@ -6,16 +7,42 @@ import flask_login def create_app(test_config=None): app = flask.Flask(__name__) - app.config['DEBUG'] = True - app.config['SECRET_KEY'] = 'KagjQoUSTtjYC3GQPpfBHcpMJvZg5R1L' -# try: -# os.makedirs(app.instance_path) -# except OSError: -# pass + # Ensure all required keys exist. + settings = { + 'debug': False, + 'secret_key': secrets.token_urlsafe(32), + 'ldap_host': '', + 'ldap_port': '636', + 'ldap_user': '', + 'ldap_pass': '', + 'ldap_admin': '', + 'ldap_base_dn': '', + 'ldap_user_dn': '', + 'ldap_login_attr': 'userPrincipalName', + 'wg_endpoint': '', + 'wg_port': '51820', + 'wg_key': '', + 'wg_net': '', + 'version': 0, + } from . import db - db.init_app(app) + with db.locked('settings'): + settings |= db.read('settings') + db.write('settings', settings) + + app.config['DEBUG'] = settings.get('debug', False) + app.config['SECRET_KEY'] = settings.get('secret_key', '') + app.config['LDAP_USE_SSL'] = True + app.config['LDAP_HOST'] = settings.get('ldap_host', '') + app.config['LDAP_PORT'] = int(settings.get('ldap_port', '636')) + app.config['LDAP_BASE_DN'] = settings.get('ldap_base_dn', '') + app.config['LDAP_USER_DN'] = settings.get('ldap_user_dn', '') + app.config['LDAP_BIND_USER_DN'] = settings.get('ldap_user', '') + app.config['LDAP_BIND_USER_PASSWORD'] = settings.get('ldap_pass', '') + app.config['LDAP_USER_LOGIN_ATTR'] = settings.get('ldap_login_attr', 'userPrincipalName') + app.config['LDAP_USER_SEARCH_SCOPE'] = 'SUBTREE' from . import auth app.register_blueprint(auth.blueprint) @@ -32,17 +59,6 @@ def create_app(test_config=None): from . import system system.init_app(app) - settings = db.load('settings') - app.config['LDAP_USE_SSL'] = True - app.config['LDAP_HOST'] = settings.get('ldap_host', '') - app.config['LDAP_PORT'] = int(settings.get('ldap_port', '636')) - app.config['LDAP_BASE_DN'] = settings.get('ldap_base_dn', '') - app.config['LDAP_USER_DN'] = settings.get('ldap_user_dn', '') - app.config['LDAP_BIND_USER_DN'] = settings.get('ldap_user', '') - app.config['LDAP_BIND_USER_PASSWORD'] = settings.get('ldap_pass', '') - app.config['LDAP_USER_LOGIN_ATTR'] = settings.get('ldap_login_attr', 'userPrincipalName') - app.config['LDAP_USER_SEARCH_SCOPE'] = 'SUBTREE' - login_manager = flask_login.LoginManager(app) ldap_manager = flask_ldap3_login.LDAP3LoginManager(app) users = {} diff --git a/web/db.py b/web/db.py index 4d8d3ae..ce10753 100644 --- a/web/db.py +++ b/web/db.py @@ -46,13 +46,3 @@ def load(name): def save(name, data): with locked(name): write(name, data) - -def init_app(app): - #app.teardown_appcontext(close_db) - app.cli.add_command(init_db) - -@click.command('init-db') -@flask.cli.with_appcontext -def init_db(): - """Clear the existing data and create new tables.""" - pass # TODO, if needed