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.

86 lines
2.9 KiB

  1. #!/usr/bin/env sh
  2. # Here is a script to deploy cert to edgio using its API
  3. # https://docs.edg.io/guides/v7/develop/rest_api/authentication
  4. # https://docs.edg.io/rest_api/#tag/tls-certs/operation/postConfigV01TlsCerts
  5. # This deployment required following variables
  6. # export EDGIO_CLIENT_ID="Your Edgio Client ID"
  7. # export EDGIO_CLIENT_SECRET="Your Edgio Client Secret"
  8. # export EDGIO_ENVIRONMENT_ID="Your Edgio Environment ID"
  9. # If have more than one Environment ID
  10. # export EDGIO_ENVIRONMENT_ID="ENVIRONMENT_ID_1 ENVIRONMENT_ID_2"
  11. # returns 0 means success, otherwise error.
  12. ######## Public functions #####################
  13. #domain keyfile certfile cafile fullchain
  14. edgio_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. if [ -z "$EDGIO_CLIENT_ID" ]; then
  26. _err "EDGIO_CLIENT_ID is not defined."
  27. return 1
  28. else
  29. _savedomainconf EDGIO_CLIENT_ID "$EDGIO_CLIENT_ID"
  30. fi
  31. if [ -z "$EDGIO_CLIENT_SECRET" ]; then
  32. _err "EDGIO_CLIENT_SECRET is not defined."
  33. return 1
  34. else
  35. _savedomainconf EDGIO_CLIENT_SECRET "$EDGIO_CLIENT_SECRET"
  36. fi
  37. if [ -z "$EDGIO_ENVIRONMENT_ID" ]; then
  38. _err "EDGIO_ENVIRONMENT_ID is not defined."
  39. return 1
  40. else
  41. _savedomainconf EDGIO_ENVIRONMENT_ID "$EDGIO_ENVIRONMENT_ID"
  42. fi
  43. _info "Getting access token"
  44. _data="client_id=$EDGIO_CLIENT_ID&client_secret=$EDGIO_CLIENT_SECRET&grant_type=client_credentials&scope=app.config"
  45. _debug Get_access_token_data "$_data"
  46. _response=$(_post "$_data" "https://id.edgio.app/connect/token" "" "POST" "application/x-www-form-urlencoded" )
  47. _debug Get_access_token_response "$_response"
  48. _access_token=$(echo "$_response" | _json_decode | _egrep_o '"access_token":"[^"]*' | cut -d : -f 2 | tr -d '"')
  49. _debug _access_token "$_access_token"
  50. if [ -z "$_access_token" ]; then
  51. _err "Error in getting access token"
  52. return 1
  53. fi
  54. _info "Uploading certificate"
  55. string_ccert=$(sed 's/$/\\n/' "$_ccert" | tr -d '\n')
  56. string_cca=$(sed 's/$/\\n/' "$_cca" | tr -d '\n')
  57. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  58. for ENVIRONMENT_ID in $EDGIO_ENVIRONMENT_ID; do
  59. _data="{\"environment_id\":\"$ENVIRONMENT_ID\",\"primary_cert\":\"$string_ccert\",\"intermediate_cert\":\"$string_cca\",\"private_key\":\"$string_key\"}"
  60. _debug Upload_certificate_data "$_data"
  61. _H1="Authorization: Bearer $_access_token"
  62. _response=$(_post "$_data" "https://edgioapis.com/config/v0.1/tls-certs" "" "POST" "application/json")
  63. if _contains "$_response" "message"; then
  64. _err "Error in deploying $_cdomain certificate to Edgio ENVIRONMENT_ID $ENVIRONMENT_ID."
  65. _err "$_response"
  66. return 1
  67. fi
  68. _debug Upload_certificate_response "$_response"
  69. _info "Domain $_cdomain certificate successfully deployed to Edgio ENVIRONMENT_ID $ENVIRONMENT_ID."
  70. done
  71. return 0
  72. }