configure-labs.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash
  2. #
  3. # Asks about the method of installation and produces a config file accordingly.
  4. #
  5. # By now we should have:
  6. # - a working RHSSO instance
  7. # - a realm called "sample"
  8. # - a client called "sample-client"
  9. #
  10. SSO_HOST_OCP=keycloak-rhsso.apps.ocp4.example.com:443
  11. SSO_HOST_LOC=sso.lab.example.com:9443
  12. SSO_ADMIN_USER=admin
  13. SSO_ADMIN_PASS_LOC='jboss#1!'
  14. SSO_ADMIN_PASS_OCP=''
  15. # Ask about the installation method.
  16. echo "Please enter the type of installation you are using:"
  17. select TYPE in "traditional service installation (local)" "operator-based installation (OpenShift)"; do
  18. if [ ${REPLY} -eq 1 ]; then
  19. SSO_HOST="${SSO_HOST_LOC}"
  20. SSO_ADMIN_PASS="${SSO_ADMIN_PASS_LOC}"
  21. break
  22. elif [ ${REPLY} -eq 2 ]; then
  23. SSO_HOST="${SSO_HOST_OCP}"
  24. SSO_ADMIN_PASS="${SSO_ADMIN_PASS_OCP}"
  25. break
  26. else
  27. echo "Incorrect response. Please try again."
  28. fi
  29. done
  30. echo "Thank you. Proceeding with settings for ${TYPE}."
  31. echo
  32. # If the installation method is OCP, try obtaining admin user's password.
  33. if [ ${REPLY} -eq 2 ]; then
  34. echo -n " - attempting to obtain password for user \"admin\"... "
  35. oc login -u admin -p redhat https://api.ocp4.example.com:6443/ >/dev/null 2>&1
  36. if [ $? -ne 0 ]; then
  37. echo "ERROR: could not log into OpenShift."
  38. echo
  39. echo "Please make sure OCP cluster is in ready state by issuing \"ssh lab@utility ./wait.sh\", then re-run this script."
  40. exit 1
  41. fi
  42. SSO_ADMIN_PASS="$(oc -n rhsso extract secrets/credential-rhsso --keys=ADMIN_PASSWORD --to=- 2>/dev/null)"
  43. if [ $? -ne 0 ]; then
  44. echo "ERROR: could not extract RHSSO admin password."
  45. echo
  46. echo "Please make sure a Keycloak resource exists in project \"rhsso\" and its deployment was successful, then re-run this script."
  47. exit 1
  48. fi
  49. echo OK
  50. fi
  51. # Make a test to see the master realm authenticates, and store the token.
  52. echo -n " - obtaining access token for \"admin-cli\"... "
  53. RSPNS=$(curl -ksf -XPOST -H "Content-Type: application/x-www-form-urlencoded" \
  54. -H "Accept: application/json" \
  55. -d "client_id=admin-cli&grant_type=password&username=${SSO_ADMIN_USER}&password=${SSO_ADMIN_PASS}" \
  56. https://${SSO_HOST}/auth/realms/master/protocol/openid-connect/token)
  57. if [ $? -ne 0 ]; then
  58. echo "ERROR: Could not authenticate against \"master\" realm as user \"${SSO_ADMIN_USER}\"."
  59. echo
  60. echo "Make sure the admin username is \"${SSO_ADMIN_USER}\" and its password is \"${SSO_ADMIN_PASS}\" and re-run this script."
  61. exit 1
  62. fi
  63. TOKEN=$(echo "${RSPNS}" | jq -r .access_token)
  64. if [ $? -ne 0 ] || [ -z "${TOKEN}" ]; then
  65. echo "ERROR: Can not parse access token out of server response."
  66. echo
  67. echo "Server response was: ${RSPNS}"
  68. exit 1
  69. fi
  70. echo OK
  71. # Make sure that the realm "sample" exists.
  72. echo -n " - checking for realm \"sample\"... "
  73. RSPNS="$(curl -ksf -XGET -H "Authorization: Bearer ${TOKEN}" \
  74. -H "Accept: application/json" \
  75. https://${SSO_HOST}/auth/admin/realms/sample)"
  76. if [ $? -ne 0 ]; then
  77. echo "ERROR: Server rejected query."
  78. echo
  79. echo "Server response was: ${RSPNS}"
  80. exit 1
  81. fi
  82. if [ -z "$(echo "${RSPNS}" | jq .realm)" ]; then
  83. echo "ERROR: Realm \"sample\" not found."
  84. echo
  85. echo "Make sure realm \"sample\" exists in \"${SSO_HOST}\" and re-run this script."
  86. exit 1
  87. fi
  88. echo OK
  89. # Make sure that the client "sample-client" exists.
  90. echo -n " - checking for client \"sample-client\"... "
  91. RSPNS="$(curl -ksf -XGET -H "Authorization: Bearer ${TOKEN}" \
  92. -H "Accept: application/json" \
  93. https://${SSO_HOST}/auth/admin/realms/sample/clients)"
  94. if [ $? -ne 0 ]; then
  95. echo "ERROR: Server rejected query."
  96. echo
  97. echo "Server response was: ${RSPNS}"
  98. exit 1
  99. fi
  100. if [ -z "$(echo "${RSPNS}" | jq '.[] | select(.clientId == "sample-client") | .id')" ]; then
  101. echo "ERROR: Client \"sample-client\" not found."
  102. echo
  103. echo "Make sure client \"sample-client\" exists in realm \"sample\" at \"${SSO_HOST}\" and re-run this script."
  104. exit 1
  105. fi
  106. echo OK
  107. echo
  108. echo "Proceeding with these settings:"
  109. echo " - SSO_HOST = ${SSO_HOST}"
  110. echo " - SSO_ADMIN_USER = ${SSO_ADMIN_USER}"
  111. echo " - SSO_ADMIN_PASS = ${SSO_ADMIN_PASS}"
  112. echo
  113. cat > ${HOME}/rhsso.conf <<EOF
  114. export SSO_HOST="${SSO_HOST}"
  115. export SSO_ADMIN_USER="${SSO_ADMIN_USER}"
  116. export SSO_ADMIN_PASS="${SSO_ADMIN_PASS}"
  117. EOF
  118. echo "Done, your configuration is now stored in ${HOME}/rhsso.conf!"
  119. echo
  120. echo "Any time you open a new terminal window, remember to load it like this:"
  121. echo
  122. echo " source ${HOME}/rhsso.conf"
  123. echo
  124. echo "You can also add this line at the end of your .bashrc to make it automatic."