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.

70 lines
2.4 KiB

4 years ago
  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. if [ -z "${DEPLOY_CLEVERREACH_CLIENT_ID}" ]; then
  26. _err "CleverReach Client ID is not found, please define DEPLOY_CLEVERREACH_CLIENT_ID."
  27. return 1
  28. fi
  29. if [ -z "${DEPLOY_CLEVERREACH_CLIENT_SECRET}" ]; then
  30. _err "CleverReach client secret is not found, please define DEPLOY_CLEVERREACH_CLIENT_SECRET."
  31. return 1
  32. fi
  33. _savedeployconf DEPLOY_CLEVERREACH_CLIENT_ID "${DEPLOY_CLEVERREACH_CLIENT_ID}"
  34. _savedeployconf DEPLOY_CLEVERREACH_CLIENT_SECRET "${DEPLOY_CLEVERREACH_CLIENT_SECRET}"
  35. _info "Obtaining a CleverReach access token"
  36. _data="{\"grant_type\": \"client_credentials\", \"client_id\": \"${DEPLOY_CLEVERREACH_CLIENT_ID}\", \"client_secret\": \"${DEPLOY_CLEVERREACH_CLIENT_SECRET}\"}"
  37. _auth_result="$(_post "$_data" "https://rest.cleverreach.com/oauth/token.php" "" "POST" "application/json")"
  38. _debug _data "$_data"
  39. _debug _auth_result "$_auth_result"
  40. _regex=".*\"access_token\":\"\([-._0-9A-Za-z]*\)\".*$"
  41. _debug _regex "$_regex"
  42. _access_token=$(echo "$_auth_result" | _json_decode | sed -n "s/$_regex/\1/p")
  43. _info "Uploading certificate and key to CleverReach"
  44. _certData="{\"cert\":\"$(_json_encode <"$_cfullchain")\", \"key\":\"$(_json_encode <"$_ckey")\"}"
  45. export _H1="Authorization: Bearer ${_access_token}"
  46. _add_cert_result="$(_post "$_certData" "https://rest.cleverreach.com/v3/ssl" "" "POST" "application/json")"
  47. _debug "Destroying token at CleverReach"
  48. _post "" "https://rest.cleverreach.com/v3/oauth/token.json" "" "DELETE" "application/json"
  49. if ! echo "$_add_cert_result" | grep '"error":' >/dev/null; then
  50. _info "Uploaded certificate successfully"
  51. return 0
  52. else
  53. _debug _add_cert_result "$_add_cert_result"
  54. _err "Unable to update certificate"
  55. return 1
  56. fi
  57. }