setup_environment.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # Ensure we're in the project root directory
  3. PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
  4. cd "${PROJECT_ROOT}"
  5. # Define directories relative to project root
  6. CREDENTIALS_DIR="config/credentials"
  7. DOCKER_DIR="docker"
  8. # Create necessary directories
  9. mkdir -p "${CREDENTIALS_DIR}"
  10. mkdir -p "${DOCKER_DIR}"
  11. # Function to generate secure passwords
  12. generate_password() {
  13. openssl rand -base64 24 | tr -dc 'a-zA-Z0-9' | head -c 24
  14. }
  15. # Date for documentation
  16. SETUP_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
  17. # Generate passwords
  18. KEYCLOAK_ADMIN_PASSWORD=$(generate_password)
  19. KC_DB_PASSWORD=$(generate_password)
  20. # Create .env file in docker directory
  21. cat > "${DOCKER_DIR}/.env" << EOL
  22. # Generated on ${SETUP_DATE}
  23. # Keycloak Admin
  24. KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD}
  25. # Keycloak Database
  26. KC_DB_USERNAME=keycloak
  27. KC_DB_PASSWORD=${KC_DB_PASSWORD}
  28. EOL
  29. # Create encrypted credentials documentation
  30. cat > "${CREDENTIALS_DIR}/credentials_${SETUP_DATE}.txt" << EOL
  31. Setup Date: ${SETUP_DATE}
  32. Keycloak Admin Credentials:
  33. Username: admin
  34. Password: ${KEYCLOAK_ADMIN_PASSWORD}
  35. Keycloak Database Credentials:
  36. Username: keycloak
  37. Password: ${KC_DB_PASSWORD}
  38. EOL
  39. # Encrypt credentials file
  40. gpg --symmetric --cipher-algo AES256 "${CREDENTIALS_DIR}/credentials_${SETUP_DATE}.txt"
  41. rm "${CREDENTIALS_DIR}/credentials_${SETUP_DATE}.txt"
  42. echo "Environment setup completed!"
  43. echo "Credentials have been saved and encrypted in: ${CREDENTIALS_DIR}/credentials_${SETUP_DATE}.txt.gpg"
  44. echo ".env file has been created in: ${DOCKER_DIR}/.env"
  45. echo ""
  46. echo "To view credentials, use:"
  47. echo "gpg -d ${CREDENTIALS_DIR}/credentials_${SETUP_DATE}.txt.gpg"