config.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. import os
  3. import yaml
  4. from typing import Dict, List, Any, Optional
  5. class KeyConfig:
  6. """Configuration for a single key"""
  7. def __init__(self, key_id: str, config_data: Dict[str, Any], global_config: Dict[str, Any]):
  8. self.key_id = key_id
  9. self.route = config_data.get('route', '')
  10. self.file_path = config_data.get('file', '')
  11. self.backends = config_data.get('backends', [])
  12. self.message = config_data.get('message', f'🚨 EMERGENCY: Key {key_id} accessed from server')
  13. if not self.route:
  14. raise Exception(f"Route not configured for key '{key_id}'")
  15. if not self.file_path:
  16. raise Exception(f"File path not configured for key '{key_id}'")
  17. if not self.backends:
  18. raise Exception(f"No notification backends configured for key '{key_id}'")
  19. class Config:
  20. def __init__(self, config_path: str = None):
  21. if config_path is None:
  22. config_path = os.environ.get('EMERGENCY_CONFIG', 'config.json')
  23. self.config_path = config_path
  24. self.config = self._load_config()
  25. self._keys = self._load_keys()
  26. def _load_config(self) -> Dict[str, Any]:
  27. """Load configuration from file"""
  28. try:
  29. with open(self.config_path, 'r') as f:
  30. if self.config_path.endswith('.yaml') or self.config_path.endswith('.yml'):
  31. return yaml.safe_load(f)
  32. else:
  33. return json.load(f)
  34. except FileNotFoundError:
  35. raise Exception(f"Configuration file {self.config_path} not found")
  36. except Exception as e:
  37. raise Exception(f"Failed to load configuration: {str(e)}")
  38. def _load_keys(self) -> Dict[str, KeyConfig]:
  39. """Load key configurations"""
  40. keys = {}
  41. if 'keys' not in self.config:
  42. raise Exception("No keys configured. Configuration must include a 'keys' section")
  43. for key_id, key_config in self.config['keys'].items():
  44. keys[key_id] = KeyConfig(key_id, key_config, self.config)
  45. if not keys:
  46. raise Exception("No valid keys found in configuration")
  47. return keys
  48. @property
  49. def server_host(self) -> str:
  50. return self.config.get('server', {}).get('host', '127.0.0.1')
  51. @property
  52. def server_port(self) -> int:
  53. return self.config.get('server', {}).get('port', 1127)
  54. @property
  55. def health_route(self) -> str:
  56. return self.config.get('routes', {}).get('health_route', '/health-check')
  57. @property
  58. def dummy_file_path(self) -> str:
  59. dummy_file = self.config.get('files', {}).get('dummy_file')
  60. if not dummy_file:
  61. raise Exception("dummy_file not configured")
  62. return dummy_file
  63. @property
  64. def keys(self) -> Dict[str, KeyConfig]:
  65. """Get all configured keys"""
  66. return self._keys
  67. def get_key_by_route(self, route: str) -> Optional[KeyConfig]:
  68. """Find a key configuration by its route"""
  69. for key_config in self._keys.values():
  70. if key_config.route == route:
  71. return key_config
  72. return None
  73. def get_key_by_id(self, key_id: str) -> Optional[KeyConfig]:
  74. """Get a key configuration by its ID"""
  75. return self._keys.get(key_id)
  76. @property
  77. def ntfy_backends_health(self) -> List[str]:
  78. backends = self.config.get('notifications', {}).get('health_backends', [])
  79. if not backends:
  80. raise Exception("No notification backends configured for health check")
  81. return backends
  82. @property
  83. def ntfy_config_path(self) -> str:
  84. return self.config.get('notifications', {}).get('config_path', '/etc/emergency-access/ntfy.yml')
  85. @property
  86. def log_level(self) -> str:
  87. return self.config.get('notifications', {}).get('log_level', 'WARNING')
  88. @property
  89. def send_all_logs(self) -> bool:
  90. return self.config.get('notifications', {}).get('send_all_logs', True)
  91. @property
  92. def ntfy_health_message(self) -> str:
  93. return self.config.get('notifications', {}).get('health_message', '✅ Emergency access server health check completed')