kc-setup.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Get KC data
  2. source kc-data.sh
  3. # Change Timezone
  4. timedatectl set-timezone Europe/Moscow
  5. # timedatectl | tee kctest.txt
  6. # Install Packages
  7. apt-get update && apt-get install -y unzip openjdk-17-jre
  8. apt-get update && apt-get install -y unzip openjdk-17-jre
  9. # Map KC_FQDN to the localhost for the simplify KC provisioning
  10. echo "127.0.0.1 $KC_FQDN" >> /etc/hosts
  11. # Move LE certificates onto the place
  12. mkdir -p $KC_CERT_PATH
  13. mv *.pem $KC_CERT_PATH
  14. # Get Keycloak distro and put files to the right place
  15. curl -sLO https://github.com/keycloak/keycloak/releases/download/$KC_VER/keycloak-$KC_VER.zip
  16. unzip -q keycloak-$KC_VER.zip
  17. rm -f keycloak-$KC_VER/bin/*.bat
  18. mkdir -p /opt/keycloak
  19. cp -R keycloak-$KC_VER/* /opt/keycloak
  20. rm -rf keycloak-$KC_VER/ keycloak-$KC_VER.zip
  21. # Import configuration from realm config file
  22. export PATH=$PATH:/opt/keycloak/bin
  23. kc.sh build
  24. kc.sh import --file=realm.json
  25. # Prepare systemd things
  26. groupadd keycloak
  27. useradd -r -g keycloak -d /opt/keycloak -s /sbin/nologin keycloak
  28. chown -R keycloak:keycloak /opt/keycloak
  29. chmod o+x /opt/keycloak/bin/
  30. cat <<EOF > /lib/systemd/system/keycloak.service
  31. [Unit]
  32. Description=Keycloak Service
  33. After=network.target
  34. [Service]
  35. User=keycloak
  36. Group=keycloak
  37. PIDFile=/var/run/keycloak/keycloak.pid
  38. WorkingDirectory=/opt/keycloak
  39. Environment="KEYCLOAK_ADMIN=$KC_ADM_USER"
  40. Environment="KEYCLOAK_ADMIN_PASSWORD=$KC_ADM_PASS"
  41. ExecStart=/opt/keycloak/bin/kc.sh start \\
  42. --db-url-database=$PG_DB_NAME \\
  43. --db-url-host=$PG_DB_HOST \\
  44. --db-username=$PG_DB_USER \\
  45. --db-password=$PG_DB_PASS \\
  46. --hostname=$KC_FQDN \\
  47. --hostname-strict=true \\
  48. --http-enabled=false \\
  49. --https-protocols=TLSv1.3,TLSv1.2 \\
  50. --https-port=$KC_PORT \\
  51. --https-certificate-file=$KC_CERT_PATH/$KC_CERT_PUB \\
  52. --https-certificate-key-file=$KC_CERT_PATH/$KC_CERT_PRIV \\
  53. --log-level=INFO
  54. [Install]
  55. WantedBy=multi-user.target
  56. EOF
  57. # Start Keycloak via systemd
  58. systemctl daemon-reload
  59. sleep 3
  60. systemctl start keycloak
  61. systemctl enable keycloak
  62. # Remove KC admin credentials from the systemd unit after the first start
  63. sed -i '/KEYCLOAK_ADMIN/d' /lib/systemd/system/keycloak.service
  64. systemctl daemon-reload
  65. sleep 3
  66. # Waiting until KC has been started
  67. while :; do
  68. curl -sf "https://$KC_FQDN:$KC_PORT" -o /dev/null && break
  69. sleep 10
  70. done
  71. # Create KC Users
  72. kcadm.sh config credentials --server https://$KC_FQDN:$KC_PORT --realm master --user $KC_ADM_USER --password $KC_ADM_PASS
  73. while read line; do
  74. user=$(echo $line | cut -f1 -d:)
  75. pass=$(echo $line | cut -f2 -d:)
  76. kcadm.sh create users -r $KC_REALM -s username="$user" -s enabled=true
  77. kcadm.sh set-password -r $KC_REALM --username "$user" -p "$pass"
  78. #sleep 2
  79. done < $KC_USERS_FN