install.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/bash
  2. # Emergency Access Server Installation Script
  3. # Run as root or with sudo
  4. set -euo pipefail
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. NC='\033[0m' # No Color
  10. # Configuration
  11. SERVICE_USER="emergency-access"
  12. SERVICE_GROUP="emergency-access"
  13. INSTALL_DIR="/opt/emergency-access"
  14. CONFIG_DIR="/etc/emergency-access"
  15. LOG_FILE="/var/log/emergency-access.log"
  16. SERVICE_FILE="/etc/systemd/system/emergency-access.service"
  17. print_status() {
  18. echo -e "${GREEN}[INFO]${NC} $1"
  19. }
  20. print_warning() {
  21. echo -e "${YELLOW}[WARNING]${NC} $1"
  22. }
  23. print_error() {
  24. echo -e "${RED}[ERROR]${NC} $1"
  25. }
  26. check_root() {
  27. if [[ $EUID -ne 0 ]]; then
  28. print_error "This script must be run as root"
  29. exit 1
  30. fi
  31. }
  32. install_dependencies() {
  33. print_status "Installing system dependencies..."
  34. # Detect package manager
  35. if command -v apt-get &> /dev/null; then
  36. apt-get update
  37. apt-get install -y python3 python3-pip python3-venv
  38. elif command -v yum &> /dev/null; then
  39. yum install -y python3 python3-pip
  40. # python3-venv not available in CentOS/RHEL, use built-in venv
  41. elif command -v dnf &> /dev/null; then
  42. dnf install -y python3 python3-pip
  43. # python3-venv not needed in newer Fedora, venv is built-in
  44. elif command -v pacman &> /dev/null; then
  45. pacman -S --noconfirm python python-pip
  46. # venv is built into Python 3.3+
  47. else
  48. print_error "Unsupported package manager. Please install Python 3 and pip manually."
  49. exit 1
  50. fi
  51. }
  52. create_user() {
  53. print_status "Creating service user and group..."
  54. if ! getent group "$SERVICE_GROUP" > /dev/null 2>&1; then
  55. groupadd --system "$SERVICE_GROUP"
  56. print_status "Created group: $SERVICE_GROUP"
  57. else
  58. print_warning "Group $SERVICE_GROUP already exists"
  59. fi
  60. if ! getent passwd "$SERVICE_USER" > /dev/null 2>&1; then
  61. useradd --system --gid "$SERVICE_GROUP" --home-dir "$INSTALL_DIR" \
  62. --shell /bin/false --comment "Emergency Access Service" "$SERVICE_USER"
  63. print_status "Created user: $SERVICE_USER"
  64. else
  65. print_warning "User $SERVICE_USER already exists"
  66. fi
  67. }
  68. setup_directories() {
  69. print_status "Setting up directories..."
  70. # Create installation directory
  71. mkdir -p "$INSTALL_DIR"
  72. mkdir -p "$CONFIG_DIR"
  73. # Set ownership
  74. chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
  75. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR"
  76. # Set permissions
  77. chmod 755 "$INSTALL_DIR"
  78. chmod 750 "$CONFIG_DIR" # More restrictive for config
  79. print_status "Created directories with proper permissions"
  80. }
  81. install_application() {
  82. print_status "Installing application files..."
  83. # Copy application files
  84. cp main.py "$INSTALL_DIR/"
  85. cp config.py "$INSTALL_DIR/"
  86. cp requirements.txt "$INSTALL_DIR/"
  87. # Copy example config if config doesn't exist
  88. if [[ ! -f "$CONFIG_DIR/config.json" ]]; then
  89. cp config.json.example "$CONFIG_DIR/config.json"
  90. print_status "Copied example configuration to $CONFIG_DIR/config.json"
  91. print_warning "Please edit $CONFIG_DIR/config.json with your backend names and authentication credentials"
  92. else
  93. print_warning "Configuration file already exists, skipping copy"
  94. fi
  95. # Copy ntfy config if it doesn't exist
  96. if [[ ! -f "$CONFIG_DIR/ntfy.yml" ]]; then
  97. cp ntfy.yml "$CONFIG_DIR/"
  98. print_status "Copied example ntfy configuration to $CONFIG_DIR/ntfy.yml"
  99. print_warning "Please edit $CONFIG_DIR/ntfy.yml with your notification backends"
  100. else
  101. print_warning "ntfy configuration file already exists, skipping copy"
  102. fi
  103. # Set permissions
  104. chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
  105. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/config.json"
  106. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/ntfy.yml"
  107. chmod 644 "$INSTALL_DIR"/*.py
  108. chmod 644 "$INSTALL_DIR/requirements.txt"
  109. chmod 640 "$CONFIG_DIR/config.json" # Restrictive permissions for config
  110. chmod 640 "$CONFIG_DIR/ntfy.yml" # Restrictive permissions for ntfy config
  111. }
  112. setup_python_environment() {
  113. print_status "Setting up Python virtual environment..."
  114. # Test if venv module is available
  115. if ! python3 -m venv --help > /dev/null 2>&1; then
  116. print_error "Python venv module not available. Please install python3-venv or equivalent."
  117. print_error "On Ubuntu/Debian: sudo apt install python3-venv"
  118. print_error "On CentOS/RHEL: venv is built-in with Python 3.3+"
  119. exit 1
  120. fi
  121. # Create virtual environment
  122. sudo -u "$SERVICE_USER" python3 -m venv "$INSTALL_DIR/venv"
  123. # Install dependencies
  124. sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install --upgrade pip
  125. sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install -r "$INSTALL_DIR/requirements.txt"
  126. print_status "Python environment setup complete"
  127. }
  128. setup_logging() {
  129. print_status "Setting up logging..."
  130. # Create log file
  131. touch "$LOG_FILE"
  132. chown "$SERVICE_USER:$SERVICE_GROUP" "$LOG_FILE"
  133. chmod 644 "$LOG_FILE"
  134. # Setup log rotation
  135. cat > /etc/logrotate.d/emergency-access << EOF
  136. $LOG_FILE {
  137. daily
  138. rotate 30
  139. compress
  140. delaycompress
  141. missingok
  142. notifempty
  143. create 644 $SERVICE_USER $SERVICE_GROUP
  144. postrotate
  145. systemctl reload emergency-access.service > /dev/null 2>&1 || true
  146. endscript
  147. }
  148. EOF
  149. print_status "Logging configuration complete"
  150. }
  151. install_systemd_service() {
  152. print_status "Installing systemd service..."
  153. # Copy service file
  154. cp emergency-access.service "$SERVICE_FILE"
  155. # Reload systemd
  156. systemctl daemon-reload
  157. print_status "Systemd service installed"
  158. }
  159. create_example_files() {
  160. print_status "Creating example key and dummy files..."
  161. # Create example key file
  162. if [[ ! -f "$CONFIG_DIR/key-part.txt" ]]; then
  163. echo "EXAMPLE_KEY_PART_$(openssl rand -hex 16)" > "$CONFIG_DIR/key-part.txt"
  164. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/key-part.txt"
  165. chmod 600 "$CONFIG_DIR/key-part.txt"
  166. print_status "Created example key file: $CONFIG_DIR/key-part.txt"
  167. print_warning "Replace this with your actual key part!"
  168. fi
  169. # Create dummy file
  170. if [[ ! -f "$CONFIG_DIR/dummy.txt" ]]; then
  171. echo "system_healthy_$(date +%s)" > "$CONFIG_DIR/dummy.txt"
  172. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/dummy.txt"
  173. chmod 644 "$CONFIG_DIR/dummy.txt"
  174. print_status "Created dummy file: $CONFIG_DIR/dummy.txt"
  175. fi
  176. }
  177. print_final_instructions() {
  178. print_status "Installation complete!"
  179. echo
  180. print_warning "IMPORTANT: Before starting the service:"
  181. echo "1. Edit $CONFIG_DIR/config.json with your backend names and authentication credentials"
  182. echo "2. Edit $CONFIG_DIR/ntfy.yml with your notification backend configurations"
  183. echo "3. Create key files for each configured key (e.g., backup-key.txt, master-key.txt)"
  184. echo "4. Use generate_passwords.py to create secure authentication credentials"
  185. echo "5. Test the configuration"
  186. echo
  187. print_status "Service management commands:"
  188. echo " Start service: sudo systemctl start emergency-access"
  189. echo " Enable at boot: sudo systemctl enable emergency-access"
  190. echo " Check status: sudo systemctl status emergency-access"
  191. echo " View logs: sudo journalctl -u emergency-access -f"
  192. echo " View log file: sudo tail -f $LOG_FILE"
  193. echo
  194. print_status "Configuration files:"
  195. echo " Service config: $CONFIG_DIR/config.json"
  196. echo " ntfy config: $CONFIG_DIR/ntfy.yml"
  197. echo " Key files: $CONFIG_DIR/[key-name]-key.txt (create as configured)"
  198. echo ""
  199. print_status "Password generator:"
  200. echo " Generate passwords: python generate_passwords.py --keys backup master"
  201. echo " Dummy file: $CONFIG_DIR/dummy.txt"
  202. echo " Log file: $LOG_FILE"
  203. echo
  204. print_warning "Security note: This server provides access to sensitive key material."
  205. print_warning "Ensure proper network security and monitoring are in place."
  206. }
  207. main() {
  208. print_status "Starting Emergency Access Server installation..."
  209. check_root
  210. install_dependencies
  211. create_user
  212. setup_directories
  213. install_application
  214. setup_python_environment
  215. setup_logging
  216. install_systemd_service
  217. create_example_files
  218. print_final_instructions
  219. print_status "Installation completed successfully!"
  220. }
  221. # Run main function
  222. main "$@"