main.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import logging
  5. import time
  6. import tempfile
  7. import threading
  8. import subprocess
  9. from flask import Flask, jsonify, request
  10. from werkzeug.security import check_password_hash
  11. from werkzeug.serving import WSGIRequestHandler
  12. from functools import wraps
  13. from config import Config
  14. from typing import List, Tuple
  15. import base64
  16. import signal
  17. # Configure logging with custom handler
  18. class NtfyLogHandler(logging.Handler):
  19. """Custom logging handler that sends logs to ntfy health backends"""
  20. def __init__(self, config_obj):
  21. super().__init__()
  22. self.config = config_obj
  23. def emit(self, record):
  24. """Send log record to health backends"""
  25. if hasattr(self.config, 'ntfy_backends_health') and self.config.send_all_logs:
  26. try:
  27. log_message = self.format(record)
  28. # Get configured log level or default to WARNING
  29. min_level = getattr(logging, self.config.log_level.upper(), logging.WARNING)
  30. if record.levelno >= min_level:
  31. # Format message with appropriate emoji based on log level
  32. emoji = "🚨" if record.levelno >= logging.ERROR else "⚠️" if record.levelno >= logging.WARNING else "ℹ️"
  33. title = f"Emergency Access {record.levelname}"
  34. message = f"{emoji} {record.name}: {record.getMessage()}"
  35. send_ntfy_notification(
  36. self.config.ntfy_backends_health,
  37. message,
  38. title
  39. )
  40. except Exception:
  41. # Don't fail the application if logging notification fails
  42. pass
  43. # Configure logging
  44. logging.basicConfig(
  45. level=logging.INFO,
  46. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  47. handlers=[
  48. logging.FileHandler('/var/log/emergency-access.log'),
  49. logging.StreamHandler()
  50. ]
  51. )
  52. logger = logging.getLogger(__name__)
  53. app = Flask(__name__)
  54. # Global config instance - initialized during main execution
  55. config = None
  56. # Systemd watchdog support
  57. def setup_systemd_watchdog():
  58. """Setup systemd watchdog notifications"""
  59. import os
  60. # Check if we have systemd watchdog enabled
  61. watchdog_usec = os.environ.get('WATCHDOG_USEC')
  62. if not watchdog_usec:
  63. logger.info("Systemd watchdog not enabled")
  64. return False
  65. try:
  66. # Convert microseconds to seconds and send notifications at half the interval
  67. interval = int(watchdog_usec) / 2000000 # Half interval in seconds
  68. logger.info(f"Systemd watchdog enabled, sending notifications every {interval:.1f}s")
  69. def watchdog_ping():
  70. while True:
  71. try:
  72. subprocess.run(['systemd-notify', 'WATCHDOG=1'],
  73. check=False, capture_output=True, timeout=5)
  74. time.sleep(interval)
  75. except Exception as e:
  76. logger.warning(f"Failed to send watchdog notification: {e}")
  77. time.sleep(interval)
  78. # Start watchdog thread
  79. watchdog_thread = threading.Thread(target=watchdog_ping, daemon=True)
  80. watchdog_thread.start()
  81. return True
  82. except Exception as e:
  83. logger.warning(f"Failed to setup systemd watchdog: {e}")
  84. return False
  85. def require_auth(key_config=None, is_health=False):
  86. """Decorator for HTTP Basic Authentication"""
  87. def decorator(f):
  88. @wraps(f)
  89. def decorated_function(*args, **kwargs):
  90. if config is None:
  91. logger.error("Configuration not loaded")
  92. return jsonify({'error': 'Server configuration error'}), 500
  93. auth = request.authorization
  94. if not auth:
  95. return jsonify({'error': 'Authentication required'}), 401, {
  96. 'WWW-Authenticate': 'Basic realm="Emergency Access"'
  97. }
  98. # Determine expected credentials
  99. if is_health:
  100. expected_username = config.health_username
  101. expected_password_hash = config.health_password_hash
  102. auth_type = "health check"
  103. else:
  104. if key_config is None:
  105. logger.error("Key configuration not provided for authentication")
  106. return jsonify({'error': 'Server configuration error'}), 500
  107. expected_username = key_config.username
  108. expected_password_hash = key_config.password_hash
  109. auth_type = f"key '{key_config.key_id}'"
  110. # Verify credentials
  111. if (auth.username != expected_username or
  112. not Config.verify_password(auth.password, expected_password_hash)):
  113. logger.warning(f"Authentication failed for {auth_type}: invalid credentials from {request.remote_addr}")
  114. return jsonify({'error': 'Invalid credentials'}), 401, {
  115. 'WWW-Authenticate': 'Basic realm="Emergency Access"'
  116. }
  117. logger.info(f"Authentication successful for {auth_type}")
  118. return f(*args, **kwargs)
  119. return decorated_function
  120. return decorator
  121. def send_ntfy_notification(backends: List[str], message: str, title: str = None) -> Tuple[bool, List[str]]:
  122. """
  123. Send notification using dschep/ntfy with dedicated config file
  124. Returns: (success, successful_backends)
  125. """
  126. successful_backends = []
  127. max_retries = 3
  128. retry_delay = 1
  129. for backend in backends:
  130. success = False
  131. last_error = None
  132. for attempt in range(max_retries):
  133. try:
  134. # Import ntfy here to avoid import issues during startup
  135. try:
  136. import ntfy
  137. from ntfy.config import load_config
  138. except ImportError:
  139. raise Exception("ntfy package not available. Please install with: pip install ntfy")
  140. if config is None:
  141. raise Exception("Configuration not loaded")
  142. # Load the ntfy config file
  143. ntfy_config = load_config(config.ntfy_config_path)
  144. ntfy_config["backends"] = [backend]
  145. # Add timeout to prevent hanging using threading
  146. result = [None]
  147. exception = [None]
  148. def notify_with_timeout():
  149. try:
  150. # Send notification using the backend name from our config file
  151. if title:
  152. result[0] = ntfy.notify(message, title=title, config=ntfy_config)
  153. else:
  154. result[0] = ntfy.notify(message, config=ntfy_config, title="Note")
  155. except Exception as e:
  156. exception[0] = e
  157. thread = threading.Thread(target=notify_with_timeout)
  158. thread.daemon = True
  159. thread.start()
  160. thread.join(timeout=15) # 15 second timeout
  161. if thread.is_alive():
  162. raise Exception("Notification timeout")
  163. if exception[0]:
  164. raise exception[0]
  165. ret = result[0]
  166. if ret == 0:
  167. successful_backends.append(backend)
  168. logger.info(f"Notification sent successfully via {backend}")
  169. success = True
  170. break
  171. else:
  172. raise Exception(f"ntfy returned error code {ret}")
  173. except ImportError as e:
  174. logger.error(f"ntfy package not available for backend {backend}: {e}")
  175. last_error = e
  176. break # Don't retry import errors
  177. except Exception as e:
  178. last_error = e
  179. if attempt < max_retries - 1:
  180. logger.warning(f"Notification attempt {attempt + 1} failed for {backend}: {str(e)}, retrying...")
  181. time.sleep(retry_delay * (attempt + 1)) # Exponential backoff
  182. else:
  183. logger.error(f"All notification attempts failed for {backend}: {str(e)}")
  184. if not success and last_error:
  185. # Don't raise exception - just log failure and continue with other backends
  186. logger.error(f"Failed to send notification to {backend} after {max_retries} attempts: {str(last_error)}")
  187. success = len(successful_backends) > 0
  188. return success, successful_backends
  189. def read_file_safely(file_path: str) -> Tuple[bool, str]:
  190. """
  191. Safely read file content
  192. Returns: (success, content)
  193. """
  194. try:
  195. if not os.path.exists(file_path):
  196. logger.error(f"File not found: {file_path}")
  197. return False, f"File not found: {file_path}"
  198. with open(file_path, 'r') as f:
  199. content = f.read().strip()
  200. if not content:
  201. logger.error(f"File is empty: {file_path}")
  202. return False, f"File is empty: {file_path}"
  203. return True, content
  204. except PermissionError:
  205. logger.error(f"Permission denied reading file: {file_path}")
  206. return False, f"Permission denied: {file_path}"
  207. except Exception as e:
  208. logger.error(f"Failed to read file {file_path}: {str(e)}")
  209. return False, f"Failed to read file: {str(e)}"
  210. def create_key_handler(key_config):
  211. """Create a key access handler for a specific key configuration"""
  212. @require_auth(key_config=key_config)
  213. def get_key_part():
  214. """Emergency key access endpoint"""
  215. if key_config is None:
  216. logger.error("Key configuration is None in get_key_part")
  217. return jsonify({'error': 'Server configuration error'}), 500
  218. logger.warning(f"EMERGENCY: Key access attempt detected for key '{key_config.key_id}'")
  219. try:
  220. # Send notification first - fail-safe approach
  221. notification_success, successful_backends = send_ntfy_notification(
  222. key_config.backends,
  223. key_config.message,
  224. "EMERGENCY ACCESS ALERT"
  225. )
  226. if not notification_success:
  227. logger.error(f"CRITICAL: Failed to send notifications to any backend for key '{key_config.key_id}'")
  228. return jsonify({
  229. 'error': 'Notification system failure',
  230. 'message': 'Access denied for security reasons'
  231. }), 500
  232. logger.info(f"Notifications sent successfully to: {successful_backends} for key '{key_config.key_id}'")
  233. # Read key file
  234. file_success, content = read_file_safely(key_config.file_path)
  235. if not file_success:
  236. logger.error(f"CRITICAL: Failed to read key file for '{key_config.key_id}': {content}")
  237. return jsonify({
  238. 'error': 'File access failure',
  239. 'message': 'Unable to retrieve key part'
  240. }), 500
  241. logger.warning(f"EMERGENCY: Key part successfully retrieved and sent for key '{key_config.key_id}'")
  242. return jsonify({
  243. 'success': True,
  244. 'key_id': key_config.key_id,
  245. 'key_part': content,
  246. 'timestamp': time.time(),
  247. 'notified_backends': successful_backends
  248. })
  249. except Exception as e:
  250. logger.error(f"CRITICAL: Unexpected error in key access for '{key_config.key_id}': {str(e)}")
  251. return jsonify({
  252. 'error': 'System error',
  253. 'message': 'Internal server error'
  254. }), 500
  255. return get_key_part
  256. @require_auth(is_health=True)
  257. def health_check():
  258. """Health check endpoint that verifies both health monitoring and all key request functionality"""
  259. logger.info("Health check requested")
  260. if config is None:
  261. logger.error("Configuration not loaded during health check")
  262. return jsonify({'status': 'error', 'message': 'Configuration not loaded'}), 500
  263. try:
  264. # Test health notification system
  265. health_notification_success, health_backends = send_ntfy_notification(
  266. config.ntfy_backends_health,
  267. config.ntfy_health_message,
  268. "Health Check"
  269. )
  270. # Test dummy file access
  271. dummy_file_success, dummy_content = read_file_safely(config.dummy_file_path)
  272. # Test all key files access (without exposing content)
  273. key_files_status = {}
  274. all_key_files_ok = True
  275. for key_id, key_config in config.keys.items():
  276. key_file_success, key_content = read_file_safely(key_config.file_path)
  277. key_files_status[key_id] = key_file_success
  278. if not key_file_success:
  279. all_key_files_ok = False
  280. # Test all key notification backends
  281. key_backends_status = {}
  282. all_key_backends_ok = True
  283. for key_id, key_config in config.keys.items():
  284. try:
  285. # Test notification without actually sending
  286. backend_test_success = len(key_config.backends) > 0
  287. key_backends_status[key_id] = {
  288. 'backends': key_config.backends,
  289. 'success': backend_test_success
  290. }
  291. if not backend_test_success:
  292. all_key_backends_ok = False
  293. except Exception as e:
  294. key_backends_status[key_id] = {
  295. 'backends': key_config.backends,
  296. 'success': False,
  297. 'error': str(e)
  298. }
  299. all_key_backends_ok = False
  300. # Determine overall health status
  301. all_systems_ok = (health_notification_success and
  302. dummy_file_success and
  303. all_key_files_ok and
  304. all_key_backends_ok)
  305. if not all_systems_ok:
  306. error_details = []
  307. if not health_notification_success:
  308. error_details.append("health notifications failed")
  309. if not dummy_file_success:
  310. error_details.append(f"dummy file access failed: {dummy_content}")
  311. # Add key-specific errors
  312. for key_id, status in key_files_status.items():
  313. if not status:
  314. error_details.append(f"key file access failed for '{key_id}'")
  315. for key_id, status in key_backends_status.items():
  316. if not status['success']:
  317. error_msg = f"key backends failed for '{key_id}'"
  318. if 'error' in status:
  319. error_msg += f": {status['error']}"
  320. error_details.append(error_msg)
  321. logger.error(f"Health check failed: {', '.join(error_details)}")
  322. return jsonify({
  323. 'status': 'error',
  324. 'message': 'System components failed',
  325. 'details': error_details,
  326. 'health_notifications': health_notification_success,
  327. 'dummy_file_access': dummy_file_success,
  328. 'key_files_status': key_files_status,
  329. 'key_backends_status': key_backends_status
  330. }), 500
  331. logger.info("Health check completed successfully - all systems operational")
  332. return jsonify({
  333. 'status': 'ok',
  334. 'timestamp': time.time(),
  335. 'health_backends_notified': health_backends,
  336. 'dummy_content_length': len(dummy_content),
  337. 'keys_accessible': len(key_files_status),
  338. 'key_files_status': key_files_status,
  339. 'key_backends_status': key_backends_status,
  340. 'emergency_system_ready': True
  341. })
  342. except Exception as e:
  343. logger.error(f"Health check error: {str(e)}")
  344. return jsonify({
  345. 'status': 'error',
  346. 'message': 'System error',
  347. 'error': str(e)
  348. }), 500
  349. @app.errorhandler(404)
  350. def not_found(error):
  351. """Handle 404 errors silently for security"""
  352. logger.warning(f"404 attempt: {error}")
  353. return jsonify({'error': 'Not found'}), 404
  354. @app.errorhandler(500)
  355. def internal_error(error):
  356. """Handle internal server errors"""
  357. logger.error(f"Internal server error: {error}")
  358. return jsonify({'error': 'Internal server error'}), 500
  359. def validate_setup():
  360. """Validate system setup before starting"""
  361. logger.info("Validating system setup...")
  362. if config is None:
  363. logger.error("Configuration not loaded")
  364. return False
  365. # Check dummy file exists
  366. if not os.path.exists(config.dummy_file_path):
  367. logger.error(f"Dummy file not found: {config.dummy_file_path}")
  368. return False
  369. # Test dummy file permissions
  370. try:
  371. with open(config.dummy_file_path, 'r') as f:
  372. f.read(1)
  373. except Exception as e:
  374. logger.error(f"Dummy file permission test failed: {str(e)}")
  375. return False
  376. # Validate all key configurations
  377. for key_id, key_config in config.keys.items():
  378. logger.info(f"Validating key '{key_id}'...")
  379. # Check key file exists
  380. if not os.path.exists(key_config.file_path):
  381. logger.error(f"Key file not found for '{key_id}': {key_config.file_path}")
  382. return False
  383. # Test key file permissions
  384. try:
  385. with open(key_config.file_path, 'r') as f:
  386. f.read(1)
  387. except Exception as e:
  388. logger.error(f"Key file permission test failed for '{key_id}': {str(e)}")
  389. return False
  390. # Validate backends are configured
  391. if not key_config.backends:
  392. logger.error(f"No notification backends configured for key '{key_id}'")
  393. return False
  394. # Test notification system
  395. logger.info("Testing notification system...")
  396. try:
  397. # Test health notification system
  398. health_success, _ = send_ntfy_notification(
  399. config.ntfy_backends_health[:1], # Test only first backend
  400. "System startup test - health notifications",
  401. "Emergency Access Startup Test"
  402. )
  403. if not health_success:
  404. logger.error("Health notification system test failed")
  405. return False
  406. # Test each key's notification backends
  407. for key_id, key_config in config.keys.items():
  408. key_success, _ = send_ntfy_notification(
  409. key_config.backends[:1], # Test only first backend
  410. f"System startup test - key '{key_id}' notifications",
  411. "Emergency Access Startup Test"
  412. )
  413. if not key_success:
  414. logger.error(f"Key notification system test failed for '{key_id}'")
  415. return False
  416. except Exception as e:
  417. logger.warning(f"Notification test failed, but continuing: {str(e)}")
  418. logger.info("System validation completed successfully")
  419. return True
  420. class ProductionRequestHandler(WSGIRequestHandler):
  421. """Custom request handler with improved error handling"""
  422. def log_error(self, format, *args):
  423. """Override to use our logger"""
  424. logger.error(f"HTTP Error: {format % args}")
  425. def log_message(self, format, *args):
  426. """Override to use our logger"""
  427. logger.info(f"HTTP: {format % args}")
  428. def main():
  429. global config
  430. try:
  431. # Load configuration
  432. config = Config()
  433. logger.info("Configuration loaded successfully")
  434. # Add ntfy log handler after config is loaded
  435. if config.send_all_logs:
  436. ntfy_handler = NtfyLogHandler(config)
  437. min_level = getattr(logging, config.log_level.upper(), logging.WARNING)
  438. ntfy_handler.setLevel(min_level)
  439. # Add to root logger to catch all application logs
  440. logging.getLogger().addHandler(ntfy_handler)
  441. # Validate system setup
  442. if not validate_setup():
  443. logger.error("System validation failed, exiting")
  444. sys.exit(1)
  445. # Add Flask routes dynamically for each key
  446. for key_id, key_config in config.keys.items():
  447. handler = create_key_handler(key_config)
  448. endpoint_name = f'get_key_part_{key_id}'
  449. app.add_url_rule(key_config.route, endpoint_name, handler, methods=['GET'])
  450. logger.info(f"Registered authenticated key route for '{key_id}': {key_config.route}")
  451. # Add health check route
  452. app.add_url_rule(config.health_route, 'health_check', health_check, methods=['GET'])
  453. # Setup systemd watchdog
  454. watchdog_enabled = setup_systemd_watchdog()
  455. logger.info(f"Starting emergency access server on {config.server_host}:{config.server_port}")
  456. logger.info(f"Health route: {config.health_route}")
  457. logger.info(f"Configured {len(config.keys)} key(s)")
  458. logger.info(f"Systemd watchdog: {'enabled' if watchdog_enabled else 'disabled'}")
  459. # Notify systemd that we're ready
  460. try:
  461. subprocess.run(['systemd-notify', 'READY=1'],
  462. check=False, capture_output=True, timeout=5)
  463. logger.info("Sent READY=1 to systemd")
  464. except Exception as e:
  465. logger.warning(f"Failed to notify systemd ready: {e}")
  466. # Use production-ready server with better error handling
  467. try:
  468. from waitress import serve
  469. logger.info("Using Waitress WSGI server for production")
  470. serve(
  471. app,
  472. host=config.server_host,
  473. port=config.server_port,
  474. threads=6,
  475. connection_limit=100,
  476. cleanup_interval=30,
  477. channel_timeout=120
  478. )
  479. except ImportError:
  480. logger.warning("Waitress not available, falling back to Flask dev server")
  481. app.run(
  482. host=config.server_host,
  483. port=config.server_port,
  484. debug=False,
  485. threaded=True,
  486. request_handler=ProductionRequestHandler
  487. )
  488. except KeyboardInterrupt:
  489. logger.info("Received keyboard interrupt, shutting down gracefully")
  490. except Exception as e:
  491. logger.error(f"Failed to start server: {str(e)}")
  492. if config and hasattr(config, 'ntfy_backends_health'):
  493. try:
  494. send_ntfy_notification(
  495. config.ntfy_backends_health,
  496. f"🚨 Emergency Access server failed to start: {str(e)}",
  497. "Emergency Access CRITICAL"
  498. )
  499. except Exception:
  500. pass
  501. sys.exit(1)
  502. finally:
  503. logger.info("Emergency Access server shutdown complete")
  504. if __name__ == '__main__':
  505. main()