install.sh 9.3 KB

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