You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

  1. #!/usr/bin/env sh
  2. # Here is the script to deploy the cert to your CleverReach Account using the CleverReach REST API.
  3. # Your OAuth needs the right scope, please contact CleverReach support for that.
  4. #
  5. # Written by Jan-Philipp Benecke <github@bnck.me>
  6. # Public domain, 2020
  7. #
  8. # Following environment variables must be set:
  9. #
  10. #export DEPLOY_CLEVERREACH_CLIENT_ID=myid
  11. #export DEPLOY_CLEVERREACH_CLIENT_SECRET=mysecret
  12. cleverreach_deploy() {
  13. _cdomain="$1"
  14. _ckey="$2"
  15. _ccert="$3"
  16. _cca="$4"
  17. _cfullchain="$5"
  18. _debug _cdomain "$_cdomain"
  19. _debug _ckey "$_ckey"
  20. _debug _ccert "$_ccert"
  21. _debug _cca "$_cca"
  22. _debug _cfullchain "$_cfullchain"
  23. _getdeployconf DEPLOY_CLEVERREACH_CLIENT_ID
  24. _getdeployconf DEPLOY_CLEVERREACH_CLIENT_SECRET
  25. _getdeployconf DEPLOY_CLEVERREACH_SUBCLIENT_ID
  26. if [ -z "${DEPLOY_CLEVERREACH_CLIENT_ID}" ]; then
  27. _err "CleverReach Client ID is not found, please define DEPLOY_CLEVERREACH_CLIENT_ID."
  28. return 1
  29. fi
  30. if [ -z "${DEPLOY_CLEVERREACH_CLIENT_SECRET}" ]; then
  31. _err "CleverReach client secret is not found, please define DEPLOY_CLEVERREACH_CLIENT_SECRET."
  32. return 1
  33. fi
  34. _savedeployconf DEPLOY_CLEVERREACH_CLIENT_ID "${DEPLOY_CLEVERREACH_CLIENT_ID}"
  35. _savedeployconf DEPLOY_CLEVERREACH_CLIENT_SECRET "${DEPLOY_CLEVERREACH_CLIENT_SECRET}"
  36. _savedeployconf DEPLOY_CLEVERREACH_SUBCLIENT_ID "${DEPLOY_CLEVERREACH_SUBCLIENT_ID}"
  37. _info "Obtaining a CleverReach access token"
  38. _data="{\"grant_type\": \"client_credentials\", \"client_id\": \"${DEPLOY_CLEVERREACH_CLIENT_ID}\", \"client_secret\": \"${DEPLOY_CLEVERREACH_CLIENT_SECRET}\"}"
  39. _auth_result="$(_post "$_data" "https://rest.cleverreach.com/oauth/token.php" "" "POST" "application/json")"
  40. _debug _data "$_data"
  41. _debug _auth_result "$_auth_result"
  42. _regex=".*\"access_token\":\"\([-._0-9A-Za-z]*\)\".*$"
  43. _debug _regex "$_regex"
  44. _access_token=$(echo "$_auth_result" | _json_decode | sed -n "s/$_regex/\1/p")
  45. if ${DEPLOY_CLEVERREACH_SUBCLIENT_ID}; then
  46. _info "Obtaining token for sub-client"
  47. export _H1="Authorization: Bearer ${_access_token}"
  48. _subclient_token_result="$(_get "https://rest.cleverreach.com/v3/clients/$DEPLOY_CLEVERREACH_SUBCLIENT_ID}/token")"
  49. _access_token=$(echo "$_subclient_token_result" | _json_decode | sed -n "s/$_regex/\1/p")
  50. _debug "Destroying parent token at CleverReach"
  51. _post "" "https://rest.cleverreach.com/v3/oauth/token.json" "" "DELETE" "application/json"
  52. fi
  53. _info "Uploading certificate and key to CleverReach"
  54. _certData="{\"cert\":\"$(_json_encode <"$_cfullchain")\", \"key\":\"$(_json_encode <"$_ckey")\"}"
  55. export _H1="Authorization: Bearer ${_access_token}"
  56. _add_cert_result="$(_post "$_certData" "https://rest.cleverreach.com/v3/ssl" "" "POST" "application/json")"
  57. if ! ${DEPLOY_CLEVERREACH_SUBCLIENT_ID}; then
  58. _debug "Destroying token at CleverReach"
  59. _post "" "https://rest.cleverreach.com/v3/oauth/token.json" "" "DELETE" "application/json"
  60. fi
  61. if ! echo "$_add_cert_result" | grep '"error":' >/dev/null; then
  62. _info "Uploaded certificate successfully"
  63. return 0
  64. else
  65. _debug _add_cert_result "$_add_cert_result"
  66. _err "Unable to update certificate"
  67. return 1
  68. fi
  69. }