#!/bin/bash # # Production Secrets Setup Script # Purpose: Initialize and manage production secrets with Ansible Vault # # Usage: # ./scripts/setup-production-secrets.sh init # Initialize new vault # ./scripts/setup-production-secrets.sh deploy # Deploy secrets to production # ./scripts/setup-production-secrets.sh rotate # Rotate secrets # ./scripts/setup-production-secrets.sh verify # Verify secrets on server # set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" ANSIBLE_DIR="${PROJECT_ROOT}/deployment/ansible" VAULT_FILE="${ANSIBLE_DIR}/secrets/production-vault.yml" INVENTORY="${ANSIBLE_DIR}/inventory/production.yml" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." if ! command -v ansible-vault &> /dev/null; then log_error "ansible-vault not found. Please install Ansible." exit 1 fi if ! command -v openssl &> /dev/null; then log_error "openssl not found. Please install OpenSSL." exit 1 fi log_info "Prerequisites OK" } # Generate secure random password generate_password() { local length="${1:-32}" openssl rand -base64 "$length" | tr -d "=+/" | cut -c1-"$length" } # Generate base64 encoded app key generate_app_key() { openssl rand -base64 32 } # Initialize vault with secure defaults init_vault() { log_info "Initializing production secrets vault..." if [[ -f "$VAULT_FILE" ]]; then log_warn "Vault file already exists: $VAULT_FILE" read -p "Do you want to overwrite it? (yes/no): " -r if [[ ! $REPLY =~ ^[Yy]es$ ]]; then log_info "Aborting initialization" exit 0 fi fi # Generate secure secrets log_info "Generating secure secrets..." DB_PASSWORD=$(generate_password 32) REDIS_PASSWORD=$(generate_password 32) APP_KEY=$(generate_app_key) JWT_SECRET=$(generate_password 64) REGISTRY_PASSWORD=$(generate_password 24) # Create vault file cat > "$VAULT_FILE" < Commands: init Initialize new secrets vault with auto-generated secure values deploy Deploy secrets from vault to production server rotate Rotate secrets (generate new values and redeploy) verify Verify secrets are properly deployed on server Examples: $0 init # First time setup $0 deploy # Deploy after manual vault updates $0 rotate # Monthly security rotation $0 verify # Check deployment status EOF ;; esac } main "$@"