import logging from typing import Any log = logging.getLogger('root') def validate_critical_config(settings: Any): """ Validates that essential settings are populated and not using placeholders. Logs warnings or errors for missing critical infrastructure. """ log.info("Checking critical system configuration...") # 1. Database Check db = getattr(settings, 'DB', {}) if not db.get('server') or db.get('server') == 'mariadb': # 'mariadb' is the default in .env, usually fine, but worth noting log.info(f"Database server: {db.get('server')}") # 2. SMTP Check smtp = getattr(settings, 'SMTP', {}) if not smtp.get('server'): log.warning("CRITICAL: SMTP server not configured. Email features will fail.") if smtp.get('password') == 'set-in-ae-sql-db-cnf-tbl': log.error("CRITICAL: SMTP password is still set to placeholder. Email authentication will fail.") # 3. Security Check jwt_key = getattr(settings, 'JWT_KEY', '') if not jwt_key or jwt_key == 'fake-super-secret-token': log.error("SECURITY: JWT_KEY is missing or using a known fake token!") log.info("Configuration validation complete.") def bootstrap_db_config(settings: Any) -> bool: """ Loads dynamic settings from the 'cfg' table and updates the settings object. Uses deferred import of sql_select to avoid circular dependencies. """ # CRITICAL: Deferred import to prevent boot-time circular dependencies from app.db_sql import sql_select cfg_id = settings.AETHER_CFG.get('id', '0') log.info(f"Bootstrapping system configuration from DB (cfg_id={cfg_id})...") try: # Fetch the config record aether_cfg_sql = sql_select( table_name='cfg', record_id=int(cfg_id), as_list=False, max_count=1, ) # In some cases sql_select might return a single-item list even with as_list=False if isinstance(aether_cfg_sql, list): if len(aether_cfg_sql) > 0: aether_cfg_sql = aether_cfg_sql[0] else: aether_cfg_sql = None if not aether_cfg_sql or not isinstance(aether_cfg_sql, dict): log.error(f"FAILED to load system config from DB for ID {cfg_id}. Table 'cfg' might be empty or ID missing.") return False # --- Update Database settings --- # Safety: Only update if the values are provided in the DB record if aether_cfg_sql.get('db_server'): settings.DB_SERVER = aether_cfg_sql.get('db_server') if aether_cfg_sql.get('db_port'): settings.DB_PORT = str(aether_cfg_sql.get('db_port')) if aether_cfg_sql.get('db_name'): settings.DB_NAME = aether_cfg_sql.get('db_name') if aether_cfg_sql.get('db_username'): settings.DB_USER = aether_cfg_sql.get('db_username') if aether_cfg_sql.get('db_password'): settings.DB_PASS = aether_cfg_sql.get('db_password') # --- Update SMTP Settings --- if aether_cfg_sql.get('smtp_server'): settings.SMTP['server'] = aether_cfg_sql.get('smtp_server') if aether_cfg_sql.get('smtp_port'): settings.SMTP['port'] = str(aether_cfg_sql.get('smtp_port')) if aether_cfg_sql.get('smtp_username'): settings.SMTP['username'] = aether_cfg_sql.get('smtp_username') if aether_cfg_sql.get('smtp_password'): settings.SMTP['password'] = aether_cfg_sql.get('smtp_password') # --- Update File Paths --- if aether_cfg_sql.get('path_hosted_files_root'): settings.FILES_PATH['hosted_files_root'] = aether_cfg_sql.get('path_hosted_files_root') if aether_cfg_sql.get('path_hosted_tmp_root'): settings.FILES_PATH['hosted_tmp_root'] = aether_cfg_sql.get('path_hosted_tmp_root') log.info("System configuration successfully synchronized with DB.") return True except Exception as e: log.exception(f"Unexpected error during system bootstrap: {e}") return False