generate-stores 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash
  2. set -euo pipefail
  3. # Ensure CA exists
  4. generate-ca
  5. # Shared configuration
  6. PASSWORD="password"
  7. HOST="localhost"
  8. # App definitions: CN, keystore name, truststore name
  9. declare -A APPS=(
  10. [api]="api"
  11. [client]="josh"
  12. )
  13. # Ensure required scripts are on PATH
  14. for cmd in generate-cert add-to-keystore add-to-truststore; do
  15. if ! command -v $cmd >/dev/null 2>&1; then
  16. echo "❌ Required script '$cmd' not found in PATH" >&2
  17. exit 1
  18. fi
  19. done
  20. # Generate certs and populate keystores and truststores
  21. for APP in "${!APPS[@]}"; do
  22. CN="${APPS[$APP]}"
  23. KEYSTORE="${CN}-keystore.p12"
  24. echo "🔐 Generating and installing cert for $APP ($CN)..."
  25. # Generate cert and install in own keystore
  26. generate-cert "$CN" "$APP.127.0.0.1.nip.io" | tee >(add-to-keystore "$KEYSTORE") > "${CN}-bundle.tar"
  27. done
  28. # Second pass: truststores — each app must trust all
  29. for RECEIVER in "${!APPS[@]}"; do
  30. RECEIVER_CN="${APPS[$RECEIVER]}"
  31. TRUSTSTORE="${RECEIVER_CN}-truststore.p12"
  32. echo "🤝 Updating truststore for $RECEIVER..."
  33. for ISSUER in "${!APPS[@]}"; do
  34. ISSUER_CN="${APPS[$ISSUER]}"
  35. BUNDLE="${ISSUER_CN}-bundle.tar"
  36. echo " ↪ Trusting $ISSUER ($ISSUER_CN)"
  37. cat "$BUNDLE" | add-to-truststore "$TRUSTSTORE"
  38. done
  39. done
  40. # Cleanup bundles
  41. rm -f ./*-bundle.tar
  42. echo "✅ All keystores and truststores generated successfully."