config.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import json
  2. import os
  3. import yaml
  4. from typing import Dict, List, Any
  5. class Config:
  6. def __init__(self, config_path: str = None):
  7. if config_path is None:
  8. config_path = os.environ.get('EMERGENCY_CONFIG', 'config.json')
  9. self.config_path = config_path
  10. self.config = self._load_config()
  11. def _load_config(self) -> Dict[str, Any]:
  12. """Load configuration from file"""
  13. try:
  14. with open(self.config_path, 'r') as f:
  15. if self.config_path.endswith('.yaml') or self.config_path.endswith('.yml'):
  16. return yaml.safe_load(f)
  17. else:
  18. return json.load(f)
  19. except FileNotFoundError:
  20. raise Exception(f"Configuration file {self.config_path} not found")
  21. except Exception as e:
  22. raise Exception(f"Failed to load configuration: {str(e)}")
  23. @property
  24. def server_host(self) -> str:
  25. return self.config.get('server', {}).get('host', '127.0.0.1')
  26. @property
  27. def server_port(self) -> int:
  28. return self.config.get('server', {}).get('port', 1127)
  29. @property
  30. def key_route(self) -> str:
  31. return self.config.get('routes', {}).get('key_route', '/emergency-key-xyz123')
  32. @property
  33. def health_route(self) -> str:
  34. return self.config.get('routes', {}).get('health_route', '/health-check')
  35. @property
  36. def key_file_path(self) -> str:
  37. key_file = self.config.get('files', {}).get('key_file')
  38. if not key_file:
  39. raise Exception("key_file not configured")
  40. return key_file
  41. @property
  42. def dummy_file_path(self) -> str:
  43. dummy_file = self.config.get('files', {}).get('dummy_file')
  44. if not dummy_file:
  45. raise Exception("dummy_file not configured")
  46. return dummy_file
  47. @property
  48. def ntfy_backends_key(self) -> List[str]:
  49. backends = self.config.get('notifications', {}).get('key_backends', [])
  50. if not backends:
  51. raise Exception("No notification backends configured for key access")
  52. return backends
  53. @property
  54. def ntfy_backends_health(self) -> List[str]:
  55. backends = self.config.get('notifications', {}).get('health_backends', [])
  56. if not backends:
  57. raise Exception("No notification backends configured for health check")
  58. return backends
  59. @property
  60. def log_level(self) -> str:
  61. return self.config.get('notifications', {}).get('log_level', 'WARNING')
  62. @property
  63. def send_all_logs(self) -> bool:
  64. return self.config.get('notifications', {}).get('send_all_logs', True)
  65. @property
  66. def ntfy_key_message(self) -> str:
  67. return self.config.get('notifications', {}).get('key_message', 'EMERGENCY: Decryption key accessed')
  68. @property
  69. def ntfy_health_message(self) -> str:
  70. return self.config.get('notifications', {}).get('health_message', 'Health check performed')