From a77dcd1878ad5fd237a0c513b936d0259b98f2a5 Mon Sep 17 00:00:00 2001 From: Simon Pirkelmann Date: Fri, 4 Feb 2022 22:13:15 +0100 Subject: [PATCH] simplified app configuration process --- imaginaerraum_door_admin/__init__.py | 57 +++++-------------- .../default_app_config.py | 10 ++-- 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/imaginaerraum_door_admin/__init__.py b/imaginaerraum_door_admin/__init__.py index 280cbbf..69ebfad 100644 --- a/imaginaerraum_door_admin/__init__.py +++ b/imaginaerraum_door_admin/__init__.py @@ -5,7 +5,6 @@ from flask_security import Security, SQLAlchemyUserDatastore, hash_password from email_validator import validate_email from pathlib import Path -#from .webapp import door_app from .door_handle import DoorHandle security = Security() @@ -63,6 +62,7 @@ def create_super_admins(app, db, user_datastore, logger): roles=roles) db.session.commit() + def setup_logging(app): # set up logging for the web app logger = logging.getLogger('webapp') @@ -86,6 +86,7 @@ def setup_logging(app): return logger + def create_app(): app = Flask(__name__) app.config.from_object('imaginaerraum_door_admin.default_app_config.DefaultConfig') @@ -93,48 +94,20 @@ def create_app(): logger = setup_logging(app) - # do some checks for file existence etc. - try: - with open(app.config['KEY_FILE']) as f: - data = f.readlines() - if 'SECRET_KEY' in data[0]: - secret_key = data[0].split()[-1] - else: - raise Exception("Could not read SECURITY_PASSWORD_SALT") - if 'SECURITY_PASSWORD_SALT' in data[1]: - security_password_salt = data[1].split()[-1] - else: - raise Exception("Could not read SECURITY_PASSWORD_SALT") - except Exception as e: + token_file = Path(app.config.get('TOKEN_FILE')) + if not token_file.exists(): logger.warning( - f"Flask keys could not be read from file at {Path(app.config['KEY_FILE']).absolute()}. Exception: {e}. Using default values instead.") - secret_key = 'Q7PJu2fg2jabYwP-Psop6c6f2G4' - security_password_salt = '10036796768252925167749545152988277953' + f"Token file not found at {token_file.absolute()}. " + "An empty token file will be created." + ) + token_file.touch() - if Path(app.config['TEMPLATE_FOLDER']).is_absolute(): - if not Path(app.config['TEMPLATE_FOLDER']).exists(): - logger.error( - f"Flask template folder not found at {Path(app.config['TEMPLATE_FOLDER']).absolute()}") - else: - if not (Path(__file__).parent / app.config['TEMPLATE_FOLDER']).exists(): - logger.error( - f"Flask template folder not found at {(Path(__file__).parent / app.config['TEMPLATE_FOLDER']).absolute()}") - if Path(app.config['STATIC_FOLDER']).is_absolute(): - if not Path(app.config['STATIC_FOLDER']).exists(): - logger.error( - f"Flask static folder not found at {Path(app.config['STATIC_FOLDER']).absolute()}") - else: - if not (Path(__file__).parent / app.config['STATIC_FOLDER']).exists(): - logger.error( - f"Flask static folder not found at {(Path(__file__).parent / app.config['STATIC_FOLDER']).absolute()}") - if not Path(app.config['TOKEN_FILE']).exists(): - logger.warning( - f"Token file not found at {Path(app.config['TOKEN_FILE']).absolute()}") - - # create door objects which provides access to the token file and current door state via MQTT - app.door = DoorHandle(token_file=app.config['TOKEN_FILE'], mqtt_host=app.config['MQTT_HOST'], - nfc_socket=app.config['NFC_SOCKET'], - logger=logger) + # create door objects which provides access to the token file and current + # door state via MQTT + app.door = DoorHandle( + token_file=token_file, mqtt_host=app.config['MQTT_HOST'], + nfc_socket=app.config['NFC_SOCKET'], logger=logger + ) # Mail Config #mail = Mail(app) @@ -153,4 +126,4 @@ def create_app(): create_super_admins(app, db, user_datastore, logger) - return app \ No newline at end of file + return app diff --git a/imaginaerraum_door_admin/default_app_config.py b/imaginaerraum_door_admin/default_app_config.py index 23c6c9f..e0c001d 100644 --- a/imaginaerraum_door_admin/default_app_config.py +++ b/imaginaerraum_door_admin/default_app_config.py @@ -11,9 +11,6 @@ class DefaultConfig(object): SECRET_KEY = 'supersecret' - TEMPLATE_FOLDER = 'templates' - STATIC_FOLDER = 'static' - SECURITY_REGISTERABLE = False SECURITY_CHANGEABLE = True SECURITY_RECOVERABLE = True @@ -47,7 +44,8 @@ class DefaultConfig(object): } KEY_FILE = '/root/flask_keys' - TOKEN_FILE = "/etc/door_tokens" + TOKEN_FILE = "door_tokens" + ADMIN_FILE = 'admins' LDAP_URL = "ldaps://ldap.imaginaerraum.de" LDAP_USER_GROUP = 'Users' @@ -55,6 +53,6 @@ class DefaultConfig(object): LDAP_DOMAIN_EXT = 'de' NFC_SOCKET = "/tmp/nfc.sock" - LOG_FILE = "/var/log/webinterface.log" - NFC_LOG = "/var/log/nfc.log" + LOG_FILE = "webinterface.log" + NFC_LOG = "nfc.log" MQTT_HOST = '10.10.21.2' \ No newline at end of file