install.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 key management script
  88. if [[ -f "add_key.py" ]]; then
  89. cp add_key.py "$INSTALL_DIR/"
  90. chmod 755 "$INSTALL_DIR/add_key.py"
  91. print_status "Installed key addition script"
  92. fi
  93. if [[ -f "generate_passwords.py" ]]; then
  94. cp generate_passwords.py "$INSTALL_DIR/"
  95. chmod 755 "$INSTALL_DIR/generate_passwords.py"
  96. print_status "Installed password generation script"
  97. fi
  98. # Copy example config if config doesn't exist
  99. if [[ ! -f "$CONFIG_DIR/config.json" ]]; then
  100. cp config.json.example "$CONFIG_DIR/config.json"
  101. print_status "Copied example configuration to $CONFIG_DIR/config.json"
  102. print_warning "Please edit $CONFIG_DIR/config.json with your backend names and authentication credentials"
  103. else
  104. print_warning "Configuration file already exists, skipping copy"
  105. fi
  106. # Copy ntfy config if it doesn't exist
  107. if [[ ! -f "$CONFIG_DIR/ntfy.yml" ]]; then
  108. cp ntfy.yml "$CONFIG_DIR/"
  109. print_status "Copied example ntfy configuration to $CONFIG_DIR/ntfy.yml"
  110. print_warning "Please edit $CONFIG_DIR/ntfy.yml with your notification backends"
  111. else
  112. print_warning "ntfy configuration file already exists, skipping copy"
  113. fi
  114. # Set permissions
  115. chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
  116. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/config.json"
  117. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/ntfy.yml"
  118. chmod 644 "$INSTALL_DIR"/*.py
  119. chmod 644 "$INSTALL_DIR/requirements.txt"
  120. chmod 640 "$CONFIG_DIR/config.json" # Restrictive permissions for config
  121. chmod 640 "$CONFIG_DIR/ntfy.yml" # Restrictive permissions for ntfy config
  122. }
  123. setup_python_environment() {
  124. print_status "Setting up Python virtual environment..."
  125. # Test if venv module is available
  126. if ! python3 -m venv --help > /dev/null 2>&1; then
  127. print_error "Python venv module not available. Please install python3-venv or equivalent."
  128. print_error "On Ubuntu/Debian: sudo apt install python3-venv"
  129. print_error "On CentOS/RHEL: venv is built-in with Python 3.3+"
  130. exit 1
  131. fi
  132. # Create virtual environment
  133. sudo -u "$SERVICE_USER" python3 -m venv "$INSTALL_DIR/venv"
  134. # Install dependencies
  135. sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install --upgrade pip
  136. sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install -r "$INSTALL_DIR/requirements.txt"
  137. print_status "Python environment setup complete"
  138. }
  139. setup_logging() {
  140. print_status "Setting up logging..."
  141. # Create log file
  142. touch "$LOG_FILE"
  143. chown "$SERVICE_USER:$SERVICE_GROUP" "$LOG_FILE"
  144. chmod 644 "$LOG_FILE"
  145. # Setup log rotation
  146. cat > /etc/logrotate.d/emergency-access << EOF
  147. $LOG_FILE {
  148. daily
  149. rotate 30
  150. compress
  151. delaycompress
  152. missingok
  153. notifempty
  154. create 644 $SERVICE_USER $SERVICE_GROUP
  155. postrotate
  156. systemctl reload emergency-access.service > /dev/null 2>&1 || true
  157. endscript
  158. }
  159. EOF
  160. print_status "Logging configuration complete"
  161. }
  162. install_systemd_service() {
  163. print_status "Installing systemd service..."
  164. # Copy service file
  165. cp emergency-access.service "$SERVICE_FILE"
  166. # Reload systemd
  167. systemctl daemon-reload
  168. print_status "Systemd service installed"
  169. }
  170. create_example_files() {
  171. print_status "Creating example key and dummy files..."
  172. # Create example key file
  173. if [[ ! -f "$CONFIG_DIR/key-part.txt" ]]; then
  174. echo "EXAMPLE_KEY_PART_$(openssl rand -hex 16)" > "$CONFIG_DIR/key-part.txt"
  175. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/key-part.txt"
  176. chmod 600 "$CONFIG_DIR/key-part.txt"
  177. print_status "Created example key file: $CONFIG_DIR/key-part.txt"
  178. print_warning "Replace this with your actual key part!"
  179. fi
  180. # Create dummy file
  181. if [[ ! -f "$CONFIG_DIR/dummy.txt" ]]; then
  182. echo "system_healthy_$(date +%s)" > "$CONFIG_DIR/dummy.txt"
  183. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/dummy.txt"
  184. chmod 644 "$CONFIG_DIR/dummy.txt"
  185. print_status "Created dummy file: $CONFIG_DIR/dummy.txt"
  186. fi
  187. }
  188. print_final_instructions() {
  189. print_status "Installation complete!"
  190. echo
  191. print_warning "IMPORTANT: Before starting the service:"
  192. echo "1. Edit $CONFIG_DIR/config.json with your backend names and authentication credentials"
  193. echo "2. Edit $CONFIG_DIR/ntfy.yml with your notification backend configurations"
  194. echo "3. Create key files for each configured key (e.g., backup-key.txt, master-key.txt)"
  195. echo "4. Use generate_passwords.py to create secure authentication credentials"
  196. echo "5. Test the configuration"
  197. echo
  198. print_status "Service management commands:"
  199. echo " Start service: sudo systemctl start emergency-access"
  200. echo " Enable at boot: sudo systemctl enable emergency-access"
  201. echo " Check status: sudo systemctl status emergency-access"
  202. echo " View logs: sudo journalctl -u emergency-access -f"
  203. echo " View log file: sudo tail -f $LOG_FILE"
  204. echo
  205. print_status "Configuration files:"
  206. echo " Service config: $CONFIG_DIR/config.json"
  207. echo " ntfy config: $CONFIG_DIR/ntfy.yml"
  208. echo " Key files: $CONFIG_DIR/[key-name]-key.txt (create as configured)"
  209. echo ""
  210. print_status "Key management tools:"
  211. echo " Generate passwords: cd $INSTALL_DIR && python generate_passwords.py --keys backup master"
  212. echo " Add new key: cd $INSTALL_DIR && python add_key.py KEY_ID /path/to/existing/key.txt backends"
  213. echo " Dummy file: $CONFIG_DIR/dummy.txt"
  214. echo " Log file: $LOG_FILE"
  215. echo
  216. print_warning "Security note: This server provides access to sensitive key material."
  217. print_warning "Ensure proper network security and monitoring are in place."
  218. }
  219. main() {
  220. print_status "Starting Emergency Access Server installation..."
  221. check_root
  222. install_dependencies
  223. create_user
  224. setup_directories
  225. install_application
  226. setup_python_environment
  227. setup_logging
  228. install_systemd_service
  229. create_example_files
  230. print_final_instructions
  231. print_status "Installation completed successfully!"
  232. }
  233. # Run main function
  234. main "$@"