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

  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. # It requires that jq are in the $PATH.
  6. #
  7. # Written by Jan-Philipp Benecke <github@bnck.me>
  8. # Public domain, 2020
  9. #
  10. # Following environment variables must be set:
  11. #
  12. #export DEPLOY_CLEVERREACH_CLIENT_ID=myid
  13. #export DEPLOY_CLEVERREACH_CLIENT_SECRET=mysecret
  14. cleverreach_deploy() {
  15. _cdomain="$1"
  16. _ckey="$2"
  17. _ccert="$3"
  18. _cca="$4"
  19. _cfullchain="$5"
  20. _debug _cdomain "$_cdomain"
  21. _debug _ckey "$_ckey"
  22. _debug _ccert "$_ccert"
  23. _debug _cca "$_cca"
  24. _debug _cfullchain "$_cfullchain"
  25. _cleverreach_client_id="${DEPLOY_CLEVERREACH_CLIENT_ID}"
  26. _cleverreach_client_secret="${DEPLOY_CLEVERREACH_CLIENT_SECRET}"
  27. if [ -z "$_cleverreach_client_id" ]; then
  28. _err "CleverReach Client ID is not found, please define DEPLOY_CLEVERREACH_CLIENT_ID."
  29. return 1
  30. fi
  31. if [ -z "$_cleverreach_client_secret" ]; then
  32. _err "CleverReach client secret is not found, please define DEPLOY_CLEVERREACH_CLIENT_SECRET."
  33. return 1
  34. fi
  35. _saveaccountconf DEPLOY_CLEVERREACH_CLIENT_ID "${_cleverreach_client_id}"
  36. _saveaccountconf DEPLOY_CLEVERREACH_CLIENT_SECRET "${_cleverreach_client_secret}"
  37. _info "Obtaining a CleverReach access token"
  38. _data="{\"grant_type\": \"client_credentials\", \"client_id\": \"${_cleverreach_client_id}\", \"client_secret\": \"${_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. _access_token=$(echo "$_auth_result" | _json_decode | jq -r .access_token)
  43. _info "Uploading certificate and key to CleverReach"
  44. _certData="{\"cert\":\"$(cat $_cfullchain | _json_encode)\", \"key\":\"$(cat $_ckey | _json_encode)\"}"
  45. export _H1="Authorization: Bearer ${_access_token}"
  46. _add_cert_result="$(_post "$_certData" "https://rest.cleverreach.com/v3/ssl/${_cdomain}" "" "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. }