- Removed miner protection module, config, API routes, and UI - Removed block_postgres firewall feature from dashboard + launcher - Fixed SSH host key policy (WarningPolicy → AutoAddPolicy) - Fixed kick race condition (threading.Lock for iptables) - Tuned Argon2 password parameters (time_cost=3, memory_cost=65536) - Added login rate-limit (10/min) + IP cooldown after 5 failed attempts - Created scripts/db_check.py for launcher pre-flight DB check - Updated README and .gitignore for sanitized repo state
310 lines
10 KiB
Python
310 lines
10 KiB
Python
"""Settings loader - reads settings.yaml with env var overrides"""
|
|
|
|
import os
|
|
import socket
|
|
import yaml
|
|
import logging
|
|
import copy
|
|
import secrets
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DEFAULTS = {
|
|
'server': {
|
|
'host': '127.0.0.1',
|
|
'user': 'dune',
|
|
'ssh_key': None,
|
|
'local_ip': None,
|
|
},
|
|
'dashboard': {
|
|
'host': '127.0.0.1',
|
|
'port': 5050,
|
|
'debug': False,
|
|
'secret_key': None, # Generated at runtime if not set
|
|
'ssl_cert': None,
|
|
'ssl_key': None,
|
|
'ssl_domain': None,
|
|
'ssl_email': None,
|
|
'http_redirect': False,
|
|
'http_redirect_port': 80,
|
|
},
|
|
'database': {
|
|
'host': '127.0.0.1',
|
|
'port': 15433,
|
|
'user': 'postgres',
|
|
'password': None, # Must be set via setup or env var
|
|
'name': 'dune',
|
|
'schema': 'dune',
|
|
'min_connections': 2,
|
|
'max_connections': 10,
|
|
'owner': 'dune',
|
|
},
|
|
'kubernetes': {
|
|
'namespace': '',
|
|
'battlegroup_script': '/home/dune/.dune/bin/battlegroup',
|
|
},
|
|
'director': {
|
|
'port': 32479,
|
|
},
|
|
'filebrowser': {
|
|
'port': 18888,
|
|
},
|
|
'firewall': {
|
|
'block_filebrowser': True,
|
|
'block_director': True,
|
|
},
|
|
'cache': {
|
|
'chat_pod_ttl': 60,
|
|
'chat_messages_ttl': 10,
|
|
'static_data_ttl': 300,
|
|
},
|
|
'auth': {
|
|
'enabled': True,
|
|
'username': None, # Must be set via setup
|
|
'password_hash': None, # Argon2 hash, never plaintext
|
|
'shell_enabled': True,
|
|
},
|
|
'logging': {
|
|
'level': 'INFO',
|
|
'file': 'logs/dashboard.log',
|
|
'debug_file': 'logs/debug.log',
|
|
'debug_enabled': False,
|
|
'max_bytes': 10485760,
|
|
'backup_count': 5,
|
|
},
|
|
'ssl': {
|
|
'check_interval_hours': 24,
|
|
'renewal_days_before_expiry': 30,
|
|
},
|
|
'maps': {
|
|
'default_map': 'HaggaBasin',
|
|
'DeepDesert': {
|
|
'image': 'maps/Deep_Desert.webp',
|
|
'label': 'The Deep Desert',
|
|
'bounds': {
|
|
'min_x': -1268624.82,
|
|
'max_x': 1163312.83,
|
|
'min_y': -1266548.17,
|
|
'max_y': 1162416.13
|
|
},
|
|
'flip_y': False,
|
|
'image_size': {'width': 8000, 'height': 8000},
|
|
'default_zoom': 0.15
|
|
},
|
|
'HaggaBasin': {
|
|
'image': 'maps/HaggaBasin_8k.webp',
|
|
'label': 'Hagga Basin',
|
|
'bounds': {
|
|
'min_x': -456752.21,
|
|
'max_x': 354547.46,
|
|
'min_y': -450630.14,
|
|
'max_y': 353821.95
|
|
},
|
|
'flip_y': False,
|
|
'image_size': {'width': 8000, 'height': 8000},
|
|
'default_zoom': 0.15
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
def deep_merge(base, override):
|
|
"""Deep merge override dict into base dict."""
|
|
result = base.copy()
|
|
for key, value in override.items():
|
|
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
|
|
result[key] = deep_merge(result[key], value)
|
|
else:
|
|
result[key] = value
|
|
return result
|
|
|
|
|
|
def _detect_local_ip():
|
|
"""Detect local IP address for VM connectivity."""
|
|
try:
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
s.settimeout(1)
|
|
s.connect(("8.8.8.8", 80))
|
|
local_ip = s.getsockname()[0]
|
|
s.close()
|
|
if local_ip:
|
|
logger.debug(f"Detected local IP: {local_ip}")
|
|
return local_ip
|
|
except Exception as e:
|
|
logger.debug(f"Could not detect local IP: {e}")
|
|
return '127.0.0.1'
|
|
|
|
|
|
def _validate_server_host(settings):
|
|
"""Validate and fix server.host if invalid or placeholder."""
|
|
server = settings.setdefault('server', {})
|
|
host = server.get('host', '')
|
|
|
|
local_ip = server.get('local_ip')
|
|
if local_ip:
|
|
local_ip_str = str(local_ip).strip()
|
|
if local_ip_str and local_ip_str not in ('', 'null', 'None', 'YOUR_SERVER_IP', 'YOUR_VM_IP'):
|
|
if local_ip_str.count('.') == 3 and all(part.isdigit() and 0 <= int(part) <= 255 for part in local_ip_str.split('.')):
|
|
server['host'] = local_ip_str
|
|
logger.info(f"Using server.local_ip override: {server['host']}")
|
|
return settings
|
|
|
|
host_str = str(host).strip() if host else ''
|
|
invalid_hosts = ('', 'YOUR_SERVER_IP', 'YOUR_VM_IP', 'null', 'None')
|
|
|
|
if host_str in invalid_hosts or not host_str:
|
|
detected_ip = _detect_local_ip()
|
|
server['host'] = detected_ip
|
|
logger.warning(f"Invalid server.host detected, using auto-detected IP: {detected_ip}")
|
|
logger.warning("To fix: Edit settings.yaml and set server.host to your VM's IP address")
|
|
|
|
return settings
|
|
|
|
|
|
def _find_missing_keys(defaults, current, path=""):
|
|
"""Find keys present in defaults but missing in current."""
|
|
missing = []
|
|
for key, val in defaults.items():
|
|
new_path = f"{path}.{key}" if path else key
|
|
if key not in current:
|
|
missing.append(new_path)
|
|
elif isinstance(val, dict) and isinstance(current.get(key), dict):
|
|
missing.extend(_find_missing_keys(val, current[key], new_path))
|
|
return missing
|
|
|
|
|
|
def _apply_defaults_to_file(settings_path, defaults):
|
|
"""Add missing default keys to settings.yaml and notify user."""
|
|
if not os.path.exists(settings_path):
|
|
return False
|
|
|
|
with open(settings_path, 'r') as f:
|
|
try:
|
|
current = yaml.safe_load(f) or {}
|
|
except Exception:
|
|
return False
|
|
|
|
missing = _find_missing_keys(defaults, current)
|
|
if not missing:
|
|
return False
|
|
|
|
# Merge and save
|
|
merged = deep_merge(defaults, current)
|
|
try:
|
|
with open(settings_path, 'w') as f:
|
|
yaml.dump(merged, f, default_flow_style=False, sort_keys=False)
|
|
print(f"\n [INFO] Settings updated with new defaults: {', '.join(missing)}")
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Failed to update settings file: {e}")
|
|
return False
|
|
|
|
|
|
def load_settings(settings_path=None):
|
|
"""Load settings from YAML file with defaults and env var overrides."""
|
|
if settings_path is None:
|
|
settings_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'settings.yaml')
|
|
|
|
settings = copy.deepcopy(DEFAULTS)
|
|
|
|
if os.path.exists(settings_path):
|
|
try:
|
|
with open(settings_path, 'r') as f:
|
|
file_settings = yaml.safe_load(f) or {}
|
|
settings = deep_merge(settings, file_settings)
|
|
logger.info(f"Settings loaded from {settings_path}")
|
|
except Exception as e:
|
|
logger.error(f"Failed to load settings from {settings_path}: {e}")
|
|
logger.warning("Using default settings")
|
|
else:
|
|
logger.warning(f"Settings file not found at {settings_path}, using defaults")
|
|
|
|
# Note: Runtime config migration moved to setup/update scripts
|
|
# _apply_defaults_to_file() removed to prevent runtime config mutation
|
|
|
|
env_overrides = {}
|
|
|
|
if os.environ.get('DUNE_SERVER_HOST'):
|
|
env_overrides.setdefault('server', {})['host'] = os.environ['DUNE_SERVER_HOST']
|
|
if os.environ.get('DUNE_SERVER_USER'):
|
|
env_overrides.setdefault('server', {})['user'] = os.environ['DUNE_SERVER_USER']
|
|
if os.environ.get('DUNE_DB_PASSWORD'):
|
|
env_overrides.setdefault('database', {})['password'] = os.environ['DUNE_DB_PASSWORD']
|
|
if os.environ.get('DUNE_K8S_NAMESPACE'):
|
|
env_overrides.setdefault('kubernetes', {})['namespace'] = os.environ['DUNE_K8S_NAMESPACE']
|
|
if os.environ.get('DUNE_AUTH_PASSWORD'):
|
|
env_overrides.setdefault('auth', {})['password'] = os.environ['DUNE_AUTH_PASSWORD']
|
|
if os.environ.get('DUNE_DASHBOARD_PORT'):
|
|
env_overrides.setdefault('dashboard', {})['port'] = int(os.environ['DUNE_DASHBOARD_PORT'])
|
|
|
|
if env_overrides:
|
|
settings = deep_merge(settings, env_overrides)
|
|
logger.info("Environment variable overrides applied")
|
|
|
|
# Generate secure secret_key if not set
|
|
if not settings['dashboard'].get('secret_key'):
|
|
settings['dashboard']['secret_key'] = secrets.token_hex(32)
|
|
logger.info("Generated secure SECRET_KEY")
|
|
|
|
# Backward compatibility: migrate old plaintext password to hash
|
|
# This is a one-time migration that hashes the password and removes plaintext
|
|
auth = settings.get('auth', {})
|
|
password_migrated = False
|
|
if auth.get('password') and not auth.get('password_hash'):
|
|
try:
|
|
from argon2 import PasswordHasher
|
|
ph = PasswordHasher(time_cost=3, memory_cost=65536)
|
|
settings['auth']['password_hash'] = ph.hash(str(auth['password']))
|
|
del settings['auth']['password']
|
|
password_migrated = True
|
|
logger.info("Migrated plaintext password to Argon2 hash")
|
|
except ImportError:
|
|
logger.error("argon2-cffi not installed. Password hashing unavailable.")
|
|
settings['auth']['password_hash'] = None
|
|
|
|
if password_migrated:
|
|
settings['dashboard']['secret_key'] = secrets.token_hex(32)
|
|
logger.warning("Rotated secret_key to invalidate sessions after password migration")
|
|
|
|
settings = _validate_server_host(settings)
|
|
|
|
return settings
|
|
|
|
|
|
# Global settings cache for runtime access
|
|
_settings_cache = None
|
|
|
|
|
|
def get_settings():
|
|
"""Get current settings (cached for performance)."""
|
|
global _settings_cache
|
|
if _settings_cache is None:
|
|
_settings_cache = load_settings()
|
|
return _settings_cache
|
|
|
|
|
|
def save_settings(settings):
|
|
"""Save settings to YAML file."""
|
|
global _settings_cache
|
|
|
|
settings_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'settings.yaml')
|
|
|
|
try:
|
|
with open(settings_path, 'w') as f:
|
|
yaml.safe_dump(settings, f, default_flow_style=False, sort_keys=False)
|
|
|
|
# Update cache
|
|
_settings_cache = settings
|
|
logger.info(f"Settings saved to {settings_path}")
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Failed to save settings to {settings_path}: {e}")
|
|
return False
|
|
|
|
|
|
def reload_settings():
|
|
"""Force reload settings from file."""
|
|
global _settings_cache
|
|
_settings_cache = load_settings()
|
|
return _settings_cache
|